From 77eab45df2328bb0068e1333cfd6e1bd6777d2d8 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Mon, 20 Nov 2023 22:56:35 -0500 Subject: [PATCH] Adjust macros and typedefs for Numpy C API - Define `Py_LIMITED_API` and `NPY_TARGET_VERSION` macros in setup.py rather than in the C source code. - Don't define Numpy's `struct _typeobject` if we are building for PyPy because PyPy doesn't implement Python's limited C API (see https://github.com/liberfa/pyerfa/pull/120). - Define `struct _typeobject` workaround after including Python.h, not before, because on PyPy, Python.h defines the macros that we depend on to detect whether we are building under PyPy. - Use `NPY_TARGET_VERSION` for builds that are backwards-compatible with old versions of Numpy. - Build with any version of Numpy >=1.25 and <2 (see https://github.com/liberfa/pyerfa/pull/121). --- pyproject.toml | 2 +- setup.py | 3 +++ src/core.c | 37 ++++++++++++++++++------------------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a7c4049..1a98f35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,6 @@ requires = ["setuptools>=42.0.0", "setuptools_scm", "wheel", "extension-helpers", - "numpy>=1.25"] + "numpy>=1.25,<2"] build-backend = 'setuptools.build_meta' diff --git a/setup.py b/setup.py index d7d110e..1d64191 100755 --- a/setup.py +++ b/setup.py @@ -34,6 +34,9 @@ def get_extensions(): 'define_macros': [ ('GSL_RANGE_CHECK_OFF', None), ('HAVE_INLINE', None), + ('Py_LIMITED_API', 0x03090000), + ('NPY_TARGET_VERSION', 'NPY_1_19_API_VERSION'), + ('NPY_NO_DEPRECATED_API', 'NPY_1_19_API_VERSION'), ], 'extra_compile_args': [ '-std=gnu11', diff --git a/src/core.c b/src/core.c index 752688e..74a7899 100644 --- a/src/core.c +++ b/src/core.c @@ -15,25 +15,6 @@ * along with this program. If not, see . */ -#define NPY_NO_DEPRECATED_API NPY_1_19_API_VERSION -#define Py_LIMITED_API 0x03090000 - -/* FIXME: - * The Numpy C-API defines PyArrayDescr_Type as: - * - * #define PyArrayDescr_Type (*(PyTypeObject *)PyArray_API[3]) - * - * and then in some places we need to take its address, &PyArrayDescr_Type. - * This is fine in GCC 10 and Clang, but earlier versions of GCC complain: - * - * error: dereferencing pointer to incomplete type 'PyTypeObject' - * {aka 'struct _typeobject'} - * - * As a workaround, provide a faux forward declaration for PyTypeObject. - * See https://github.com/numpy/numpy/issues/16970. - */ -struct _typeobject {}; - #ifdef _OPENMP #include #endif @@ -56,6 +37,24 @@ WARNINGS_POP #include "cubic_interp.h" #include "omp_interruptible.h" +/* FIXME: + * The Numpy C-API defines PyArrayDescr_Type as: + * + * #define PyArrayDescr_Type (*(PyTypeObject *)PyArray_API[3]) + * + * and then in some places we need to take its address, &PyArrayDescr_Type. + * This is fine in GCC 10 and Clang, but earlier versions of GCC complain: + * + * error: dereferencing pointer to incomplete type 'PyTypeObject' + * {aka 'struct _typeobject'} + * + * As a workaround, provide a faux forward declaration for PyTypeObject. + * See https://github.com/numpy/numpy/issues/16970. + */ +#ifndef PYPY_VERSION +struct _typeobject {}; +#endif + static PyObject * get_num_threads(PyObject *NPY_UNUSED(module), void *NPY_UNUSED(args))