|
7 | 7 | #include "numpy/ndarraytypes.h"
|
8 | 8 | #include "get_attr_string.h"
|
9 | 9 | #include "npy_import.h"
|
| 10 | +#include "npy_static_data.h" |
10 | 11 | #include "multiarraymodule.h"
|
11 | 12 |
|
12 | 13 | #include "arrayfunction_override.h"
|
13 | 14 |
|
14 |
| -/* Return the ndarray.__array_function__ method. */ |
15 |
| -static PyObject * |
16 |
| -get_ndarray_array_function(void) |
17 |
| -{ |
18 |
| - PyObject* method = PyObject_GetAttrString((PyObject *)&PyArray_Type, |
19 |
| - "__array_function__"); |
20 |
| - assert(method != NULL); |
21 |
| - return method; |
22 |
| -} |
23 |
| - |
24 |
| - |
25 | 15 | /*
|
26 | 16 | * Get an object's __array_function__ method in the fastest way possible.
|
27 | 17 | * Never raises an exception. Returns NULL if the method doesn't exist.
|
28 | 18 | */
|
29 | 19 | static PyObject *
|
30 | 20 | get_array_function(PyObject *obj)
|
31 | 21 | {
|
32 |
| - static PyObject *ndarray_array_function = NULL; |
33 |
| - |
34 |
| - if (ndarray_array_function == NULL) { |
35 |
| - ndarray_array_function = get_ndarray_array_function(); |
36 |
| - } |
37 |
| - |
38 | 22 | /* Fast return for ndarray */
|
39 | 23 | if (PyArray_CheckExact(obj)) {
|
40 |
| - Py_INCREF(ndarray_array_function); |
41 |
| - return ndarray_array_function; |
| 24 | + Py_INCREF(npy_static_pydata.ndarray_array_function); |
| 25 | + return npy_static_pydata.ndarray_array_function; |
42 | 26 | }
|
43 | 27 |
|
44 |
| - PyObject *array_function = PyArray_LookupSpecial(obj, npy_ma_str_array_function); |
| 28 | + PyObject *array_function = PyArray_LookupSpecial(obj, npy_interned_str.array_function); |
45 | 29 | if (array_function == NULL && PyErr_Occurred()) {
|
46 | 30 | PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */
|
47 | 31 | }
|
@@ -142,12 +126,7 @@ get_implementing_args_and_methods(PyObject *relevant_args,
|
142 | 126 | static int
|
143 | 127 | is_default_array_function(PyObject *obj)
|
144 | 128 | {
|
145 |
| - static PyObject *ndarray_array_function = NULL; |
146 |
| - |
147 |
| - if (ndarray_array_function == NULL) { |
148 |
| - ndarray_array_function = get_ndarray_array_function(); |
149 |
| - } |
150 |
| - return obj == ndarray_array_function; |
| 129 | + return obj == npy_static_pydata.ndarray_array_function; |
151 | 130 | }
|
152 | 131 |
|
153 | 132 |
|
@@ -175,7 +154,7 @@ array_function_method_impl(PyObject *func, PyObject *types, PyObject *args,
|
175 | 154 | }
|
176 | 155 | }
|
177 | 156 |
|
178 |
| - PyObject *implementation = PyObject_GetAttr(func, npy_ma_str_implementation); |
| 157 | + PyObject *implementation = PyObject_GetAttr(func, npy_interned_str.implementation); |
179 | 158 | if (implementation == NULL) {
|
180 | 159 | return NULL;
|
181 | 160 | }
|
@@ -252,14 +231,14 @@ get_args_and_kwargs(
|
252 | 231 | static void
|
253 | 232 | set_no_matching_types_error(PyObject *public_api, PyObject *types)
|
254 | 233 | {
|
255 |
| - static PyObject *errmsg_formatter = NULL; |
256 | 234 | /* No acceptable override found, raise TypeError. */
|
257 | 235 | npy_cache_import("numpy._core._internal",
|
258 | 236 | "array_function_errmsg_formatter",
|
259 |
| - &errmsg_formatter); |
260 |
| - if (errmsg_formatter != NULL) { |
| 237 | + &npy_thread_unsafe_state.array_function_errmsg_formatter); |
| 238 | + if (npy_thread_unsafe_state.array_function_errmsg_formatter != NULL) { |
261 | 239 | PyObject *errmsg = PyObject_CallFunctionObjArgs(
|
262 |
| - errmsg_formatter, public_api, types, NULL); |
| 240 | + npy_thread_unsafe_state.array_function_errmsg_formatter, |
| 241 | + public_api, types, NULL); |
263 | 242 | if (errmsg != NULL) {
|
264 | 243 | PyErr_SetObject(PyExc_TypeError, errmsg);
|
265 | 244 | Py_DECREF(errmsg);
|
@@ -321,12 +300,12 @@ array_implement_c_array_function_creation(
|
321 | 300 | }
|
322 | 301 |
|
323 | 302 | /* The like argument must be present in the keyword arguments, remove it */
|
324 |
| - if (PyDict_DelItem(kwargs, npy_ma_str_like) < 0) { |
| 303 | + if (PyDict_DelItem(kwargs, npy_interned_str.like) < 0) { |
325 | 304 | goto finish;
|
326 | 305 | }
|
327 | 306 |
|
328 | 307 | /* Fetch the actual symbol (the long way right now) */
|
329 |
| - numpy_module = PyImport_Import(npy_ma_str_numpy); |
| 308 | + numpy_module = PyImport_Import(npy_interned_str.numpy); |
330 | 309 | if (numpy_module == NULL) {
|
331 | 310 | goto finish;
|
332 | 311 | }
|
|
0 commit comments