Debian Codegraph @

extra_tests/hpy_tests/conftest.py

raw symbols SCIP
1 import sys
2 import pytest
3
4 try:
5 import _hpy_universal
6 disable = False
7 except ImportError:
8 disable = True
9
10 if sys.platform == "win32":
11 disable = True
12
13
14 def pytest_ignore_collect(path, config):
15 return disable
16
17
18 @pytest.fixture(scope='session')
19 def hpy_devel(tmp_path_factory):
20 """
21 Session-scoped HPyDevel. If possible, compile the HPy runtime helper
22 sources into a single static library up-front so that individual test
23 extensions only need to link against it instead of recompiling those
24 sources every time (mirrors hpy PR #379).
25 """
26 from hpy.devel import HPyDevel
27 devel = HPyDevel()
28 try:
29 _build_static_lib(devel, 'universal', tmp_path_factory)
30 except Exception as e:
31 # If the static-lib build fails we fall back to per-test source
32 # compilation. Print a warning so the developer knows why tests
33 # are slower than expected but do not abort the session.
34 import warnings
35 warnings.warn(
36 "HPy static-lib build failed (%s); "
37 "falling back to per-test source compilation." % e,
38 stacklevel=1,
39 )
40 return devel
41
42
43 def _build_static_lib(hpy_devel, hpy_abi, tmp_path_factory):
44 """
45 Compile the HPy extra (helper) sources into a static archive and register
46 it on *hpy_devel* so that ``get_static_libs(hpy_abi)`` returns it.
47
48 The archive is placed under::
49
50 <session-tmp>/hpy_staticlib/<hpy_abi>/lib<name>.a
51 """
52 import distutils.ccompiler
53 import distutils.sysconfig
54
55 base = tmp_path_factory.mktemp('hpy_staticlib', numbered=False)
56 abi_dir = base / hpy_abi
57 abi_dir.mkdir(parents=True, exist_ok=True)
58 obj_dir = base / 'obj' / hpy_abi
59 obj_dir.mkdir(parents=True, exist_ok=True)
60
61 compiler = distutils.ccompiler.new_compiler()
62 distutils.sysconfig.customize_compiler(compiler)
63
64 include_dirs = hpy_devel.get_extra_include_dirs()
65 include_dirs.append(str(hpy_devel.get_include_dir_forbid_python_h()))
66
67 macros = [('HPY', None), ('HPY_ABI_UNIVERSAL', None)]
68
69 objects = compiler.compile(
70 hpy_devel.get_extra_sources(),
71 output_dir=str(obj_dir),
72 include_dirs=include_dirs,
73 macros=macros,
74 )
75
76 lib_name = 'hpyextra'
77 compiler.create_static_lib(objects, lib_name, output_dir=str(abi_dir))
78
79 lib_filename = compiler.library_filename(lib_name, lib_type='static')
80 hpy_devel._available_static_libs = {
81 hpy_abi: [str(abi_dir / lib_filename)],
82 }