Skip to content

Commit

Permalink
BLD: switch to Cython 3.0, forbid deprecated Numpy C API in generated…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
neutrinoceros committed Aug 18, 2023
1 parent b765e14 commit f137de0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 1 addition & 3 deletions 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",
]

Expand Down
22 changes: 20 additions & 2 deletions setup.py
Expand Up @@ -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,
),
]

Expand All @@ -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,
),
]

Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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)


Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f137de0

Please sign in to comment.