From f137de005216cfc5476184c9bfa6c033b5621447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Fri, 28 Jul 2023 14:44:22 +0200 Subject: [PATCH] BLD: switch to Cython 3.0, forbid deprecated Numpy C API in generated code --- pyproject.toml | 4 +--- setup.py | 22 +++++++++++++++++-- .../rockstar/rockstar_interface.pyx | 4 ++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1f2f54e7..ab9e13a4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,7 @@ [build-system] requires = [ "setuptools>=61.2", - # see https://github.com/numpy/numpy/pull/18389 - "wheel>=0.36.2", - "Cython>=0.29.21,<3.0", + "Cython>=3.0", "oldest-supported-numpy", ] diff --git a/setup.py b/setup.py index 838871dd..44616f03 100644 --- a/setup.py +++ b/setup.py @@ -41,11 +41,15 @@ def get_version(filename): else: std_libs = ["m"] + +define_macros = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")] + cython_extensions = [ Extension( "yt_astro_analysis.ppv_cube.ppv_utils", ["yt_astro_analysis/ppv_cube/ppv_utils.pyx"], libraries=std_libs, + define_macros=define_macros, ), ] @@ -57,10 +61,12 @@ def get_version(filename): "yt_astro_analysis/halo_analysis/halo_finding/fof/kd.c", ], libraries=std_libs, + define_macros=define_macros, ), Extension( "yt_astro_analysis.halo_analysis.halo_finding.hop.EnzoHop", glob.glob("yt_astro_analysis/halo_analysis/halo_finding/hop/*.c"), + define_macros=define_macros, ), ] @@ -80,10 +86,12 @@ def get_version(filename): Extension( "yt_astro_analysis.halo_analysis.halo_finding.rockstar.rockstar_interface", sources=[os.path.join(rockstar_extdir, "rockstar_interface.pyx")], + define_macros=define_macros, ), Extension( "yt_astro_analysis.halo_analysis.halo_finding.rockstar.rockstar_groupies", sources=[os.path.join(rockstar_extdir, "rockstar_groupies.pyx")], + define_macros=define_macros, ), ] for ext in rockstar_extensions: @@ -94,13 +102,20 @@ def get_version(filename): extensions += rockstar_extensions +CYTHONIZE_KWARGS = { + "compiler_directives": {"language_level": 3}, +} + + class build_ext(_build_ext): # subclass setuptools extension builder to avoid importing cython and numpy # at top level in setup.py. See http://stackoverflow.com/a/21621689/1382869 def finalize_options(self): from Cython.Build import cythonize - self.distribution.ext_modules[:] = cythonize(self.distribution.ext_modules) + self.distribution.ext_modules[:] = cythonize( + self.distribution.ext_modules, **CYTHONIZE_KWARGS + ) _build_ext.finalize_options(self) # Prevent numpy from thinking it is still in its setup process # see http://stackoverflow.com/a/21621493/1382869 @@ -123,7 +138,10 @@ def run(self): # Make sure the compiled Cython files in the distribution are up-to-date from Cython.Build import cythonize - cythonize(cython_extensions) + cythonize( + cython_extensions, + **CYTHONIZE_KWARGS, + ) _sdist.run(self) diff --git a/yt_astro_analysis/halo_analysis/halo_finding/rockstar/rockstar_interface.pyx b/yt_astro_analysis/halo_analysis/halo_finding/rockstar/rockstar_interface.pyx index 364144f1..a67a85d3 100644 --- a/yt_astro_analysis/halo_analysis/halo_finding/rockstar/rockstar_interface.pyx +++ b/yt_astro_analysis/halo_analysis/halo_finding/rockstar/rockstar_interface.pyx @@ -180,7 +180,7 @@ cdef import from "config_vars.h": # Forward declare cdef class RockstarInterface -cdef void rh_analyze_halo(halo *h, particle *hp): +cdef void rh_analyze_halo(halo *h, particle *hp) noexcept: # I don't know why, but sometimes we get halos with 0 particles. if h.num_p == 0: return cdef particleflat[:] pslice @@ -190,7 +190,7 @@ cdef void rh_analyze_halo(halo *h, particle *hp): cb(rh.ds, parray) # This is where we call our functions -cdef void rh_read_particles(char *filename, particle **p, np.int64_t *num_p): +cdef void rh_read_particles(char *filename, particle **p, np.int64_t *num_p) noexcept: global SCALE_NOW cdef np.float64_t left_edge[6] cdef np.ndarray[np.int64_t, ndim=1] arri # index