Skip to content

Commit cdebcf3

Browse files
authored
Merge pull request #1 from mtsokol/rename-numpy-core-c-api
MAINT: Check numpy version in C-API
2 parents 9b79ed9 + 2f08ba1 commit cdebcf3

File tree

5 files changed

+30
-42
lines changed

5 files changed

+30
-42
lines changed

numpy/_core/code_generators/generate_numpy_api.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@
4141
4242
%s
4343
44+
#include "numpy/utils.h"
45+
4446
#if !defined(NO_IMPORT_ARRAY) && !defined(NO_IMPORT)
4547
static int
4648
_import_array(void)
4749
{
4850
int st;
49-
PyObject *numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
50-
PyObject *c_api = NULL;
51-
51+
PyObject *numpy = _npy_import_numpy_multiarray_umath();
5252
if (numpy == NULL) {
53+
PyErr_SetString(PyExc_ImportError,
54+
"_multiarray_umath failed to import");
5355
return -1;
5456
}
55-
c_api = PyObject_GetAttrString(numpy, "_ARRAY_API");
57+
58+
PyObject *c_api = PyObject_GetAttrString(numpy, "_ARRAY_API");
5659
Py_DECREF(numpy);
5760
if (c_api == NULL) {
5861
PyErr_SetString(PyExc_AttributeError, "_ARRAY_API not found");

numpy/_core/code_generators/generate_ufunc_api.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
3131
%s
3232
33+
#include "numpy/utils.h"
34+
3335
static inline int
3436
_import_umath(void)
3537
{
36-
PyObject *numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
37-
PyObject *c_api = NULL;
38-
38+
PyObject *numpy = _npy_import_numpy_multiarray_umath();
3939
if (numpy == NULL) {
4040
PyErr_SetString(PyExc_ImportError,
41-
"numpy._core._multiarray_umath failed to import");
41+
"_multiarray_umath failed to import");
4242
return -1;
4343
}
44-
c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
44+
45+
PyObject *c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
4546
Py_DECREF(numpy);
4647
if (c_api == NULL) {
4748
PyErr_SetString(PyExc_AttributeError, "_UFUNC_API not found");

numpy/_core/include/numpy/experimental_dtype_api.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
#include <Python.h>
129129
#include "ndarraytypes.h"
130130
#include "_dtype_api.h"
131+
#include "utils.h"
131132

132133
/*
133134
* The contents of PyArrayMethodObject are currently opaque (is there a way
@@ -353,7 +354,7 @@ import_experimental_dtype_api(int version)
353354
return 0;
354355
}
355356

356-
PyObject *multiarray = PyImport_ImportModule("numpy._core._multiarray_umath");
357+
PyObject *multiarray = _npy_import_numpy_multiarray_umath();
357358
if (multiarray == NULL) {
358359
return -1;
359360
}

numpy/_core/include/numpy/oldnumeric.h

Lines changed: 0 additions & 32 deletions
This file was deleted.

numpy/_core/include/numpy/utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_
22
#define NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_
33

4+
#include <Python.h>
5+
46
#ifndef __COMP_NPY_UNUSED
57
#if defined(__GNUC__)
68
#define __COMP_NPY_UNUSED __attribute__ ((__unused__))
@@ -34,4 +36,17 @@
3436
#define NPY_CAT_(a, b) NPY_CAT__(a, b)
3537
#define NPY_CAT(a, b) NPY_CAT_(a, b)
3638

39+
static PyObject *_npy_import_numpy_multiarray_umath()
40+
{
41+
PyObject *multiarray = PyImport_ImportModule("numpy._core._multiarray_umath");
42+
if (
43+
multiarray == NULL &&
44+
PyErr_ExceptionMatches(PyExc_ModuleNotFoundError)
45+
) {
46+
PyErr_Clear();
47+
multiarray = PyImport_ImportModule("numpy.core._multiarray_umath");
48+
}
49+
return multiarray;
50+
}
51+
3752
#endif /* NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_ */

0 commit comments

Comments
 (0)