Skip to content

Commit

Permalink
Adjust macros and typedefs for Numpy C API
Browse files Browse the repository at this point in the history
- 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 liberfa/pyerfa#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 liberfa/pyerfa#121).
  • Loading branch information
lpsinger committed Nov 21, 2023
1 parent 2fc42ab commit 77eab45
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
37 changes: 18 additions & 19 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#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 <omp.h>
#endif
Expand All @@ -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))
Expand Down

0 comments on commit 77eab45

Please sign in to comment.