Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39daec6

Browse files
committed
MAINT: mark static data structs as const
1 parent 41c7f43 commit 39daec6

File tree

8 files changed

+92
-60
lines changed

8 files changed

+92
-60
lines changed

numpy/_core/src/common/npy_cpu_dispatch.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ npy_cpu_dispatch_tracer_init(PyObject *mod)
2525
if (err != 0) {
2626
return -1;
2727
}
28-
npy_static_pydata.cpu_dispatch_registry = reg_dict;
28+
// cast away const to initialize
29+
((npy_static_pydata_struct *)&npy_static_pydata)->cpu_dispatch_registry = reg_dict;
2930
return 0;
3031
}
3132

numpy/_core/src/multiarray/arraytypes.c.src

+15-9
Original file line numberDiff line numberDiff line change
@@ -4187,6 +4187,8 @@ NPY_NO_EXPORT _PyArray_LegacyDescr @from@_Descr = {
41874187
/* The smallest type number is ?, the largest bounded by 'z'. */
41884188
#define _MAX_LETTER ('z' + 1)
41894189
#define LETTER_TO_NUM(letter) npy_static_cdata._letter_to_num[letter - '?']
4190+
#define LETTER_TO_NUM_ASSIGN(letter, value) \
4191+
npy_static_cdata_mut->_letter_to_num[letter - '?'] = value;
41904192

41914193
static _PyArray_LegacyDescr *_builtin_descrs[] = {
41924194
&BOOL_Descr,
@@ -4441,8 +4443,12 @@ set_typeinfo(PyObject *dict)
44414443
return -1;
44424444
}
44434445

4446+
// cast away const to initialize
4447+
npy_static_cdata_struct *npy_static_cdata_mut =
4448+
(npy_static_cdata_struct *)&npy_static_cdata;
4449+
44444450
for (i = '?'; i < _MAX_LETTER; i++) {
4445-
LETTER_TO_NUM(i) = -1;
4451+
LETTER_TO_NUM_ASSIGN(i, -1);
44464452
}
44474453

44484454
/**begin repeat
@@ -4456,23 +4462,23 @@ set_typeinfo(PyObject *dict)
44564462
* DATETIME,TIMEDELTA#
44574463
*/
44584464

4459-
LETTER_TO_NUM(NPY_@name@LTR) = NPY_@name@;
4465+
LETTER_TO_NUM_ASSIGN(NPY_@name@LTR, NPY_@name@);
44604466

44614467
/**end repeat**/
4462-
LETTER_TO_NUM('n') = NPY_INTP;
4463-
LETTER_TO_NUM('N') = NPY_UINTP;
4468+
LETTER_TO_NUM_ASSIGN('n', NPY_INTP);
4469+
LETTER_TO_NUM_ASSIGN('N', NPY_UINTP);
44644470

44654471
#if NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_INTP
4466-
LETTER_TO_NUM('p') = NPY_INTP;
4467-
LETTER_TO_NUM('P') = NPY_UINTP;
4472+
LETTER_TO_NUM_ASSIGN('p', NPY_INTP);
4473+
LETTER_TO_NUM_ASSIGN('P', NPY_UINTP);
44684474
#elif NPY_SIZEOF_PY_INTPTR_T == NPY_SIZEOF_LONGLONG
4469-
LETTER_TO_NUM('p') = NPY_LONGLONG;
4470-
LETTER_TO_NUM('P') = NPY_ULONGLONG;
4475+
LETTER_TO_NUM_ASSIGN('p', NPY_LONGLONG);
4476+
LETTER_TO_NUM_ASSIGN('P', NPY_ULONGLONG);
44714477
#else
44724478
#error "Did not find correct pointer sized integer."
44734479
#endif
44744480

4475-
LETTER_TO_NUM('T') = NPY_VSTRING;
4481+
LETTER_TO_NUM_ASSIGN('T', NPY_VSTRING);
44764482

44774483
/**begin repeat
44784484
* #name = BOOL,

numpy/_core/src/multiarray/convert_datatype.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -3841,14 +3841,18 @@ initialize_void_and_object_globals(void) {
38413841
return -1;
38423842
}
38433843

3844+
// cast away const to initialize
3845+
npy_static_pydata_struct *npy_static_pydata_mut =
3846+
(npy_static_pydata_struct *)&npy_static_pydata;
3847+
38443848
method->name = "void_to_any_cast";
38453849
method->flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI;
38463850
method->casting = -1;
38473851
method->resolve_descriptors = &structured_to_nonstructured_resolve_descriptors;
38483852
method->get_strided_loop = &structured_to_nonstructured_get_loop;
38493853
method->nin = 1;
38503854
method->nout = 1;
3851-
npy_static_pydata.VoidToGenericMethod = (PyObject *)method;
3855+
npy_static_pydata_mut->VoidToGenericMethod = (PyObject *)method;
38523856

38533857
method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type);
38543858
if (method == NULL) {
@@ -3863,7 +3867,7 @@ initialize_void_and_object_globals(void) {
38633867
method->get_strided_loop = &nonstructured_to_structured_get_loop;
38643868
method->nin = 1;
38653869
method->nout = 1;
3866-
npy_static_pydata.GenericToVoidMethod = (PyObject *)method;
3870+
npy_static_pydata_mut->GenericToVoidMethod = (PyObject *)method;
38673871

38683872
method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type);
38693873
if (method == NULL) {
@@ -3878,7 +3882,7 @@ initialize_void_and_object_globals(void) {
38783882
method->casting = NPY_UNSAFE_CASTING;
38793883
method->resolve_descriptors = &object_to_any_resolve_descriptors;
38803884
method->get_strided_loop = &object_to_any_get_loop;
3881-
npy_static_pydata.ObjectToGenericMethod = (PyObject *)method;
3885+
npy_static_pydata_mut->ObjectToGenericMethod = (PyObject *)method;
38823886

38833887
method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type);
38843888
if (method == NULL) {
@@ -3893,7 +3897,7 @@ initialize_void_and_object_globals(void) {
38933897
method->casting = NPY_SAFE_CASTING;
38943898
method->resolve_descriptors = &any_to_object_resolve_descriptors;
38953899
method->get_strided_loop = &any_to_object_get_loop;
3896-
npy_static_pydata.GenericToObjectMethod = (PyObject *)method;
3900+
npy_static_pydata_mut->GenericToObjectMethod = (PyObject *)method;
38973901

38983902
return 0;
38993903
}

numpy/_core/src/multiarray/multiarraymodule.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -5069,18 +5069,22 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
50695069
goto err;
50705070
}
50715071

5072+
// cast away const to initialize
5073+
npy_static_pydata_struct *npy_static_pydata_mut =
5074+
(npy_static_pydata_struct *)&npy_static_pydata;
5075+
50725076
// initialize static references to ndarray.__array_*__ special methods
5073-
npy_static_pydata.ndarray_array_finalize = PyObject_GetAttrString(
5077+
npy_static_pydata_mut->ndarray_array_finalize = PyObject_GetAttrString(
50745078
(PyObject *)&PyArray_Type, "__array_finalize__");
50755079
if (npy_static_pydata.ndarray_array_finalize == NULL) {
50765080
goto err;
50775081
}
5078-
npy_static_pydata.ndarray_array_ufunc = PyObject_GetAttrString(
5082+
npy_static_pydata_mut->ndarray_array_ufunc = PyObject_GetAttrString(
50795083
(PyObject *)&PyArray_Type, "__array_ufunc__");
50805084
if (npy_static_pydata.ndarray_array_ufunc == NULL) {
50815085
goto err;
50825086
}
5083-
npy_static_pydata.ndarray_array_function = PyObject_GetAttrString(
5087+
npy_static_pydata_mut->ndarray_array_function = PyObject_GetAttrString(
50845088
(PyObject *)&PyArray_Type, "__array_function__");
50855089
if (npy_static_pydata.ndarray_array_function == NULL) {
50865090
goto err;
@@ -5126,7 +5130,7 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
51265130
}
51275131

51285132
// initialize static reference to a zero-like array
5129-
npy_static_pydata.zero_pyint_like_arr = PyArray_ZEROS(
5133+
npy_static_pydata_mut->zero_pyint_like_arr = PyArray_ZEROS(
51305134
0, NULL, NPY_LONG, NPY_FALSE);
51315135
if (npy_static_pydata.zero_pyint_like_arr == NULL) {
51325136
goto err;

numpy/_core/src/multiarray/npy_static_data.c

+43-34
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
#include "extobj.h"
1616

1717
// static variables are zero-filled by default, no need to explicitly do so
18-
NPY_VISIBILITY_HIDDEN npy_interned_str_struct npy_interned_str;
19-
NPY_VISIBILITY_HIDDEN npy_static_pydata_struct npy_static_pydata;
20-
NPY_VISIBILITY_HIDDEN npy_static_cdata_struct npy_static_cdata;
21-
22-
#define INTERN_STRING(struct_member, string) \
23-
assert(npy_interned_str.struct_member == NULL); \
24-
npy_interned_str.struct_member = PyUnicode_InternFromString(string); \
25-
if (npy_interned_str.struct_member == NULL) { \
26-
return -1; \
27-
} \
18+
NPY_VISIBILITY_HIDDEN const npy_interned_str_struct npy_interned_str;
19+
NPY_VISIBILITY_HIDDEN const npy_static_pydata_struct npy_static_pydata;
20+
NPY_VISIBILITY_HIDDEN const npy_static_cdata_struct npy_static_cdata;
21+
22+
#define INTERN_STRING(struct_member, string) \
23+
assert(npy_interned_str.struct_member == NULL); \
24+
((npy_interned_str_struct *)&npy_interned_str)->struct_member = \
25+
PyUnicode_InternFromString(string); \
26+
if (npy_interned_str.struct_member == NULL) { \
27+
return -1; \
28+
} \
2829

2930
NPY_NO_EXPORT int
3031
intern_strings(void)
@@ -100,80 +101,84 @@ initialize_static_globals(void)
100101
* module for performance reasons
101102
*/
102103

104+
// cast away const to initialize
105+
npy_static_pydata_struct *npy_static_pydata_mut =
106+
(npy_static_pydata_struct *)&npy_static_pydata;
107+
103108
IMPORT_GLOBAL("math", "floor",
104-
npy_static_pydata.math_floor_func);
109+
npy_static_pydata_mut->math_floor_func);
105110

106111
IMPORT_GLOBAL("math", "ceil",
107-
npy_static_pydata.math_ceil_func);
112+
npy_static_pydata_mut->math_ceil_func);
108113

109114
IMPORT_GLOBAL("math", "trunc",
110-
npy_static_pydata.math_trunc_func);
115+
npy_static_pydata_mut->math_trunc_func);
111116

112117
IMPORT_GLOBAL("math", "gcd",
113-
npy_static_pydata.math_gcd_func);
118+
npy_static_pydata_mut->math_gcd_func);
114119

115120
IMPORT_GLOBAL("numpy.exceptions", "AxisError",
116-
npy_static_pydata.AxisError);
121+
npy_static_pydata_mut->AxisError);
117122

118123
IMPORT_GLOBAL("numpy.exceptions", "ComplexWarning",
119-
npy_static_pydata.ComplexWarning);
124+
npy_static_pydata_mut->ComplexWarning);
120125

121126
IMPORT_GLOBAL("numpy.exceptions", "DTypePromotionError",
122-
npy_static_pydata.DTypePromotionError);
127+
npy_static_pydata_mut->DTypePromotionError);
123128

124129
IMPORT_GLOBAL("numpy.exceptions", "TooHardError",
125-
npy_static_pydata.TooHardError);
130+
npy_static_pydata_mut->TooHardError);
126131

127132
IMPORT_GLOBAL("numpy.exceptions", "VisibleDeprecationWarning",
128-
npy_static_pydata.VisibleDeprecationWarning);
133+
npy_static_pydata_mut->VisibleDeprecationWarning);
129134

130135
IMPORT_GLOBAL("numpy._globals", "_CopyMode",
131-
npy_static_pydata._CopyMode);
136+
npy_static_pydata_mut->_CopyMode);
132137

133138
IMPORT_GLOBAL("numpy._globals", "_NoValue",
134-
npy_static_pydata._NoValue);
139+
npy_static_pydata_mut->_NoValue);
135140

136141
IMPORT_GLOBAL("numpy._core._exceptions", "_ArrayMemoryError",
137-
npy_static_pydata._ArrayMemoryError);
142+
npy_static_pydata_mut->_ArrayMemoryError);
138143

139144
IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncBinaryResolutionError",
140-
npy_static_pydata._UFuncBinaryResolutionError);
145+
npy_static_pydata_mut->_UFuncBinaryResolutionError);
141146

142147
IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncInputCastingError",
143-
npy_static_pydata._UFuncInputCastingError);
148+
npy_static_pydata_mut->_UFuncInputCastingError);
144149

145150
IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncNoLoopError",
146-
npy_static_pydata._UFuncNoLoopError);
151+
npy_static_pydata_mut->_UFuncNoLoopError);
147152

148153
IMPORT_GLOBAL("numpy._core._exceptions", "_UFuncOutputCastingError",
149-
npy_static_pydata._UFuncOutputCastingError);
154+
npy_static_pydata_mut->_UFuncOutputCastingError);
150155

151156
IMPORT_GLOBAL("os", "fspath",
152-
npy_static_pydata.os_fspath);
157+
npy_static_pydata_mut->os_fspath);
153158

154159
IMPORT_GLOBAL("os", "PathLike",
155-
npy_static_pydata.os_PathLike);
160+
npy_static_pydata_mut->os_PathLike);
156161

157162
// default_truediv_type_tup
158163
PyArray_Descr *tmp = PyArray_DescrFromType(NPY_DOUBLE);
159-
npy_static_pydata.default_truediv_type_tup =
164+
npy_static_pydata_mut->default_truediv_type_tup =
160165
PyTuple_Pack(3, tmp, tmp, tmp);
161166
Py_DECREF(tmp);
162167
if (npy_static_pydata.default_truediv_type_tup == NULL) {
163168
return -1;
164169
}
165170

166-
npy_static_pydata.kwnames_is_copy = Py_BuildValue("(s)", "copy");
171+
npy_static_pydata_mut->kwnames_is_copy = Py_BuildValue("(s)", "copy");
167172
if (npy_static_pydata.kwnames_is_copy == NULL) {
168173
return -1;
169174
}
170175

171-
npy_static_pydata.one_obj = PyLong_FromLong((long) 1);
176+
npy_static_pydata_mut->one_obj = PyLong_FromLong((long) 1);
172177
if (npy_static_pydata.one_obj == NULL) {
173178
return -1;
174179
}
175180

176-
npy_static_pydata.zero_obj = PyLong_FromLong((long) 0);
181+
npy_static_pydata_mut->zero_obj = PyLong_FromLong((long) 0);
177182
if (npy_static_pydata.zero_obj == NULL) {
178183
return -1;
179184
}
@@ -189,6 +194,10 @@ initialize_static_globals(void)
189194
* up this way for performance reasons.
190195
*/
191196

197+
// cast away const to initialize
198+
npy_static_cdata_struct *npy_static_cdata_mut =
199+
(npy_static_cdata_struct *)&npy_static_cdata;
200+
192201
PyObject *flags = PySys_GetObject("flags"); /* borrowed object */
193202
if (flags == NULL) {
194203
PyErr_SetString(PyExc_AttributeError, "cannot get sys.flags");
@@ -198,7 +207,7 @@ initialize_static_globals(void)
198207
if (level == NULL) {
199208
return -1;
200209
}
201-
npy_static_cdata.optimize = PyLong_AsLong(level);
210+
npy_static_cdata_mut->optimize = PyLong_AsLong(level);
202211
Py_DECREF(level);
203212

204213
/*
@@ -214,7 +223,7 @@ initialize_static_globals(void)
214223
npy_intp k;
215224
for (k=0; k < 8; k++) {
216225
npy_uint8 v = (j & (1 << k)) == (1 << k);
217-
npy_static_cdata.unpack_lookup_big[j].bytes[7 - k] = v;
226+
npy_static_cdata_mut->unpack_lookup_big[j].bytes[7 - k] = v;
218227
}
219228
}
220229

numpy/_core/src/multiarray/npy_static_data.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ typedef struct npy_static_cdata_struct {
160160
npy_int16 _letter_to_num['z' + 1 - '?'];
161161
} npy_static_cdata_struct;
162162

163-
NPY_VISIBILITY_HIDDEN extern npy_interned_str_struct npy_interned_str;
164-
NPY_VISIBILITY_HIDDEN extern npy_static_pydata_struct npy_static_pydata;
165-
NPY_VISIBILITY_HIDDEN extern npy_static_cdata_struct npy_static_cdata;
163+
NPY_VISIBILITY_HIDDEN extern const npy_interned_str_struct npy_interned_str;
164+
NPY_VISIBILITY_HIDDEN extern const npy_static_pydata_struct npy_static_pydata;
165+
NPY_VISIBILITY_HIDDEN extern const npy_static_cdata_struct npy_static_cdata;
166166

167167
#endif // NUMPY_CORE_SRC_MULTIARRAY_STATIC_DATA_H_

numpy/_core/src/multiarray/number.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,18 @@ _PyArray_SetNumericOps(PyObject *dict)
123123
SET(matmul);
124124
SET(clip);
125125

126+
// cast away const to initialize
127+
npy_static_pydata_struct *npy_static_pydata_mut =
128+
(npy_static_pydata_struct *)&npy_static_pydata;
129+
126130
// initialize static globals needed for matmul
127-
npy_static_pydata.axes_1d_obj_kwargs = Py_BuildValue(
131+
npy_static_pydata_mut->axes_1d_obj_kwargs = Py_BuildValue(
128132
"{s, [(i), (i, i), (i)]}", "axes", -1, -2, -1, -1);
129133
if (npy_static_pydata.axes_1d_obj_kwargs == NULL) {
130134
return -1;
131135
}
132136

133-
npy_static_pydata.axes_2d_obj_kwargs = Py_BuildValue(
137+
npy_static_pydata_mut->axes_2d_obj_kwargs = Py_BuildValue(
134138
"{s, [(i, i), (i, i), (i, i)]}", "axes", -2, -1, -2, -1, -2, -1);
135139
if (npy_static_pydata.axes_2d_obj_kwargs == NULL) {
136140
return -1;

numpy/_core/src/umath/extobj.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,19 @@ fetch_curr_extobj_state(npy_extobj *extobj)
140140
NPY_NO_EXPORT int
141141
init_extobj(void)
142142
{
143-
npy_static_pydata.default_extobj_capsule = make_extobj_capsule(
143+
// cast away const to initialize
144+
npy_static_pydata_struct *npy_static_pydata_mut =
145+
(npy_static_pydata_struct *)&npy_static_pydata;
146+
147+
npy_static_pydata_mut->default_extobj_capsule = make_extobj_capsule(
144148
NPY_BUFSIZE, UFUNC_ERR_DEFAULT, Py_None);
145149
if (npy_static_pydata.default_extobj_capsule == NULL) {
146150
return -1;
147151
}
148-
npy_static_pydata.npy_extobj_contextvar = PyContextVar_New(
152+
npy_static_pydata_mut->npy_extobj_contextvar = PyContextVar_New(
149153
"numpy.ufunc.extobj", npy_static_pydata.default_extobj_capsule);
150154
if (npy_static_pydata.npy_extobj_contextvar == NULL) {
151-
Py_CLEAR(npy_static_pydata.default_extobj_capsule);
155+
Py_CLEAR(npy_static_pydata_mut->default_extobj_capsule);
152156
return -1;
153157
}
154158
return 0;

0 commit comments

Comments
 (0)