Skip to content

Commit 69075c1

Browse files
committed
MNT: move cached imports into a global struct
1 parent baee891 commit 69075c1

27 files changed

+326
-315
lines changed

numpy/_core/src/common/npy_ctypes.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <Python.h>
55

66
#include "npy_import.h"
7+
#include "multiarraymodule.h"
78

89
/*
910
* Check if a python type is a ctypes class.
@@ -17,16 +18,17 @@
1718
static inline int
1819
npy_ctypes_check(PyTypeObject *obj)
1920
{
20-
static PyObject *py_func = NULL;
2121
PyObject *ret_obj;
2222
int ret;
2323

24-
npy_cache_import("numpy._core._internal", "npy_ctypes_check", &py_func);
25-
if (py_func == NULL) {
24+
npy_cache_import("numpy._core._internal", "npy_ctypes_check",
25+
&npy_ma_global_data->npy_ctypes_check);
26+
if (npy_ma_global_data->npy_ctypes_check == NULL) {
2627
goto fail;
2728
}
2829

29-
ret_obj = PyObject_CallFunctionObjArgs(py_func, (PyObject *)obj, NULL);
30+
ret_obj = PyObject_CallFunctionObjArgs(npy_ma_global_data->npy_ctypes_check,
31+
(PyObject *)obj, NULL);
3032
if (ret_obj == NULL) {
3133
goto fail;
3234
}

numpy/_core/src/multiarray/arrayfunction_override.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ get_args_and_kwargs(
252252
static void
253253
set_no_matching_types_error(PyObject *public_api, PyObject *types)
254254
{
255-
static PyObject *errmsg_formatter = NULL;
256255
/* No acceptable override found, raise TypeError. */
257256
npy_cache_import("numpy._core._internal",
258257
"array_function_errmsg_formatter",
259-
&errmsg_formatter);
260-
if (errmsg_formatter != NULL) {
258+
&npy_ma_global_data->array_function_errmsg_formatter);
259+
if (npy_ma_global_data->array_function_errmsg_formatter != NULL) {
261260
PyObject *errmsg = PyObject_CallFunctionObjArgs(
262-
errmsg_formatter, public_api, types, NULL);
261+
npy_ma_global_data->array_function_errmsg_formatter,
262+
public_api, types, NULL);
263263
if (errmsg != NULL) {
264264
PyErr_SetObject(PyExc_TypeError, errmsg);
265265
Py_DECREF(errmsg);

numpy/_core/src/multiarray/arrayobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,8 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
927927
*/
928928
if (result == NULL
929929
&& (cmp_op == Py_EQ || cmp_op == Py_NE)
930-
&& PyErr_ExceptionMatches(npy_UFuncNoLoopError)) {
930+
&& PyErr_ExceptionMatches(
931+
npy_ma_global_data->_UFuncNoLoopError)) {
931932
PyErr_Clear();
932933

933934
PyArrayObject *array_other = (PyArrayObject *)PyArray_FROM_O(other);

numpy/_core/src/multiarray/common.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "npy_cpu_dispatch.h"
99
#include "numpy/npy_cpu.h"
1010

11+
#include "multiarraymodule.h"
1112
#include "npy_import.h"
1213
#include <limits.h>
1314

@@ -139,25 +140,14 @@ check_and_adjust_axis_msg(int *axis, int ndim, PyObject *msg_prefix)
139140
{
140141
/* Check that index is valid, taking into account negative indices */
141142
if (NPY_UNLIKELY((*axis < -ndim) || (*axis >= ndim))) {
142-
/*
143-
* Load the exception type, if we don't already have it. Unfortunately
144-
* we don't have access to npy_cache_import here
145-
*/
146-
static PyObject *AxisError_cls = NULL;
147-
PyObject *exc;
148-
149-
npy_cache_import("numpy.exceptions", "AxisError", &AxisError_cls);
150-
if (AxisError_cls == NULL) {
151-
return -1;
152-
}
153-
154143
/* Invoke the AxisError constructor */
155-
exc = PyObject_CallFunction(AxisError_cls, "iiO",
156-
*axis, ndim, msg_prefix);
144+
PyObject *exc = PyObject_CallFunction(
145+
npy_ma_global_data->AxisError, "iiO", *axis, ndim,
146+
msg_prefix);
157147
if (exc == NULL) {
158148
return -1;
159149
}
160-
PyErr_SetObject(AxisError_cls, exc);
150+
PyErr_SetObject(npy_ma_global_data->AxisError, exc);
161151
Py_DECREF(exc);
162152

163153
return -1;

numpy/_core/src/multiarray/common_dtype.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "convert_datatype.h"
1111
#include "dtypemeta.h"
1212
#include "abstractdtypes.h"
13+
#include "multiarraymodule.h"
1314

1415

1516
/*
@@ -63,7 +64,7 @@ PyArray_CommonDType(PyArray_DTypeMeta *dtype1, PyArray_DTypeMeta *dtype2)
6364
}
6465
if (common_dtype == (PyArray_DTypeMeta *)Py_NotImplemented) {
6566
Py_DECREF(Py_NotImplemented);
66-
PyErr_Format(npy_DTypePromotionError,
67+
PyErr_Format(npy_ma_global_data->DTypePromotionError,
6768
"The DTypes %S and %S do not have a common DType. "
6869
"For example they cannot be stored in a single array unless "
6970
"the dtype is `object`.", dtype1, dtype2);
@@ -284,7 +285,7 @@ PyArray_PromoteDTypeSequence(
284285
Py_INCREF(dtypes_in[l]);
285286
PyTuple_SET_ITEM(dtypes_in_tuple, l, (PyObject *)dtypes_in[l]);
286287
}
287-
PyErr_Format(npy_DTypePromotionError,
288+
PyErr_Format(npy_ma_global_data->DTypePromotionError,
288289
"The DType %S could not be promoted by %S. This means that "
289290
"no common DType exists for the given inputs. "
290291
"For example they cannot be stored in a single array unless "

numpy/_core/src/multiarray/conversion_utils.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,8 @@ PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copymode) {
234234
}
235235

236236
int int_copymode;
237-
static PyObject* numpy_CopyMode = NULL;
238-
npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode);
239237

240-
if (numpy_CopyMode != NULL && (PyObject *)Py_TYPE(obj) == numpy_CopyMode) {
238+
if ((PyObject *)Py_TYPE(obj) == npy_ma_global_data->_CopyMode) {
241239
PyObject* mode_value = PyObject_GetAttrString(obj, "value");
242240
if (mode_value == NULL) {
243241
return NPY_FAIL;
@@ -271,10 +269,8 @@ NPY_NO_EXPORT int
271269
PyArray_AsTypeCopyConverter(PyObject *obj, NPY_ASTYPECOPYMODE *copymode)
272270
{
273271
int int_copymode;
274-
static PyObject* numpy_CopyMode = NULL;
275-
npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode);
276272

277-
if (numpy_CopyMode != NULL && (PyObject *)Py_TYPE(obj) == numpy_CopyMode) {
273+
if ((PyObject *)Py_TYPE(obj) == npy_ma_global_data->_CopyMode) {
278274
PyErr_SetString(PyExc_ValueError,
279275
"_CopyMode enum is not allowed for astype function. "
280276
"Use true/false instead.");
@@ -1415,12 +1411,7 @@ PyArray_IntTupleFromIntp(int len, npy_intp const *vals)
14151411
NPY_NO_EXPORT int
14161412
_not_NoValue(PyObject *obj, PyObject **out)
14171413
{
1418-
static PyObject *NoValue = NULL;
1419-
npy_cache_import("numpy", "_NoValue", &NoValue);
1420-
if (NoValue == NULL) {
1421-
return 0;
1422-
}
1423-
if (obj == NoValue) {
1414+
if (obj == npy_ma_global_data->_NoValue) {
14241415
*out = NULL;
14251416
}
14261417
else {

numpy/_core/src/multiarray/convert_datatype.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@
4848
*/
4949
NPY_NO_EXPORT npy_intp REQUIRED_STR_LEN[] = {0, 3, 5, 10, 10, 20, 20, 20, 20};
5050

51-
/*
52-
* Whether or not legacy value-based promotion/casting is used.
53-
*/
54-
55-
NPY_NO_EXPORT PyObject *NO_NEP50_WARNING_CTX = NULL;
56-
NPY_NO_EXPORT PyObject *npy_DTypePromotionError = NULL;
57-
NPY_NO_EXPORT PyObject *npy_UFuncNoLoopError = NULL;
58-
5951
static NPY_TLS int npy_promotion_state = NPY_USE_LEGACY_PROMOTION;
6052

6153
NPY_NO_EXPORT int
@@ -92,13 +84,14 @@ npy_give_promotion_warnings(void)
9284

9385
npy_cache_import(
9486
"numpy._core._ufunc_config", "NO_NEP50_WARNING",
95-
&NO_NEP50_WARNING_CTX);
96-
if (NO_NEP50_WARNING_CTX == NULL) {
87+
&npy_ma_global_data->NO_NEP50_WARNING);
88+
if (npy_ma_global_data->NO_NEP50_WARNING == NULL) {
9789
PyErr_WriteUnraisable(NULL);
9890
return 1;
9991
}
10092

101-
if (PyContextVar_Get(NO_NEP50_WARNING_CTX, Py_False, &val) < 0) {
93+
if (PyContextVar_Get(npy_ma_global_data->NO_NEP50_WARNING,
94+
Py_False, &val) < 0) {
10295
/* Errors should not really happen, but if it does assume we warn. */
10396
PyErr_WriteUnraisable(NULL);
10497
return 1;
@@ -409,12 +402,7 @@ PyArray_GetCastFunc(PyArray_Descr *descr, int type_num)
409402
!PyTypeNum_ISCOMPLEX(type_num) &&
410403
PyTypeNum_ISNUMBER(type_num) &&
411404
!PyTypeNum_ISBOOL(type_num)) {
412-
static PyObject *cls = NULL;
413-
npy_cache_import("numpy.exceptions", "ComplexWarning", &cls);
414-
if (cls == NULL) {
415-
return NULL;
416-
}
417-
int ret = PyErr_WarnEx(cls,
405+
int ret = PyErr_WarnEx(npy_ma_global_data->ComplexWarning,
418406
"Casting complex values to real discards "
419407
"the imaginary part", 1);
420408
if (ret < 0) {
@@ -2638,13 +2626,7 @@ complex_to_noncomplex_get_loop(
26382626
PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata,
26392627
NPY_ARRAYMETHOD_FLAGS *flags)
26402628
{
2641-
static PyObject *cls = NULL;
2642-
int ret;
2643-
npy_cache_import("numpy.exceptions", "ComplexWarning", &cls);
2644-
if (cls == NULL) {
2645-
return -1;
2646-
}
2647-
ret = PyErr_WarnEx(cls,
2629+
int ret = PyErr_WarnEx(npy_ma_global_data->ComplexWarning,
26482630
"Casting complex values to real discards "
26492631
"the imaginary part", 1);
26502632
if (ret < 0) {

numpy/_core/src/multiarray/convert_datatype.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ extern NPY_NO_EXPORT npy_intp REQUIRED_STR_LEN[];
1313
#define NPY_USE_WEAK_PROMOTION 1
1414
#define NPY_USE_WEAK_PROMOTION_AND_WARN 2
1515

16-
extern NPY_NO_EXPORT PyObject *NO_NEP50_WARNING_CTX;
17-
extern NPY_NO_EXPORT PyObject *npy_DTypePromotionError;
18-
extern NPY_NO_EXPORT PyObject *npy_UFuncNoLoopError;
19-
2016
NPY_NO_EXPORT int
2117
npy_give_promotion_warnings(void);
2218

numpy/_core/src/multiarray/ctors.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,6 @@ PyArray_AssignFromCache(PyArrayObject *self, coercion_cache_obj *cache) {
611611
static void
612612
raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr)
613613
{
614-
static PyObject *exc_type = NULL;
615-
616-
npy_cache_import(
617-
"numpy._core._exceptions", "_ArrayMemoryError",
618-
&exc_type);
619-
if (exc_type == NULL) {
620-
goto fail;
621-
}
622-
623614
PyObject *shape = PyArray_IntTupleFromIntp(nd, dims);
624615
if (shape == NULL) {
625616
goto fail;
@@ -631,7 +622,7 @@ raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr)
631622
if (exc_value == NULL){
632623
goto fail;
633624
}
634-
PyErr_SetObject(exc_type, exc_value);
625+
PyErr_SetObject(npy_ma_global_data->_ArrayMemoryError, exc_value);
635626
Py_DECREF(exc_value);
636627
return;
637628

numpy/_core/src/multiarray/descriptor.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -724,13 +724,13 @@ _convert_from_commastring(PyObject *obj, int align)
724724
{
725725
PyObject *parsed;
726726
PyArray_Descr *res;
727-
static PyObject *_commastring = NULL;
728727
assert(PyUnicode_Check(obj));
729-
npy_cache_import("numpy._core._internal", "_commastring", &_commastring);
730-
if (_commastring == NULL) {
728+
npy_cache_import("numpy._core._internal", "_commastring",
729+
&npy_ma_global_data->_commastring);
730+
if (npy_ma_global_data->_commastring == NULL) {
731731
return NULL;
732732
}
733-
parsed = PyObject_CallOneArg(_commastring, obj);
733+
parsed = PyObject_CallOneArg(npy_ma_global_data->_commastring, obj);
734734
if (parsed == NULL) {
735735
return NULL;
736736
}

0 commit comments

Comments
 (0)