Skip to content

Commit 0c22126

Browse files
committed
MNT: move default extobj contextvar to global data dict
1 parent 536e5fb commit 0c22126

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

numpy/_core/src/multiarray/multiarraymodule.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ typedef struct npy_ma_global_data_struct {
3838
*/
3939
PyObject *default_truediv_type_tup;
4040

41+
/*
42+
* Used to set up the default extobj context variable
43+
*
44+
* This is immutable and set at module initialization so can be used
45+
* without acquiring the global data mutex
46+
*/
47+
PyObject *default_extobj_capsule;
48+
49+
/*
50+
* The global ContextVar to store the extobject. It is exposed to Python
51+
* as `_extobj_contextvar`.
52+
*/
53+
PyObject *npy_extobj_contextvar;
54+
4155
/*
4256
* A reference to ndarray's implementations for __array_*__ special methods
4357
*/

numpy/_core/src/umath/extobj.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
#include "common.h"
1919

2020

21-
/*
22-
* The global ContextVar to store the extobject. It is exposed to Python
23-
* as `_extobj_contextvar`.
24-
*/
25-
static PyObject *default_extobj_capsule = NULL;
26-
NPY_NO_EXPORT PyObject *npy_extobj_contextvar = NULL;
27-
28-
2921
#define UFUNC_ERR_IGNORE 0
3022
#define UFUNC_ERR_WARN 1
3123
#define UFUNC_ERR_RAISE 2
@@ -130,7 +122,8 @@ fetch_curr_extobj_state(npy_extobj *extobj)
130122
{
131123
PyObject *capsule;
132124
if (PyContextVar_Get(
133-
npy_extobj_contextvar, default_extobj_capsule, &capsule) < 0) {
125+
npy_ma_global_data->npy_extobj_contextvar,
126+
npy_ma_global_data->default_extobj_capsule, &capsule) < 0) {
134127
return -1;
135128
}
136129
npy_extobj *obj = PyCapsule_GetPointer(capsule, "numpy.ufunc.extobj");
@@ -164,15 +157,15 @@ init_extobj(void)
164157
}
165158
}
166159

167-
default_extobj_capsule = make_extobj_capsule(
160+
npy_ma_global_data->default_extobj_capsule = make_extobj_capsule(
168161
NPY_BUFSIZE, UFUNC_ERR_DEFAULT, Py_None);
169-
if (default_extobj_capsule == NULL) {
162+
if (npy_ma_global_data->default_extobj_capsule == NULL) {
170163
return -1;
171164
}
172-
npy_extobj_contextvar = PyContextVar_New(
173-
"numpy.ufunc.extobj", default_extobj_capsule);
174-
if (npy_extobj_contextvar == NULL) {
175-
Py_CLEAR(default_extobj_capsule);
165+
npy_ma_global_data->npy_extobj_contextvar = PyContextVar_New(
166+
"numpy.ufunc.extobj", npy_ma_global_data->default_extobj_capsule);
167+
if (npy_ma_global_data->npy_extobj_contextvar == NULL) {
168+
Py_CLEAR(npy_ma_global_data->default_extobj_capsule);
176169
return -1;
177170
}
178171
return 0;
@@ -213,7 +206,7 @@ errmodeconverter(PyObject *obj, int *mode)
213206
/*
214207
* This function is currently exposed as `umath._seterrobj()`, it is private
215208
* and returns a capsule representing the errstate. This capsule is then
216-
* assigned to the `npy_extobj_contextvar` in Python.
209+
* assigned to the `_extobj_contextvar` in Python.
217210
*/
218211
NPY_NO_EXPORT PyObject *
219212
extobj_make_extobj(PyObject *NPY_UNUSED(mod),

numpy/_core/src/umath/extobj.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include <numpy/ndarraytypes.h> /* for NPY_NO_EXPORT */
55

66

7-
/* For the private exposure of the extobject contextvar to Python */
8-
extern NPY_NO_EXPORT PyObject *npy_extobj_contextvar;
9-
107
/*
118
* Represent the current ufunc error (and buffer) state. we are using a
129
* capsule for now to store this, but it could make sense to refactor it into

numpy/_core/src/umath/umathmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ int initumath(PyObject *m)
273273
#undef ADDSCONST
274274
PyModule_AddIntConstant(m, "UFUNC_BUFSIZE_DEFAULT", (long)NPY_BUFSIZE);
275275

276-
Py_INCREF(npy_extobj_contextvar);
277-
PyModule_AddObject(m, "_extobj_contextvar", npy_extobj_contextvar);
276+
Py_INCREF(npy_ma_global_data->npy_extobj_contextvar);
277+
PyModule_AddObject(m, "_extobj_contextvar", npy_ma_global_data->npy_extobj_contextvar);
278278

279279
PyModule_AddObject(m, "PINF", PyFloat_FromDouble(NPY_INFINITY));
280280
PyModule_AddObject(m, "NINF", PyFloat_FromDouble(-NPY_INFINITY));

0 commit comments

Comments
 (0)