|
1 | | -import importlib |
| 1 | +from importlib.util import spec_from_file_location, module_from_spec |
2 | 2 | import os |
3 | 3 | import pathlib |
4 | 4 | import pytest |
@@ -60,7 +60,9 @@ def test_cython(tmp_path): |
60 | 60 | # We don't want a wheel build, so do the steps in a controlled way |
61 | 61 | # The meson.build file is not copied as part of the build, so generate it |
62 | 62 | with open(build_dir / "meson.build", "wt", encoding="utf-8") as fid: |
63 | | - fid.write(textwrap.dedent("""\ |
| 63 | + get_inc = ('import os; os.chdir(".."); import numpy; ' |
| 64 | + 'print(os.path.abspath(numpy.get_include() + "../../.."))') |
| 65 | + fid.write(textwrap.dedent(f"""\ |
64 | 66 | project('random-build-examples', 'c', 'cpp', 'cython') |
65 | 67 |
|
66 | 68 | # https://mesonbuild.com/Python-module.html |
@@ -77,10 +79,8 @@ def test_cython(tmp_path): |
77 | 79 | error('tests requires Cython >= 0.29.35') |
78 | 80 | endif |
79 | 81 |
|
80 | | - _numpy_abs = run_command(py3, |
81 | | - ['-c', 'import os; os.chdir(".."); import numpy; print(os.path.abspath(numpy.get_include() + "../../.."))'], |
82 | | - check: true |
83 | | - ).stdout().strip() |
| 82 | + _numpy_abs = run_command(py3, ['-c', '{get_inc}'], |
| 83 | + check: true).stdout().strip() |
84 | 84 |
|
85 | 85 | npymath_path = _numpy_abs / 'core' / 'lib' |
86 | 86 | npy_include_path = _numpy_abs / 'core' / 'include' |
@@ -123,11 +123,11 @@ def test_cython(tmp_path): |
123 | 123 | # import without adding the directory to sys.path |
124 | 124 | so1 = sorted(glob.glob(str(target_dir / "extending.*")))[0] |
125 | 125 | so2 = sorted(glob.glob(str(target_dir / "extending_distributions.*")))[0] |
126 | | - spec1 = importlib.util.spec_from_file_location("extending", so1) |
127 | | - spec2 = importlib.util.spec_from_file_location("extending_distributions", so2) |
128 | | - extending = importlib.util.module_from_spec(spec1) |
| 126 | + spec1 = spec_from_file_location("extending", so1) |
| 127 | + spec2 = spec_from_file_location("extending_distributions", so2) |
| 128 | + extending = module_from_spec(spec1) |
129 | 129 | spec1.loader.exec_module(extending) |
130 | | - extending_distributions = importlib.util.module_from_spec(spec2) |
| 130 | + extending_distributions = module_from_spec(spec2) |
131 | 131 | spec2.loader.exec_module(extending_distributions) |
132 | 132 | # actually test the cython c-extension |
133 | 133 | from numpy.random import PCG64 |
|