Skip to content

Commit

Permalink
TST: convert cython test from setup.py to meson (numpy#24206)
Browse files Browse the repository at this point in the history
The limited-api test has to wait for a new Meson version (see numpygh-24206).
This converts the regular Cython test for `numpy.core`.

[skip ci]
  • Loading branch information
mattip authored and charris committed Sep 1, 2023
1 parent 4fb4d7a commit ed0ba4d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
27 changes: 27 additions & 0 deletions numpy/core/tests/examples/cython/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
project('checks', 'c', 'cython')

py = import('python').find_installation(pure: false)

cc = meson.get_compiler('c')
cy = meson.get_compiler('cython')

if not cy.version().version_compare('>=0.29.35')
error('tests requires Cython >= 0.29.35')
endif

npy_include_path = run_command(py, [
'-c',
'import os; os.chdir(".."); import numpy; print(os.path.abspath(numpy.get_include()))'
], check: true).stdout().strip()

py.extension_module(
'checks',
'checks.pyx',
install: false,
c_args: [
'-DNPY_NO_DEPRECATED_API=0', # Cython still uses old NumPy C API
# Require 1.25+ to test datetime additions
'-DNPY_TARGET_VERSION=NPY_2_0_API_VERSION',
],
include_directories: [npy_include_path],
)
55 changes: 21 additions & 34 deletions numpy/core/tests/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,31 @@


@pytest.fixture
def install_temp(request, tmp_path):
def install_temp(tmp_path):
# Based in part on test_cython from random.tests.test_extending
if IS_WASM:
pytest.skip("No subprocess")

here = os.path.dirname(__file__)
ext_dir = os.path.join(here, "examples", "cython")

cytest = str(tmp_path / "cytest")

shutil.copytree(ext_dir, cytest)
# build the examples and "install" them into a temporary directory

install_log = str(tmp_path / "tmp_install_log.txt")
subprocess.check_output(
[
sys.executable,
"setup.py",
"build",
"install",
"--prefix", str(tmp_path / "installdir"),
"--single-version-externally-managed",
"--record",
install_log,
],
cwd=cytest,
)

# In order to import the built module, we need its path to sys.path
# so parse that out of the record
with open(install_log) as fid:
for line in fid:
if "checks" in line:
sys.path.append(os.path.dirname(line))
break
else:
raise RuntimeError(f'could not parse "{install_log}"')

srcdir = os.path.join(os.path.dirname(__file__), 'examples', 'cython')
build_dir = tmp_path / "build"
os.makedirs(build_dir, exist_ok=True)
try:
subprocess.check_call(["meson", "--version"])
except FileNotFoundError:
pytest.skip("No usable 'meson' found")
if sys.platform == "win32":
subprocess.check_call(["meson", "setup",
"--buildtype=release",
"--vsenv", str(srcdir)],
cwd=build_dir,
)
else:
subprocess.check_call(["meson", "setup", str(srcdir)],
cwd=build_dir
)
subprocess.check_call(["meson", "compile", "-vv"], cwd=build_dir)

sys.path.append(str(build_dir))

def test_is_timedelta64_object(install_temp):
import checks
Expand Down

0 comments on commit ed0ba4d

Please sign in to comment.