codekingpro/portable-devtools
115k
1import os
2import shutil
3import subprocess
4import sys
5import sysconfig
6import warnings
7from importlib.util import module_from_spec, spec_from_file_location
8
9import pytest
10
11import numpy as np
12from numpy.testing import IS_EDITABLE, IS_WASM
13
14try:
15 import cffi
16except ImportError:
17 cffi = None
18
19if sys.flags.optimize > 1:
20 # no docstrings present to inspect when PYTHONOPTIMIZE/Py_OptimizeFlag > 1
21 # cffi cannot succeed
22 cffi = None
23
24try:
25 with warnings.catch_warnings(record=True) as w:
26 # numba issue gh-4733
27 warnings.filterwarnings('always', '', DeprecationWarning)
28 import numba
29except (ImportError, SystemError):
30 # Certain numpy/numba versions trigger a SystemError due to a numba bug
31 numba = None
32
33try:
34 import cython
35 from Cython.Compiler.Version import version as cython_version
36except ImportError:
37 cython = None
38else:
39 from numpy._utils import _pep440
40 # Note: keep in sync with the one in pyproject.toml
41 required_version = '3.0.6'
42 if _pep440.parse(cython_version) < _pep440.Version(required_version):
43 # too old or wrong cython, skip the test
44 cython = None
45
46
47@pytest.mark.skipif(
48 IS_EDITABLE,
49 reason='Editable install cannot find .pxd headers'
50)
51@pytest.mark.skipif(
52 sys.platform == "win32" and sys.maxsize < 2**32,
53 reason="Failing in 32-bit Windows wheel build job, skip for now"
54)
55@pytest.mark.skipif(IS_WASM, reason="Can't start subprocess")
56@pytest.mark.skipif(cython is None, reason="requires cython")
57@pytest.mark.skipif(sysconfig.get_platform() == 'win-arm64',
58 reason='Meson unable to find MSVC linker on win-arm64')
59@pytest.mark.slow
60@pytest.mark.thread_unsafe(
61 reason="building cython code in a subprocess doesn't make sense to do in many "
62 "threads and sometimes crashes"
63)
64def test_cython(tmp_path):
65 import glob
66 # build the examples in a temporary directory
67 srcdir = os.path.join(os.path.dirname(__file__), '..')
68 shutil.copytree(srcdir, tmp_path / 'random')
69 build_dir = tmp_path / 'random' / '_examples' / 'cython'
70 target_dir = build_dir / "build"
71 os.makedirs(target_dir, exist_ok=True)
72 # Ensure we use the correct Python interpreter even when `meson` is
73 # installed in a different Python environment (see gh-24956)
74 native_file = str(build_dir / 'interpreter-native-file.ini')
75 with open(native_file, 'w') as f:
76 f.write("[binaries]\n")
77 f.write(f"python = '{sys.executable}'\n")
78 f.write(f"python3 = '{sys.executable}'")
79 if sys.platform == "win32":
80 subprocess.check_call(["meson", "setup",
81 "--buildtype=release",
82 "--vsenv", "--native-file", native_file,
83 str(build_dir)],
84 cwd=target_dir,
85 )
86 else:
87 subprocess.check_call(["meson", "setup",
88 "--native-file", native_file, str(build_dir)],
89 cwd=target_dir
90 )
91 subprocess.check_call(["meson", "compile", "-vv"], cwd=target_dir)
92
93 # gh-16162: make sure numpy's __init__.pxd was used for cython
94 # not really part of this test, but it is a convenient place to check
95
96 g = glob.glob(str(target_dir / "*" / "extending.pyx.c"))
97 with open(g[0]) as fid:
98 txt_to_find = 'NumPy API declarations from "numpy/__init__'
99 for line in fid:
100 if txt_to_find in line:
101 break
102 else:
103 assert False, f"Could not find '{txt_to_find}' in C file, wrong pxd used"
104 # import without adding the directory to sys.path
105 suffix = sysconfig.get_config_var('EXT_SUFFIX')
106
107 def load(modname):
108 so = (target_dir / modname).with_suffix(suffix)
109 spec = spec_from_file_location(modname, so)
110 mod = module_from_spec(spec)
111 spec.loader.exec_module(mod)
112 return mod
113
114 # test that the module can be imported
115 load("extending")
116 load("extending_cpp")
117 # actually test the cython c-extension
118 extending_distributions = load("extending_distributions")
119 from numpy.random import PCG64
120 values = extending_distributions.uniforms_ex(PCG64(0), 10, 'd')
121 assert values.shape == (10,)
122 assert values.dtype == np.float64
123
124@pytest.mark.skipif(numba is None or cffi is None,
125 reason="requires numba and cffi")
126def test_numba():
127 from numpy.random._examples.numba import extending # noqa: F401
128
129@pytest.mark.skipif(cffi is None, reason="requires cffi")
130def test_cffi():
131 from numpy.random._examples.cffi import extending # noqa: F401
132 