From ed0ba4da66b99244978ac1142cd49df44eeda3cf Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Thu, 31 Aug 2023 12:12:50 +0300 Subject: [PATCH] TST: convert cython test from setup.py to meson (#24206) The limited-api test has to wait for a new Meson version (see gh-24206). This converts the regular Cython test for `numpy.core`. [skip ci] --- numpy/core/tests/examples/cython/meson.build | 27 ++++++++++ numpy/core/tests/test_cython.py | 55 ++++++++------------ 2 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 numpy/core/tests/examples/cython/meson.build diff --git a/numpy/core/tests/examples/cython/meson.build b/numpy/core/tests/examples/cython/meson.build new file mode 100644 index 000000000000..12fc640b88b4 --- /dev/null +++ b/numpy/core/tests/examples/cython/meson.build @@ -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], +) diff --git a/numpy/core/tests/test_cython.py b/numpy/core/tests/test_cython.py index e916adceb114..29473f5ba424 100644 --- a/numpy/core/tests/test_cython.py +++ b/numpy/core/tests/test_cython.py @@ -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