diff --git a/numpy/_core/meson.build b/numpy/_core/meson.build index dbe76e0a3dea..aba0b8212d1e 100644 --- a/numpy/_core/meson.build +++ b/numpy/_core/meson.build @@ -1101,6 +1101,7 @@ src_multiarray = multiarray_gen_headers + [ 'src/multiarray/nditer_constr.c', 'src/multiarray/nditer_pywrap.c', src_file.process('src/multiarray/nditer_templ.c.src'), + 'src/multiarray/npy_static_data.c', 'src/multiarray/number.c', 'src/multiarray/refcount.c', src_file.process('src/multiarray/scalartypes.c.src'), diff --git a/numpy/_core/src/common/binop_override.h b/numpy/_core/src/common/binop_override.h index ec3d046796ab..def9b895c872 100644 --- a/numpy/_core/src/common/binop_override.h +++ b/numpy/_core/src/common/binop_override.h @@ -6,6 +6,7 @@ #include "numpy/arrayobject.h" #include "get_attr_string.h" +#include "npy_static_data.h" /* * Logic for deciding when binops should return NotImplemented versus when @@ -128,7 +129,7 @@ binop_should_defer(PyObject *self, PyObject *other, int inplace) * Classes with __array_ufunc__ are living in the future, and only need to * check whether __array_ufunc__ equals None. */ - attr = PyArray_LookupSpecial(other, npy_um_str_array_ufunc); + attr = PyArray_LookupSpecial(other, npy_interned_str.array_ufunc); if (attr != NULL) { defer = !inplace && (attr == Py_None); Py_DECREF(attr); diff --git a/numpy/_core/src/common/npy_cpu_dispatch.c b/numpy/_core/src/common/npy_cpu_dispatch.c index 992a470ada04..ff22f234a7c6 100644 --- a/numpy/_core/src/common/npy_cpu_dispatch.c +++ b/numpy/_core/src/common/npy_cpu_dispatch.c @@ -1,11 +1,14 @@ -#include "npy_cpu_dispatch.h" +#define NPY_NO_DEPRECATED_API NPY_API_VERSION +#define _MULTIARRAYMODULE -static PyObject *npy__cpu_dispatch_registery = NULL; +#include "npy_cpu_dispatch.h" +#include "numpy/ndarraytypes.h" +#include "npy_static_data.h" NPY_VISIBILITY_HIDDEN int npy_cpu_dispatch_tracer_init(PyObject *mod) { - if (npy__cpu_dispatch_registery != NULL) { + if (npy_static_pydata.cpu_dispatch_registry != NULL) { PyErr_Format(PyExc_RuntimeError, "CPU dispatcher tracer already initlized"); return -1; } @@ -22,7 +25,7 @@ npy_cpu_dispatch_tracer_init(PyObject *mod) if (err != 0) { return -1; } - npy__cpu_dispatch_registery = reg_dict; + npy_static_pydata.cpu_dispatch_registry = reg_dict; return 0; } @@ -30,13 +33,13 @@ NPY_VISIBILITY_HIDDEN void npy_cpu_dispatch_trace(const char *fname, const char *signature, const char **dispatch_info) { - PyObject *func_dict = PyDict_GetItemString(npy__cpu_dispatch_registery, fname); + PyObject *func_dict = PyDict_GetItemString(npy_static_pydata.cpu_dispatch_registry, fname); if (func_dict == NULL) { func_dict = PyDict_New(); if (func_dict == NULL) { return; } - int err = PyDict_SetItemString(npy__cpu_dispatch_registery, fname, func_dict); + int err = PyDict_SetItemString(npy_static_pydata.cpu_dispatch_registry, fname, func_dict); Py_DECREF(func_dict); if (err != 0) { return; diff --git a/numpy/_core/src/common/npy_cpu_features.c b/numpy/_core/src/common/npy_cpu_features.c index 4f4448d13bcd..04a5449e5b8e 100644 --- a/numpy/_core/src/common/npy_cpu_features.c +++ b/numpy/_core/src/common/npy_cpu_features.c @@ -5,6 +5,11 @@ /******************** Private Definitions *********************/ +// This is initialized during module initialization and thereafter immutable. +// We don't include it in the global data struct because the definitions in +// this file are shared by the _simd, _umath_tests, and +// _multiarray_umath modules + // Hold all CPU features boolean values static unsigned char npy__cpu_have[NPY_CPU_FEATURE_MAX]; diff --git a/numpy/_core/src/common/npy_ctypes.h b/numpy/_core/src/common/npy_ctypes.h index 578de06397bd..c72d2dff7fcb 100644 --- a/numpy/_core/src/common/npy_ctypes.h +++ b/numpy/_core/src/common/npy_ctypes.h @@ -4,6 +4,7 @@ #include #include "npy_import.h" +#include "multiarraymodule.h" /* * Check if a python type is a ctypes class. @@ -17,16 +18,17 @@ static inline int npy_ctypes_check(PyTypeObject *obj) { - static PyObject *py_func = NULL; PyObject *ret_obj; int ret; - npy_cache_import("numpy._core._internal", "npy_ctypes_check", &py_func); - if (py_func == NULL) { + npy_cache_import("numpy._core._internal", "npy_ctypes_check", + &npy_thread_unsafe_state.npy_ctypes_check); + if (npy_thread_unsafe_state.npy_ctypes_check == NULL) { goto fail; } - ret_obj = PyObject_CallFunctionObjArgs(py_func, (PyObject *)obj, NULL); + ret_obj = PyObject_CallFunctionObjArgs(npy_thread_unsafe_state.npy_ctypes_check, + (PyObject *)obj, NULL); if (ret_obj == NULL) { goto fail; } diff --git a/numpy/_core/src/common/ufunc_override.c b/numpy/_core/src/common/ufunc_override.c index dd7706d41475..17b678edd4bf 100644 --- a/numpy/_core/src/common/ufunc_override.c +++ b/numpy/_core/src/common/ufunc_override.c @@ -7,6 +7,7 @@ #include "npy_import.h" #include "ufunc_override.h" #include "scalartypes.h" +#include "npy_static_data.h" /* * Check whether an object has __array_ufunc__ defined on its class and it @@ -19,15 +20,8 @@ NPY_NO_EXPORT PyObject * PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj) { - static PyObject *ndarray_array_ufunc = NULL; PyObject *cls_array_ufunc; - /* On first entry, cache ndarray's __array_ufunc__ */ - if (ndarray_array_ufunc == NULL) { - ndarray_array_ufunc = PyObject_GetAttrString((PyObject *)&PyArray_Type, - "__array_ufunc__"); - } - /* Fast return for ndarray */ if (PyArray_CheckExact(obj)) { return NULL; @@ -41,7 +35,7 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj) * Does the class define __array_ufunc__? (Note that LookupSpecial has fast * return for basic python types, so no need to worry about those here) */ - cls_array_ufunc = PyArray_LookupSpecial(obj, npy_um_str_array_ufunc); + cls_array_ufunc = PyArray_LookupSpecial(obj, npy_interned_str.array_ufunc); if (cls_array_ufunc == NULL) { if (PyErr_Occurred()) { PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */ @@ -49,7 +43,7 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj) return NULL; } /* Ignore if the same as ndarray.__array_ufunc__ */ - if (cls_array_ufunc == ndarray_array_ufunc) { + if (cls_array_ufunc == npy_static_pydata.ndarray_array_ufunc) { Py_DECREF(cls_array_ufunc); return NULL; } diff --git a/numpy/_core/src/multiarray/alloc.c b/numpy/_core/src/multiarray/alloc.c index df64a13a26e8..b7e7c9948ce1 100644 --- a/numpy/_core/src/multiarray/alloc.c +++ b/numpy/_core/src/multiarray/alloc.c @@ -11,6 +11,8 @@ #include "numpy/npy_common.h" #include "npy_config.h" #include "alloc.h" +#include "npy_static_data.h" +#include "multiarraymodule.h" #include #ifdef NPY_OS_LINUX @@ -35,13 +37,11 @@ typedef struct { static cache_bucket datacache[NBUCKETS]; static cache_bucket dimcache[NBUCKETS_DIM]; -static int _madvise_hugepage = 1; - /* * This function tells whether NumPy attempts to call `madvise` with * `MADV_HUGEPAGE`. `madvise` is only ever used on linux, so the value - * of `_madvise_hugepage` may be ignored. + * of `madvise_hugepage` may be ignored. * * It is exposed to Python as `np._core.multiarray._get_madvise_hugepage`. */ @@ -49,7 +49,7 @@ NPY_NO_EXPORT PyObject * _get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) { #ifdef NPY_OS_LINUX - if (_madvise_hugepage) { + if (npy_thread_unsafe_state.madvise_hugepage) { Py_RETURN_TRUE; } #endif @@ -59,20 +59,20 @@ _get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) /* * This function enables or disables the use of `MADV_HUGEPAGE` on Linux - * by modifying the global static `_madvise_hugepage`. - * It returns the previous value of `_madvise_hugepage`. + * by modifying the global static `madvise_hugepage`. + * It returns the previous value of `madvise_hugepage`. * * It is exposed to Python as `np._core.multiarray._set_madvise_hugepage`. */ NPY_NO_EXPORT PyObject * _set_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *enabled_obj) { - int was_enabled = _madvise_hugepage; + int was_enabled = npy_thread_unsafe_state.madvise_hugepage; int enabled = PyObject_IsTrue(enabled_obj); if (enabled < 0) { return NULL; } - _madvise_hugepage = enabled; + npy_thread_unsafe_state.madvise_hugepage = enabled; if (was_enabled) { Py_RETURN_TRUE; } @@ -110,7 +110,8 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz, #endif #ifdef NPY_OS_LINUX /* allow kernel allocating huge pages for large arrays */ - if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) && _madvise_hugepage) { + if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) && + npy_thread_unsafe_state.madvise_hugepage) { npy_uintp offset = 4096u - (npy_uintp)p % (4096u); npy_uintp length = nelem * esz - offset; /** diff --git a/numpy/_core/src/multiarray/array_converter.c b/numpy/_core/src/multiarray/array_converter.c index 5dea748688e9..496173038954 100644 --- a/numpy/_core/src/multiarray/array_converter.c +++ b/numpy/_core/src/multiarray/array_converter.c @@ -21,7 +21,7 @@ #include "abstractdtypes.h" #include "convert_datatype.h" #include "descriptor.h" -#include "multiarraymodule.h" +#include "npy_static_data.h" #include "ctors.h" #include "npy_config.h" @@ -186,8 +186,8 @@ static int pyscalar_mode_conv(PyObject *obj, scalar_policy *policy) { PyObject *strings[3] = { - npy_ma_str_convert, npy_ma_str_preserve, - npy_ma_str_convert_if_no_array}; + npy_interned_str.convert, npy_interned_str.preserve, + npy_interned_str.convert_if_no_array}; /* First quick pass using the identity (should practically always match) */ for (int i = 0; i < 3; i++) { diff --git a/numpy/_core/src/multiarray/arrayfunction_override.c b/numpy/_core/src/multiarray/arrayfunction_override.c index 20223e1449fb..aa3ab42433c7 100644 --- a/numpy/_core/src/multiarray/arrayfunction_override.c +++ b/numpy/_core/src/multiarray/arrayfunction_override.c @@ -7,21 +7,11 @@ #include "numpy/ndarraytypes.h" #include "get_attr_string.h" #include "npy_import.h" +#include "npy_static_data.h" #include "multiarraymodule.h" #include "arrayfunction_override.h" -/* Return the ndarray.__array_function__ method. */ -static PyObject * -get_ndarray_array_function(void) -{ - PyObject* method = PyObject_GetAttrString((PyObject *)&PyArray_Type, - "__array_function__"); - assert(method != NULL); - return method; -} - - /* * Get an object's __array_function__ method in the fastest way possible. * Never raises an exception. Returns NULL if the method doesn't exist. @@ -29,19 +19,13 @@ get_ndarray_array_function(void) static PyObject * get_array_function(PyObject *obj) { - static PyObject *ndarray_array_function = NULL; - - if (ndarray_array_function == NULL) { - ndarray_array_function = get_ndarray_array_function(); - } - /* Fast return for ndarray */ if (PyArray_CheckExact(obj)) { - Py_INCREF(ndarray_array_function); - return ndarray_array_function; + Py_INCREF(npy_static_pydata.ndarray_array_function); + return npy_static_pydata.ndarray_array_function; } - PyObject *array_function = PyArray_LookupSpecial(obj, npy_ma_str_array_function); + PyObject *array_function = PyArray_LookupSpecial(obj, npy_interned_str.array_function); if (array_function == NULL && PyErr_Occurred()) { PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */ } @@ -142,12 +126,7 @@ get_implementing_args_and_methods(PyObject *relevant_args, static int is_default_array_function(PyObject *obj) { - static PyObject *ndarray_array_function = NULL; - - if (ndarray_array_function == NULL) { - ndarray_array_function = get_ndarray_array_function(); - } - return obj == ndarray_array_function; + return obj == npy_static_pydata.ndarray_array_function; } @@ -175,7 +154,7 @@ array_function_method_impl(PyObject *func, PyObject *types, PyObject *args, } } - PyObject *implementation = PyObject_GetAttr(func, npy_ma_str_implementation); + PyObject *implementation = PyObject_GetAttr(func, npy_interned_str.implementation); if (implementation == NULL) { return NULL; } @@ -252,14 +231,14 @@ get_args_and_kwargs( static void set_no_matching_types_error(PyObject *public_api, PyObject *types) { - static PyObject *errmsg_formatter = NULL; /* No acceptable override found, raise TypeError. */ npy_cache_import("numpy._core._internal", "array_function_errmsg_formatter", - &errmsg_formatter); - if (errmsg_formatter != NULL) { + &npy_thread_unsafe_state.array_function_errmsg_formatter); + if (npy_thread_unsafe_state.array_function_errmsg_formatter != NULL) { PyObject *errmsg = PyObject_CallFunctionObjArgs( - errmsg_formatter, public_api, types, NULL); + npy_thread_unsafe_state.array_function_errmsg_formatter, + public_api, types, NULL); if (errmsg != NULL) { PyErr_SetObject(PyExc_TypeError, errmsg); Py_DECREF(errmsg); @@ -321,12 +300,12 @@ array_implement_c_array_function_creation( } /* The like argument must be present in the keyword arguments, remove it */ - if (PyDict_DelItem(kwargs, npy_ma_str_like) < 0) { + if (PyDict_DelItem(kwargs, npy_interned_str.like) < 0) { goto finish; } /* Fetch the actual symbol (the long way right now) */ - numpy_module = PyImport_Import(npy_ma_str_numpy); + numpy_module = PyImport_Import(npy_interned_str.numpy); if (numpy_module == NULL) { goto finish; } diff --git a/numpy/_core/src/multiarray/arrayobject.c b/numpy/_core/src/multiarray/arrayobject.c index 5139bc8b4f00..15596f1f86a2 100644 --- a/numpy/_core/src/multiarray/arrayobject.c +++ b/numpy/_core/src/multiarray/arrayobject.c @@ -62,9 +62,7 @@ maintainer email: oliphant.travis@ieee.org #include "binop_override.h" #include "array_coercion.h" - - -NPY_NO_EXPORT npy_bool numpy_warn_if_no_mem_policy = 0; +#include "multiarraymodule.h" /*NUMPY_API Compute the size of an array (in number of items) @@ -429,7 +427,7 @@ array_dealloc(PyArrayObject *self) } } if (fa->mem_handler == NULL) { - if (numpy_warn_if_no_mem_policy) { + if (npy_thread_unsafe_state.warn_if_no_mem_policy) { char const *msg = "Trying to dealloc data, but a memory policy " "is not set. If you take ownership of the data, you must " "set a base owning the data (e.g. a PyCapsule)."; @@ -927,7 +925,8 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) */ if (result == NULL && (cmp_op == Py_EQ || cmp_op == Py_NE) - && PyErr_ExceptionMatches(npy_UFuncNoLoopError)) { + && PyErr_ExceptionMatches( + npy_static_pydata._UFuncNoLoopError)) { PyErr_Clear(); PyArrayObject *array_other = (PyArrayObject *)PyArray_FROM_O(other); diff --git a/numpy/_core/src/multiarray/arrayobject.h b/numpy/_core/src/multiarray/arrayobject.h index 03e59c41ca92..8d6f84faa6b1 100644 --- a/numpy/_core/src/multiarray/arrayobject.h +++ b/numpy/_core/src/multiarray/arrayobject.h @@ -9,8 +9,6 @@ extern "C" { #endif -extern NPY_NO_EXPORT npy_bool numpy_warn_if_no_mem_policy; - NPY_NO_EXPORT PyObject * _strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op, int rstrip); diff --git a/numpy/_core/src/multiarray/arraytypes.c.src b/numpy/_core/src/multiarray/arraytypes.c.src index 1ecfc6d94cd7..9524be8a0c89 100644 --- a/numpy/_core/src/multiarray/arraytypes.c.src +++ b/numpy/_core/src/multiarray/arraytypes.c.src @@ -42,6 +42,7 @@ #include "arraytypes.h" #include "umathmodule.h" +#include "npy_static_data.h" /* * Define a stack allocated dummy array with only the minimum information set: @@ -4185,8 +4186,7 @@ NPY_NO_EXPORT _PyArray_LegacyDescr @from@_Descr = { /* The smallest type number is ?, the largest bounded by 'z'. */ #define _MAX_LETTER ('z' + 1) -static npy_int16 _letter_to_num[_MAX_LETTER - '?']; -#define LETTER_TO_NUM(letter) _letter_to_num[letter - '?'] +#define LETTER_TO_NUM(letter) npy_static_cdata._letter_to_num[letter - '?'] static _PyArray_LegacyDescr *_builtin_descrs[] = { &BOOL_Descr, diff --git a/numpy/_core/src/multiarray/arraywrap.c b/numpy/_core/src/multiarray/arraywrap.c index 8b37798f983b..ae7b6e987ff8 100644 --- a/numpy/_core/src/multiarray/arraywrap.c +++ b/numpy/_core/src/multiarray/arraywrap.c @@ -12,7 +12,7 @@ #include "get_attr_string.h" #include "arraywrap.h" -#include "multiarraymodule.h" +#include "npy_static_data.h" /* @@ -57,7 +57,7 @@ npy_find_array_wrap( } } else { - PyObject *new_wrap = PyArray_LookupSpecial_OnInstance(obj, npy_ma_str_array_wrap); + PyObject *new_wrap = PyArray_LookupSpecial_OnInstance(obj, npy_interned_str.array_wrap); if (new_wrap == NULL) { if (PyErr_Occurred()) { goto fail; @@ -160,7 +160,7 @@ npy_apply_wrap( else { /* Replace passed wrap/wrap_type (borrowed refs) with new_wrap/type. */ new_wrap = PyArray_LookupSpecial_OnInstance( - original_out, npy_ma_str_array_wrap); + original_out, npy_interned_str.array_wrap); if (new_wrap != NULL) { wrap = new_wrap; wrap_type = (PyObject *)Py_TYPE(original_out); diff --git a/numpy/_core/src/multiarray/common.h b/numpy/_core/src/multiarray/common.h index 1a01224b1670..19fba9e66d01 100644 --- a/numpy/_core/src/multiarray/common.h +++ b/numpy/_core/src/multiarray/common.h @@ -8,6 +8,7 @@ #include "npy_cpu_dispatch.h" #include "numpy/npy_cpu.h" +#include "npy_static_data.h" #include "npy_import.h" #include @@ -139,25 +140,14 @@ check_and_adjust_axis_msg(int *axis, int ndim, PyObject *msg_prefix) { /* Check that index is valid, taking into account negative indices */ if (NPY_UNLIKELY((*axis < -ndim) || (*axis >= ndim))) { - /* - * Load the exception type, if we don't already have it. Unfortunately - * we don't have access to npy_cache_import here - */ - static PyObject *AxisError_cls = NULL; - PyObject *exc; - - npy_cache_import("numpy.exceptions", "AxisError", &AxisError_cls); - if (AxisError_cls == NULL) { - return -1; - } - /* Invoke the AxisError constructor */ - exc = PyObject_CallFunction(AxisError_cls, "iiO", - *axis, ndim, msg_prefix); + PyObject *exc = PyObject_CallFunction( + npy_static_pydata.AxisError, "iiO", *axis, ndim, + msg_prefix); if (exc == NULL) { return -1; } - PyErr_SetObject(AxisError_cls, exc); + PyErr_SetObject(npy_static_pydata.AxisError, exc); Py_DECREF(exc); return -1; diff --git a/numpy/_core/src/multiarray/common_dtype.c b/numpy/_core/src/multiarray/common_dtype.c index f2ec41e0c7aa..beba6acef149 100644 --- a/numpy/_core/src/multiarray/common_dtype.c +++ b/numpy/_core/src/multiarray/common_dtype.c @@ -10,6 +10,7 @@ #include "convert_datatype.h" #include "dtypemeta.h" #include "abstractdtypes.h" +#include "npy_static_data.h" /* @@ -63,7 +64,7 @@ PyArray_CommonDType(PyArray_DTypeMeta *dtype1, PyArray_DTypeMeta *dtype2) } if (common_dtype == (PyArray_DTypeMeta *)Py_NotImplemented) { Py_DECREF(Py_NotImplemented); - PyErr_Format(npy_DTypePromotionError, + PyErr_Format(npy_static_pydata.DTypePromotionError, "The DTypes %S and %S do not have a common DType. " "For example they cannot be stored in a single array unless " "the dtype is `object`.", dtype1, dtype2); @@ -284,7 +285,7 @@ PyArray_PromoteDTypeSequence( Py_INCREF(dtypes_in[l]); PyTuple_SET_ITEM(dtypes_in_tuple, l, (PyObject *)dtypes_in[l]); } - PyErr_Format(npy_DTypePromotionError, + PyErr_Format(npy_static_pydata.DTypePromotionError, "The DType %S could not be promoted by %S. This means that " "no common DType exists for the given inputs. " "For example they cannot be stored in a single array unless " diff --git a/numpy/_core/src/multiarray/compiled_base.c b/numpy/_core/src/multiarray/compiled_base.c index 27455797cfa3..5876247cec9c 100644 --- a/numpy/_core/src/multiarray/compiled_base.c +++ b/numpy/_core/src/multiarray/compiled_base.c @@ -1414,14 +1414,7 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *const *args, Py_ssize_t /* Don't add docstrings */ #if PY_VERSION_HEX > 0x030b0000 - static long optimize = -1000; - if (optimize < 0) { - PyObject *flags = PySys_GetObject("flags"); /* borrowed object */ - PyObject *level = PyObject_GetAttrString(flags, "optimize"); - optimize = PyLong_AsLong(level); - Py_DECREF(level); - } - if (optimize > 1) { + if (npy_static_cdata.optimize > 1) { #else if (Py_OptimizeFlag > 1) { #endif @@ -1754,15 +1747,6 @@ pack_bits(PyObject *input, int axis, char order) static PyObject * unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order) { - static int unpack_init = 0; - /* - * lookuptable for bitorder big as it has been around longer - * bitorder little is handled via byteswapping in the loop - */ - static union { - npy_uint8 bytes[8]; - npy_uint64 uint64; - } unpack_lookup_big[256]; PyArrayObject *inp; PyArrayObject *new = NULL; PyArrayObject *out = NULL; @@ -1848,22 +1832,6 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order) goto fail; } - /* - * setup lookup table under GIL, 256 8 byte blocks representing 8 bits - * expanded to 1/0 bytes - */ - if (unpack_init == 0) { - npy_intp j; - for (j=0; j < 256; j++) { - npy_intp k; - for (k=0; k < 8; k++) { - npy_uint8 v = (j & (1 << k)) == (1 << k); - unpack_lookup_big[j].bytes[7 - k] = v; - } - } - unpack_init = 1; - } - count = PyArray_DIM(new, axis) * 8; if (outdims[axis] > count) { in_n = count / 8; @@ -1890,7 +1858,7 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order) /* for unity stride we can just copy out of the lookup table */ if (order == 'b') { for (index = 0; index < in_n; index++) { - npy_uint64 v = unpack_lookup_big[*inptr].uint64; + npy_uint64 v = npy_static_cdata.unpack_lookup_big[*inptr].uint64; memcpy(outptr, &v, 8); outptr += 8; inptr += in_stride; @@ -1898,7 +1866,7 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order) } else { for (index = 0; index < in_n; index++) { - npy_uint64 v = unpack_lookup_big[*inptr].uint64; + npy_uint64 v = npy_static_cdata.unpack_lookup_big[*inptr].uint64; if (order != 'b') { v = npy_bswap8(v); } @@ -1909,7 +1877,7 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order) } /* Clean up the tail portion */ if (in_tail) { - npy_uint64 v = unpack_lookup_big[*inptr].uint64; + npy_uint64 v = npy_static_cdata.unpack_lookup_big[*inptr].uint64; if (order != 'b') { v = npy_bswap8(v); } diff --git a/numpy/_core/src/multiarray/conversion_utils.c b/numpy/_core/src/multiarray/conversion_utils.c index c30b31a633cc..e7b1936d1706 100644 --- a/numpy/_core/src/multiarray/conversion_utils.c +++ b/numpy/_core/src/multiarray/conversion_utils.c @@ -18,6 +18,7 @@ #include "conversion_utils.h" #include "alloc.h" #include "npy_buffer.h" +#include "npy_static_data.h" #include "multiarraymodule.h" static int @@ -234,10 +235,8 @@ PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copymode) { } int int_copymode; - static PyObject* numpy_CopyMode = NULL; - npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode); - if (numpy_CopyMode != NULL && (PyObject *)Py_TYPE(obj) == numpy_CopyMode) { + if ((PyObject *)Py_TYPE(obj) == npy_static_pydata._CopyMode) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); if (mode_value == NULL) { return NPY_FAIL; @@ -271,10 +270,8 @@ NPY_NO_EXPORT int PyArray_AsTypeCopyConverter(PyObject *obj, NPY_ASTYPECOPYMODE *copymode) { int int_copymode; - static PyObject* numpy_CopyMode = NULL; - npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode); - if (numpy_CopyMode != NULL && (PyObject *)Py_TYPE(obj) == numpy_CopyMode) { + if ((PyObject *)Py_TYPE(obj) == npy_static_pydata._CopyMode) { PyErr_SetString(PyExc_ValueError, "_CopyMode enum is not allowed for astype function. " "Use true/false instead."); @@ -1415,12 +1412,7 @@ PyArray_IntTupleFromIntp(int len, npy_intp const *vals) NPY_NO_EXPORT int _not_NoValue(PyObject *obj, PyObject **out) { - static PyObject *NoValue = NULL; - npy_cache_import("numpy", "_NoValue", &NoValue); - if (NoValue == NULL) { - return 0; - } - if (obj == NoValue) { + if (obj == npy_static_pydata._NoValue) { *out = NULL; } else { @@ -1440,7 +1432,7 @@ PyArray_DeviceConverterOptional(PyObject *object, NPY_DEVICE *device) } if (PyUnicode_Check(object) && - PyUnicode_Compare(object, npy_ma_str_cpu) == 0) { + PyUnicode_Compare(object, npy_interned_str.cpu) == 0) { *device = NPY_DEVICE_CPU; return NPY_SUCCEED; } diff --git a/numpy/_core/src/multiarray/convert_datatype.c b/numpy/_core/src/multiarray/convert_datatype.c index 5711bce7bc08..f029ad8a5986 100644 --- a/numpy/_core/src/multiarray/convert_datatype.c +++ b/numpy/_core/src/multiarray/convert_datatype.c @@ -35,7 +35,8 @@ #include "dtype_transfer.h" #include "dtype_traversal.h" #include "arrayobject.h" - +#include "npy_static_data.h" +#include "multiarraymodule.h" /* * Required length of string when converting from unsigned integer type. @@ -48,14 +49,6 @@ */ NPY_NO_EXPORT npy_intp REQUIRED_STR_LEN[] = {0, 3, 5, 10, 10, 20, 20, 20, 20}; -/* - * Whether or not legacy value-based promotion/casting is used. - */ - -NPY_NO_EXPORT PyObject *NO_NEP50_WARNING_CTX = NULL; -NPY_NO_EXPORT PyObject *npy_DTypePromotionError = NULL; -NPY_NO_EXPORT PyObject *npy_UFuncNoLoopError = NULL; - static NPY_TLS int npy_promotion_state = NPY_USE_LEGACY_PROMOTION; NPY_NO_EXPORT int @@ -92,13 +85,14 @@ npy_give_promotion_warnings(void) npy_cache_import( "numpy._core._ufunc_config", "NO_NEP50_WARNING", - &NO_NEP50_WARNING_CTX); - if (NO_NEP50_WARNING_CTX == NULL) { + &npy_thread_unsafe_state.NO_NEP50_WARNING); + if (npy_thread_unsafe_state.NO_NEP50_WARNING == NULL) { PyErr_WriteUnraisable(NULL); return 1; } - if (PyContextVar_Get(NO_NEP50_WARNING_CTX, Py_False, &val) < 0) { + if (PyContextVar_Get(npy_thread_unsafe_state.NO_NEP50_WARNING, + Py_False, &val) < 0) { /* Errors should not really happen, but if it does assume we warn. */ PyErr_WriteUnraisable(NULL); return 1; @@ -409,12 +403,7 @@ PyArray_GetCastFunc(PyArray_Descr *descr, int type_num) !PyTypeNum_ISCOMPLEX(type_num) && PyTypeNum_ISNUMBER(type_num) && !PyTypeNum_ISBOOL(type_num)) { - static PyObject *cls = NULL; - npy_cache_import("numpy.exceptions", "ComplexWarning", &cls); - if (cls == NULL) { - return NULL; - } - int ret = PyErr_WarnEx(cls, + int ret = PyErr_WarnEx(npy_static_pydata.ComplexWarning, "Casting complex values to real discards " "the imaginary part", 1); if (ret < 0) { @@ -2179,7 +2168,6 @@ PyArray_Zero(PyArrayObject *arr) { char *zeroval; int ret, storeflags; - static PyObject * zero_obj = NULL; if (_check_object_rec(PyArray_DESCR(arr)) < 0) { return NULL; @@ -2190,12 +2178,6 @@ PyArray_Zero(PyArrayObject *arr) return NULL; } - if (zero_obj == NULL) { - zero_obj = PyLong_FromLong((long) 0); - if (zero_obj == NULL) { - return NULL; - } - } if (PyArray_ISOBJECT(arr)) { /* XXX this is dangerous, the caller probably is not aware that zeroval is actually a static PyObject* @@ -2203,12 +2185,12 @@ PyArray_Zero(PyArrayObject *arr) if they simply memcpy it into a ndarray without using setitem(), refcount errors will occur */ - memcpy(zeroval, &zero_obj, sizeof(PyObject *)); + memcpy(zeroval, &npy_static_pydata.zero_obj, sizeof(PyObject *)); return zeroval; } storeflags = PyArray_FLAGS(arr); PyArray_ENABLEFLAGS(arr, NPY_ARRAY_BEHAVED); - ret = PyArray_SETITEM(arr, zeroval, zero_obj); + ret = PyArray_SETITEM(arr, zeroval, npy_static_pydata.zero_obj); ((PyArrayObject_fields *)arr)->flags = storeflags; if (ret < 0) { PyDataMem_FREE(zeroval); @@ -2225,7 +2207,6 @@ PyArray_One(PyArrayObject *arr) { char *oneval; int ret, storeflags; - static PyObject * one_obj = NULL; if (_check_object_rec(PyArray_DESCR(arr)) < 0) { return NULL; @@ -2236,12 +2217,6 @@ PyArray_One(PyArrayObject *arr) return NULL; } - if (one_obj == NULL) { - one_obj = PyLong_FromLong((long) 1); - if (one_obj == NULL) { - return NULL; - } - } if (PyArray_ISOBJECT(arr)) { /* XXX this is dangerous, the caller probably is not aware that oneval is actually a static PyObject* @@ -2249,13 +2224,13 @@ PyArray_One(PyArrayObject *arr) if they simply memcpy it into a ndarray without using setitem(), refcount errors will occur */ - memcpy(oneval, &one_obj, sizeof(PyObject *)); + memcpy(oneval, &npy_static_pydata.one_obj, sizeof(PyObject *)); return oneval; } storeflags = PyArray_FLAGS(arr); PyArray_ENABLEFLAGS(arr, NPY_ARRAY_BEHAVED); - ret = PyArray_SETITEM(arr, oneval, one_obj); + ret = PyArray_SETITEM(arr, oneval, npy_static_pydata.one_obj); ((PyArrayObject_fields *)arr)->flags = storeflags; if (ret < 0) { PyDataMem_FREE(oneval); @@ -2638,13 +2613,7 @@ complex_to_noncomplex_get_loop( PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata, NPY_ARRAYMETHOD_FLAGS *flags) { - static PyObject *cls = NULL; - int ret; - npy_cache_import("numpy.exceptions", "ComplexWarning", &cls); - if (cls == NULL) { - return -1; - } - ret = PyErr_WarnEx(cls, + int ret = PyErr_WarnEx(npy_static_pydata.ComplexWarning, "Casting complex values to real discards " "the imaginary part", 1); if (ret < 0) { @@ -3268,31 +3237,11 @@ nonstructured_to_structured_get_loop( return 0; } - static PyObject * PyArray_GetGenericToVoidCastingImpl(void) { - static PyArrayMethodObject *method = NULL; - - if (method != NULL) { - Py_INCREF(method); - return (PyObject *)method; - } - - method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); - if (method == NULL) { - return PyErr_NoMemory(); - } - - method->name = "any_to_void_cast"; - method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; - method->casting = -1; - method->resolve_descriptors = &nonstructured_to_structured_resolve_descriptors; - method->get_strided_loop = &nonstructured_to_structured_get_loop; - method->nin = 1; - method->nout = 1; - - return (PyObject *)method; + Py_INCREF(npy_static_pydata.GenericToVoidMethod); + return npy_static_pydata.GenericToVoidMethod; } @@ -3429,27 +3378,8 @@ structured_to_nonstructured_get_loop( static PyObject * PyArray_GetVoidToGenericCastingImpl(void) { - static PyArrayMethodObject *method = NULL; - - if (method != NULL) { - Py_INCREF(method); - return (PyObject *)method; - } - - method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); - if (method == NULL) { - return PyErr_NoMemory(); - } - - method->name = "void_to_any_cast"; - method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; - method->casting = -1; - method->resolve_descriptors = &structured_to_nonstructured_resolve_descriptors; - method->get_strided_loop = &structured_to_nonstructured_get_loop; - method->nin = 1; - method->nout = 1; - - return (PyObject *)method; + Py_INCREF(npy_static_pydata.VoidToGenericMethod); + return npy_static_pydata.VoidToGenericMethod; } @@ -3813,31 +3743,11 @@ object_to_any_resolve_descriptors( static PyObject * PyArray_GetObjectToGenericCastingImpl(void) { - static PyArrayMethodObject *method = NULL; - - if (method != NULL) { - Py_INCREF(method); - return (PyObject *)method; - } - - method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); - if (method == NULL) { - return PyErr_NoMemory(); - } - - method->nin = 1; - method->nout = 1; - method->name = "object_to_any_cast"; - method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; - method->casting = NPY_UNSAFE_CASTING; - method->resolve_descriptors = &object_to_any_resolve_descriptors; - method->get_strided_loop = &object_to_any_get_loop; - - return (PyObject *)method; + Py_INCREF(npy_static_pydata.ObjectToGenericMethod); + return npy_static_pydata.ObjectToGenericMethod; } - /* Any object is simple (could even use the default) */ static NPY_CASTING any_to_object_resolve_descriptors( @@ -3870,27 +3780,8 @@ any_to_object_resolve_descriptors( static PyObject * PyArray_GetGenericToObjectCastingImpl(void) { - static PyArrayMethodObject *method = NULL; - - if (method != NULL) { - Py_INCREF(method); - return (PyObject *)method; - } - - method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); - if (method == NULL) { - return PyErr_NoMemory(); - } - - method->nin = 1; - method->nout = 1; - method->name = "any_to_object_cast"; - method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; - method->casting = NPY_SAFE_CASTING; - method->resolve_descriptors = &any_to_object_resolve_descriptors; - method->get_strided_loop = &any_to_object_get_loop; - - return (PyObject *)method; + Py_INCREF(npy_static_pydata.GenericToObjectMethod); + return npy_static_pydata.GenericToObjectMethod; } @@ -3942,6 +3833,71 @@ PyArray_InitializeObjectToObjectCast(void) return res; } +static int +initialize_void_and_object_globals(void) { + PyArrayMethodObject *method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); + if (method == NULL) { + PyErr_NoMemory(); + return -1; + } + + method->name = "void_to_any_cast"; + method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; + method->casting = -1; + method->resolve_descriptors = &structured_to_nonstructured_resolve_descriptors; + method->get_strided_loop = &structured_to_nonstructured_get_loop; + method->nin = 1; + method->nout = 1; + npy_static_pydata.VoidToGenericMethod = (PyObject *)method; + + method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); + if (method == NULL) { + PyErr_NoMemory(); + return -1; + } + + method->name = "any_to_void_cast"; + method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; + method->casting = -1; + method->resolve_descriptors = &nonstructured_to_structured_resolve_descriptors; + method->get_strided_loop = &nonstructured_to_structured_get_loop; + method->nin = 1; + method->nout = 1; + npy_static_pydata.GenericToVoidMethod = (PyObject *)method; + + method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); + if (method == NULL) { + PyErr_NoMemory(); + return -1; + } + + method->nin = 1; + method->nout = 1; + method->name = "object_to_any_cast"; + method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; + method->casting = NPY_UNSAFE_CASTING; + method->resolve_descriptors = &object_to_any_resolve_descriptors; + method->get_strided_loop = &object_to_any_get_loop; + npy_static_pydata.ObjectToGenericMethod = (PyObject *)method; + + method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type); + if (method == NULL) { + PyErr_NoMemory(); + return -1; + } + + method->nin = 1; + method->nout = 1; + method->name = "any_to_object_cast"; + method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI; + method->casting = NPY_SAFE_CASTING; + method->resolve_descriptors = &any_to_object_resolve_descriptors; + method->get_strided_loop = &any_to_object_get_loop; + npy_static_pydata.GenericToObjectMethod = (PyObject *)method; + + return 0; +} + NPY_NO_EXPORT int PyArray_InitializeCasts() @@ -3962,5 +3918,10 @@ PyArray_InitializeCasts() if (PyArray_InitializeDatetimeCasts() < 0) { return -1; } + + if (initialize_void_and_object_globals() < 0) { + return -1; + } + return 0; } diff --git a/numpy/_core/src/multiarray/convert_datatype.h b/numpy/_core/src/multiarray/convert_datatype.h index 02f25ad0b383..f848ad3b4c8e 100644 --- a/numpy/_core/src/multiarray/convert_datatype.h +++ b/numpy/_core/src/multiarray/convert_datatype.h @@ -13,10 +13,6 @@ extern NPY_NO_EXPORT npy_intp REQUIRED_STR_LEN[]; #define NPY_USE_WEAK_PROMOTION 1 #define NPY_USE_WEAK_PROMOTION_AND_WARN 2 -extern NPY_NO_EXPORT PyObject *NO_NEP50_WARNING_CTX; -extern NPY_NO_EXPORT PyObject *npy_DTypePromotionError; -extern NPY_NO_EXPORT PyObject *npy_UFuncNoLoopError; - NPY_NO_EXPORT int npy_give_promotion_warnings(void); diff --git a/numpy/_core/src/multiarray/ctors.c b/numpy/_core/src/multiarray/ctors.c index 25319f2f6bf5..8ee9d28c3086 100644 --- a/numpy/_core/src/multiarray/ctors.c +++ b/numpy/_core/src/multiarray/ctors.c @@ -16,7 +16,7 @@ #include "npy_pycompat.h" #include "npy_ctypes.h" -#include "multiarraymodule.h" +#include "npy_static_data.h" #include "common.h" #include "ctors.h" @@ -611,15 +611,6 @@ PyArray_AssignFromCache(PyArrayObject *self, coercion_cache_obj *cache) { static void raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr) { - static PyObject *exc_type = NULL; - - npy_cache_import( - "numpy._core._exceptions", "_ArrayMemoryError", - &exc_type); - if (exc_type == NULL) { - goto fail; - } - PyObject *shape = PyArray_IntTupleFromIntp(nd, dims); if (shape == NULL) { goto fail; @@ -631,7 +622,7 @@ raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr) if (exc_value == NULL){ goto fail; } - PyErr_SetObject(exc_type, exc_value); + PyErr_SetObject(npy_static_pydata._ArrayMemoryError, exc_value); Py_DECREF(exc_value); return; @@ -937,17 +928,11 @@ PyArray_NewFromDescr_int( */ if (subtype != &PyArray_Type) { PyObject *res, *func; - static PyObject *ndarray_array_finalize = NULL; - /* First time, cache ndarray's __array_finalize__ */ - if (ndarray_array_finalize == NULL) { - ndarray_array_finalize = PyObject_GetAttr( - (PyObject *)&PyArray_Type, npy_ma_str_array_finalize); - } - func = PyObject_GetAttr((PyObject *)subtype, npy_ma_str_array_finalize); + func = PyObject_GetAttr((PyObject *)subtype, npy_interned_str.array_finalize); if (func == NULL) { goto fail; } - else if (func == ndarray_array_finalize) { + else if (func == npy_static_pydata.ndarray_array_finalize) { Py_DECREF(func); } else if (func == Py_None) { @@ -2045,7 +2030,7 @@ PyArray_FromStructInterface(PyObject *input) PyObject *attr; char endian = NPY_NATBYTE; - attr = PyArray_LookupSpecial_OnInstance(input, npy_ma_str_array_struct); + attr = PyArray_LookupSpecial_OnInstance(input, npy_interned_str.array_struct); if (attr == NULL) { if (PyErr_Occurred()) { return NULL; @@ -2169,7 +2154,7 @@ PyArray_FromInterface(PyObject *origin) npy_intp dims[NPY_MAXDIMS], strides[NPY_MAXDIMS]; int dataflags = NPY_ARRAY_BEHAVED; - iface = PyArray_LookupSpecial_OnInstance(origin, npy_ma_str_array_interface); + iface = PyArray_LookupSpecial_OnInstance(origin, npy_interned_str.array_interface); if (iface == NULL) { if (PyErr_Occurred()) { @@ -2472,7 +2457,7 @@ check_or_clear_and_warn_error_if_due_to_copy_kwarg(PyObject *kwnames) goto restore_error; } int copy_kwarg_unsupported = PyUnicode_Contains( - str_value, npy_ma_str_array_err_msg_substr); + str_value, npy_interned_str.array_err_msg_substr); Py_DECREF(str_value); if (copy_kwarg_unsupported == -1) { goto restore_error; @@ -2524,7 +2509,7 @@ PyArray_FromArrayAttr_int(PyObject *op, PyArray_Descr *descr, int copy, PyObject *new; PyObject *array_meth; - array_meth = PyArray_LookupSpecial_OnInstance(op, npy_ma_str_array); + array_meth = PyArray_LookupSpecial_OnInstance(op, npy_interned_str.array); if (array_meth == NULL) { if (PyErr_Occurred()) { return NULL; @@ -2543,15 +2528,6 @@ PyArray_FromArrayAttr_int(PyObject *op, PyArray_Descr *descr, int copy, return Py_NotImplemented; } - static PyObject *kwnames_is_copy = NULL; - if (kwnames_is_copy == NULL) { - kwnames_is_copy = Py_BuildValue("(s)", "copy"); - if (kwnames_is_copy == NULL) { - Py_DECREF(array_meth); - return NULL; - } - } - Py_ssize_t nargs = 0; PyObject *arguments[2]; PyObject *kwnames = NULL; @@ -2567,7 +2543,7 @@ PyArray_FromArrayAttr_int(PyObject *op, PyArray_Descr *descr, int copy, * signature of the __array__ method being called does not have `copy`. */ if (copy != -1) { - kwnames = kwnames_is_copy; + kwnames = npy_static_pydata.kwnames_is_copy; arguments[nargs] = copy == 1 ? Py_True : Py_False; } diff --git a/numpy/_core/src/multiarray/descriptor.c b/numpy/_core/src/multiarray/descriptor.c index 548ecd9d8df1..b9d30c80a2f8 100644 --- a/numpy/_core/src/multiarray/descriptor.c +++ b/numpy/_core/src/multiarray/descriptor.c @@ -22,7 +22,8 @@ #include "conversion_utils.h" /* for PyArray_TypestrConvert */ #include "templ_common.h" /* for npy_mul_sizes_with_overflow */ #include "descriptor.h" -#include "multiarraymodule.h" +#include "npy_static_data.h" +#include "multiarraymodule.h" // for thread unsafe state access #include "alloc.h" #include "assert.h" #include "npy_buffer.h" @@ -724,13 +725,13 @@ _convert_from_commastring(PyObject *obj, int align) { PyObject *parsed; PyArray_Descr *res; - static PyObject *_commastring = NULL; assert(PyUnicode_Check(obj)); - npy_cache_import("numpy._core._internal", "_commastring", &_commastring); - if (_commastring == NULL) { + npy_cache_import("numpy._core._internal", "_commastring", + &npy_thread_unsafe_state._commastring); + if (npy_thread_unsafe_state._commastring == NULL) { return NULL; } - parsed = PyObject_CallOneArg(_commastring, obj); + parsed = PyObject_CallOneArg(npy_thread_unsafe_state._commastring, obj); if (parsed == NULL) { return NULL; } @@ -2717,7 +2718,7 @@ arraydescr_reduce(PyArray_Descr *self, PyObject *NPY_UNUSED(args)) Py_DECREF(ret); return NULL; } - obj = PyObject_GetAttr(mod, npy_ma_str_dtype); + obj = PyObject_GetAttr(mod, npy_interned_str.dtype); Py_DECREF(mod); if (obj == NULL) { Py_DECREF(ret); diff --git a/numpy/_core/src/multiarray/dlpack.c b/numpy/_core/src/multiarray/dlpack.c index 05935f608a29..51cb454b3a66 100644 --- a/numpy/_core/src/multiarray/dlpack.c +++ b/numpy/_core/src/multiarray/dlpack.c @@ -8,7 +8,7 @@ #include "numpy/arrayobject.h" #include "npy_argparse.h" #include "npy_dlpack.h" -#include "multiarraymodule.h" +#include "npy_static_data.h" #include "conversion_utils.h" @@ -549,7 +549,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *capsule = PyObject_VectorcallMethod( - npy_ma_str___dlpack__, call_args, nargsf, call_kwnames); + npy_interned_str.__dlpack__, call_args, nargsf, call_kwnames); if (capsule == NULL) { /* * TODO: This path should be deprecated in NumPy 2.1. Once deprecated @@ -563,7 +563,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), /* max_version may be unsupported, try without kwargs */ PyErr_Clear(); capsule = PyObject_VectorcallMethod( - npy_ma_str___dlpack__, call_args, nargsf, NULL); + npy_interned_str.__dlpack__, call_args, nargsf, NULL); } if (capsule == NULL) { return NULL; diff --git a/numpy/_core/src/multiarray/dtypemeta.c b/numpy/_core/src/multiarray/dtypemeta.c index 062243aa1402..87a69d8348c1 100644 --- a/numpy/_core/src/multiarray/dtypemeta.c +++ b/numpy/_core/src/multiarray/dtypemeta.c @@ -26,6 +26,8 @@ #include "templ_common.h" #include "refcount.h" #include "dtype_traversal.h" +#include "npy_static_data.h" +#include "multiarraymodule.h" #include @@ -752,7 +754,7 @@ void_common_instance(_PyArray_LegacyDescr *descr1, _PyArray_LegacyDescr *descr2) if (descr1->subarray == NULL && descr1->names == NULL && descr2->subarray == NULL && descr2->names == NULL) { if (descr1->elsize != descr2->elsize) { - PyErr_SetString(npy_DTypePromotionError, + PyErr_SetString(npy_static_pydata.DTypePromotionError, "Invalid type promotion with void datatypes of different " "lengths. Use the `np.bytes_` datatype instead to pad the " "shorter value with trailing zero bytes."); @@ -764,13 +766,13 @@ void_common_instance(_PyArray_LegacyDescr *descr1, _PyArray_LegacyDescr *descr2) if (descr1->names != NULL && descr2->names != NULL) { /* If both have fields promoting individual fields may be possible */ - static PyObject *promote_fields_func = NULL; npy_cache_import("numpy._core._internal", "_promote_fields", - &promote_fields_func); - if (promote_fields_func == NULL) { + &npy_thread_unsafe_state._promote_fields); + if (npy_thread_unsafe_state._promote_fields == NULL) { return NULL; } - PyObject *result = PyObject_CallFunctionObjArgs(promote_fields_func, + PyObject *result = PyObject_CallFunctionObjArgs( + npy_thread_unsafe_state._promote_fields, descr1, descr2, NULL); if (result == NULL) { return NULL; @@ -791,7 +793,7 @@ void_common_instance(_PyArray_LegacyDescr *descr1, _PyArray_LegacyDescr *descr2) return NULL; } if (!cmp) { - PyErr_SetString(npy_DTypePromotionError, + PyErr_SetString(npy_static_pydata.DTypePromotionError, "invalid type promotion with subarray datatypes " "(shape mismatch)."); return NULL; @@ -821,7 +823,7 @@ void_common_instance(_PyArray_LegacyDescr *descr1, _PyArray_LegacyDescr *descr2) return new_descr; } - PyErr_SetString(npy_DTypePromotionError, + PyErr_SetString(npy_static_pydata.DTypePromotionError, "invalid type promotion with structured datatype(s)."); return NULL; } @@ -1238,14 +1240,14 @@ dtypemeta_wrap_legacy_descriptor( /* And it to the types submodule if it is a builtin dtype */ if (!PyTypeNum_ISUSERDEF(descr->type_num)) { - static PyObject *add_dtype_helper = NULL; - npy_cache_import("numpy.dtypes", "_add_dtype_helper", &add_dtype_helper); - if (add_dtype_helper == NULL) { + npy_cache_import("numpy.dtypes", "_add_dtype_helper", + &npy_thread_unsafe_state._add_dtype_helper); + if (npy_thread_unsafe_state._add_dtype_helper == NULL) { return -1; } if (PyObject_CallFunction( - add_dtype_helper, + npy_thread_unsafe_state._add_dtype_helper, "Os", (PyObject *)dtype_class, alias) == NULL) { return -1; } diff --git a/numpy/_core/src/multiarray/getset.c b/numpy/_core/src/multiarray/getset.c index d18463f27bb5..092ac65bbbc3 100644 --- a/numpy/_core/src/multiarray/getset.c +++ b/numpy/_core/src/multiarray/getset.c @@ -25,6 +25,7 @@ #include "alloc.h" #include "npy_buffer.h" #include "shape.h" +#include "multiarraymodule.h" /******************* array attribute get and set routines ******************/ @@ -385,16 +386,16 @@ array_descr_set(PyArrayObject *self, PyObject *arg, void *NPY_UNUSED(ignored)) /* check that we are not reinterpreting memory containing Objects. */ if (_may_have_objects(PyArray_DESCR(self)) || _may_have_objects(newtype)) { - static PyObject *checkfunc = NULL; PyObject *safe; - npy_cache_import("numpy._core._internal", "_view_is_safe", &checkfunc); - if (checkfunc == NULL) { + npy_cache_import("numpy._core._internal", "_view_is_safe", + &npy_thread_unsafe_state._view_is_safe); + if (npy_thread_unsafe_state._view_is_safe == NULL) { goto fail; } - safe = PyObject_CallFunction(checkfunc, "OO", - PyArray_DESCR(self), newtype); + safe = PyObject_CallFunction(npy_thread_unsafe_state._view_is_safe, + "OO", PyArray_DESCR(self), newtype); if (safe == NULL) { goto fail; } diff --git a/numpy/_core/src/multiarray/item_selection.c b/numpy/_core/src/multiarray/item_selection.c index b4943851938d..4d98ce0c350c 100644 --- a/numpy/_core/src/multiarray/item_selection.c +++ b/numpy/_core/src/multiarray/item_selection.c @@ -15,7 +15,7 @@ -#include "multiarraymodule.h" +#include "npy_static_data.h" #include "common.h" #include "dtype_transfer.h" #include "dtypemeta.h" @@ -2262,10 +2262,10 @@ PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) } /* Handle negative axes with standard Python indexing rules */ - if (check_and_adjust_axis_msg(&axis1, ndim, npy_ma_str_axis1) < 0) { + if (check_and_adjust_axis_msg(&axis1, ndim, npy_interned_str.axis1) < 0) { return NULL; } - if (check_and_adjust_axis_msg(&axis2, ndim, npy_ma_str_axis2) < 0) { + if (check_and_adjust_axis_msg(&axis2, ndim, npy_interned_str.axis2) < 0) { return NULL; } if (axis1 == axis2) { diff --git a/numpy/_core/src/multiarray/methods.c b/numpy/_core/src/multiarray/methods.c index da9bd30c8b10..dd40fc4e2f3d 100644 --- a/numpy/_core/src/multiarray/methods.c +++ b/numpy/_core/src/multiarray/methods.c @@ -29,6 +29,7 @@ #include "strfuncs.h" #include "array_assign.h" #include "npy_dlpack.h" +#include "npy_static_data.h" #include "multiarraymodule.h" #include "methods.h" @@ -111,13 +112,15 @@ npy_forward_method( * initialization is not thread-safe, but relies on the CPython GIL to * be correct. */ -#define NPY_FORWARD_NDARRAY_METHOD(name) \ - static PyObject *callable = NULL; \ - npy_cache_import("numpy._core._methods", name, &callable); \ - if (callable == NULL) { \ - return NULL; \ - } \ - return npy_forward_method(callable, (PyObject *)self, args, len_args, kwnames) +#define NPY_FORWARD_NDARRAY_METHOD(name) \ + npy_cache_import( \ + "numpy._core._methods", #name, \ + &npy_thread_unsafe_state.name); \ + if (npy_thread_unsafe_state.name == NULL) { \ + return NULL; \ + } \ + return npy_forward_method(npy_thread_unsafe_state.name, \ + (PyObject *)self, args, len_args, kwnames) static PyObject * @@ -356,14 +359,14 @@ static PyObject * array_max(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_amax"); + NPY_FORWARD_NDARRAY_METHOD(_amax); } static PyObject * array_min(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_amin"); + NPY_FORWARD_NDARRAY_METHOD(_amin); } static PyObject * @@ -387,7 +390,6 @@ PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset) { PyObject *ret = NULL; PyObject *safe; - static PyObject *checkfunc = NULL; int self_elsize, typed_elsize; if (self == NULL) { @@ -405,14 +407,15 @@ PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset) /* check that we are not reinterpreting memory containing Objects. */ if (_may_have_objects(PyArray_DESCR(self)) || _may_have_objects(typed)) { npy_cache_import("numpy._core._internal", "_getfield_is_safe", - &checkfunc); - if (checkfunc == NULL) { + &npy_thread_unsafe_state._getfield_is_safe); + if (npy_thread_unsafe_state._getfield_is_safe == NULL) { Py_DECREF(typed); return NULL; } /* only returns True or raises */ - safe = PyObject_CallFunction(checkfunc, "OOi", PyArray_DESCR(self), + safe = PyObject_CallFunction(npy_thread_unsafe_state._getfield_is_safe, + "OOi", PyArray_DESCR(self), typed, offset); if (safe == NULL) { Py_DECREF(typed); @@ -1044,7 +1047,7 @@ any_array_ufunc_overrides(PyObject *args, PyObject *kwds) } Py_DECREF(out_kwd_obj); /* check where if it exists */ - where_obj = PyDict_GetItemWithError(kwds, npy_ma_str_where); + where_obj = PyDict_GetItemWithError(kwds, npy_interned_str.where); if (where_obj == NULL) { if (PyErr_Occurred()) { return -1; @@ -2244,17 +2247,19 @@ array_setstate(PyArrayObject *self, PyObject *args) NPY_NO_EXPORT int PyArray_Dump(PyObject *self, PyObject *file, int protocol) { - static PyObject *method = NULL; PyObject *ret; - npy_cache_import("numpy._core._methods", "_dump", &method); - if (method == NULL) { + npy_cache_import("numpy._core._methods", "_dump", + &npy_thread_unsafe_state._dump); + if (npy_thread_unsafe_state._dump == NULL) { return -1; } if (protocol < 0) { - ret = PyObject_CallFunction(method, "OO", self, file); + ret = PyObject_CallFunction( + npy_thread_unsafe_state._dump, "OO", self, file); } else { - ret = PyObject_CallFunction(method, "OOi", self, file, protocol); + ret = PyObject_CallFunction( + npy_thread_unsafe_state._dump, "OOi", self, file, protocol); } if (ret == NULL) { return -1; @@ -2267,16 +2272,17 @@ PyArray_Dump(PyObject *self, PyObject *file, int protocol) NPY_NO_EXPORT PyObject * PyArray_Dumps(PyObject *self, int protocol) { - static PyObject *method = NULL; - npy_cache_import("numpy._core._methods", "_dumps", &method); - if (method == NULL) { + npy_cache_import("numpy._core._methods", "_dumps", + &npy_thread_unsafe_state._dumps); + if (npy_thread_unsafe_state._dumps == NULL) { return NULL; } if (protocol < 0) { - return PyObject_CallFunction(method, "O", self); + return PyObject_CallFunction(npy_thread_unsafe_state._dumps, "O", self); } else { - return PyObject_CallFunction(method, "Oi", self, protocol); + return PyObject_CallFunction( + npy_thread_unsafe_state._dumps, "Oi", self, protocol); } } @@ -2285,7 +2291,7 @@ static PyObject * array_dump(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_dump"); + NPY_FORWARD_NDARRAY_METHOD(_dump); } @@ -2293,7 +2299,7 @@ static PyObject * array_dumps(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_dumps"); + NPY_FORWARD_NDARRAY_METHOD(_dumps); } @@ -2345,14 +2351,14 @@ static PyObject * array_mean(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_mean"); + NPY_FORWARD_NDARRAY_METHOD(_mean); } static PyObject * array_sum(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_sum"); + NPY_FORWARD_NDARRAY_METHOD(_sum); } @@ -2382,7 +2388,7 @@ static PyObject * array_prod(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_prod"); + NPY_FORWARD_NDARRAY_METHOD(_prod); } static PyObject * @@ -2442,7 +2448,7 @@ static PyObject * array_any(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_any"); + NPY_FORWARD_NDARRAY_METHOD(_any); } @@ -2450,21 +2456,21 @@ static PyObject * array_all(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_all"); + NPY_FORWARD_NDARRAY_METHOD(_all); } static PyObject * array_stddev(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_std"); + NPY_FORWARD_NDARRAY_METHOD(_std); } static PyObject * array_variance(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_var"); + NPY_FORWARD_NDARRAY_METHOD(_var); } static PyObject * @@ -2545,7 +2551,7 @@ static PyObject * array_clip(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) { - NPY_FORWARD_NDARRAY_METHOD("_clip"); + NPY_FORWARD_NDARRAY_METHOD(_clip); } diff --git a/numpy/_core/src/multiarray/methods.h b/numpy/_core/src/multiarray/methods.h index 9d06794de2aa..f49e0205894d 100644 --- a/numpy/_core/src/multiarray/methods.h +++ b/numpy/_core/src/multiarray/methods.h @@ -1,6 +1,7 @@ #ifndef NUMPY_CORE_SRC_MULTIARRAY_METHODS_H_ #define NUMPY_CORE_SRC_MULTIARRAY_METHODS_H_ +#include "npy_static_data.h" #include "npy_import.h" extern NPY_NO_EXPORT PyMethodDef array_methods[]; @@ -13,22 +14,12 @@ extern NPY_NO_EXPORT PyMethodDef array_methods[]; static inline PyObject * NpyPath_PathlikeToFspath(PyObject *file) { - static PyObject *os_PathLike = NULL; - static PyObject *os_fspath = NULL; - npy_cache_import("os", "PathLike", &os_PathLike); - if (os_PathLike == NULL) { - return NULL; - } - npy_cache_import("os", "fspath", &os_fspath); - if (os_fspath == NULL) { - return NULL; - } - - if (!PyObject_IsInstance(file, os_PathLike)) { + if (!PyObject_IsInstance(file, npy_static_pydata.os_PathLike)) { Py_INCREF(file); return file; } - return PyObject_CallFunctionObjArgs(os_fspath, file, NULL); + return PyObject_CallFunctionObjArgs(npy_static_pydata.os_fspath, + file, NULL); } #endif /* NUMPY_CORE_SRC_MULTIARRAY_METHODS_H_ */ diff --git a/numpy/_core/src/multiarray/multiarraymodule.c b/numpy/_core/src/multiarray/multiarraymodule.c index 993258f4543b..c099b63f4633 100644 --- a/numpy/_core/src/multiarray/multiarraymodule.c +++ b/numpy/_core/src/multiarray/multiarraymodule.c @@ -23,6 +23,7 @@ #include "numpy/arrayobject.h" #include "numpy/arrayscalars.h" +#include "multiarraymodule.h" #include "numpy/npy_math.h" #include "npy_argparse.h" #include "npy_config.h" @@ -63,7 +64,7 @@ NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0; #include "ctors.h" #include "array_assign.h" #include "common.h" -#include "multiarraymodule.h" +#include "npy_static_data.h" #include "cblasfuncs.h" #include "vdot.h" #include "templ_common.h" /* for npy_mul_sizes_with_overflow */ @@ -97,24 +98,15 @@ NPY_NO_EXPORT PyObject * _umath_strings_richcompare( PyArrayObject *self, PyArrayObject *other, int cmp_op, int rstrip); -/* - * global variable to determine if legacy printing is enabled, accessible from - * C. For simplicity the mode is encoded as an integer where INT_MAX means no - * legacy mode, and '113'/'121' means 1.13/1.21 legacy mode; and 0 maps to - * INT_MAX. We can upgrade this if we have more complex requirements in the - * future. - */ -int npy_legacy_print_mode = INT_MAX; - static PyObject * set_legacy_print_mode(PyObject *NPY_UNUSED(self), PyObject *args) { - if (!PyArg_ParseTuple(args, "i", &npy_legacy_print_mode)) { + if (!PyArg_ParseTuple(args, "i", &npy_thread_unsafe_state.legacy_print_mode)) { return NULL; } - if (!npy_legacy_print_mode) { - npy_legacy_print_mode = INT_MAX; + if (!npy_thread_unsafe_state.legacy_print_mode) { + npy_thread_unsafe_state.legacy_print_mode = INT_MAX; } Py_RETURN_NONE; } @@ -136,7 +128,7 @@ PyArray_GetPriority(PyObject *obj, double default_) return NPY_SCALAR_PRIORITY; } - ret = PyArray_LookupSpecial_OnInstance(obj, npy_ma_str_array_priority); + ret = PyArray_LookupSpecial_OnInstance(obj, npy_interned_str.array_priority); if (ret == NULL) { if (PyErr_Occurred()) { /* TODO[gh-14801]: propagate crashes during attribute access? */ @@ -3493,7 +3485,7 @@ array_can_cast_safely(PyObject *NPY_UNUSED(self), * weak-promotion branch is in practice identical to dtype one. */ if (get_npy_promotion_state() == NPY_USE_WEAK_PROMOTION) { - PyObject *descr = PyObject_GetAttr(from_obj, npy_ma_str_dtype); + PyObject *descr = PyObject_GetAttr(from_obj, npy_interned_str.dtype); if (descr == NULL) { goto finish; } @@ -4265,11 +4257,8 @@ array_shares_memory_impl(PyObject *args, PyObject *kwds, Py_ssize_t default_max_ } else if (result == MEM_OVERLAP_TOO_HARD) { if (raise_exceptions) { - static PyObject *too_hard_cls = NULL; - npy_cache_import("numpy.exceptions", "TooHardError", &too_hard_cls); - if (too_hard_cls) { - PyErr_SetString(too_hard_cls, "Exceeded max_work"); - } + PyErr_SetString(npy_static_pydata.TooHardError, + "Exceeded max_work"); return NULL; } else { @@ -4335,8 +4324,8 @@ _set_numpy_warn_if_no_mem_policy(PyObject *NPY_UNUSED(self), PyObject *arg) if (res < 0) { return NULL; } - int old_value = numpy_warn_if_no_mem_policy; - numpy_warn_if_no_mem_policy = res; + int old_value = npy_thread_unsafe_state.warn_if_no_mem_policy; + npy_thread_unsafe_state.warn_if_no_mem_policy = res; if (old_value) { Py_RETURN_TRUE; } @@ -4348,8 +4337,6 @@ _set_numpy_warn_if_no_mem_policy(PyObject *NPY_UNUSED(self), PyObject *arg) static PyObject * _reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) { - static int initialized = 0; - #if !defined(PYPY_VERSION) if (PyThreadState_Get()->interp != PyInterpreterState_Main()) { if (PyErr_WarnEx(PyExc_UserWarning, @@ -4365,11 +4352,11 @@ _reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) { return NULL; } /* No need to give the other warning in a sub-interpreter as well... */ - initialized = 1; + npy_thread_unsafe_state.reload_guard_initialized = 1; Py_RETURN_NONE; } #endif - if (initialized) { + if (npy_thread_unsafe_state.reload_guard_initialized) { if (PyErr_WarnEx(PyExc_UserWarning, "The NumPy module was reloaded (imported a second time). " "This can in some cases result in small but subtle issues " @@ -4377,7 +4364,7 @@ _reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) { return NULL; } } - initialized = 1; + npy_thread_unsafe_state.reload_guard_initialized = 1; Py_RETURN_NONE; } @@ -4771,160 +4758,24 @@ set_flaginfo(PyObject *d) return; } -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_current_allocator = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_function = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_struct = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_interface = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_priority = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_wrap = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_finalize = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_implementation = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_axis1 = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_axis2 = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_like = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_numpy = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_where = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_convert = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_preserve = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_convert_if_no_array = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_cpu = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_dtype = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_err_msg_substr = NULL; -NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str___dlpack__ = NULL; +// static variables are automatically zero-initialized +NPY_VISIBILITY_HIDDEN npy_thread_unsafe_state_struct npy_thread_unsafe_state; static int -intern_strings(void) -{ - npy_ma_str_current_allocator = PyUnicode_InternFromString("current_allocator"); - if (npy_ma_str_current_allocator == NULL) { - return -1; - } - npy_ma_str_array = PyUnicode_InternFromString("__array__"); - if (npy_ma_str_array == NULL) { - return -1; - } - npy_ma_str_array_function = PyUnicode_InternFromString("__array_function__"); - if (npy_ma_str_array_function == NULL) { - return -1; - } - npy_ma_str_array_struct = PyUnicode_InternFromString("__array_struct__"); - if (npy_ma_str_array_struct == NULL) { - return -1; - } - npy_ma_str_array_priority = PyUnicode_InternFromString("__array_priority__"); - if (npy_ma_str_array_priority == NULL) { - return -1; - } - npy_ma_str_array_interface = PyUnicode_InternFromString("__array_interface__"); - if (npy_ma_str_array_interface == NULL) { - return -1; - } - npy_ma_str_array_wrap = PyUnicode_InternFromString("__array_wrap__"); - if (npy_ma_str_array_wrap == NULL) { - return -1; - } - npy_ma_str_array_finalize = PyUnicode_InternFromString("__array_finalize__"); - if (npy_ma_str_array_finalize == NULL) { - return -1; - } - npy_ma_str_implementation = PyUnicode_InternFromString("_implementation"); - if (npy_ma_str_implementation == NULL) { - return -1; - } - npy_ma_str_axis1 = PyUnicode_InternFromString("axis1"); - if (npy_ma_str_axis1 == NULL) { - return -1; - } - npy_ma_str_axis2 = PyUnicode_InternFromString("axis2"); - if (npy_ma_str_axis2 == NULL) { - return -1; - } - npy_ma_str_like = PyUnicode_InternFromString("like"); - if (npy_ma_str_like == NULL) { - return -1; - } - npy_ma_str_numpy = PyUnicode_InternFromString("numpy"); - if (npy_ma_str_numpy == NULL) { - return -1; - } - npy_ma_str_where = PyUnicode_InternFromString("where"); - if (npy_ma_str_where == NULL) { - return -1; - } - /* scalar policies */ - npy_ma_str_convert = PyUnicode_InternFromString("convert"); - if (npy_ma_str_convert == NULL) { - return -1; - } - npy_ma_str_preserve = PyUnicode_InternFromString("preserve"); - if (npy_ma_str_preserve == NULL) { - return -1; - } - npy_ma_str_convert_if_no_array = PyUnicode_InternFromString("convert_if_no_array"); - if (npy_ma_str_convert_if_no_array == NULL) { - return -1; - } - npy_ma_str_cpu = PyUnicode_InternFromString("cpu"); - if (npy_ma_str_cpu == NULL) { - return -1; - } - npy_ma_str_dtype = PyUnicode_InternFromString("dtype"); - if (npy_ma_str_dtype == NULL) { - return -1; - } - npy_ma_str_array_err_msg_substr = PyUnicode_InternFromString( - "__array__() got an unexpected keyword argument 'copy'"); - if (npy_ma_str_array_err_msg_substr == NULL) { - return -1; - } - npy_ma_str___dlpack__ = PyUnicode_InternFromString("__dlpack__"); - if (npy_ma_str___dlpack__ == NULL) { - return -1; - } - return 0; -} - - -/* - * Initializes global constants. At some points these need to be cleaned - * up, and sometimes we also import them where they are needed. But for - * some things, adding an `npy_cache_import` everywhere seems inconvenient. - * - * These globals should not need the C-layer at all and will be imported - * before anything on the C-side is initialized. - */ -static int -initialize_static_globals(void) -{ - assert(npy_DTypePromotionError == NULL); - npy_cache_import( - "numpy.exceptions", "DTypePromotionError", - &npy_DTypePromotionError); - if (npy_DTypePromotionError == NULL) { - return -1; - } - - assert(npy_UFuncNoLoopError == NULL); - npy_cache_import( - "numpy._core._exceptions", "_UFuncNoLoopError", - &npy_UFuncNoLoopError); - if (npy_UFuncNoLoopError == NULL) { - return -1; - } - +initialize_thread_unsafe_state(void) { char *env = getenv("NUMPY_WARN_IF_NO_MEM_POLICY"); if ((env != NULL) && (strncmp(env, "1", 1) == 0)) { - numpy_warn_if_no_mem_policy = 1; + npy_thread_unsafe_state.warn_if_no_mem_policy = 1; } else { - numpy_warn_if_no_mem_policy = 0; + npy_thread_unsafe_state.warn_if_no_mem_policy = 0; } + npy_thread_unsafe_state.legacy_print_mode = INT_MAX; + return 0; } - static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_multiarray_umath", @@ -4987,6 +4838,10 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { goto err; } + if (initialize_thread_unsafe_state() < 0) { + goto err; + } + if (init_extobj() < 0) { goto err; } @@ -5186,6 +5041,23 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { goto err; } + // initialize static references to ndarray.__array_*__ special methods + npy_static_pydata.ndarray_array_finalize = PyObject_GetAttrString( + (PyObject *)&PyArray_Type, "__array_finalize__"); + if (npy_static_pydata.ndarray_array_finalize == NULL) { + goto err; + } + npy_static_pydata.ndarray_array_ufunc = PyObject_GetAttrString( + (PyObject *)&PyArray_Type, "__array_ufunc__"); + if (npy_static_pydata.ndarray_array_ufunc == NULL) { + goto err; + } + npy_static_pydata.ndarray_array_function = PyObject_GetAttrString( + (PyObject *)&PyArray_Type, "__array_function__"); + if (npy_static_pydata.ndarray_array_function == NULL) { + goto err; + } + /* * Initialize np.dtypes.StringDType * @@ -5195,14 +5067,14 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { * init_string_dtype() but that needs to happen after * the legacy dtypemeta classes are available. */ - static PyObject *add_dtype_helper = NULL; - npy_cache_import("numpy.dtypes", "_add_dtype_helper", &add_dtype_helper); - if (add_dtype_helper == NULL) { + npy_cache_import("numpy.dtypes", "_add_dtype_helper", + &npy_thread_unsafe_state._add_dtype_helper); + if (npy_thread_unsafe_state._add_dtype_helper == NULL) { goto err; } if (PyObject_CallFunction( - add_dtype_helper, + npy_thread_unsafe_state._add_dtype_helper, "Os", (PyObject *)&PyArray_StringDType, NULL) == NULL) { goto err; } @@ -5225,6 +5097,19 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { goto err; } + // initialize static reference to a zero-like array + npy_static_pydata.zero_pyint_like_arr = PyArray_ZEROS( + 0, NULL, NPY_LONG, NPY_FALSE); + if (npy_static_pydata.zero_pyint_like_arr == NULL) { + goto err; + } + ((PyArrayObject_fields *)npy_static_pydata.zero_pyint_like_arr)->flags |= + (NPY_ARRAY_WAS_PYTHON_INT|NPY_ARRAY_WAS_INT_AND_REPLACED); + + if (verify_static_structs_initialized() < 0) { + goto err; + } + /* * Export the API tables */ diff --git a/numpy/_core/src/multiarray/multiarraymodule.h b/numpy/_core/src/multiarray/multiarraymodule.h index b3f15686dfe0..218dc601613a 100644 --- a/numpy/_core/src/multiarray/multiarraymodule.h +++ b/numpy/_core/src/multiarray/multiarraymodule.h @@ -1,26 +1,95 @@ #ifndef NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_ #define NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_ -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_current_allocator; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_function; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_struct; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_priority; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_interface; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_wrap; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_finalize; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_implementation; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_axis1; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_axis2; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_like; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_numpy; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_where; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_convert; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_preserve; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_convert_if_no_array; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_cpu; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_dtype; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_err_msg_substr; -NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str___dlpack__; +/* + * A struct storing thread-unsafe global state for the _multiarray_umath + * module. We should refactor so the global state is thread-safe, + * e.g. by adding locking. + */ +typedef struct npy_thread_unsafe_state_struct { + /* + * Cached references to objects obtained via an import. All of these are + * can be initialized at any time by npy_cache_import. + * + * Currently these are not initialized in a thread-safe manner but the + * failure mode is a reference leak for references to imported immortal + * modules so it will never lead to a crash unless users are doing something + * janky that we don't support like reloading. + * + * TODO: maybe make each entry a struct that looks like: + * + * struct { + * atomic_int initialized; + * PyObject *value; + * } + * + * so the initialization is thread-safe and the only possibile lock + * contention happens before the cache is initialized, not on every single + * read. + */ + PyObject *_add_dtype_helper; + PyObject *_all; + PyObject *_amax; + PyObject *_amin; + PyObject *_any; + PyObject *array_function_errmsg_formatter; + PyObject *array_ufunc_errmsg_formatter; + PyObject *_clip; + PyObject *_commastring; + PyObject *_convert_to_stringdtype_kwargs; + PyObject *_default_array_repr; + PyObject *_default_array_str; + PyObject *_dump; + PyObject *_dumps; + PyObject *_getfield_is_safe; + PyObject *internal_gcd_func; + PyObject *_mean; + PyObject *NO_NEP50_WARNING; + PyObject *npy_ctypes_check; + PyObject *numpy_matrix; + PyObject *_prod; + PyObject *_promote_fields; + PyObject *_std; + PyObject *_sum; + PyObject *_ufunc_doc_signature_formatter; + PyObject *_var; + PyObject *_view_is_safe; + PyObject *_void_scalar_to_string; + + /* + * Used to test the internal-only scaled float test dtype + */ + npy_bool get_sfloat_dtype_initialized; + + /* + * controls the global madvise hugepage setting + */ + int madvise_hugepage; + + /* + * used to detect module reloading in the reload guard + */ + int reload_guard_initialized; + + /* + * global variable to determine if legacy printing is enabled, + * accessible from C. For simplicity the mode is encoded as an + * integer where INT_MAX means no legacy mode, and '113'/'121' + * means 1.13/1.21 legacy mode; and 0 maps to INT_MAX. We can + * upgrade this if we have more complex requirements in the future. + */ + int legacy_print_mode; + + /* + * Holds the user-defined setting for whether or not to warn + * if there is no memory policy set + */ + int warn_if_no_mem_policy; + +} npy_thread_unsafe_state_struct; + + +NPY_VISIBILITY_HIDDEN extern npy_thread_unsafe_state_struct npy_thread_unsafe_state; + #endif /* NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_ */ diff --git a/numpy/_core/src/multiarray/npy_static_data.c b/numpy/_core/src/multiarray/npy_static_data.c new file mode 100644 index 000000000000..7f5e58dde21a --- /dev/null +++ b/numpy/_core/src/multiarray/npy_static_data.c @@ -0,0 +1,258 @@ +/* numpy static data structs and initialization */ +#define NPY_NO_DEPRECATED_API NPY_API_VERSION +#define _UMATHMODULE +#define _MULTIARRAYMODULE + +#define PY_SSIZE_T_CLEAN +#include +#include + +#include "numpy/ndarraytypes.h" +#include "numpy/npy_common.h" +#include "numpy/arrayobject.h" +#include "npy_import.h" +#include "npy_static_data.h" +#include "extobj.h" + +// static variables are zero-filled by default, no need to explicitly do so +NPY_VISIBILITY_HIDDEN npy_interned_str_struct npy_interned_str; +NPY_VISIBILITY_HIDDEN npy_static_pydata_struct npy_static_pydata; +NPY_VISIBILITY_HIDDEN npy_static_cdata_struct npy_static_cdata; + +#define INTERN_STRING(struct_member, string) \ + assert(npy_interned_str.struct_member == NULL); \ + npy_interned_str.struct_member = PyUnicode_InternFromString(string); \ + if (npy_interned_str.struct_member == NULL) { \ + return -1; \ + } \ + +NPY_NO_EXPORT int +intern_strings(void) +{ + INTERN_STRING(current_allocator, "current_allocator"); + INTERN_STRING(array, "__array__"); + INTERN_STRING(array_function, "__array_function__"); + INTERN_STRING(array_struct, "__array_struct__"); + INTERN_STRING(array_priority, "__array_priority__"); + INTERN_STRING(array_interface, "__array_interface__"); + INTERN_STRING(array_ufunc, "__array_ufunc__"); + INTERN_STRING(array_wrap, "__array_wrap__"); + INTERN_STRING(array_finalize, "__array_finalize__"); + INTERN_STRING(implementation, "_implementation"); + INTERN_STRING(axis1, "axis1"); + INTERN_STRING(axis2, "axis2"); + INTERN_STRING(like, "like"); + INTERN_STRING(numpy, "numpy"); + INTERN_STRING(where, "where"); + INTERN_STRING(convert, "convert"); + INTERN_STRING(preserve, "preserve"); + INTERN_STRING(convert_if_no_array, "convert_if_no_array"); + INTERN_STRING(cpu, "cpu"); + INTERN_STRING(dtype, "dtype"); + INTERN_STRING( + array_err_msg_substr, + "__array__() got an unexpected keyword argument 'copy'"); + INTERN_STRING(out, "out"); + INTERN_STRING(errmode_strings[0], "ignore"); + INTERN_STRING(errmode_strings[1], "warn"); + INTERN_STRING(errmode_strings[2], "raise"); + INTERN_STRING(errmode_strings[3], "call"); + INTERN_STRING(errmode_strings[4], "print"); + INTERN_STRING(errmode_strings[5], "log"); + INTERN_STRING(__dlpack__, "__dlpack__"); + INTERN_STRING(pyvals_name, "UFUNC_PYVALS_NAME"); + return 0; +} + +#define IMPORT_GLOBAL(base_path, name, object) \ + assert(object == NULL); \ + npy_cache_import(base_path, name, &object); \ + if (object == NULL) { \ + return -1; \ + } + + +/* + * Initializes global constants. + * + * All global constants should live inside the npy_static_pydata + * struct. + * + * Not all entries in the struct are initialized here, some are + * initialized later but care must be taken in those cases to initialize + * the constant in a thread-safe manner, ensuring it is initialized + * exactly once. + * + * Anything initialized here is initialized during module import which + * the python interpreter ensures is done in a single thread. + * + * Anything imported here should not need the C-layer at all and will be + * imported before anything on the C-side is initialized. + */ +NPY_NO_EXPORT int +initialize_static_globals(void) +{ + /* + * Initialize contents of npy_static_pydata struct + * + * This struct holds cached references to python objects + * that we want to keep alive for the lifetime of the + * module for performance reasons + */ + + IMPORT_GLOBAL("math", "floor", + npy_static_pydata.math_floor_func); + + IMPORT_GLOBAL("math", "ceil", + npy_static_pydata.math_ceil_func); + + IMPORT_GLOBAL("math", "trunc", + npy_static_pydata.math_trunc_func); + + IMPORT_GLOBAL("math", "gcd", + npy_static_pydata.math_gcd_func); + + IMPORT_GLOBAL("numpy.exceptions", "AxisError", + npy_static_pydata.AxisError); + + IMPORT_GLOBAL("numpy.exceptions", "ComplexWarning", + npy_static_pydata.ComplexWarning); + + IMPORT_GLOBAL("numpy.exceptions", "DTypePromotionError", + npy_static_pydata.DTypePromotionError); + + IMPORT_GLOBAL("numpy.exceptions", "TooHardError", + npy_static_pydata.TooHardError); + + IMPORT_GLOBAL("numpy.exceptions", "VisibleDeprecationWarning", + npy_static_pydata.VisibleDeprecationWarning); + + IMPORT_GLOBAL("numpy._globals", "_CopyMode", + npy_static_pydata._CopyMode); + + IMPORT_GLOBAL("numpy._globals", "_NoValue", + npy_static_pydata._NoValue); + + IMPORT_GLOBAL("numpy._core._exceptions", "_ArrayMemoryError", + npy_static_pydata._ArrayMemoryError); + + IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncBinaryResolutionError", + npy_static_pydata._UFuncBinaryResolutionError); + + IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncInputCastingError", + npy_static_pydata._UFuncInputCastingError); + + IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncNoLoopError", + npy_static_pydata._UFuncNoLoopError); + + IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncOutputCastingError", + npy_static_pydata._UFuncOutputCastingError); + + IMPORT_GLOBAL("os", "fspath", + npy_static_pydata.os_fspath); + + IMPORT_GLOBAL("os", "PathLike", + npy_static_pydata.os_PathLike); + + // default_truediv_type_tup + PyArray_Descr *tmp = PyArray_DescrFromType(NPY_DOUBLE); + npy_static_pydata.default_truediv_type_tup = + PyTuple_Pack(3, tmp, tmp, tmp); + Py_DECREF(tmp); + if (npy_static_pydata.default_truediv_type_tup == NULL) { + return -1; + } + + npy_static_pydata.kwnames_is_copy = Py_BuildValue("(s)", "copy"); + if (npy_static_pydata.kwnames_is_copy == NULL) { + return -1; + } + + npy_static_pydata.one_obj = PyLong_FromLong((long) 1); + if (npy_static_pydata.one_obj == NULL) { + return -1; + } + + npy_static_pydata.zero_obj = PyLong_FromLong((long) 0); + if (npy_static_pydata.zero_obj == NULL) { + return -1; + } + + /* + * Initialize contents of npy_static_cdata struct + * + * Note that some entries are initialized elsewhere. Care + * must be taken to ensure all entries are initialized during + * module initialization and immutable thereafter. + * + * This struct holds global static caches. These are set + * up this way for performance reasons. + */ + + PyObject *flags = PySys_GetObject("flags"); /* borrowed object */ + if (flags == NULL) { + PyErr_SetString(PyExc_AttributeError, "cannot get sys.flags"); + return -1; + } + PyObject *level = PyObject_GetAttrString(flags, "optimize"); + if (level == NULL) { + return -1; + } + npy_static_cdata.optimize = PyLong_AsLong(level); + Py_DECREF(level); + + /* + * see unpack_bits for how this table is used. + * + * LUT for bigendian bitorder, littleendian is handled via + * byteswapping in the loop. + * + * 256 8 byte blocks representing 8 bits expanded to 1 or 0 bytes + */ + npy_intp j; + for (j=0; j < 256; j++) { + npy_intp k; + for (k=0; k < 8; k++) { + npy_uint8 v = (j & (1 << k)) == (1 << k); + npy_static_cdata.unpack_lookup_big[j].bytes[7 - k] = v; + } + } + + return 0; +} + + +/* + * Verifies all entries in npy_interned_str and npy_static_pydata are + * non-NULL. + * + * Called at the end of initialization for _multiarray_umath. Some + * entries are initialized outside of this file because they depend on + * items that are initialized late in module initialization but they + * should all be initialized by the time this function is called. + */ +NPY_NO_EXPORT int +verify_static_structs_initialized(void) { + // verify all entries in npy_interned_str are filled in + for (int i=0; i < (sizeof(npy_interned_str_struct)/sizeof(PyObject *)); i++) { + if (*(((PyObject **)&npy_interned_str) + i) == NULL) { + PyErr_Format( + PyExc_SystemError, + "NumPy internal error: NULL entry detected in " + "npy_interned_str at index %d", i); + return -1; + } + } + + // verify all entries in npy_static_pydata are filled in + for (int i=0; i < (sizeof(npy_static_pydata_struct)/sizeof(PyObject *)); i++) { + if (*(((PyObject **)&npy_static_pydata) + i) == NULL) { + PyErr_Format( + PyExc_SystemError, + "NumPy internal error: NULL entry detected in " + "npy_static_pydata at index %d", i); + return -1; + } + } + return 0; +} diff --git a/numpy/_core/src/multiarray/npy_static_data.h b/numpy/_core/src/multiarray/npy_static_data.h new file mode 100644 index 000000000000..c4d3ef4cdfee --- /dev/null +++ b/numpy/_core/src/multiarray/npy_static_data.h @@ -0,0 +1,167 @@ +#ifndef NUMPY_CORE_SRC_MULTIARRAY_STATIC_DATA_H_ +#define NUMPY_CORE_SRC_MULTIARRAY_STATIC_DATA_H_ + +NPY_NO_EXPORT int +initialize_static_globals(void); + +NPY_NO_EXPORT int +intern_strings(void); + +NPY_NO_EXPORT int +verify_static_structs_initialized(void); + +typedef struct npy_interned_str_struct { + PyObject *current_allocator; + PyObject *array; + PyObject *array_function; + PyObject *array_struct; + PyObject *array_priority; + PyObject *array_interface; + PyObject *array_wrap; + PyObject *array_finalize; + PyObject *array_ufunc; + PyObject *implementation; + PyObject *axis1; + PyObject *axis2; + PyObject *like; + PyObject *numpy; + PyObject *where; + PyObject *convert; + PyObject *preserve; + PyObject *convert_if_no_array; + PyObject *cpu; + PyObject *dtype; + PyObject *array_err_msg_substr; + PyObject *out; + PyObject *errmode_strings[6]; + PyObject *__dlpack__; + PyObject *pyvals_name; +} npy_interned_str_struct; + +/* + * A struct that stores static global data used throughout + * _multiarray_umath, mostly to cache results that would be + * prohibitively expensive to compute at runtime in a tight loop. + * + * All items in this struct should be initialized during module + * initialization and thereafter should be immutable. Mutating items in + * this struct after module initialization is likely not thread-safe. + */ + +typedef struct npy_static_pydata_struct { + /* + * Used in ufunc_type_resolution.c to avoid reconstructing a tuple + * storing the default true division return types. + */ + PyObject *default_truediv_type_tup; + + /* + * Used to set up the default extobj context variable + */ + PyObject *default_extobj_capsule; + + /* + * The global ContextVar to store the extobject. It is exposed to Python + * as `_extobj_contextvar`. + */ + PyObject *npy_extobj_contextvar; + + /* + * A reference to ndarray's implementations for __array_*__ special methods + */ + PyObject *ndarray_array_ufunc; + PyObject *ndarray_array_finalize; + PyObject *ndarray_array_function; + + /* + * References to the '1' and '0' PyLong objects + */ + PyObject *one_obj; + PyObject *zero_obj; + + /* + * Reference to an np.array(0, dtype=np.long) instance + */ + PyObject *zero_pyint_like_arr; + + /* + * References to items obtained via an import at module initialization + */ + PyObject *AxisError; + PyObject *ComplexWarning; + PyObject *DTypePromotionError; + PyObject *TooHardError; + PyObject *VisibleDeprecationWarning; + PyObject *_CopyMode; + PyObject *_NoValue; + PyObject *_ArrayMemoryError; + PyObject *_UFuncBinaryResolutionError; + PyObject *_UFuncInputCastingError; + PyObject *_UFuncNoLoopError; + PyObject *_UFuncOutputCastingError; + PyObject *math_floor_func; + PyObject *math_ceil_func; + PyObject *math_trunc_func; + PyObject *math_gcd_func; + PyObject *os_PathLike; + PyObject *os_fspath; + + /* + * Used in the __array__ internals to avoid building a tuple inline + */ + PyObject *kwnames_is_copy; + + /* + * Used in __imatmul__ to avoid building tuples inline + */ + PyObject *axes_1d_obj_kwargs; + PyObject *axes_2d_obj_kwargs; + + /* + * Used for CPU feature detection and dispatch + */ + PyObject *cpu_dispatch_registry; + + /* + * references to ArrayMethod implementations that are cached + * to avoid repeatedly creating them + */ + PyObject *VoidToGenericMethod; + PyObject *GenericToVoidMethod; + PyObject *ObjectToGenericMethod; + PyObject *GenericToObjectMethod; +} npy_static_pydata_struct; + + +typedef struct npy_static_cdata_struct { + /* + * stores sys.flags.optimize as a long, which is used in the add_docstring + * implementation + */ + long optimize; + + /* + * LUT used by unpack_bits + */ + union { + npy_uint8 bytes[8]; + npy_uint64 uint64; + } unpack_lookup_big[256]; + + /* + * A look-up table to recover integer type numbers from type characters. + * + * See the _MAX_LETTER and LETTER_TO_NUM macros in arraytypes.c.src. + * + * The smallest type number is ?, the largest is bounded by 'z'. + * + * This is initialized alongside the built-in dtypes + */ + npy_int16 _letter_to_num['z' + 1 - '?']; +} npy_static_cdata_struct; + +NPY_VISIBILITY_HIDDEN extern npy_interned_str_struct npy_interned_str; +NPY_VISIBILITY_HIDDEN extern npy_static_pydata_struct npy_static_pydata; +NPY_VISIBILITY_HIDDEN extern npy_static_cdata_struct npy_static_cdata; + +#endif // NUMPY_CORE_SRC_MULTIARRAY_STATIC_DATA_H_ diff --git a/numpy/_core/src/multiarray/number.c b/numpy/_core/src/multiarray/number.c index 9532662b327a..f537d2b68e41 100644 --- a/numpy/_core/src/multiarray/number.c +++ b/numpy/_core/src/multiarray/number.c @@ -23,6 +23,10 @@ **************** Implement Number Protocol **************************** *************************************************************************/ +// this is not in the global data struct to avoid needing to include the +// definition of the NumericOps struct in multiarraymodule.h +// +// it is filled in during module initialization in a thread-safe manner NPY_NO_EXPORT NumericOps n_ops; /* NB: static objects initialized to zero */ /* @@ -118,6 +122,20 @@ _PyArray_SetNumericOps(PyObject *dict) SET(conjugate); SET(matmul); SET(clip); + + // initialize static globals needed for matmul + npy_static_pydata.axes_1d_obj_kwargs = Py_BuildValue( + "{s, [(i), (i, i), (i)]}", "axes", -1, -2, -1, -1); + if (npy_static_pydata.axes_1d_obj_kwargs == NULL) { + return -1; + } + + npy_static_pydata.axes_2d_obj_kwargs = Py_BuildValue( + "{s, [(i, i), (i, i), (i, i)]}", "axes", -2, -1, -2, -1, -2, -1); + if (npy_static_pydata.axes_2d_obj_kwargs == NULL) { + return -1; + } + return 0; } @@ -268,15 +286,15 @@ array_matrix_multiply(PyObject *m1, PyObject *m2) static PyObject * array_inplace_matrix_multiply(PyArrayObject *self, PyObject *other) { - static PyObject *AxisError_cls = NULL; - npy_cache_import("numpy.exceptions", "AxisError", &AxisError_cls); - if (AxisError_cls == NULL) { - return NULL; - } - INPLACE_GIVE_UP_IF_NEEDED(self, other, nb_inplace_matrix_multiply, array_inplace_matrix_multiply); + PyObject *args = PyTuple_Pack(3, self, other, self); + if (args == NULL) { + return NULL; + } + PyObject *kwargs; + /* * Unlike `matmul(a, b, out=a)` we ensure that the result is not broadcast * if the result without `out` would have less dimensions than `a`. @@ -286,33 +304,11 @@ array_inplace_matrix_multiply(PyArrayObject *self, PyObject *other) * The error here will be confusing, but for now, we enforce this by * passing the correct `axes=`. */ - static PyObject *axes_1d_obj_kwargs = NULL; - static PyObject *axes_2d_obj_kwargs = NULL; - if (NPY_UNLIKELY(axes_1d_obj_kwargs == NULL)) { - axes_1d_obj_kwargs = Py_BuildValue( - "{s, [(i), (i, i), (i)]}", "axes", -1, -2, -1, -1); - if (axes_1d_obj_kwargs == NULL) { - return NULL; - } - } - if (NPY_UNLIKELY(axes_2d_obj_kwargs == NULL)) { - axes_2d_obj_kwargs = Py_BuildValue( - "{s, [(i, i), (i, i), (i, i)]}", "axes", -2, -1, -2, -1, -2, -1); - if (axes_2d_obj_kwargs == NULL) { - return NULL; - } - } - - PyObject *args = PyTuple_Pack(3, self, other, self); - if (args == NULL) { - return NULL; - } - PyObject *kwargs; if (PyArray_NDIM(self) == 1) { - kwargs = axes_1d_obj_kwargs; + kwargs = npy_static_pydata.axes_1d_obj_kwargs; } else { - kwargs = axes_2d_obj_kwargs; + kwargs = npy_static_pydata.axes_2d_obj_kwargs; } PyObject *res = PyObject_Call(n_ops.matmul, args, kwargs); Py_DECREF(args); @@ -322,7 +318,7 @@ array_inplace_matrix_multiply(PyArrayObject *self, PyObject *other) * AxisError should indicate that the axes argument didn't work out * which should mean the second operand not being 2 dimensional. */ - if (PyErr_ExceptionMatches(AxisError_cls)) { + if (PyErr_ExceptionMatches(npy_static_pydata.AxisError)) { PyErr_SetString(PyExc_ValueError, "inplace matrix multiplication requires the first operand to " "have at least one and the second at least two dimensions."); diff --git a/numpy/_core/src/multiarray/scalartypes.c.src b/numpy/_core/src/multiarray/scalartypes.c.src index a5185cba60aa..a0517c247215 100644 --- a/numpy/_core/src/multiarray/scalartypes.c.src +++ b/numpy/_core/src/multiarray/scalartypes.c.src @@ -33,14 +33,13 @@ #include "dragon4.h" #include "npy_longdouble.h" #include "npy_buffer.h" +#include "npy_static_data.h" +#include "multiarraymodule.h" #include #include "binop_override.h" -/* determines if legacy mode is enabled, global set in multiarraymodule.c */ -extern int npy_legacy_print_mode; - /* * used for allocating a single scalar, so use the default numpy * memory allocators instead of the (maybe) user overrides @@ -337,7 +336,7 @@ genint_type_repr(PyObject *self) if (value_string == NULL) { return NULL; } - if (npy_legacy_print_mode <= 125) { + if (npy_thread_unsafe_state.legacy_print_mode <= 125) { return value_string; } @@ -374,7 +373,7 @@ genbool_type_str(PyObject *self) static PyObject * genbool_type_repr(PyObject *self) { - if (npy_legacy_print_mode <= 125) { + if (npy_thread_unsafe_state.legacy_print_mode <= 125) { return genbool_type_str(self); } return PyUnicode_FromString( @@ -500,7 +499,7 @@ stringtype_@form@(PyObject *self) if (ret == NULL) { return NULL; } - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { Py_SETREF(ret, PyUnicode_FromFormat("np.bytes_(%S)", ret)); } #endif /* IS_repr */ @@ -547,7 +546,7 @@ unicodetype_@form@(PyObject *self) if (ret == NULL) { return NULL; } - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { Py_SETREF(ret, PyUnicode_FromFormat("np.str_(%S)", ret)); } #endif /* IS_repr */ @@ -609,14 +608,15 @@ _void_to_hex(const char* argbuf, const Py_ssize_t arglen, static PyObject * _void_scalar_to_string(PyObject *obj, int repr) { - static PyObject *tostring_func = NULL; npy_cache_import("numpy._core.arrayprint", - "_void_scalar_to_string", &tostring_func); - if (tostring_func == NULL) { + "_void_scalar_to_string", + &npy_thread_unsafe_state._void_scalar_to_string); + if (npy_thread_unsafe_state._void_scalar_to_string == NULL) { return NULL; } PyObject *is_repr = repr ? Py_True : Py_False; - return PyObject_CallFunctionObjArgs(tostring_func, obj, is_repr, NULL); + return PyObject_CallFunctionObjArgs( + npy_thread_unsafe_state._void_scalar_to_string, obj, is_repr, NULL); } static PyObject * @@ -627,7 +627,7 @@ voidtype_repr(PyObject *self) /* Python helper checks for the legacy mode printing */ return _void_scalar_to_string(self, 1); } - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { return _void_to_hex(s->obval, s->descr->elsize, "np.void(b'", "\\x", "')"); } else { @@ -679,7 +679,7 @@ datetimetype_repr(PyObject *self) */ if ((scal->obmeta.num == 1 && scal->obmeta.base != NPY_FR_h) || scal->obmeta.base == NPY_FR_GENERIC) { - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { ret = PyUnicode_FromFormat("np.datetime64('%s')", iso); } else { @@ -691,7 +691,7 @@ datetimetype_repr(PyObject *self) if (meta == NULL) { return NULL; } - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { ret = PyUnicode_FromFormat("np.datetime64('%s','%S')", iso, meta); } else { @@ -735,7 +735,7 @@ timedeltatype_repr(PyObject *self) /* The metadata unit */ if (scal->obmeta.base == NPY_FR_GENERIC) { - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { ret = PyUnicode_FromFormat("np.timedelta64(%S)", val); } else { @@ -748,7 +748,7 @@ timedeltatype_repr(PyObject *self) Py_DECREF(val); return NULL; } - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { ret = PyUnicode_FromFormat("np.timedelta64(%S,'%S')", val, meta); } else { @@ -1050,7 +1050,7 @@ static PyObject * npy_bool sign) { - if (npy_legacy_print_mode <= 113) { + if (npy_thread_unsafe_state.legacy_print_mode <= 113) { return legacy_@name@_format@kind@(val); } @@ -1081,7 +1081,7 @@ static PyObject * if (string == NULL) { return NULL; } - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { Py_SETREF(string, PyUnicode_FromFormat("@repr_format@", string)); } #endif /* IS_repr */ @@ -1096,7 +1096,7 @@ c@name@type_@kind@(PyObject *self) npy_c@name@ val = PyArrayScalar_VAL(self, C@Name@); TrimMode trim = TrimMode_DptZeros; - if (npy_legacy_print_mode <= 113) { + if (npy_thread_unsafe_state.legacy_print_mode <= 113) { return legacy_c@name@_format@kind@(val); } @@ -1109,7 +1109,7 @@ c@name@type_@kind@(PyObject *self) #ifdef IS_str ret = PyUnicode_FromFormat("%Sj", istr); #else /* IS_repr */ - if (npy_legacy_print_mode <= 125) { + if (npy_thread_unsafe_state.legacy_print_mode <= 125) { ret = PyUnicode_FromFormat("%Sj", istr); } else { @@ -1157,7 +1157,7 @@ c@name@type_@kind@(PyObject *self) #ifdef IS_str string = PyUnicode_FromFormat("(%S%Sj)", rstr, istr); #else /* IS_repr */ - if (npy_legacy_print_mode > 125) { + if (npy_thread_unsafe_state.legacy_print_mode > 125) { string = PyUnicode_FromFormat("@crepr_format@", rstr, istr); } else { @@ -1182,7 +1182,7 @@ halftype_@kind@(PyObject *self) float floatval = npy_half_to_float(val); float absval; - if (npy_legacy_print_mode <= 113) { + if (npy_thread_unsafe_state.legacy_print_mode <= 113) { return legacy_float_format@kind@(floatval); } @@ -1198,7 +1198,7 @@ halftype_@kind@(PyObject *self) #ifdef IS_str return string; #else - if (string == NULL || npy_legacy_print_mode <= 125) { + if (string == NULL || npy_thread_unsafe_state.legacy_print_mode <= 125) { return string; } PyObject *res = PyUnicode_FromFormat("np.float16(%S)", string); @@ -3036,13 +3036,7 @@ object_arrtype_alloc(PyTypeObject *type, Py_ssize_t items) * Object scalars should not actually exist, if they exist we should * consider it to be a bug. */ - static PyObject *visibleDeprecationWarning = NULL; - npy_cache_import("numpy", "VisibleDeprecationWarning", - &visibleDeprecationWarning); - if (visibleDeprecationWarning == NULL) { - return NULL; - } - if (PyErr_WarnEx(visibleDeprecationWarning, + if (PyErr_WarnEx(npy_static_pydata.VisibleDeprecationWarning, "Creating a NumPy object scalar. NumPy object scalars should " "never be created. If you see this message please inform the " "NumPy developers. Since this message should never be shown " diff --git a/numpy/_core/src/multiarray/shape.c b/numpy/_core/src/multiarray/shape.c index e766a61ed12f..c33272de4eb5 100644 --- a/numpy/_core/src/multiarray/shape.c +++ b/numpy/_core/src/multiarray/shape.c @@ -19,7 +19,7 @@ #include "shape.h" -#include "multiarraymodule.h" /* for interned strings */ +#include "npy_static_data.h" /* for interned strings */ #include "templ_common.h" /* for npy_mul_sizes_with_overflow */ #include "common.h" /* for convert_shape_to_string */ #include "alloc.h" @@ -668,10 +668,10 @@ PyArray_SwapAxes(PyArrayObject *ap, int a1, int a2) int n = PyArray_NDIM(ap); int i; - if (check_and_adjust_axis_msg(&a1, n, npy_ma_str_axis1) < 0) { + if (check_and_adjust_axis_msg(&a1, n, npy_interned_str.axis1) < 0) { return NULL; } - if (check_and_adjust_axis_msg(&a2, n, npy_ma_str_axis2) < 0) { + if (check_and_adjust_axis_msg(&a2, n, npy_interned_str.axis2) < 0) { return NULL; } diff --git a/numpy/_core/src/multiarray/strfuncs.c b/numpy/_core/src/multiarray/strfuncs.c index 596a32e64aaf..759c730c7cfa 100644 --- a/numpy/_core/src/multiarray/strfuncs.c +++ b/numpy/_core/src/multiarray/strfuncs.c @@ -7,6 +7,7 @@ #include "numpy/arrayobject.h" #include "npy_pycompat.h" #include "npy_import.h" +#include "multiarraymodule.h" #include "strfuncs.h" static void @@ -33,36 +34,38 @@ PyArray_SetStringFunction(PyObject *op, int repr) NPY_NO_EXPORT PyObject * array_repr(PyArrayObject *self) { - static PyObject *repr = NULL; /* * We need to do a delayed import here as initialization on module load * leads to circular import problems. */ - npy_cache_import("numpy._core.arrayprint", "_default_array_repr", &repr); - if (repr == NULL) { + npy_cache_import("numpy._core.arrayprint", "_default_array_repr", + &npy_thread_unsafe_state._default_array_repr); + if (npy_thread_unsafe_state._default_array_repr == NULL) { npy_PyErr_SetStringChained(PyExc_RuntimeError, "Unable to configure default ndarray.__repr__"); return NULL; } - return PyObject_CallFunctionObjArgs(repr, self, NULL); + return PyObject_CallFunctionObjArgs( + npy_thread_unsafe_state._default_array_repr, self, NULL); } NPY_NO_EXPORT PyObject * array_str(PyArrayObject *self) { - static PyObject *str = NULL; /* * We need to do a delayed import here as initialization on module load leads * to circular import problems. */ - npy_cache_import("numpy._core.arrayprint", "_default_array_str", &str); - if (str == NULL) { + npy_cache_import("numpy._core.arrayprint", "_default_array_str", + &npy_thread_unsafe_state._default_array_str); + if (npy_thread_unsafe_state._default_array_str == NULL) { npy_PyErr_SetStringChained(PyExc_RuntimeError, "Unable to configure default ndarray.__str__"); return NULL; } - return PyObject_CallFunctionObjArgs(str, self, NULL); + return PyObject_CallFunctionObjArgs( + npy_thread_unsafe_state._default_array_str, self, NULL); } diff --git a/numpy/_core/src/multiarray/stringdtype/dtype.c b/numpy/_core/src/multiarray/stringdtype/dtype.c index bcaeaa5be5f8..038fa8159171 100644 --- a/numpy/_core/src/multiarray/stringdtype/dtype.c +++ b/numpy/_core/src/multiarray/stringdtype/dtype.c @@ -17,6 +17,7 @@ #include "gil_utils.h" #include "conversion_utils.h" #include "npy_import.h" +#include "multiarraymodule.h" /* * Internal helper to create new instances @@ -707,8 +708,6 @@ stringdtype_repr(PyArray_StringDTypeObject *self) return ret; } -static PyObject *_convert_to_stringdtype_kwargs = NULL; - // implementation of __reduce__ magic method to reconstruct a StringDType // object from the serialized data in the pickle. Uses the python // _convert_to_stringdtype_kwargs for convenience because this isn't @@ -717,18 +716,21 @@ static PyObject * stringdtype__reduce__(PyArray_StringDTypeObject *self, PyObject *NPY_UNUSED(args)) { npy_cache_import("numpy._core._internal", "_convert_to_stringdtype_kwargs", - &_convert_to_stringdtype_kwargs); + &npy_thread_unsafe_state._convert_to_stringdtype_kwargs); - if (_convert_to_stringdtype_kwargs == NULL) { + if (npy_thread_unsafe_state._convert_to_stringdtype_kwargs == NULL) { return NULL; } if (self->na_object != NULL) { - return Py_BuildValue("O(iO)", _convert_to_stringdtype_kwargs, - self->coerce, self->na_object); + return Py_BuildValue( + "O(iO)", npy_thread_unsafe_state._convert_to_stringdtype_kwargs, + self->coerce, self->na_object); } - return Py_BuildValue("O(i)", _convert_to_stringdtype_kwargs, self->coerce); + return Py_BuildValue( + "O(i)", npy_thread_unsafe_state._convert_to_stringdtype_kwargs, + self->coerce); } static PyMethodDef PyArray_StringDType_methods[] = { diff --git a/numpy/_core/src/umath/_scaled_float_dtype.c b/numpy/_core/src/umath/_scaled_float_dtype.c index 02278806751f..fbdbbb8d2375 100644 --- a/numpy/_core/src/umath/_scaled_float_dtype.c +++ b/numpy/_core/src/umath/_scaled_float_dtype.c @@ -25,6 +25,7 @@ #include "dtypemeta.h" #include "dispatching.h" #include "gil_utils.h" +#include "multiarraymodule.h" typedef struct { PyArray_Descr base; @@ -867,10 +868,7 @@ sfloat_init_ufuncs(void) { NPY_NO_EXPORT PyObject * get_sfloat_dtype(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args)) { - /* Allow calling the function multiple times. */ - static npy_bool initialized = NPY_FALSE; - - if (initialized) { + if (npy_thread_unsafe_state.get_sfloat_dtype_initialized) { Py_INCREF(&PyArray_SFloatDType); return (PyObject *)&PyArray_SFloatDType; } @@ -899,6 +897,6 @@ get_sfloat_dtype(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args)) return NULL; } - initialized = NPY_TRUE; + npy_thread_unsafe_state.get_sfloat_dtype_initialized = NPY_TRUE; return (PyObject *)&PyArray_SFloatDType; } diff --git a/numpy/_core/src/umath/dispatching.c b/numpy/_core/src/umath/dispatching.c index 673d4fd68b5c..a3b9e7584434 100644 --- a/numpy/_core/src/umath/dispatching.c +++ b/numpy/_core/src/umath/dispatching.c @@ -1062,7 +1062,7 @@ promote_and_get_ufuncimpl(PyUFuncObject *ufunc, * then we chain it, because DTypePromotionError effectively means that there * is no loop available. (We failed finding a loop by using promotion.) */ - else if (PyErr_ExceptionMatches(npy_DTypePromotionError)) { + else if (PyErr_ExceptionMatches(npy_static_pydata.DTypePromotionError)) { PyObject *err_type = NULL, *err_value = NULL, *err_traceback = NULL; PyErr_Fetch(&err_type, &err_value, &err_traceback); raise_no_loop_found_error(ufunc, (PyObject **)op_dtypes); diff --git a/numpy/_core/src/umath/extobj.c b/numpy/_core/src/umath/extobj.c index d32feaaa31da..755d8665b11d 100644 --- a/numpy/_core/src/umath/extobj.c +++ b/numpy/_core/src/umath/extobj.c @@ -14,18 +14,9 @@ #include "extobj.h" #include "numpy/ufuncobject.h" -#include "ufunc_object.h" /* for npy_um_str_pyvals_name */ #include "common.h" -/* - * The global ContextVar to store the extobject. It is exposed to Python - * as `_extobj_contextvar`. - */ -static PyObject *default_extobj_capsule = NULL; -NPY_NO_EXPORT PyObject *npy_extobj_contextvar = NULL; - - #define UFUNC_ERR_IGNORE 0 #define UFUNC_ERR_WARN 1 #define UFUNC_ERR_RAISE 2 @@ -44,11 +35,6 @@ NPY_NO_EXPORT PyObject *npy_extobj_contextvar = NULL; #define UFUNC_SHIFT_UNDERFLOW 6 #define UFUNC_SHIFT_INVALID 9 -/* The python strings for the above error modes defined in extobj.h */ -const char *errmode_cstrings[] = { - "ignore", "warn", "raise", "call", "print", "log"}; -static PyObject *errmode_strings[6] = {NULL}; - /* Default user error mode (underflows are ignored, others warn) */ #define UFUNC_ERR_DEFAULT \ (UFUNC_ERR_WARN << UFUNC_SHIFT_DIVIDEBYZERO) + \ @@ -131,7 +117,8 @@ fetch_curr_extobj_state(npy_extobj *extobj) { PyObject *capsule; if (PyContextVar_Get( - npy_extobj_contextvar, default_extobj_capsule, &capsule) < 0) { + npy_static_pydata.npy_extobj_contextvar, + npy_static_pydata.default_extobj_capsule, &capsule) < 0) { return -1; } npy_extobj *obj = PyCapsule_GetPointer(capsule, "numpy.ufunc.extobj"); @@ -153,26 +140,15 @@ fetch_curr_extobj_state(npy_extobj *extobj) NPY_NO_EXPORT int init_extobj(void) { - /* - * First initialize the string constants we need to parse `errstate()` - * inputs. - */ - for (int i = 0; i <= UFUNC_ERR_LOG; i++) { - errmode_strings[i] = PyUnicode_InternFromString(errmode_cstrings[i]); - if (errmode_strings[i] == NULL) { - return -1; - } - } - - default_extobj_capsule = make_extobj_capsule( + npy_static_pydata.default_extobj_capsule = make_extobj_capsule( NPY_BUFSIZE, UFUNC_ERR_DEFAULT, Py_None); - if (default_extobj_capsule == NULL) { + if (npy_static_pydata.default_extobj_capsule == NULL) { return -1; } - npy_extobj_contextvar = PyContextVar_New( - "numpy.ufunc.extobj", default_extobj_capsule); - if (npy_extobj_contextvar == NULL) { - Py_CLEAR(default_extobj_capsule); + npy_static_pydata.npy_extobj_contextvar = PyContextVar_New( + "numpy.ufunc.extobj", npy_static_pydata.default_extobj_capsule); + if (npy_static_pydata.npy_extobj_contextvar == NULL) { + Py_CLEAR(npy_static_pydata.default_extobj_capsule); return -1; } return 0; @@ -191,7 +167,8 @@ errmodeconverter(PyObject *obj, int *mode) } int i = 0; for (; i <= UFUNC_ERR_LOG; i++) { - int eq = PyObject_RichCompareBool(obj, errmode_strings[i], Py_EQ); + int eq = PyObject_RichCompareBool( + obj, npy_interned_str.errmode_strings[i], Py_EQ); if (eq == -1) { return 0; } @@ -212,7 +189,7 @@ errmodeconverter(PyObject *obj, int *mode) /* * This function is currently exposed as `umath._seterrobj()`, it is private * and returns a capsule representing the errstate. This capsule is then - * assigned to the `npy_extobj_contextvar` in Python. + * assigned to the `_extobj_contextvar` in Python. */ NPY_NO_EXPORT PyObject * extobj_make_extobj(PyObject *NPY_UNUSED(mod), @@ -338,19 +315,23 @@ extobj_get_extobj_dict(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(noarg)) } /* Set all error modes: */ mode = (extobj.errmask & UFUNC_MASK_DIVIDEBYZERO) >> UFUNC_SHIFT_DIVIDEBYZERO; - if (PyDict_SetItemString(result, "divide", errmode_strings[mode]) < 0) { + if (PyDict_SetItemString(result, "divide", + npy_interned_str.errmode_strings[mode]) < 0) { goto fail; } mode = (extobj.errmask & UFUNC_MASK_OVERFLOW) >> UFUNC_SHIFT_OVERFLOW; - if (PyDict_SetItemString(result, "over", errmode_strings[mode]) < 0) { + if (PyDict_SetItemString(result, "over", + npy_interned_str.errmode_strings[mode]) < 0) { goto fail; } mode = (extobj.errmask & UFUNC_MASK_UNDERFLOW) >> UFUNC_SHIFT_UNDERFLOW; - if (PyDict_SetItemString(result, "under", errmode_strings[mode]) < 0) { + if (PyDict_SetItemString(result, "under", + npy_interned_str.errmode_strings[mode]) < 0) { goto fail; } mode = (extobj.errmask & UFUNC_MASK_INVALID) >> UFUNC_SHIFT_INVALID; - if (PyDict_SetItemString(result, "invalid", errmode_strings[mode]) < 0) { + if (PyDict_SetItemString(result, "invalid", + npy_interned_str.errmode_strings[mode]) < 0) { goto fail; } diff --git a/numpy/_core/src/umath/extobj.h b/numpy/_core/src/umath/extobj.h index 0cd5afd76218..9176af6a3539 100644 --- a/numpy/_core/src/umath/extobj.h +++ b/numpy/_core/src/umath/extobj.h @@ -4,9 +4,6 @@ #include /* for NPY_NO_EXPORT */ -/* For the private exposure of the extobject contextvar to Python */ -extern NPY_NO_EXPORT PyObject *npy_extobj_contextvar; - /* * Represent the current ufunc error (and buffer) state. we are using a * capsule for now to store this, but it could make sense to refactor it into diff --git a/numpy/_core/src/umath/funcs.inc.src b/numpy/_core/src/umath/funcs.inc.src index df81c835034a..3825bd869468 100644 --- a/numpy/_core/src/umath/funcs.inc.src +++ b/numpy/_core/src/umath/funcs.inc.src @@ -9,7 +9,8 @@ #define NPY_NO_DEPRECATED_API NPY_API_VERSION #include "npy_import.h" - +#include "npy_static_data.h" +#include "multiarraymodule.h" /* ***************************************************************************** @@ -157,35 +158,20 @@ npy_ObjectLogicalNot(PyObject *i1) static PyObject * npy_ObjectFloor(PyObject *obj) { - static PyObject *math_floor_func = NULL; - - npy_cache_import("math", "floor", &math_floor_func); - if (math_floor_func == NULL) { - return NULL; - } - return PyObject_CallFunction(math_floor_func, "O", obj); + return PyObject_CallFunction(npy_static_pydata.math_floor_func, + "O", obj); } static PyObject * npy_ObjectCeil(PyObject *obj) { - static PyObject *math_ceil_func = NULL; - - npy_cache_import("math", "ceil", &math_ceil_func); - if (math_ceil_func == NULL) { - return NULL; - } - return PyObject_CallFunction(math_ceil_func, "O", obj); + return PyObject_CallFunction(npy_static_pydata.math_ceil_func, + "O", obj); } static PyObject * npy_ObjectTrunc(PyObject *obj) { - static PyObject *math_trunc_func = NULL; - - npy_cache_import("math", "trunc", &math_trunc_func); - if (math_trunc_func == NULL) { - return NULL; - } - return PyObject_CallFunction(math_trunc_func, "O", obj); + return PyObject_CallFunction(npy_static_pydata.math_trunc_func, + "O", obj); } static PyObject * @@ -195,13 +181,8 @@ npy_ObjectGCD(PyObject *i1, PyObject *i2) /* use math.gcd if valid on the provided types */ { - static PyObject *math_gcd_func = NULL; - - npy_cache_import("math", "gcd", &math_gcd_func); - if (math_gcd_func == NULL) { - return NULL; - } - gcd = PyObject_CallFunction(math_gcd_func, "OO", i1, i2); + gcd = PyObject_CallFunction(npy_static_pydata.math_gcd_func, + "OO", i1, i2); if (gcd != NULL) { return gcd; } @@ -211,13 +192,13 @@ npy_ObjectGCD(PyObject *i1, PyObject *i2) /* otherwise, use our internal one, written in python */ { - static PyObject *internal_gcd_func = NULL; - - npy_cache_import("numpy._core._internal", "_gcd", &internal_gcd_func); - if (internal_gcd_func == NULL) { + npy_cache_import("numpy._core._internal", "_gcd", + &npy_thread_unsafe_state.internal_gcd_func); + if (npy_thread_unsafe_state.internal_gcd_func == NULL) { return NULL; } - gcd = PyObject_CallFunction(internal_gcd_func, "OO", i1, i2); + gcd = PyObject_CallFunction(npy_thread_unsafe_state.internal_gcd_func, + "OO", i1, i2); if (gcd == NULL) { return NULL; } diff --git a/numpy/_core/src/umath/override.c b/numpy/_core/src/umath/override.c index d10b86be7b57..55cca0857229 100644 --- a/numpy/_core/src/umath/override.c +++ b/numpy/_core/src/umath/override.c @@ -4,6 +4,8 @@ #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" #include "npy_import.h" +#include "npy_static_data.h" +#include "multiarraymodule.h" #include "npy_pycompat.h" #include "override.h" #include "ufunc_override.h" @@ -110,29 +112,22 @@ initialize_normal_kwds(PyObject *out_args, } } } - static PyObject *out_str = NULL; - if (out_str == NULL) { - out_str = PyUnicode_InternFromString("out"); - if (out_str == NULL) { - return -1; - } - } if (out_args != NULL) { /* Replace `out` argument with the normalized version */ - int res = PyDict_SetItem(normal_kwds, out_str, out_args); + int res = PyDict_SetItem(normal_kwds, npy_interned_str.out, out_args); if (res < 0) { return -1; } } else { /* Ensure that `out` is not present. */ - int res = PyDict_Contains(normal_kwds, out_str); + int res = PyDict_Contains(normal_kwds, npy_interned_str.out); if (res < 0) { return -1; } if (res) { - return PyDict_DelItem(normal_kwds, out_str); + return PyDict_DelItem(normal_kwds, npy_interned_str.out); } } return 0; @@ -182,10 +177,8 @@ copy_positional_args_to_kwargs(const char **keywords, * This is only relevant for reduce, which is the only one with * 5 keyword arguments. */ - static PyObject *NoValue = NULL; assert(strcmp(keywords[i], "initial") == 0); - npy_cache_import("numpy", "_NoValue", &NoValue); - if (args[i] == NoValue) { + if (args[i] == npy_static_pydata._NoValue) { continue; } } @@ -371,23 +364,22 @@ PyUFunc_CheckOverride(PyUFuncObject *ufunc, char *method, /* Check if there is a method left to call */ if (!override_obj) { /* No acceptable override found. */ - static PyObject *errmsg_formatter = NULL; PyObject *errmsg; - npy_cache_import("numpy._core._internal", - "array_ufunc_errmsg_formatter", - &errmsg_formatter); - - if (errmsg_formatter != NULL) { - /* All tuple items must be set before use */ - Py_INCREF(Py_None); - PyTuple_SET_ITEM(override_args, 0, Py_None); - errmsg = PyObject_Call(errmsg_formatter, override_args, - normal_kwds); - if (errmsg != NULL) { - PyErr_SetObject(PyExc_TypeError, errmsg); - Py_DECREF(errmsg); - } + /* All tuple items must be set before use */ + Py_INCREF(Py_None); + PyTuple_SET_ITEM(override_args, 0, Py_None); + npy_cache_import( + "numpy._core._internal", + "array_ufunc_errmsg_formatter", + &npy_thread_unsafe_state.array_ufunc_errmsg_formatter); + assert(npy_thread_unsafe_state.array_ufunc_errmsg_formatter != NULL); + errmsg = PyObject_Call( + npy_thread_unsafe_state.array_ufunc_errmsg_formatter, + override_args, normal_kwds); + if (errmsg != NULL) { + PyErr_SetObject(PyExc_TypeError, errmsg); + Py_DECREF(errmsg); } Py_DECREF(override_args); goto fail; diff --git a/numpy/_core/src/umath/scalarmath.c.src b/numpy/_core/src/umath/scalarmath.c.src index cc8f82aca11b..b6bf6938d914 100644 --- a/numpy/_core/src/umath/scalarmath.c.src +++ b/numpy/_core/src/umath/scalarmath.c.src @@ -1788,12 +1788,7 @@ static int static int emit_complexwarning(void) { - static PyObject *cls = NULL; - npy_cache_import("numpy.exceptions", "ComplexWarning", &cls); - if (cls == NULL) { - return -1; - } - return PyErr_WarnEx(cls, + return PyErr_WarnEx(npy_static_pydata.ComplexWarning, "Casting complex values to real discards the imaginary part", 1); } diff --git a/numpy/_core/src/umath/ufunc_object.c b/numpy/_core/src/umath/ufunc_object.c index efad2a7be2b4..f6ad33f68016 100644 --- a/numpy/_core/src/umath/ufunc_object.c +++ b/numpy/_core/src/umath/ufunc_object.c @@ -62,6 +62,8 @@ #include "legacy_array_method.h" #include "abstractdtypes.h" #include "mapping.h" +#include "npy_static_data.h" +#include "multiarraymodule.h" /* TODO: Only for `NpyIter_GetTransferFlags` until it is public */ #define NPY_ITERATOR_IMPLEMENTATION_CODE @@ -698,18 +700,9 @@ convert_ufunc_arguments(PyUFuncObject *ufunc, * TODO: Just like the general dual NEP 50/legacy promotion * support this is meant as a temporary hack for NumPy 1.25. */ - static PyArrayObject *zero_arr = NULL; - if (NPY_UNLIKELY(zero_arr == NULL)) { - zero_arr = (PyArrayObject *)PyArray_ZEROS( - 0, NULL, NPY_LONG, NPY_FALSE); - if (zero_arr == NULL) { - goto fail; - } - ((PyArrayObject_fields *)zero_arr)->flags |= ( - NPY_ARRAY_WAS_PYTHON_INT|NPY_ARRAY_WAS_INT_AND_REPLACED); - } - Py_INCREF(zero_arr); - Py_SETREF(out_op[i], zero_arr); + Py_INCREF(npy_static_pydata.zero_pyint_like_arr); + Py_SETREF(out_op[i], + (PyArrayObject *)npy_static_pydata.zero_pyint_like_arr); } *promoting_pyscalars = NPY_TRUE; } @@ -1341,8 +1334,6 @@ _check_keepdims_support(PyUFuncObject *ufunc) { static int _parse_axes_arg(PyUFuncObject *ufunc, int op_core_num_dims[], PyObject *axes, PyArrayObject **op, int broadcast_ndim, int **remap_axis) { - static PyObject *AxisError_cls = NULL; - int nin = ufunc->nin; int nop = ufunc->nargs; int iop, list_size; @@ -1388,12 +1379,7 @@ _parse_axes_arg(PyUFuncObject *ufunc, int op_core_num_dims[], PyObject *axes, if (PyTuple_Check(op_axes_tuple)) { if (PyTuple_Size(op_axes_tuple) != op_ncore) { /* must have been a tuple with too many entries. */ - npy_cache_import( - "numpy.exceptions", "AxisError", &AxisError_cls); - if (AxisError_cls == NULL) { - return -1; - } - PyErr_Format(AxisError_cls, + PyErr_Format(npy_static_pydata.AxisError, "%s: operand %d has %d core dimensions, " "but %zd dimensions are specified by axes tuple.", ufunc_get_name_cstr(ufunc), iop, op_ncore, @@ -1417,11 +1403,7 @@ _parse_axes_arg(PyUFuncObject *ufunc, int op_core_num_dims[], PyObject *axes, return -1; } /* If it is a single integer, inform user that more are needed */ - npy_cache_import("numpy.exceptions", "AxisError", &AxisError_cls); - if (AxisError_cls == NULL) { - return -1; - } - PyErr_Format(AxisError_cls, + PyErr_Format(npy_static_pydata.AxisError, "%s: operand %d has %d core dimensions, " "but the axes item is a single integer.", ufunc_get_name_cstr(ufunc), iop, op_ncore); @@ -5254,8 +5236,7 @@ prepare_input_arguments_for_outer(PyObject *args, PyUFuncObject *ufunc) { PyArrayObject *ap1 = NULL; PyObject *tmp; - static PyObject *_numpy_matrix; - npy_cache_import("numpy", "matrix", &_numpy_matrix); + npy_cache_import("numpy", "matrix", &npy_thread_unsafe_state.numpy_matrix); const char *matrix_deprecation_msg = ( "%s.outer() was passed a numpy matrix as %s argument. " @@ -5266,7 +5247,7 @@ prepare_input_arguments_for_outer(PyObject *args, PyUFuncObject *ufunc) tmp = PyTuple_GET_ITEM(args, 0); - if (PyObject_IsInstance(tmp, _numpy_matrix)) { + if (PyObject_IsInstance(tmp, npy_thread_unsafe_state.numpy_matrix)) { /* DEPRECATED 2020-05-13, NumPy 1.20 */ if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, matrix_deprecation_msg, ufunc->name, "first") < 0) { @@ -5283,7 +5264,7 @@ prepare_input_arguments_for_outer(PyObject *args, PyUFuncObject *ufunc) PyArrayObject *ap2 = NULL; tmp = PyTuple_GET_ITEM(args, 1); - if (PyObject_IsInstance(tmp, _numpy_matrix)) { + if (PyObject_IsInstance(tmp, npy_thread_unsafe_state.numpy_matrix)) { /* DEPRECATED 2020-05-13, NumPy 1.20 */ if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, matrix_deprecation_msg, ufunc->name, "second") < 0) { @@ -6432,15 +6413,14 @@ _typecharfromnum(int num) { static PyObject * ufunc_get_doc(PyUFuncObject *ufunc, void *NPY_UNUSED(ignored)) { - static PyObject *_sig_formatter; PyObject *doc; npy_cache_import( "numpy._core._internal", "_ufunc_doc_signature_formatter", - &_sig_formatter); + &npy_thread_unsafe_state._ufunc_doc_signature_formatter); - if (_sig_formatter == NULL) { + if (npy_thread_unsafe_state._ufunc_doc_signature_formatter == NULL) { return NULL; } @@ -6449,7 +6429,7 @@ ufunc_get_doc(PyUFuncObject *ufunc, void *NPY_UNUSED(ignored)) * introspection on name and nin + nout to automate the first part * of it the doc string shouldn't need the calling convention */ - doc = PyObject_CallFunctionObjArgs(_sig_formatter, + doc = PyObject_CallFunctionObjArgs(npy_thread_unsafe_state._ufunc_doc_signature_formatter, (PyObject *)ufunc, NULL); if (doc == NULL) { return NULL; @@ -6543,10 +6523,6 @@ ufunc_get_signature(PyUFuncObject *ufunc, void *NPY_UNUSED(ignored)) #undef _typecharfromnum -/* - * Docstring is now set from python - * static char *Ufunctype__doc__ = NULL; - */ static PyGetSetDef ufunc_getset[] = { {"__doc__", (getter)ufunc_get_doc, diff --git a/numpy/_core/src/umath/ufunc_object.h b/numpy/_core/src/umath/ufunc_object.h index 645023f66aa5..f8e522374394 100644 --- a/numpy/_core/src/umath/ufunc_object.h +++ b/numpy/_core/src/umath/ufunc_object.h @@ -10,9 +10,4 @@ ufunc_get_name_cstr(PyUFuncObject *ufunc); NPY_NO_EXPORT PyObject * PyUFunc_GetDefaultIdentity(PyUFuncObject *ufunc, npy_bool *reorderable); -/* strings from umathmodule.c that are interned on umath import */ -NPY_VISIBILITY_HIDDEN extern PyObject *npy_um_str_array_ufunc; -NPY_VISIBILITY_HIDDEN extern PyObject *npy_um_str_array_wrap; -NPY_VISIBILITY_HIDDEN extern PyObject *npy_um_str_pyvals_name; - #endif diff --git a/numpy/_core/src/umath/ufunc_type_resolution.c b/numpy/_core/src/umath/ufunc_type_resolution.c index f6f231223f63..b523bd0b4d83 100644 --- a/numpy/_core/src/umath/ufunc_type_resolution.c +++ b/numpy/_core/src/umath/ufunc_type_resolution.c @@ -78,16 +78,8 @@ npy_casting_to_py_object(NPY_CASTING casting) */ static int raise_binary_type_reso_error(PyUFuncObject *ufunc, PyArrayObject **operands) { - static PyObject *exc_type = NULL; PyObject *exc_value; - npy_cache_import( - "numpy._core._exceptions", "_UFuncBinaryResolutionError", - &exc_type); - if (exc_type == NULL) { - return -1; - } - /* produce an error object */ exc_value = Py_BuildValue( "O(OO)", ufunc, @@ -97,7 +89,8 @@ raise_binary_type_reso_error(PyUFuncObject *ufunc, PyArrayObject **operands) { if (exc_value == NULL){ return -1; } - PyErr_SetObject(exc_type, exc_value); + PyErr_SetObject( + npy_static_pydata._UFuncBinaryResolutionError, exc_value); Py_DECREF(exc_value); return -1; @@ -110,15 +103,6 @@ NPY_NO_EXPORT int raise_no_loop_found_error( PyUFuncObject *ufunc, PyObject **dtypes) { - static PyObject *exc_type = NULL; - - npy_cache_import( - "numpy._core._exceptions", "_UFuncNoLoopError", - &exc_type); - if (exc_type == NULL) { - return -1; - } - PyObject *dtypes_tup = PyArray_TupleFromItems(ufunc->nargs, dtypes, 1); if (dtypes_tup == NULL) { return -1; @@ -129,7 +113,7 @@ raise_no_loop_found_error( if (exc_value == NULL) { return -1; } - PyErr_SetObject(exc_type, exc_value); + PyErr_SetObject(npy_static_pydata._UFuncNoLoopError, exc_value); Py_DECREF(exc_value); return -1; @@ -181,15 +165,8 @@ raise_input_casting_error( PyArray_Descr *to, npy_intp i) { - static PyObject *exc_type = NULL; - npy_cache_import( - "numpy._core._exceptions", "_UFuncInputCastingError", - &exc_type); - if (exc_type == NULL) { - return -1; - } - - return raise_casting_error(exc_type, ufunc, casting, from, to, i); + return raise_casting_error(npy_static_pydata._UFuncInputCastingError, + ufunc, casting, from, to, i); } @@ -204,15 +181,8 @@ raise_output_casting_error( PyArray_Descr *to, npy_intp i) { - static PyObject *exc_type = NULL; - npy_cache_import( - "numpy._core._exceptions", "_UFuncOutputCastingError", - &exc_type); - if (exc_type == NULL) { - return -1; - } - - return raise_casting_error(exc_type, ufunc, casting, from, to, i); + return raise_casting_error(npy_static_pydata._UFuncOutputCastingError, + ufunc, casting, from, to, i); } @@ -1441,22 +1411,6 @@ PyUFunc_TrueDivisionTypeResolver(PyUFuncObject *ufunc, PyArray_Descr **out_dtypes) { int type_num1, type_num2; - static PyObject *default_type_tup = NULL; - - /* Set default type for integer inputs to NPY_DOUBLE */ - if (default_type_tup == NULL) { - PyArray_Descr *tmp = PyArray_DescrFromType(NPY_DOUBLE); - - if (tmp == NULL) { - return -1; - } - default_type_tup = PyTuple_Pack(3, tmp, tmp, tmp); - if (default_type_tup == NULL) { - Py_DECREF(tmp); - return -1; - } - Py_DECREF(tmp); - } type_num1 = PyArray_DESCR(operands[0])->type_num; type_num2 = PyArray_DESCR(operands[1])->type_num; @@ -1464,8 +1418,9 @@ PyUFunc_TrueDivisionTypeResolver(PyUFuncObject *ufunc, if (type_tup == NULL && (PyTypeNum_ISINTEGER(type_num1) || PyTypeNum_ISBOOL(type_num1)) && (PyTypeNum_ISINTEGER(type_num2) || PyTypeNum_ISBOOL(type_num2))) { - return PyUFunc_DefaultTypeResolver(ufunc, casting, operands, - default_type_tup, out_dtypes); + return PyUFunc_DefaultTypeResolver( + ufunc, casting, operands, + npy_static_pydata.default_truediv_type_tup, out_dtypes); } return PyUFunc_DivisionTypeResolver(ufunc, casting, operands, type_tup, out_dtypes); diff --git a/numpy/_core/src/umath/umathmodule.c b/numpy/_core/src/umath/umathmodule.c index 7c774f9fffc3..5402b17c399a 100644 --- a/numpy/_core/src/umath/umathmodule.c +++ b/numpy/_core/src/umath/umathmodule.c @@ -31,6 +31,7 @@ #include "stringdtype_ufuncs.h" #include "special_integer_comparisons.h" #include "extobj.h" /* for _extobject_contextvar exposure */ +#include "ufunc_type_resolution.h" /* Automatically generated code to define all ufuncs: */ #include "funcs.inc" @@ -208,29 +209,6 @@ add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args) ***************************************************************************** */ -NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_array_ufunc = NULL; -NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_array_wrap = NULL; -NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_pyvals_name = NULL; - -/* intern some strings used in ufuncs, returns 0 on success */ -static int -intern_strings(void) -{ - npy_um_str_array_ufunc = PyUnicode_InternFromString("__array_ufunc__"); - if (npy_um_str_array_ufunc == NULL) { - return -1; - } - npy_um_str_array_wrap = PyUnicode_InternFromString("__array_wrap__"); - if (npy_um_str_array_wrap == NULL) { - return -1; - } - npy_um_str_pyvals_name = PyUnicode_InternFromString(UFUNC_PYVALS_NAME); - if (npy_um_str_pyvals_name == NULL) { - return -1; - } - return 0; -} - /* Setup the umath part of the module */ int initumath(PyObject *m) @@ -272,8 +250,8 @@ int initumath(PyObject *m) #undef ADDSCONST PyModule_AddIntConstant(m, "UFUNC_BUFSIZE_DEFAULT", (long)NPY_BUFSIZE); - Py_INCREF(npy_extobj_contextvar); - PyModule_AddObject(m, "_extobj_contextvar", npy_extobj_contextvar); + Py_INCREF(npy_static_pydata.npy_extobj_contextvar); + PyModule_AddObject(m, "_extobj_contextvar", npy_static_pydata.npy_extobj_contextvar); PyModule_AddObject(m, "PINF", PyFloat_FromDouble(NPY_INFINITY)); PyModule_AddObject(m, "NINF", PyFloat_FromDouble(-NPY_INFINITY)); @@ -286,19 +264,16 @@ int initumath(PyObject *m) s = PyDict_GetItemString(d, "conjugate"); s2 = PyDict_GetItemString(d, "remainder"); + /* Setup the array object's numerical structures with appropriate ufuncs in d*/ - _PyArray_SetNumericOps(d); + if (_PyArray_SetNumericOps(d) < 0) { + return -1; + } PyDict_SetItemString(d, "conj", s); PyDict_SetItemString(d, "mod", s2); - if (intern_strings() < 0) { - PyErr_SetString(PyExc_RuntimeError, - "cannot intern umath strings while initializing _multiarray_umath."); - return -1; - } - /* * Set up promoters for logical functions * TODO: This should probably be done at a better place, or even in the