10000 MAINT: apply more of Sebastian's suggestions · numpy/numpy@41c7f43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41c7f43

Browse files
committed
MAINT: apply more of Sebastian's suggestions
1 parent 41790b6 commit 41c7f43

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
@@ -5072,10 +5072,19 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
50725072
// initialize static references to ndarray.__array_*__ special methods
50735073
npy_static_pydata.ndarray_array_finalize = PyObject_GetAttrString(
50745074
(PyObject *)&PyArray_Type, "__array_finalize__");
5075+
if (npy_static_pydata.ndarray_array_finalize == NULL) {
5076+
goto err;
5077+
}
50755078
npy_static_pydata.ndarray_array_ufunc = PyObject_GetAttrString(
50765079
(PyObject *)&PyArray_Type, "__array_ufunc__");
5080+
if (npy_static_pydata.ndarray_array_ufunc == NULL) {
5081+
goto err;
5082+
}
50775083
npy_static_pydata.ndarray_array_function = PyObject_GetAttrString(
50785084
(PyObject *)&PyArray_Type, "__array_function__");
5085+
if (npy_static_pydata.ndarray_array_function == NULL) {
5086+
goto err;
5087+
}
50795088

50805089
/*
50815090
* Initialize np.dtypes.StringDType
@@ -5116,6 +5125,15 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
51165125
goto err;
51175126
}
51185127

5128+
// initialize static reference to a zero-like array
5129+
npy_static_pydata.zero_pyint_like_arr = PyArray_ZEROS(
5130+
0, NULL, NPY_LONG, NPY_FALSE);
5131+
if (npy_static_pydata.zero_pyint_like_arr == NULL) {
5132+
goto err;
5133+
}
5134+
((PyArrayObject_fields *)npy_static_pydata.zero_pyint_like_arr)->flags |=
5135+
(NPY_ARRAY_WAS_PYTHON_INT|NPY_ARRAY_WAS_INT_AND_REPLACED);
5136+
51195137
if (verify_static_structs_initialized() < 0) {
51205138
goto err;
51215139
}

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)
0