| 1 |
from _pytest.tmpdir import TempdirFactory |
| 2 |
from pypy.interpreter.baseobjspace import W_Root |
| 3 |
from pypy.interpreter.gateway import (unwrap_spec, interp2app) |
| 4 |
from pypy.interpreter.typedef import TypeDef |
| 5 |
from pypy.module._hpy_universal.test._vendored.support import ExtensionCompiler |
| 6 |
from pypy.module._hpy_universal import llapi |
| 7 |
from pypy.module._hpy_universal._vendored.hpy.devel import HPyDevel |
| 8 |
|
| 9 |
COMPILER_VERBOSE = False |
| 10 |
hpy_abi = 'debug' |
| 11 |
|
| 12 |
class W_ExtensionCompiler(W_Root): |
| 13 |
def __init__(self, compiler): |
| 14 |
self.compiler = compiler |
| 15 |
|
| 16 |
@staticmethod |
| 17 |
def descr_new(space, w_type): |
| 18 |
return W_ExtensionCompiler() |
| 19 |
|
| 20 |
@unwrap_spec(main_src='text', name='text', w_extra_sources=W_Root, hpy_abi='text') |
| 21 |
def descr_make_module(self, space, main_src, name='mytest', |
| 22 |
w_extra_sources=None, hpy_abi=hpy_abi): |
| 23 |
if w_extra_sources is None: |
| 24 |
extra_sources = () |
| 25 |
else: |
| 26 |
items_w = space.unpackiterable(w_extra_sources) |
| 27 |
extra_sources = [space.text_w(item) for item in items_w] |
| 28 |
module = self.compiler.compile_module( |
| 29 |
main_src, self.compiler.ExtensionTemplate, name, extra_sources) |
| 30 |
if hpy_abi in ("debug", "hybrid+debug"): |
| 31 |
mode = llapi.MODE_DEBUG |
| 32 |
elif hpy_abi in ("universal", "hybrid"): |
| 33 |
mode = llapi.MODE_UNIVERSAL |
| 34 |
elif hpy_abi == "trace": |
| 35 |
mode = llapi.MODE_TRACE |
| 36 |
else: |
| 37 |
mode = -1 |
| 38 |
w_mod = space.appexec([space.newtext(name), |
| 39 |
space.newtext(module.so_filename), |
| 40 |
space.newint(mode)], |
| 41 |
"""(name, so_filename, mode): |
| 42 |
import sys |
| 43 |
import _hpy_universal |
| 44 |
import importlib.util |
| 45 |
assert name not in sys.modules |
| 46 |
spec = importlib.util.spec_from_file_location(name, so_filename) |
| 47 |
mod = _hpy_universal.load(name, so_filename, spec, mode=mode) |
| 48 |
mod.__file__ = so_filename |
| 49 |
mod.__spec__ = spec |
| 50 |
return mod |
| 51 |
""" |
| 52 |
) |
| 53 |
return w_mod |
| 54 |
|
| 55 |
W_ExtensionCompiler.typedef = TypeDef("ExtensionCompiler", |
| 56 |
|
| 57 |
make_module=interp2app(W_ExtensionCompiler.descr_make_module), |
| 58 |
) |
| 59 |
|
| 60 |
def compiler(space, config): |
| 61 |
hpy_abi = 'debug' |
| 62 |
hpy_devel = HPyDevel(str(llapi.BASE_DIR)) |
| 63 |
if space.config.objspace.usemodules.cpyext: |
| 64 |
from pypy.module import cpyext |
| 65 |
cpyext_include_dirs = cpyext.api.include_dirs |
| 66 |
else: |
| 67 |
cpyext_include_dirs = None |
| 68 |
tmpdir = TempdirFactory(config).getbasetemp() |
| 69 |
compiler = ExtensionCompiler(tmpdir, hpy_devel, hpy_abi, |
| 70 |
compiler_verbose=COMPILER_VERBOSE, |
| 71 |
extra_include_dirs=cpyext_include_dirs) |
| 72 |
w_compiler = W_ExtensionCompiler(compiler) |
| 73 |
return w_compiler |