Skip to content

Commit a1f7200

Browse files
committed
MNT: move references to int(1) and int(0) to global static struct
1 parent d2ca21b commit a1f7200

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

numpy/_core/src/multiarray/convert_datatype.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,7 +2167,6 @@ PyArray_Zero(PyArrayObject *arr)
21672167
{
21682168
char *zeroval;
21692169
int ret, storeflags;
2170-
static PyObject * zero_obj = NULL;
21712170

21722171
if (_check_object_rec(PyArray_DESCR(arr)) < 0) {
21732172
return NULL;
@@ -2178,25 +2177,19 @@ PyArray_Zero(PyArrayObject *arr)
21782177
return NULL;
21792178
}
21802179

2181-
if (zero_obj == NULL) {
2182-
zero_obj = PyLong_FromLong((long) 0);
2183-
if (zero_obj == NULL) {
2184-
return NULL;
2185-
}
2186-
}
21872180
if (PyArray_ISOBJECT(arr)) {
21882181
/* XXX this is dangerous, the caller probably is not
21892182
aware that zeroval is actually a static PyObject*
21902183
In the best case they will only use it as-is, but
21912184
if they simply memcpy it into a ndarray without using
21922185
setitem(), refcount errors will occur
21932186
*/
2194-
memcpy(zeroval, &zero_obj, sizeof(PyObject *));
2187+
memcpy(zeroval, &npy_ma_global_data->zero_obj, sizeof(PyObject *));
21952188
return zeroval;
21962189
}
21972190
storeflags = PyArray_FLAGS(arr);
21982191
PyArray_ENABLEFLAGS(arr, NPY_ARRAY_BEHAVED);
2199-
ret = PyArray_SETITEM(arr, zeroval, zero_obj);
2192+
ret = PyArray_SETITEM(arr, zeroval, npy_ma_global_data->zero_obj);
22002193
((PyArrayObject_fields *)arr)->flags = storeflags;
22012194
if (ret < 0) {
22022195
PyDataMem_FREE(zeroval);
@@ -2213,7 +2206,6 @@ PyArray_One(PyArrayObject *arr)
22132206
{
22142207
char *oneval;
22152208
int ret, storeflags;
2216-
static PyObject * one_obj = NULL;
22172209

22182210
if (_check_object_rec(PyArray_DESCR(arr)) < 0) {
22192211
return NULL;
@@ -2224,26 +2216,20 @@ PyArray_One(PyArrayObject *arr)
22242216
return NULL;
22252217
}
22262218

2227-
if (one_obj == NULL) {
2228-
one_obj = PyLong_FromLong((long) 1);
2229-
if (one_obj == NULL) {
2230-
return NULL;
2231-
}
2232-
}
22332219
if (PyArray_ISOBJECT(arr)) {
22342220
/* XXX this is dangerous, the caller probably is not
22352221
aware that oneval is actually a static PyObject*
22362222
In the best case they will only use it as-is, but
22372223
if they simply memcpy it into a ndarray without using
22382224
setitem(), refcount errors will occur
22392225
*/
2240-
memcpy(oneval, &one_obj, sizeof(PyObject *));
2226+
memcpy(oneval, &npy_ma_global_data->one_obj, sizeof(PyObject *));
22412227
return oneval;
22422228
}
22432229

22442230
storeflags = PyArray_FLAGS(arr);
22452231
PyArray_ENABLEFLAGS(arr, NPY_ARRAY_BEHAVED);
2246-
ret = PyArray_SETITEM(arr, oneval, one_obj);
2232+
ret = PyArray_SETITEM(arr, oneval, npy_ma_global_data->one_obj);
22472233
((PyArrayObject_fields *)arr)->flags = storeflags;
22482234
if (ret < 0) {
22492235
PyDataMem_FREE(oneval);

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5008,6 +5008,16 @@ initialize_static_globals(void)
50085008
}
50095009
}
50105010

5011+
npy_ma_global_data->one_obj = PyLong_FromLong((long) 1);
5012+
if (npy_ma_global_data->one_obj == NULL) {
5013+
return -1;
5014+
}
5015+
5016+
npy_ma_global_data->zero_obj = PyLong_FromLong((long) 0);
5017+
if (npy_ma_global_data->zero_obj == NULL) {
5018+
return -1;
5019+
}
5020+
50115021
return 0;
50125022
}
50135023

numpy/_core/src/multiarray/multiarraymodule.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ typedef struct npy_ma_global_data_struct {
4444
PyObject *ndarray_array_ufunc;
4545
PyObject *ndarray_array_finalize;
4646
PyObject *ndarray_array_function;
47+
48+
/*
49+
* References to the '1' and '0' PyLong objects
50+
*/
51+
PyObject *one_obj;
52+
PyObject *zero_obj;
53+
4754
/*
4855
* References to items obtained via an import at module initialization
4956
*

0 commit comments

Comments
 (0)