| 57 | @pytest.mark.skipif(cython is None, reason="requires cython") |
| 58 | @pytest.mark.slow |
| 59 | def test_cython(tmp_path): |
| 60 | import glob |
| 61 | # build the examples in a temporary directory |
| 62 | srcdir = os.path.join(os.path.dirname(__file__), '..') |
| 63 | shutil.copytree(srcdir, tmp_path / 'random') |
| 64 | build_dir = tmp_path / 'random' / '_examples' / 'cython' |
| 65 | target_dir = build_dir / "build" |
| 66 | os.makedirs(target_dir, exist_ok=True) |
| 67 | if sys.platform == "win32": |
| 68 | subprocess.check_call(["meson", "setup", |
| 69 | "--buildtype=release", |
| 70 | "--vsenv", str(build_dir)], |
| 71 | cwd=target_dir, |
| 72 | ) |
| 73 | else: |
| 74 | subprocess.check_call(["meson", "setup", str(build_dir)], |
| 75 | cwd=target_dir |
| 76 | ) |
| 77 | subprocess.check_call(["meson", "compile", "-vv"], cwd=target_dir) |
| 78 | |
| 79 | # gh-16162: make sure numpy's __init__.pxd was used for cython |
| 80 | # not really part of this test, but it is a convenient place to check |
| 81 | |
| 82 | g = glob.glob(str(target_dir / "*" / "extending.pyx.c")) |
| 83 | with open(g[0]) as fid: |
| 84 | txt_to_find = 'NumPy API declarations from "numpy/__init__' |
| 85 | for i, line in enumerate(fid): |
| 86 | if txt_to_find in line: |
| 87 | break |
| 88 | else: |
| 89 | assert False, ("Could not find '{}' in C file, " |
| 90 | "wrong pxd used".format(txt_to_find)) |
| 91 | # import without adding the directory to sys.path |
| 92 | suffix = sysconfig.get_config_var('EXT_SUFFIX') |
| 93 | |
| 94 | def load(modname): |
| 95 | so = (target_dir / modname).with_suffix(suffix) |
| 96 | spec = spec_from_file_location(modname, so) |
| 97 | mod = module_from_spec(spec) |
| 98 | spec.loader.exec_module(mod) |
| 99 | return mod |
| 100 | |
| 101 | # test that the module can be imported |
| 102 | load("extending") |
| 103 | load("extending_cpp") |
| 104 | # actually test the cython c-extension |
| 105 | extending_distributions = load("extending_distributions") |
| 106 | from numpy.random import PCG64 |
| 107 | values = extending_distributions.uniforms_ex(PCG64(0), 10, 'd') |
| 108 | assert values.shape == (10,) |
| 109 | assert values.dtype == np.float64 |
| 110 | |
| 111 | @pytest.mark.skipif(numba is None or cffi is None, |
| 112 | reason="requires numba and cffi") |