(tmp_path)
| 62 | "threads and sometimes crashes" |
| 63 | ) |
| 64 | def 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 | run_subprocess(["meson", "setup", |
| 81 | "--buildtype=release", |
| 82 | "--vsenv", "--native-file", native_file, |
| 83 | str(build_dir)], |
| 84 | target_dir) |
| 85 | else: |
| 86 | run_subprocess(["meson", "setup", |
| 87 | "--native-file", native_file, str(build_dir)], |
| 88 | target_dir) |
| 89 | run_subprocess(["meson", "compile", "-vv"], target_dir) |
| 90 | |
| 91 | # gh-16162: make sure numpy's __init__.pxd was used for cython |
| 92 | # not really part of this test, but it is a convenient place to check |
| 93 | |
| 94 | g = glob.glob(str(target_dir / "*" / "extending.pyx.c")) |
| 95 | with open(g[0]) as fid: |
| 96 | txt_to_find = 'NumPy API declarations from "numpy/__init__' |
| 97 | for line in fid: |
| 98 | if txt_to_find in line: |
| 99 | break |
| 100 | else: |
| 101 | assert False, f"Could not find '{txt_to_find}' in C file, wrong pxd used" |
| 102 | # import without adding the directory to sys.path |
| 103 | suffix = sysconfig.get_config_var('EXT_SUFFIX') |
| 104 | |
| 105 | def load(modname): |
| 106 | so = (target_dir / modname).with_suffix(suffix) |
| 107 | spec = spec_from_file_location(modname, so) |
| 108 | mod = module_from_spec(spec) |
| 109 | spec.loader.exec_module(mod) |
| 110 | return mod |
| 111 | |
| 112 | # test that the module can be imported |
| 113 | load("extending") |
| 114 | load("extending_cpp") |
| 115 | # actually test the cython c-extension |
| 116 | extending_distributions = load("extending_distributions") |
| 117 | from numpy.random import PCG64 |
| 118 | values = extending_distributions.uniforms_ex(PCG64(0), 10, 'd') |
| 119 | assert values.shape == (10,) |
| 120 | assert values.dtype == np.float64 |
| 121 |
nothing calls this directly
no test coverage detected
searching dependent graphs…