Skip to content

Commit 3ae66b1

Browse files
committed
MAINT: apply more of Sebastian's suggestions
1 parent 9ed317f commit 3ae66b1

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5044,10 +5044,19 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
50445044
// initialize static references to ndarray.__array_*__ special methods
50455045
npy_static_pydata.ndarray_array_finalize = PyObject_GetAttrString(
50465046
(PyObject *)&PyArray_Type, "__array_finalize__");
5047+
if (npy_static_pydata.ndarray_array_finalize == NULL) {
5048+
goto err;
5049+
}
50475050
npy_static_pydata.ndarray_array_ufunc = PyObject_GetAttrString(
50485051
(PyObject *)&PyArray_Type, "__array_ufunc__");
5052+
if (npy_static_pydata.ndarray_array_ufunc == NULL) {
5053+
goto err;
5054+
}
50495055
npy_static_pydata.ndarray_array_function = PyObject_GetAttrString(
50505056
(PyObject *)&PyArray_Type, "__array_function__");
5057+
if (npy_static_pydata.ndarray_array_function == NULL) {
5058+
goto err;
5059+
}
50515060

50525061
/*
50535062
* Initialize np.dtypes.StringDType
@@ -5088,6 +5097,15 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
50885097
goto err;
50895098
}
50905099

5100+
// initialize static reference to a zero-like array
5101+
npy_static_pydata.zero_pyint_like_arr = PyArray_ZEROS(
5102+
0, NULL, NPY_LONG, NPY_FALSE);
5103+
if (npy_static_pydata.zero_pyint_like_arr == NULL) {
5104+
goto err;
5105+
}
5106+
((PyArrayObject_fields *)npy_static_pydata.zero_pyint_like_arr)->flags |=
5107+
(NPY_ARRAY_WAS_PYTHON_INT|NPY_ARRAY_WAS_INT_AND_REPLACED);
5108+
50915109
if (verify_static_structs_initialized() < 0) {
50925110
goto err;
50935111
}

numpy/_core/src/multiarray/npy_static_data.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ initialize_static_globals(void)
154154
IMPORT_GLOBAL("os", "PathLike",
155155
npy_static_pydata.os_PathLike);
156156

157-
// default_truediv_type_tupS
157+
// default_truediv_type_tup
158158
PyArray_Descr *tmp = PyArray_DescrFromType(NPY_DOUBLE);
159159
npy_static_pydata.default_truediv_type_tup =
160160
PyTuple_Pack(3, tmp, tmp, tmp);

numpy/_core/src/multiarray/npy_static_data.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ typedef struct npy_static_pydata_struct {
7979
PyObject *one_obj;
8080
PyObject *zero_obj;
8181

82+
/*
83+
* Reference to an np.array(0, dtype=np.long) instance
84+
*/
85+
PyObject *zero_pyint_like_arr;
86+
8287
/*
8388
* References to items obtained via an import at module initialization
8489
*/

numpy/_core/src/umath/override.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,13 @@ PyUFunc_CheckOverride(PyUFuncObject *ufunc, char *method,
373373
"numpy._core._internal",
374374
"array_ufunc_errmsg_formatter",
375375
&npy_thread_unsafe_state.array_ufunc_errmsg_formatter);
376-
if (npy_thread_unsafe_state.array_ufunc_errmsg_formatter != NULL) {
377-
errmsg = PyObject_Call(
378-
npy_thread_unsafe_state.array_ufunc_errmsg_formatter,
379-
override_args, normal_kwds);
380-
if (errmsg != NULL) {
381-
PyErr_SetObject(PyExc_TypeError, errmsg);
382-
Py_DECREF(errmsg);
383-
}
376+
assert(npy_thread_unsafe_state.array_ufunc_errmsg_formatter != NULL);
377+
errmsg = PyObject_Call(
378+
npy_thread_unsafe_state.array_ufunc_errmsg_formatter,
379+
override_args, normal_kwds);
380+
if (errmsg != NULL) {
381+
PyErr_SetObject(PyExc_TypeError, errmsg);
382+
Py_DECREF(errmsg);
384383
}
385384
Py_DECREF(override_args);
386385
goto fail;

numpy/_core/src/umath/ufunc_object.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -700,15 +700,9 @@ convert_ufunc_arguments(PyUFuncObject *ufunc,
700700
* TODO: Just like the general dual NEP 50/legacy promotion
701701
* support this is meant as a temporary hack for NumPy 1.25.
702702
*/
703-
PyArrayObject *zero_arr = (PyArrayObject *)PyArray_ZEROS(
704-
0, NULL, NPY_LONG, NPY_FALSE);
705-
if (zero_arr == NULL) {
706-
goto fail;
707-
}
708-
((PyArrayObject_fields *)zero_arr)->flags |= (
709-
NPY_ARRAY_WAS_PYTHON_INT|NPY_ARRAY_WAS_INT_AND_REPLACED);
710-
Py_INCREF(zero_arr);
711-
Py_SETREF(out_op[i], zero_arr);
703+
Py_INCREF(npy_static_pydata.zero_pyint_like_arr);
704+
Py_SETREF(out_op[i],
705+
(PyArrayObject *)npy_static_pydata.zero_pyint_like_arr);
712706
}
713707
*promoting_pyscalars = NPY_TRUE;
714708
}

0 commit comments

Comments
 (0)