Skip to content

Commit 3cbb68d

Browse files
committed
MNT: move sys.flags.optimize cache to global data struct
1 parent 7719cf2 commit 3cbb68d

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

numpy/_core/src/multiarray/compiled_base.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,14 +1414,7 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *const *args, Py_ssize_t
14141414

14151415
/* Don't add docstrings */
14161416
#if PY_VERSION_HEX > 0x030b0000
1417-
static long optimize = -1000;
1418-
if (optimize < 0) {
1419-
PyObject *flags = PySys_GetObject("flags"); /* borrowed object */
1420-
PyObject *level = PyObject_GetAttrString(flags, "optimize");
1421-
optimize = PyLong_AsLong(level);
1422-
Py_DECREF(level);
1423-
}
1424-
if (optimize > 1) {
1417+
if (npy_ma_global_data->optimize > 1) {
14251418
#else
14261419
if (Py_OptimizeFlag > 1) {
14271420
#endif

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4965,10 +4965,20 @@ initialize_static_globals(void)
49654965
numpy_warn_if_no_mem_policy = 0;
49664966
}
49674967

4968+
PyObject *flags = PySys_GetObject("flags"); /* borrowed object */
4969+
if (flags == NULL) {
4970+
PyErr_SetString(PyExc_AttributeError, "cannot get sys.flags");
4971+
return -1;
4972+
}
4973+
PyObject *level = PyObject_GetAttrString(flags, "optimize");
4974+
if (level == NULL) {
4975+
return -1;
4976+
}
4977+
npy_ma_global_data->optimize = PyLong_AsLong(level);
4978+
Py_DECREF(level);
49684979
return 0;
49694980
}
49704981

4971-
49724982
static struct PyModuleDef moduledef = {
49734983
PyModuleDef_HEAD_INIT,
49744984
"_multiarray_umath",

numpy/_core/src/multiarray/multiarraymodule.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ typedef struct npy_ma_global_data_struct {
6060
PyObject *os_PathLike;
6161
PyObject *os_fspath;
6262

63+
/*
64+
* stores sys.flags.optimize as a long, which is used in the add_docstring
65+
* implementation
66+
*/
67+
long optimize;
68+
6369
/*
6470
* Used for CPU feature detection and dispatch
6571
*

0 commit comments

Comments
 (0)