| 1 |
import pytest |
| 2 |
from .support import ExtensionCompiler, DefaultExtensionTemplate,\ |
| 3 |
PythonSubprocessRunner, HPyDebugCapture, make_hpy_abi_fixture |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
IS_VALGRIND_RUN = False |
| 7 |
|
| 8 |
|
| 9 |
def _pytest_addoption(parser): |
| 10 |
parser.addoption( |
| 11 |
"--compiler-v", action="store_true", |
| 12 |
help="Print to stdout the commands used to invoke the compiler") |
| 13 |
parser.addoption( |
| 14 |
"--subprocess-v", action="store_true", |
| 15 |
help="Print to stdout the stdout and stderr of Python subprocesses " |
| 16 |
"executed via run_python_subprocess") |
| 17 |
parser.addoption( |
| 18 |
"--dump-dir", |
| 19 |
help="Enables dump mode and specifies where to write generated test " |
| 20 |
"sources. This will then only generate the sources and skip " |
| 21 |
"evaluation of the tests.") |
| 22 |
parser.addoption( |
| 23 |
'--reuse-venv', action="store_true", |
| 24 |
help="Development only: reuse the venv for test_distutils.py instead of " |
| 25 |
"creating a new one for every test") |
| 26 |
|
| 27 |
|
| 28 |
@pytest.hookimpl(trylast=True) |
| 29 |
def pytest_configure(config): |
| 30 |
global IS_VALGRIND_RUN |
| 31 |
IS_VALGRIND_RUN = config.pluginmanager.hasplugin('valgrind_checker') |
| 32 |
config.addinivalue_line( |
| 33 |
"markers", "syncgc: Mark tests that rely on a synchronous GC." |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
hpy_abi = make_hpy_abi_fixture('default') |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture(scope='session') |
| 42 |
def hpy_devel(tmp_path_factory): |
| 43 |
from hpy.devel import HPyDevel |
| 44 |
import distutils.ccompiler |
| 45 |
import distutils.sysconfig |
| 46 |
devel = HPyDevel() |
| 47 |
try: |
| 48 |
hpy_abi = 'universal' |
| 49 |
base = tmp_path_factory.mktemp('hpy_staticlib', numbered=False) |
| 50 |
abi_dir = base / hpy_abi |
| 51 |
abi_dir.mkdir(parents=True, exist_ok=True) |
| 52 |
obj_dir = base / 'obj' / hpy_abi |
| 53 |
obj_dir.mkdir(parents=True, exist_ok=True) |
| 54 |
compiler = distutils.ccompiler.new_compiler() |
| 55 |
distutils.sysconfig.customize_compiler(compiler) |
| 56 |
include_dirs = devel.get_extra_include_dirs() |
| 57 |
include_dirs.append(str(devel.get_include_dir_forbid_python_h())) |
| 58 |
objects = compiler.compile( |
| 59 |
devel.get_extra_sources(), |
| 60 |
output_dir=str(obj_dir), |
| 61 |
include_dirs=include_dirs, |
| 62 |
macros=[('HPY', None), ('HPY_ABI_UNIVERSAL', None)], |
| 63 |
) |
| 64 |
lib_name = 'hpyextra' |
| 65 |
compiler.create_static_lib(objects, lib_name, output_dir=str(abi_dir)) |
| 66 |
lib_filename = compiler.library_filename(lib_name, lib_type='static') |
| 67 |
lib_path = str(abi_dir / lib_filename) |
| 68 |
devel._available_static_libs = { |
| 69 |
'universal': [lib_path], |
| 70 |
'hybrid': [lib_path], |
| 71 |
} |
| 72 |
except Exception as e: |
| 73 |
import warnings |
| 74 |
warnings.warn( |
| 75 |
"HPy static-lib build failed (%s); " |
| 76 |
"falling back to per-test source compilation." % e, |
| 77 |
stacklevel=1, |
| 78 |
) |
| 79 |
return devel |
| 80 |
|
| 81 |
@pytest.fixture |
| 82 |
def leakdetector(hpy_abi): |
| 83 |
""" |
| 84 |
Automatically detect leaks when the hpy_abi == 'debug' |
| 85 |
""" |
| 86 |
from hpy.debug.leakdetector import LeakDetector |
| 87 |
if 'debug' in hpy_abi: |
| 88 |
with LeakDetector() as ld: |
| 89 |
yield ld |
| 90 |
else: |
| 91 |
yield None |
| 92 |
|
| 93 |
@pytest.fixture |
| 94 |
def ExtensionTemplate(): |
| 95 |
return DefaultExtensionTemplate |
| 96 |
|
| 97 |
@pytest.fixture |
| 98 |
def compiler(request, tmpdir, hpy_devel, hpy_abi, ExtensionTemplate): |
| 99 |
compiler_verbose = request.config.getoption('--compiler-v') |
| 100 |
dump_dir = request.config.getoption('--dump-dir') |
| 101 |
if dump_dir: |
| 102 |
|
| 103 |
qname_parts = [] |
| 104 |
if request.module: |
| 105 |
qname_parts.append(request.module.__name__) |
| 106 |
if request.cls: |
| 107 |
qname_parts.append(request.cls.__name__) |
| 108 |
qname_parts.append(request.function.__name__) |
| 109 |
test_dump_dir = "_".join(qname_parts).replace(".", "_") |
| 110 |
dump_dir = Path(dump_dir).joinpath(test_dump_dir) |
| 111 |
dump_dir.mkdir(parents=True, exist_ok=True) |
| 112 |
return ExtensionCompiler(tmpdir, hpy_devel, hpy_abi, |
| 113 |
compiler_verbose=compiler_verbose, |
| 114 |
dump_dir=dump_dir, |
| 115 |
ExtensionTemplate=ExtensionTemplate) |
| 116 |
|
| 117 |
|
| 118 |
@pytest.fixture(scope="session") |
| 119 |
def fatal_exit_code(request): |
| 120 |
import sys |
| 121 |
return { |
| 122 |
"linux": -6, |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
"win32": 0xC0000409, |
| 127 |
}.get(sys.platform, -6) |
| 128 |
|
| 129 |
|
| 130 |
@pytest.fixture |
| 131 |
def python_subprocess(request, hpy_abi): |
| 132 |
verbose = request.config.getoption('--subprocess-v') |
| 133 |
yield PythonSubprocessRunner(verbose, hpy_abi) |
| 134 |
|
| 135 |
|
| 136 |
@pytest.fixture() |
| 137 |
def hpy_debug_capture(request, hpy_abi): |
| 138 |
assert hpy_abi == 'debug' |
| 139 |
with HPyDebugCapture() as reporter: |
| 140 |
yield reporter |