8000 bpo-46541: Remove usage of _Py_IDENTIFIER from array module (GH-31376) · python/cpython@8cb5f70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cb5f70

Browse files
authored
bpo-46541: Remove usage of _Py_IDENTIFIER from array module (GH-31376)
1 parent 6e7b813 commit 8cb5f70

File tree

2 files changed

+139
-45
lines changed

2 files changed

+139
-45
lines changed

Modules/arraymodule.c

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#ifndef Py_BUILD_CORE_BUILTIN
77
# define Py_BUILD_CORE_MODULE 1
88
#endif
9-
#define NEEDS_PY_IDENTIFIER
109

1110
#define PY_SSIZE_T_CLEAN
1211
#include "Python.h"
@@ -58,6 +57,12 @@ typedef struct {
5857
typedef struct {
5958
PyTypeObject *ArrayType;
6059
PyTypeObject *ArrayIterType;
60+
61+
PyObject *str_read;
62+
PyObject *str_write;
63+
PyObject *str__array_reconstructor;
64+
PyObject *str___dict__;
65+
PyObject *str_iter;
6166
} array_state;
6267

6368
static array_state *
@@ -1464,6 +1469,7 @@ array_array_reverse_impl(arrayobject *self)
14641469
/*[clinic input]
14651470
array.array.fromfile
14661471
1472+
cls: defining_class
14671473
f: object
14681474
n: Py_ssize_t
14691475
/
@@ -1472,13 +1478,13 @@ Read n objects from the file object f and append them to the end of the array.
14721478
[clinic start generated code]*/
14731479

14741480
static PyObject *
1475-
array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1476-
/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
1481+
array_array_fromfile_impl(arrayobject *self, PyTypeObject *cls, PyObject *f,
1482+
Py_ssize_t n)
1483+
/*[clinic end generated code: output=83a667080b345ebc input=3822e907c1c11f1a]*/
14771484
{
14781485
PyObject *b, *res;
14791486
Py_ssize_t itemsize = self->ob_descr->itemsize;
14801487
Py_ssize_t nbytes;
1481-
_Py_IDENTIFIER(read);
14821488
int not_enough_bytes;
14831489

14841490
if (n < 0) {
@@ -1489,9 +1495,14 @@ array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
14891495
PyErr_NoMemory();
14901496
return NULL;
14911497
}
1498+
1499+
1500+
array_state *state = get_array_state_by_class(cls);
1501+
assert(state != NULL);
1502+
14921503
nbytes = n * itemsize;
14931504

1494-
b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
1505+
b = _PyObject_CallMethod(f, state->str_read, "n", nbytes);
14951506
if (b == NULL)
14961507
return NULL;
14971508

@@ -1522,15 +1533,16 @@ array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
15221533
/*[clinic input]
15231534
array.array.tofile
15241535
1536+
cls: defining_class
15251537
f: object
15261538
/
15271539
15281540
Write all items (as machine values) to the file object f.
15291541
[clinic start generated code]*/
15301542

15311543
static PyObject *
1532-
array_array_tofile(arrayobject *self, PyObject *f)
1533-
/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
1544+
array_array_tofile_impl(arrayobject *self, PyTypeObject *cls, PyObject *f)
1545+
/*[clinic end generated code: output=4560c628d9c18bc2 input=5a24da7a7b407b52]*/
15341546
{
15351547
Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
15361548
/* Write 64K blocks at a time */
@@ -1542,18 +1554,21 @@ array_array_tofile(arrayobject *self, PyObject *f)
15421554
if (Py_SIZE(self) == 0)
15431555
goto done;
15441556

1557+
1558+
array_state *state = get_array_state_by_class(cls);
1559+
assert(state != NULL);
1560+
15451561
for (i = 0; i < nblocks; i++) {
15461562
char* ptr = self->ob_item + i*BLOCKSIZE;
15471563
Py_ssize_t size = BLOCKSIZE;
15481564
PyObject *bytes, *res;
1549-
_Py_IDENTIFIER(write);
15501565

15511566
if (i*BLOCKSIZE + size > nbytes)
15521567
size = nbytes - i*BLOCKSIZE;
15531568
bytes = PyBytes_FromStringAndSize(ptr, size);
15541569
if (bytes == NULL)
15551570
return NULL;
1556-
res = _PyObject_CallMethodIdOneArg(f, &PyId_write, bytes);
1571+
res = PyObject_CallMethodOneArg(f, state->str_write, bytes);
15571572
Py_DECREF(bytes);
15581573
if (res == NULL)
15591574
return NULL;
@@ -2176,15 +2191,17 @@ array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
21762191
/*[clinic input]
21772192
array.array.__reduce_ex__
21782193
2194+
cls: defining_class
21792195
value: object
21802196
/
21812197
21822198
Return state information for pickling.
21832199
[clinic start generated code]*/
21842200

21852201
static PyObject *
2186-
array_array___reduce_ex__(arrayobject *self, PyObject *value)
2187-
/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
2202+
array_array___reduce_ex___impl(arrayobject *self, PyTypeObject *cls,
2203+
PyObject *value)
2204+
/*[clinic end generated code: output=4958ee5d79452ad5 input=19968cf0f91d3eea]*/
21882205
{
21892206
PyObject *dict;
21902207
PyObject *result;
@@ -2193,16 +2210,17 @@ array_array___reduce_ex__(arrayobject *self, PyObject *value)
21932210
int mformat_code;
21942211
static PyObject *array_reconstructor = NULL;
21952212
long protocol;
2196-
_Py_IDENTIFIER(_array_reconstructor);
2197-
_Py_IDENTIFIER(__dict__);
2213+
2214+
array_state *state = get_array_state_by_class(cls);
2215+
assert(state != NULL);
21982216

21992217
if (array_reconstructor == NULL) {
22002218
PyObject *array_module = PyImport_ImportModule("array");
22012219
if (array_module == NULL)
22022220
return NULL;
2203-
array_reconstructor = _PyObject_GetAttrId(
2221+
array_reconstructor = PyObject_GetAttr(
22042222
array_module,
2205-
&PyId__array_reconstructor);
2223+
state->str__array_reconstructor);
22062224
Py_DECREF(array_module);
22072225
if (array_reconstructor == NULL)
22082226
return NULL;
@@ -2217,7 +2235,7 @@ array_array___reduce_ex__(arrayobject *self, PyObject *value)
22172235
if (protocol == -1 && PyErr_Occurred())
22182236
return NULL;
22192237

2220-
if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2238+
if (_PyObject_LookupAttr((PyObject *)self, state->str___dict__, &dict) < 0) {
22212239
return NULL;
22222240
}
22232241
if (dict == NULL) {
@@ -2939,15 +2957,20 @@ arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
29392957
/*[clinic input]
29402958
array.arrayiterator.__reduce__
29412959
2960+
cls: defining_class
2961+
/
2962+
29422963
Return state information for pickling.
29432964
[clinic start generated code]*/
29442965

29452966
static PyObject *
2946-
array_arrayiterator___reduce___impl(arrayiterobject *self)
2947-
/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2967+
array_arrayiterator___reduce___impl(arrayiterobject *self, PyTypeObject *cls)
2968+
/*[clinic end generated code: output=4b032417a2c8f5e6 input=ac64e65a87ad452e]*/
29482969
{
2949-
_Py_IDENTIFIER(iter);
2950-
PyObject *func = _PyEval_GetBuiltinId(&PyId_iter);
2970+
2971+
array_state *state = get_array_state_by_class(cls);
2972+
assert(state != NULL);
2973+
PyObject *func = _PyEval_GetBuiltin(state->str_iter);
29512974
if (self->ao == NULL) {
29522975
return Py_BuildValue("N(())", func);
29532976
}
@@ -3011,6 +3034,11 @@ array_traverse(PyObject *module, visitproc visit, void *arg)
30113034
array_state *state = get_array_state(module);
30123035
Py_VISIT(state->ArrayType);
30133036
Py_VISIT(state->ArrayIterType);
3037+
Py_VISIT(state->str_read);
3038+
Py_VISIT(state->str_write);
3039+
Py_VISIT(state->str__array_reconstructor);
3040+
Py_VISIT(state->str___dict__);
3041+
Py_VISIT(state->str_iter);
30143042
return 0;
30153043
}
30163044

@@ -3020,6 +3048,11 @@ array_clear(PyObject *module)
30203048
array_state *state = get_array_state(module);
30213049
Py_CLEAR(state->ArrayType);
30223050
Py_CLEAR(state->ArrayIterType);
3051+
Py_CLEAR(state->str_read);
3052+
Py_CLEAR(state->str_write);
3053+
Py_CLEAR(state->str__array_reconstructor);
3054+
Py_CLEAR(state->str___dict__);
3055+
Py_CLEAR(state->str_iter);
30233056
return 0;
30243057
}
30253058

@@ -3043,6 +3076,15 @@ do { \
30433076
} \
30443077
} while (0)
30453078

3079+
#define ADD_INTERNED(state, string) \
3080+
do { \
3081+
PyObject *tmp = PyUnicode_InternFromString(#string); \
3082+
if (tmp == NULL) { \
3083+
return -1; \
3084+
} \
3085+
state->str_ ## string = tmp; \
3086+
} while (0)
3087+
30463088
static int
30473089
array_modexec(PyObject *m)
30483090
{
@@ -3051,6 +3093,13 @@ array_modexec(PyObject *m)
30513093
PyObject *typecodes;
30523094
const struct arraydescr *descr;
30533095

3096+
/* Add interned strings */
3097+
ADD_INTERNED(state, read);
3098+
ADD_INTERNED(state, write);
3099+
ADD_INTERNED(state, _array_reconstructor);
3100+
ADD_INTERNED(state, __dict__);
3101+
ADD_INTERNED(state, iter);
3102+
30543103
CREATE_TYPE(m, state->ArrayType, &array_spec);
30553104
CREATE_TYPE(m, state->ArrayIterType, &arrayiter_spec);
30563105
Py_SET_TYPE(state->ArrayIterType, &PyType_Type);

Modules/clinic/arraymodule.c.h

Lines changed: 70 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0