| 1 |
import py |
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
from rpython.tool.udir import udir |
| 5 |
from pypy.interpreter.gateway import interp2app, unwrap_spec, W_Root |
| 6 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 7 |
from pypy.module._hpy_universal.llapi import BASE_DIR |
| 8 |
from pypy.module._hpy_universal.test._vendored import support as _support |
| 9 |
from pypy.module._hpy_universal._vendored.hpy.devel import HPyDevel |
| 10 |
from ..state import State |
| 11 |
from .. import llapi |
| 12 |
|
| 13 |
COMPILER_VERBOSE = False |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
_static_lib_cache = {} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
_compiled_module_cache = {} |
| 24 |
|
| 25 |
def _try_build_static_lib(hpy_devel, hpy_abi): |
| 26 |
""" |
| 27 |
Compile the HPy helper sources into a static archive once per session. |
| 28 |
Returns {hpy_abi: [lib_path]} on success, {} on failure. |
| 29 |
""" |
| 30 |
try: |
| 31 |
import distutils.ccompiler |
| 32 |
import distutils.sysconfig |
| 33 |
import os |
| 34 |
|
| 35 |
base = udir.join('hpy_staticlib') |
| 36 |
abi_dir = base.join(hpy_abi) |
| 37 |
abi_dir.ensure(dir=True) |
| 38 |
obj_dir = base.join('obj', hpy_abi) |
| 39 |
obj_dir.ensure(dir=True) |
| 40 |
|
| 41 |
compiler = distutils.ccompiler.new_compiler() |
| 42 |
distutils.sysconfig.customize_compiler(compiler) |
| 43 |
|
| 44 |
include_dirs = hpy_devel.get_extra_include_dirs() |
| 45 |
include_dirs.append(str(hpy_devel.get_include_dir_forbid_python_h())) |
| 46 |
macros = [('HPY', None), ('HPY_ABI_UNIVERSAL', None)] |
| 47 |
|
| 48 |
objects = compiler.compile( |
| 49 |
hpy_devel.get_extra_sources(), |
| 50 |
output_dir=str(obj_dir), |
| 51 |
include_dirs=include_dirs, |
| 52 |
macros=macros, |
| 53 |
) |
| 54 |
|
| 55 |
lib_name = 'hpyextra' |
| 56 |
compiler.create_static_lib(objects, lib_name, output_dir=str(abi_dir)) |
| 57 |
lib_filename = compiler.library_filename(lib_name, lib_type='static') |
| 58 |
lib_path = str(abi_dir.join(lib_filename)) |
| 59 |
return {'universal': [lib_path], 'hybrid': [lib_path]} |
| 60 |
except Exception as e: |
| 61 |
import warnings |
| 62 |
warnings.warn( |
| 63 |
"HPy static-lib build failed (%s); " |
| 64 |
"falling back to per-test source compilation." % e, |
| 65 |
stacklevel=2, |
| 66 |
) |
| 67 |
return {} |
| 68 |
|
| 69 |
|
| 70 |
def debug_collect(): |
| 71 |
import gc |
| 72 |
gc.collect() |
| 73 |
|
| 74 |
class HPyAppTest(object): |
| 75 |
""" |
| 76 |
Base class for HPy app tests. This is used as a mixin, and individual |
| 77 |
subclasses are created by conftest.make_hpy_apptest |
| 78 |
""" |
| 79 |
|
| 80 |
extra_link_args = [] |
| 81 |
spaceconfig = { |
| 82 |
'usemodules': ['_hpy_universal'], |
| 83 |
'objspace.hpy_cpyext_API': False, |
| 84 |
} |
| 85 |
|
| 86 |
def setup_class(cls): |
| 87 |
if cls.runappdirect: |
| 88 |
pytest.skip() |
| 89 |
cls.w_runappdirect = cls.space.wrap(cls.runappdirect) |
| 90 |
cls.w_debug_collect = cls.space.wrap(interp2app(debug_collect)) |
| 91 |
|
| 92 |
@pytest.fixture |
| 93 |
def compiler(self): |
| 94 |
|
| 95 |
return 'The fixture "compiler" is not used on pypy' |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@pytest.fixture(params=['universal', 'debug'], autouse=True) |
| 103 |
def initargs(self, request, capfd): |
| 104 |
hpy_abi = request.param |
| 105 |
self._init(request, hpy_abi) |
| 106 |
self.capfd = capfd |
| 107 |
|
| 108 |
def _init(self, request, hpy_abi): |
| 109 |
state = self.space.fromcache(State) |
| 110 |
if state.was_already_setup(): |
| 111 |
state.reset() |
| 112 |
if self.space.config.objspace.usemodules.cpyext: |
| 113 |
from pypy.module import cpyext |
| 114 |
cpyext_include_dirs = cpyext.api.include_dirs |
| 115 |
else: |
| 116 |
cpyext_include_dirs = None |
| 117 |
self.w_hpy_abi = self.space.newtext(hpy_abi) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
prefix = request.function.__name__ + '-' |
| 123 |
if sys.platform == 'win32': |
| 124 |
prefix = prefix.lower() |
| 125 |
tmpdir = py.path.local.make_numbered_dir(rootdir=udir, |
| 126 |
prefix=prefix, |
| 127 |
keep=0) |
| 128 |
|
| 129 |
hpy_devel = HPyDevel(str(BASE_DIR)) |
| 130 |
if 'libs' not in _static_lib_cache: |
| 131 |
_static_lib_cache['libs'] = _try_build_static_lib(hpy_devel, 'universal') |
| 132 |
if _static_lib_cache['libs']: |
| 133 |
hpy_devel._available_static_libs = _static_lib_cache['libs'] |
| 134 |
if hpy_abi in ("debug", "hybrid+debug"): |
| 135 |
mode = llapi.MODE_DEBUG |
| 136 |
elif hpy_abi in ("universal", "hybrid"): |
| 137 |
mode = llapi.MODE_UNIVERSAL |
| 138 |
elif hpy_abi == "trace": |
| 139 |
mode = llapi.MODE_TRACE |
| 140 |
else: |
| 141 |
mode = -1 |
| 142 |
if hpy_abi == 'debug' or hpy_abi == 'trace': |
| 143 |
|
| 144 |
|
| 145 |
hpy_abi = 'universal' |
| 146 |
elif hpy_abi in ('hybrid+debug', 'hybrid+trace'): |
| 147 |
hpy_abi = 'hybrid' |
| 148 |
compiler = _support.ExtensionCompiler(tmpdir, hpy_devel, hpy_abi, |
| 149 |
compiler_verbose=COMPILER_VERBOSE, |
| 150 |
extra_link_args=self.extra_link_args, |
| 151 |
extra_include_dirs=cpyext_include_dirs) |
| 152 |
ExtensionTemplate = self.ExtensionTemplate |
| 153 |
|
| 154 |
@unwrap_spec(main_src='text', name='text', w_extra_sources=W_Root) |
| 155 |
def descr_make_module(space, main_src, name='mytest', |
| 156 |
w_extra_sources=None): |
| 157 |
if w_extra_sources is None: |
| 158 |
extra_sources = () |
| 159 |
else: |
| 160 |
items_w = space.unpackiterable(w_extra_sources) |
| 161 |
extra_sources = [space.text_w(item) for item in items_w] |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
cache_key = (main_src, ExtensionTemplate.__name__, |
| 169 |
name, tuple(extra_sources), hpy_abi, mode) |
| 170 |
if cache_key in _compiled_module_cache: |
| 171 |
so_filename = _compiled_module_cache[cache_key] |
| 172 |
else: |
| 173 |
module = compiler.compile_module(main_src, ExtensionTemplate, |
| 174 |
name, extra_sources) |
| 175 |
so_filename = module.so_filename |
| 176 |
_compiled_module_cache[cache_key] = so_filename |
| 177 |
w_mod = space.appexec([space.newtext(name), |
| 178 |
space.newtext(so_filename), |
| 179 |
space.newint(mode)], |
| 180 |
"""(name, so_filename, mode): |
| 181 |
import sys |
| 182 |
import _hpy_universal |
| 183 |
import importlib.util |
| 184 |
assert name not in sys.modules |
| 185 |
spec = importlib.util.spec_from_file_location(name, so_filename) |
| 186 |
mod = _hpy_universal.load(name, so_filename, spec, mode=mode) |
| 187 |
mod.__file__ = so_filename |
| 188 |
mod.__spec__ = spec |
| 189 |
return mod |
| 190 |
""" |
| 191 |
) |
| 192 |
return w_mod |
| 193 |
self.w_make_module = self.space.wrap(interp2app(descr_make_module)) |
| 194 |
|
| 195 |
@unwrap_spec(main_src='text', w_ExtensionTemplate=W_Root, name='text', |
| 196 |
w_extra_sources=W_Root) |
| 197 |
def descr_compile_module(space, main_src, w_ExtensionTemplate=None, |
| 198 |
name='mytest', w_extra_sources=None): |
| 199 |
if w_extra_sources is None: |
| 200 |
extra_sources = () |
| 201 |
else: |
| 202 |
items_w = space.unpackiterable(w_extra_sources) |
| 203 |
extra_sources = [space.text_w(item) for item in items_w] |
| 204 |
if w_ExtensionTemplate is not None: |
| 205 |
raise NotImplementedError |
| 206 |
cache_key = (main_src, ExtensionTemplate.__name__, |
| 207 |
name, tuple(extra_sources), hpy_abi, mode) |
| 208 |
if cache_key in _compiled_module_cache: |
| 209 |
so_filename = _compiled_module_cache[cache_key] |
| 210 |
else: |
| 211 |
module = compiler.compile_module(main_src, ExtensionTemplate, |
| 212 |
name, extra_sources) |
| 213 |
so_filename = module.so_filename |
| 214 |
_compiled_module_cache[cache_key] = so_filename |
| 215 |
|
| 216 |
w_class_with_so_filename = space.appexec([ |
| 217 |
space.newtext(so_filename)], |
| 218 |
"""(so_filename,): |
| 219 |
class ClassWithSoFilename(): |
| 220 |
def __init__(self, so_filename): |
| 221 |
self.so_filename = so_filename |
| 222 |
return ClassWithSoFilename(so_filename) |
| 223 |
""" |
| 224 |
) |
| 225 |
return w_class_with_so_filename |
| 226 |
self.w_compile_module = self.space.wrap(interp2app(descr_compile_module)) |
| 227 |
|
| 228 |
def supports_refcounts(space): |
| 229 |
return space.w_False |
| 230 |
self.w_supports_refcounts = self.space.wrap(interp2app(supports_refcounts)) |
| 231 |
|
| 232 |
def supports_ordinary_make_module_imports(space): |
| 233 |
return space.w_False |
| 234 |
self.w_supports_ordinary_make_module_imports = self.space.wrap( |
| 235 |
interp2app(supports_ordinary_make_module_imports)) |
| 236 |
|
| 237 |
def supports_sys_executable(space): |
| 238 |
return space.w_False |
| 239 |
self.w_supports_sys_executable = self.space.wrap( |
| 240 |
interp2app(supports_sys_executable)) |
| 241 |
|
| 242 |
@unwrap_spec(name='text', so_filename='text', mode=int) |
| 243 |
def descr_load_universal_module(space, name, so_filename, mode): |
| 244 |
w_mod = space.appexec([space.newtext(name), |
| 245 |
space.newtext(so_filename), |
| 246 |
space.newint(mode)], |
| 247 |
"""(modname, so_filename, mode): |
| 248 |
import _hpy_universal |
| 249 |
return _hpy_universal.load(modname, so_filename, mode) |
| 250 |
""" |
| 251 |
) |
| 252 |
return w_mod |
| 253 |
|
| 254 |
w_load_universal_module = self.space.wrap(interp2app(descr_load_universal_module)) |
| 255 |
|
| 256 |
self.w_compiler = self.space.appexec([self.space.newtext(hpy_abi), |
| 257 |
w_load_universal_module, |
| 258 |
], |
| 259 |
"""(abi, _load_universal_module): |
| 260 |
class compiler: |
| 261 |
hpy_abi = abi |
| 262 |
load_universal_module = _load_universal_module |
| 263 |
return compiler |
| 264 |
""") |
| 265 |
|
| 266 |
@unwrap_spec(main_src='text', error='text') |
| 267 |
def descr_expect_make_error(space, main_src, error): |
| 268 |
try: |
| 269 |
compiler.compile_module(main_src, ExtensionTemplate, "mytest") |
| 270 |
except Exception as err: |
| 271 |
pass |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
cap = self.capfd.readouterr() |
| 276 |
sys.stdout.write(cap[0]) |
| 277 |
sys.stderr.write(cap[1]) |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
if error in cap[0] or error in cap[1]: |
| 282 |
|
| 283 |
return |
| 284 |
raise Exception("The following error message was not found in the compiler " |
| 285 |
"output:\n " + error) |
| 286 |
|
| 287 |
self.w_expect_make_error = self.space.wrap(interp2app(descr_expect_make_error)) |
| 288 |
|
| 289 |
class HPyDebugAppTest(HPyAppTest): |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
@pytest.fixture(autouse=True) |
| 294 |
def initargs(self, request): |
| 295 |
self._init(request, hpy_abi='debug') |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
if sys.platform == 'win32': |
| 304 |
|
| 305 |
|
| 306 |
from pypy.module.sys import version |
| 307 |
ver = version.CPYTHON_VERSION[:2] |
| 308 |
untranslated_link_args = ["/NODEFAULTLIB:Python%d%d.lib" % ver] |
| 309 |
untranslated_link_args.append(str(udir / "module_cache" / "pypyapi.lib")) |
| 310 |
else: |
| 311 |
untranslated_link_args = [] |
| 312 |
|
| 313 |
class HPyCPyextAppTest(AppTestCpythonExtensionBase, HPyAppTest): |
| 314 |
""" |
| 315 |
Base class for hpy tests which also need cpyext |
| 316 |
""" |
| 317 |
|
| 318 |
extra_link_args = untranslated_link_args |
| 319 |
|
| 320 |
|
| 321 |
spaceconfig = {'usemodules': ['_hpy_universal', 'cpyext', 'mmap']} |
| 322 |
|
| 323 |
def setup_class(cls): |
| 324 |
HPyAppTest.setup_class.im_func(cls) |
| 325 |
AppTestCpythonExtensionBase.setup_class.im_func(cls) |
| 326 |
|
| 327 |
|
| 328 |
@pytest.fixture(params=['hybrid', 'hybrid+debug'], autouse=True) |
| 329 |
def initargs(self, request): |
| 330 |
hpy_abi = request.param |
| 331 |
self._init(request, hpy_abi) |
| 332 |
|
| 333 |
|