| 1 |
import os, sys |
| 2 |
from filelock import FileLock |
| 3 |
import pytest |
| 4 |
from pathlib import Path |
| 5 |
import re |
| 6 |
import subprocess |
| 7 |
import textwrap |
| 8 |
import distutils |
| 9 |
|
| 10 |
PY2 = sys.version_info[0] == 2 |
| 11 |
|
| 12 |
HPY_ROOT = Path(__file__).parent.parent |
| 13 |
LOCK = FileLock(HPY_ROOT / ".hpy.lock") |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
SUPPORTS_SYS_EXECUTABLE = bool(getattr(sys, "executable", None)) |
| 19 |
|
| 20 |
IS_PYTHON_DEBUG_BUILD = hasattr(sys, 'gettotalrefcount') |
| 21 |
|
| 22 |
|
| 23 |
ONLY_LINUX = pytest.mark.skipif(sys.platform!='linux', reason='linux only') |
| 24 |
|
| 25 |
SUPPORTS_MEM_PROTECTION = '_HPY_DEBUG_FORCE_DEFAULT_MEM_PROTECT' not in os.environ |
| 26 |
|
| 27 |
def reindent(s, indent): |
| 28 |
s = textwrap.dedent(s) |
| 29 |
return ''.join(' '*indent + line if line.strip() else line |
| 30 |
for line in s.splitlines(True)) |
| 31 |
|
| 32 |
def atomic_run(*args, **kwargs): |
| 33 |
with LOCK: |
| 34 |
return subprocess.run(*args, **kwargs) |
| 35 |
|
| 36 |
|
| 37 |
def make_hpy_abi_fixture(ABIs, class_fixture=False): |
| 38 |
""" |
| 39 |
Make an hpy_abi fixture. |
| 40 |
|
| 41 |
conftest.py defines a default hpy_abi for all tests, but individual files |
| 42 |
and classes can override the set of ABIs they want to use. The indented |
| 43 |
usage is the following: |
| 44 |
|
| 45 |
# at the top of a file |
| 46 |
hpy_abi = make_hpy_abi_fixture(['universal', 'debug']) |
| 47 |
|
| 48 |
# in a class |
| 49 |
class TestFoo(HPyTest): |
| 50 |
hpy_abi = make_hpy_abi_fixture('with hybrid', class_fixture=True) |
| 51 |
""" |
| 52 |
if ABIs == 'default': |
| 53 |
if sys.implementation.name == 'cpython': |
| 54 |
ABIs = ['cpython', 'universal', 'debug'] |
| 55 |
else: |
| 56 |
ABIs = ['universal', 'debug'] |
| 57 |
elif ABIs == 'with hybrid': |
| 58 |
if sys.implementation.name == 'cpython': |
| 59 |
ABIs = ['cpython', 'hybrid', 'hybrid+debug'] |
| 60 |
else: |
| 61 |
ABIs = ['hybrid', 'hybrid+debug'] |
| 62 |
elif isinstance(ABIs, list): |
| 63 |
pass |
| 64 |
else: |
| 65 |
raise ValueError("ABIs must be 'default', 'with hybrid' " |
| 66 |
"or a list of strings. Got: %s" % ABIs) |
| 67 |
|
| 68 |
if class_fixture: |
| 69 |
@pytest.fixture(params=ABIs) |
| 70 |
def hpy_abi(self, request): |
| 71 |
abi = request.param |
| 72 |
yield abi |
| 73 |
else: |
| 74 |
@pytest.fixture(params=ABIs) |
| 75 |
def hpy_abi(request): |
| 76 |
abi = request.param |
| 77 |
yield abi |
| 78 |
|
| 79 |
return hpy_abi |
| 80 |
|
| 81 |
|
| 82 |
class DefaultExtensionTemplate(object): |
| 83 |
|
| 84 |
INIT_TEMPLATE = textwrap.dedent( |
| 85 |
""" |
| 86 |
static HPyDef *moduledefs[] = { |
| 87 |
%(defines)s |
| 88 |
NULL |
| 89 |
}; |
| 90 |
%(globals_defs)s |
| 91 |
static HPyModuleDef moduledef = { |
| 92 |
.doc = "some test for hpy", |
| 93 |
.size = 0, |
| 94 |
.legacy_methods = %(legacy_methods)s, |
| 95 |
.defines = moduledefs, |
| 96 |
%(globals_field)s |
| 97 |
}; |
| 98 |
|
| 99 |
HPy_MODINIT(%(name)s, moduledef) |
| 100 |
""") |
| 101 |
|
| 102 |
INIT_TEMPLATE_WITH_TYPES_INIT = textwrap.dedent( |
| 103 |
""" |
| 104 |
HPyDef_SLOT(generated_init, HPy_mod_exec) |
| 105 |
static int generated_init_impl(HPyContext *ctx, HPy m) |
| 106 |
{ |
| 107 |
// Shouldn't really happen, but jut to silence the unused label warning |
| 108 |
if (HPy_IsNull(m)) |
| 109 |
goto MODINIT_ERROR; |
| 110 |
|
| 111 |
%(init_types)s |
| 112 |
return 0; |
| 113 |
|
| 114 |
MODINIT_ERROR: |
| 115 |
return -1; |
| 116 |
} |
| 117 |
""" + INIT_TEMPLATE) |
| 118 |
|
| 119 |
r_marker = re.compile(r"^\s*@([A-Za-z_]+)(\(.*\))?$") |
| 120 |
|
| 121 |
def __init__(self, src, name): |
| 122 |
self.src = textwrap.dedent(src) |
| 123 |
self.name = name |
| 124 |
self.defines_table = None |
| 125 |
self.legacy_methods = 'NULL' |
| 126 |
self.type_table = None |
| 127 |
self.globals_table = None |
| 128 |
|
| 129 |
def expand(self): |
| 130 |
self.defines_table = [] |
| 131 |
self.globals_table = [] |
| 132 |
self.type_table = [] |
| 133 |
self.output = ['#include <hpy.h>'] |
| 134 |
for line in self.src.split('\n'): |
| 135 |
match = self.r_marker.match(line) |
| 136 |
if match: |
| 137 |
name, args = self.parse_marker(match) |
| 138 |
meth = getattr(self, name) |
| 139 |
out = meth(*args) |
| 140 |
if out is not None: |
| 141 |
out = textwrap.dedent(out) |
| 142 |
self.output.append(out) |
| 143 |
else: |
| 144 |
self.output.append(line) |
| 145 |
return '\n'.join(self.output) |
| 146 |
|
| 147 |
def parse_marker(self, match): |
| 148 |
name = match.group(1) |
| 149 |
args = match.group(2) |
| 150 |
if args is None: |
| 151 |
args = () |
| 152 |
else: |
| 153 |
assert args[0] == '(' |
| 154 |
assert args[-1] == ')' |
| 155 |
args = args[1:-1].split(',') |
| 156 |
args = [x.strip() for x in args] |
| 157 |
return name, args |
| 158 |
|
| 159 |
def INIT(self): |
| 160 |
NL_INDENT = '\n ' |
| 161 |
if self.type_table: |
| 162 |
init_types = '\n'.join(self.type_table) |
| 163 |
else: |
| 164 |
init_types = '' |
| 165 |
|
| 166 |
globals_defs = '' |
| 167 |
globals_field = '' |
| 168 |
if self.globals_table: |
| 169 |
globals_defs = \ |
| 170 |
textwrap.dedent(''' |
| 171 |
static HPyGlobal *module_globals[] = { |
| 172 |
%s |
| 173 |
NULL |
| 174 |
};''') % NL_INDENT.join(self.globals_table) |
| 175 |
globals_field = '.globals = module_globals' |
| 176 |
|
| 177 |
template = self.INIT_TEMPLATE |
| 178 |
if init_types: |
| 179 |
template = self.INIT_TEMPLATE_WITH_TYPES_INIT |
| 180 |
self.EXPORT('generated_init') |
| 181 |
exp = template % { |
| 182 |
'legacy_methods': self.legacy_methods, |
| 183 |
'defines': NL_INDENT.join(self.defines_table), |
| 184 |
'init_types': init_types, |
| 185 |
'name': self.name, |
| 186 |
'globals_defs': globals_defs, |
| 187 |
'globals_field': globals_field} |
| 188 |
self.output.append(exp) |
| 189 |
|
| 190 |
self.defines_table = None |
| 191 |
self.type_table = None |
| 192 |
|
| 193 |
def EXPORT(self, meth): |
| 194 |
self.defines_table.append('&%s,' % meth) |
| 195 |
|
| 196 |
def EXPORT_GLOBAL(self, var): |
| 197 |
self.globals_table.append('&%s,' % var) |
| 198 |
|
| 199 |
def EXPORT_LEGACY(self, pymethoddef): |
| 200 |
self.legacy_methods = pymethoddef |
| 201 |
|
| 202 |
def EXPORT_TYPE(self, name, spec): |
| 203 |
src = """ |
| 204 |
if (!HPyHelpers_AddType(ctx, m, {name}, &{spec}, NULL)) {{ |
| 205 |
goto MODINIT_ERROR; |
| 206 |
}} |
| 207 |
""" |
| 208 |
src = reindent(src, 4) |
| 209 |
self.type_table.append(src.format( |
| 210 |
name=name, |
| 211 |
spec=spec)) |
| 212 |
|
| 213 |
def EXTRA_INIT_FUNC(self, func): |
| 214 |
src = """ |
| 215 |
{func}(ctx, m); |
| 216 |
if (HPyErr_Occurred(ctx)) |
| 217 |
goto MODINIT_ERROR; |
| 218 |
""" |
| 219 |
src = reindent(src, 4) |
| 220 |
self.type_table.append(src.format(func=func)) |
| 221 |
|
| 222 |
def HPy_MODINIT(self, mod): |
| 223 |
return "HPy_MODINIT({}, {})".format(self.name, mod) |
| 224 |
|
| 225 |
|
| 226 |
class Spec(object): |
| 227 |
def __init__(self, name, origin): |
| 228 |
self.name = name |
| 229 |
self.origin = origin |
| 230 |
|
| 231 |
|
| 232 |
class HPyModule(object): |
| 233 |
def __init__(self, name, so_file): |
| 234 |
self.name = name |
| 235 |
self.so_filename = so_file |
| 236 |
|
| 237 |
|
| 238 |
class ExtensionCompiler: |
| 239 |
def __init__(self, tmpdir, hpy_devel, hpy_abi, compiler_verbose=False, |
| 240 |
dump_dir=None, |
| 241 |
ExtensionTemplate=DefaultExtensionTemplate, |
| 242 |
extra_include_dirs=None): |
| 243 |
""" |
| 244 |
hpy_devel is an instance of HPyDevel which specifies where to find |
| 245 |
include/, runtime/src, etc. Usually it will point to hpy/devel/, but |
| 246 |
alternate implementations can point to their own place (e.g. pypy puts |
| 247 |
it into pypy/module/_hpy_universal/_vendored) |
| 248 |
|
| 249 |
extra_include_dirs is a list of include dirs which is put BEFORE all |
| 250 |
others. By default it is empty, but it is used e.g. by PyPy to make |
| 251 |
sure that #include <Python.h> picks its own version, instead of the |
| 252 |
system-wide one. |
| 253 |
""" |
| 254 |
self.tmpdir = tmpdir |
| 255 |
self.dump_dir = dump_dir |
| 256 |
self.hpy_devel = hpy_devel |
| 257 |
self.hpy_abi = hpy_abi |
| 258 |
self.compiler_verbose = compiler_verbose |
| 259 |
self.ExtensionTemplate = ExtensionTemplate |
| 260 |
self.extra_include_dirs = extra_include_dirs |
| 261 |
|
| 262 |
def _expand(self, ExtensionTemplate, name, template): |
| 263 |
source = ExtensionTemplate(template, name).expand() |
| 264 |
filename = self.tmpdir.join(name + '.c') |
| 265 |
dump_file = self.dump_dir.joinpath(name + '.c') if self.dump_dir else None |
| 266 |
if PY2: |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if dump_file: |
| 272 |
dump_file.write_text(source) |
| 273 |
filename.write(source, mode='wb') |
| 274 |
else: |
| 275 |
if dump_file: |
| 276 |
dump_file.write_text(source) |
| 277 |
filename.write(source) |
| 278 |
return name + '.c' |
| 279 |
|
| 280 |
def _fixup_template(self, ExtensionTemplate): |
| 281 |
return self.ExtensionTemplate if ExtensionTemplate is None else ExtensionTemplate |
| 282 |
|
| 283 |
def compile_module(self, main_src, ExtensionTemplate=None, name='mytest', extra_sources=()): |
| 284 |
""" |
| 285 |
Create and compile a HPy module from the template |
| 286 |
""" |
| 287 |
ExtensionTemplate = self._fixup_template(ExtensionTemplate) |
| 288 |
from distutils.core import Extension |
| 289 |
filename = self._expand(ExtensionTemplate, name, main_src) |
| 290 |
sources = [str(filename)] |
| 291 |
for i, src in enumerate(extra_sources): |
| 292 |
extra_filename = self._expand(ExtensionTemplate, 'extmod_%d' % i, src) |
| 293 |
sources.append(extra_filename) |
| 294 |
|
| 295 |
if self.dump_dir: |
| 296 |
pytest.skip("dumping test sources only") |
| 297 |
if sys.platform == 'win32': |
| 298 |
|
| 299 |
compile_args = [ |
| 300 |
'/Od', |
| 301 |
'/WX', |
| 302 |
|
| 303 |
'/Zi', |
| 304 |
'-D_CRT_SECURE_NO_WARNINGS', |
| 305 |
'/FS', |
| 306 |
] |
| 307 |
link_args = [ |
| 308 |
'/DEBUG', |
| 309 |
'/LTCG', |
| 310 |
] |
| 311 |
else: |
| 312 |
compile_args = [ |
| 313 |
'-g', '-O0', |
| 314 |
'-Wfatal-errors', |
| 315 |
'-Werror', |
| 316 |
] |
| 317 |
link_args = [ |
| 318 |
'-g', |
| 319 |
] |
| 320 |
|
| 321 |
ext = Extension( |
| 322 |
name, |
| 323 |
sources=sources, |
| 324 |
include_dirs=self.extra_include_dirs, |
| 325 |
extra_compile_args=compile_args, |
| 326 |
extra_link_args=link_args) |
| 327 |
|
| 328 |
hpy_abi = self.hpy_abi |
| 329 |
if hpy_abi == 'debug' or hpy_abi == 'trace': |
| 330 |
|
| 331 |
|
| 332 |
hpy_abi = 'universal' |
| 333 |
elif hpy_abi in ('hybrid+debug', 'hybrid+trace'): |
| 334 |
hpy_abi = 'hybrid' |
| 335 |
so_filename = c_compile(str(self.tmpdir), ext, |
| 336 |
hpy_devel=self.hpy_devel, |
| 337 |
hpy_abi=hpy_abi, |
| 338 |
compiler_verbose=self.compiler_verbose) |
| 339 |
return HPyModule(name, so_filename) |
| 340 |
|
| 341 |
def make_module(self, main_src, ExtensionTemplate=None, name='mytest', |
| 342 |
extra_sources=()): |
| 343 |
""" |
| 344 |
Compile & load a module. This is NOT a proper import: e.g. |
| 345 |
the module is not put into sys.modules. |
| 346 |
|
| 347 |
We don't want to unnecessarily modify the global state inside tests: |
| 348 |
if you are writing a test which needs a proper import, you should not |
| 349 |
use make_module but explicitly use compile_module and import it |
| 350 |
manually as required by your test. |
| 351 |
""" |
| 352 |
ExtensionTemplate = self._fixup_template(ExtensionTemplate) |
| 353 |
module = self.compile_module( |
| 354 |
main_src, ExtensionTemplate, name, extra_sources) |
| 355 |
so_filename = module.so_filename |
| 356 |
from hpy.universal import MODE_UNIVERSAL, MODE_DEBUG, MODE_TRACE |
| 357 |
if self.hpy_abi in ('universal', 'hybrid'): |
| 358 |
return self.load_universal_module(name, so_filename, mode=MODE_UNIVERSAL) |
| 359 |
elif self.hpy_abi in ('debug', 'hybrid+debug'): |
| 360 |
return self.load_universal_module(name, so_filename, mode=MODE_DEBUG) |
| 361 |
elif self.hpy_abi in ('trace', 'hybrid+trace'): |
| 362 |
return self.load_universal_module(name, so_filename, mode=MODE_TRACE) |
| 363 |
elif self.hpy_abi == 'cpython': |
| 364 |
return self.load_cpython_module(name, so_filename) |
| 365 |
else: |
| 366 |
assert False |
| 367 |
|
| 368 |
def load_universal_module(self, name, so_filename, mode): |
| 369 |
assert self.hpy_abi in ('universal', 'hybrid', 'debug', 'hybrid+debug', |
| 370 |
'trace', 'hybrid+trace') |
| 371 |
import sys |
| 372 |
import hpy.universal |
| 373 |
import importlib.util |
| 374 |
assert name not in sys.modules |
| 375 |
spec = importlib.util.spec_from_file_location(name, so_filename) |
| 376 |
mod = hpy.universal.load(name, so_filename, spec, mode=mode) |
| 377 |
mod.__file__ = so_filename |
| 378 |
mod.__spec__ = spec |
| 379 |
return mod |
| 380 |
|
| 381 |
def load_cpython_module(self, name, so_filename): |
| 382 |
assert self.hpy_abi == 'cpython' |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
import importlib.util |
| 388 |
import sys |
| 389 |
assert name not in sys.modules |
| 390 |
spec = importlib.util.spec_from_file_location(name, so_filename) |
| 391 |
try: |
| 392 |
|
| 393 |
module = importlib.util.module_from_spec(spec) |
| 394 |
finally: |
| 395 |
if name in sys.modules: |
| 396 |
del sys.modules[name] |
| 397 |
spec.loader.exec_module(module) |
| 398 |
return module |
| 399 |
|
| 400 |
|
| 401 |
class PythonSubprocessRunner: |
| 402 |
def __init__(self, verbose, hpy_abi): |
| 403 |
self.verbose = verbose |
| 404 |
self.hpy_abi = hpy_abi |
| 405 |
|
| 406 |
def run(self, mod, code): |
| 407 |
""" Starts new subprocess that loads given module as 'mod' using the |
| 408 |
correct ABI mode and then executes given code snippet. Use |
| 409 |
"--subprocess-v" to enable logging from this. |
| 410 |
""" |
| 411 |
env = os.environ.copy() |
| 412 |
pythonpath = [os.path.dirname(mod.so_filename)] |
| 413 |
if 'PYTHONPATH' in env: |
| 414 |
pythonpath.append(env['PYTHONPATH']) |
| 415 |
env["PYTHONPATH"] = os.pathsep.join(pythonpath) |
| 416 |
if self.hpy_abi in ['universal', 'debug']: |
| 417 |
|
| 418 |
load_module = "import sys;" + \ |
| 419 |
"import hpy.universal;" + \ |
| 420 |
"import importlib.util;" + \ |
| 421 |
"spec = importlib.util.spec_from_file_location('{name}', '{so_filename}');" + \ |
| 422 |
"mod = hpy.universal.load('{name}', '{so_filename}', spec, debug={debug});" |
| 423 |
escaped_filename = mod.so_filename.replace("\\", "\\\\") |
| 424 |
load_module = load_module.format(name=mod.name, so_filename=escaped_filename, |
| 425 |
debug=self.hpy_abi == 'debug') |
| 426 |
else: |
| 427 |
|
| 428 |
assert self.hpy_abi == 'cpython' |
| 429 |
load_module = "import {} as mod;".format(mod.name) |
| 430 |
if self.verbose: |
| 431 |
print("\n---\nExecuting in subprocess: {}".format(load_module + code)) |
| 432 |
result = atomic_run([sys.executable, "-c", load_module + code], env=env, |
| 433 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 434 |
if self.verbose: |
| 435 |
print("stdout/stderr:") |
| 436 |
try: |
| 437 |
out = result.stdout.decode('latin-1') |
| 438 |
err = result.stderr.decode('latin-1') |
| 439 |
print("----\n{out}--\n{err}-----".format(out=out, err=err)) |
| 440 |
except UnicodeDecodeError: |
| 441 |
print("Warning: stdout or stderr could not be decoded with 'latin-1' encoding") |
| 442 |
return result |
| 443 |
|
| 444 |
|
| 445 |
@pytest.mark.usefixtures('initargs') |
| 446 |
class HPyTest: |
| 447 |
ExtensionTemplate = DefaultExtensionTemplate |
| 448 |
|
| 449 |
@pytest.fixture() |
| 450 |
def initargs(self, compiler, leakdetector, capfd): |
| 451 |
self.compiler = compiler |
| 452 |
self.leakdetector = leakdetector |
| 453 |
self.capfd = capfd |
| 454 |
|
| 455 |
def make_module(self, main_src, name='mytest', extra_sources=()): |
| 456 |
ExtensionTemplate = self.ExtensionTemplate |
| 457 |
return self.compiler.make_module(main_src, ExtensionTemplate, name, |
| 458 |
extra_sources) |
| 459 |
|
| 460 |
def compile_module(self, main_src, name='mytest', extra_sources=()): |
| 461 |
ExtensionTemplate = self.ExtensionTemplate |
| 462 |
return self.compiler.compile_module(main_src, ExtensionTemplate, name, |
| 463 |
extra_sources) |
| 464 |
|
| 465 |
def expect_make_error(self, main_src, error): |
| 466 |
with pytest.raises(distutils.errors.CompileError): |
| 467 |
self.make_module(main_src) |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
cap = self.capfd.readouterr() |
| 472 |
sys.stdout.write(cap.out) |
| 473 |
sys.stderr.write(cap.err) |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
if error in cap.out or error in cap.err: |
| 478 |
|
| 479 |
return |
| 480 |
raise Exception("The following error message was not found in the compiler " |
| 481 |
"output:\n " + error) |
| 482 |
|
| 483 |
|
| 484 |
def supports_refcounts(self): |
| 485 |
""" Returns True if the underlying Python implementation supports |
| 486 |
reference counts. |
| 487 |
|
| 488 |
By default, returns True on CPython and False on other |
| 489 |
implementations. |
| 490 |
""" |
| 491 |
return sys.implementation.name == "cpython" |
| 492 |
|
| 493 |
def supports_ordinary_make_module_imports(self): |
| 494 |
""" Returns True if `.make_module(...)` loads modules using a |
| 495 |
standard Python import mechanism (e.g. `importlib.import_module`). |
| 496 |
|
| 497 |
By default, returns True because the base implementation of |
| 498 |
`.make_module(...)` uses an ordinary import. Sub-classes that |
| 499 |
override `.make_module(...)` may also want to override this |
| 500 |
method. |
| 501 |
""" |
| 502 |
return True |
| 503 |
|
| 504 |
|
| 505 |
class HPyDebugCapture: |
| 506 |
""" |
| 507 |
Context manager that sets HPy debug invalid handle hook and remembers the |
| 508 |
number of invalid handles reported. Once closed, sets the invalid handle |
| 509 |
hook back to None. |
| 510 |
""" |
| 511 |
def __init__(self): |
| 512 |
self.invalid_handles_count = 0 |
| 513 |
self.invalid_builders_count = 0 |
| 514 |
|
| 515 |
def _capture_report(self): |
| 516 |
self.invalid_handles_count += 1 |
| 517 |
|
| 518 |
def _capture_builder_report(self): |
| 519 |
self.invalid_builders_count += 1 |
| 520 |
|
| 521 |
def __enter__(self): |
| 522 |
from hpy.universal import _debug |
| 523 |
_debug.set_on_invalid_handle(self._capture_report) |
| 524 |
_debug.set_on_invalid_builder_handle(self._capture_builder_report) |
| 525 |
return self |
| 526 |
|
| 527 |
def __exit__(self, exc_type, exc_val, exc_tb): |
| 528 |
from hpy.universal import _debug |
| 529 |
_debug.set_on_invalid_handle(None) |
| 530 |
_debug.set_on_invalid_builder_handle(None) |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
def c_compile(tmpdir, ext, hpy_devel, hpy_abi, compiler_verbose=0, debug=None): |
| 536 |
"""Compile a C extension module using distutils.""" |
| 537 |
saved_environ = os.environ.copy() |
| 538 |
try: |
| 539 |
outputfilename = _build(tmpdir, ext, hpy_devel, hpy_abi, compiler_verbose, debug) |
| 540 |
outputfilename = os.path.abspath(outputfilename) |
| 541 |
finally: |
| 542 |
|
| 543 |
|
| 544 |
for key, value in saved_environ.items(): |
| 545 |
if os.environ.get(key) != value: |
| 546 |
os.environ[key] = value |
| 547 |
return outputfilename |
| 548 |
|
| 549 |
|
| 550 |
def _build(tmpdir, ext, hpy_devel, hpy_abi, compiler_verbose=0, debug=None): |
| 551 |
|
| 552 |
from distutils.core import Distribution |
| 553 |
import distutils.errors |
| 554 |
import distutils.log |
| 555 |
|
| 556 |
dist = Distribution() |
| 557 |
dist.parse_config_files() |
| 558 |
if debug is None: |
| 559 |
debug = sys.flags.debug |
| 560 |
options_build_ext = dist.get_option_dict('build_ext') |
| 561 |
options_build_ext['debug'] = ('ffiplatform', debug) |
| 562 |
options_build_ext['force'] = ('ffiplatform', True) |
| 563 |
options_build_ext['build_lib'] = ('ffiplatform', tmpdir) |
| 564 |
options_build_ext['build_temp'] = ('ffiplatform', tmpdir) |
| 565 |
options_build_py = dist.get_option_dict('build_py') |
| 566 |
options_build_py['build_lib'] = ('ffiplatform', tmpdir) |
| 567 |
|
| 568 |
|
| 569 |
dist.hpy_abi = hpy_abi |
| 570 |
|
| 571 |
|
| 572 |
dist.hpy_use_static_libs = bool(hpy_devel.get_static_libs(hpy_abi)) |
| 573 |
dist.hpy_ext_modules = [ext] |
| 574 |
|
| 575 |
|
| 576 |
dist.py_modules = [ext.name] |
| 577 |
hpy_devel.fix_distribution(dist) |
| 578 |
|
| 579 |
old_level = distutils.log.set_threshold(0) or 0 |
| 580 |
old_dir = os.getcwd() |
| 581 |
try: |
| 582 |
os.chdir(tmpdir) |
| 583 |
distutils.log.set_verbosity(compiler_verbose) |
| 584 |
dist.run_command('build_ext') |
| 585 |
cmd_obj = dist.get_command_obj('build_ext') |
| 586 |
outputs = cmd_obj.get_outputs() |
| 587 |
sonames = [x for x in outputs if |
| 588 |
not x.endswith(".py") and not x.endswith(".pyc")] |
| 589 |
assert len(sonames) == 1, 'build_ext is not supposed to return multiple DLLs' |
| 590 |
soname = sonames[0] |
| 591 |
finally: |
| 592 |
os.chdir(old_dir) |
| 593 |
distutils.log.set_threshold(old_level) |
| 594 |
|
| 595 |
return soname |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
def trampoline(fun, *args, **kwargs): |
| 601 |
fun(*args, **kwargs) |