Skip to content

Commit

Permalink
Merge pull request #1 from mtsokol/rename-numpy-core-c-api
Browse files Browse the repository at this point in the history
MAINT: Check numpy version in C-API
  • Loading branch information
mtsokol committed Sep 11, 2023
2 parents 9b79ed9 + 2f08ba1 commit cdebcf3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 42 deletions.
11 changes: 7 additions & 4 deletions numpy/_core/code_generators/generate_numpy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@
%s
#include "numpy/utils.h"
#if !defined(NO_IMPORT_ARRAY) && !defined(NO_IMPORT)
static int
_import_array(void)
{
int st;
PyObject *numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
PyObject *c_api = NULL;
PyObject *numpy = _npy_import_numpy_multiarray_umath();
if (numpy == NULL) {
PyErr_SetString(PyExc_ImportError,
"_multiarray_umath failed to import");
return -1;
}
c_api = PyObject_GetAttrString(numpy, "_ARRAY_API");
PyObject *c_api = PyObject_GetAttrString(numpy, "_ARRAY_API");
Py_DECREF(numpy);
if (c_api == NULL) {
PyErr_SetString(PyExc_AttributeError, "_ARRAY_API not found");
Expand Down
11 changes: 6 additions & 5 deletions numpy/_core/code_generators/generate_ufunc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
%s
#include "numpy/utils.h"
static inline int
_import_umath(void)
{
PyObject *numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
PyObject *c_api = NULL;
PyObject *numpy = _npy_import_numpy_multiarray_umath();
if (numpy == NULL) {
PyErr_SetString(PyExc_ImportError,
"numpy._core._multiarray_umath failed to import");
"_multiarray_umath failed to import");
return -1;
}
c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
PyObject *c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
Py_DECREF(numpy);
if (c_api == NULL) {
PyErr_SetString(PyExc_AttributeError, "_UFUNC_API not found");
Expand Down
3 changes: 2 additions & 1 deletion numpy/_core/include/numpy/experimental_dtype_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
#include <Python.h>
#include "ndarraytypes.h"
#include "_dtype_api.h"
#include "utils.h"

/*
* The contents of PyArrayMethodObject are currently opaque (is there a way
Expand Down Expand Up @@ -353,7 +354,7 @@ import_experimental_dtype_api(int version)
return 0;
}

PyObject *multiarray = PyImport_ImportModule("numpy._core._multiarray_umath");
PyObject *multiarray = _npy_import_numpy_multiarray_umath();
if (multiarray == NULL) {
return -1;
}
Expand Down
32 changes: 0 additions & 32 deletions numpy/_core/include/numpy/oldnumeric.h

This file was deleted.

15 changes: 15 additions & 0 deletions numpy/_core/include/numpy/utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_
#define NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_

#include <Python.h>

#ifndef __COMP_NPY_UNUSED
#if defined(__GNUC__)
#define __COMP_NPY_UNUSED __attribute__ ((__unused__))
Expand Down Expand Up @@ -34,4 +36,17 @@
#define NPY_CAT_(a, b) NPY_CAT__(a, b)
#define NPY_CAT(a, b) NPY_CAT_(a, b)

static PyObject *_npy_import_numpy_multiarray_umath()
{
PyObject *multiarray = PyImport_ImportModule("numpy._core._multiarray_umath");
if (
multiarray == NULL &&
PyErr_ExceptionMatches(PyExc_ModuleNotFoundError)
) {
PyErr_Clear();
multiarray = PyImport_ImportModule("numpy.core._multiarray_umath");
}
return multiarray;
}

#endif /* NUMPY_CORE_INCLUDE_NUMPY_UTILS_H_ */

0 comments on commit cdebcf3

Please sign in to comment.