11
11
#include "Python.h"
12
12
#include "pycore_floatobject.h" // _PyFloat_Unpack4()
13
13
#include "pycore_moduleobject.h" // _PyModule_GetState()
14
- #include "pycore_runtime.h" // _Py_ID()
15
14
#include "structmember.h" // PyMemberDef
16
15
#include <stddef.h> // offsetof()
17
16
#include <stddef.h>
@@ -58,6 +57,12 @@ typedef struct {
58
57
typedef struct {
59
58
PyTypeObject * ArrayType ;
60
59
PyTypeObject * ArrayIterType ;
60
+
61
+ PyObject * str_read ;
62
+ PyObject * str_write ;
63
+ PyObject * str__array_reconstructor ;
64
+ PyObject * str___dict__ ;
65
+ PyObject * str_iter ;
61
66
} array_state ;
62
67
63
68
static array_state *
@@ -1464,6 +1469,7 @@ array_array_reverse_impl(arrayobject *self)
1464
1469
/*[clinic input]
1465
1470
array.array.fromfile
1466
1471
1472
+ cls: defining_class
1467
1473
f: object
1468
1474
n: Py_ssize_t
1469
1475
/
@@ -1472,8 +1478,9 @@ Read n objects from the file object f and append them to the end of the array.
1472
1478
[clinic start generated code]*/
1473
1479
1474
1480
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]*/
1477
1484
{
1478
1485
PyObject * b , * res ;
1479
1486
Py_ssize_t itemsize = self -> ob_descr -> itemsize ;
@@ -1488,9 +1495,14 @@ array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1488
1495
PyErr_NoMemory<
57AE
/span>();
1489
1496
return NULL ;
1490
1497
}
1498
+
1499
+
1500
+ array_state * state = get_array_state_by_class (cls );
1501
+ assert (state != NULL );
1502
+
1491
1503
nbytes = n * itemsize ;
1492
1504
1493
- b = _PyObject_CallMethod (f , & _Py_ID ( read ) , "n" , nbytes );
1505
+ b = _PyObject_CallMethod (f , state -> str_read , "n" , nbytes );
1494
1506
if (b == NULL )
1495
1507
return NULL ;
1496
1508
@@ -1521,15 +1533,16 @@ array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1521
1533
/*[clinic input]
1522
1534
array.array.tofile
1523
1535
1536
+ cls: defining_class
1524
1537
f: object
1525
1538
/
1526
1539
1527
1540
Write all items (as machine values) to the file object f.
1528
1541
[clinic start generated code]*/
1529
1542
1530
1543
static PyObject *
1531
- array_array_tofile (arrayobject * self , PyObject * f )
1532
- /*[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 ]*/
1533
1546
{
1534
1547
Py_ssize_t nbytes = Py_SIZE (self ) * self -> ob_descr -> itemsize ;
1535
1548
/* Write 64K blocks at a time */
@@ -1541,6 +1554,10 @@ array_array_tofile(arrayobject *self, PyObject *f)
1541
1554
if (Py_SIZE (self ) == 0 )
1542
1555
goto done ;
1543
1556
1557
+
1558
+ array_state * state = get_array_state_by_class (cls );
1559
+ assert (state != NULL );
1560
+
1544
1561
for (i = 0 ; i < nblocks ; i ++ ) {
1545
1562
char * ptr = self -> ob_item + i * BLOCKSIZE ;
1546
1563
Py_ssize_t size = BLOCKSIZE ;
@@ -1551,7 +1568,7 @@ array_array_tofile(arrayobject *self, PyObject *f)
1551
1568
bytes = PyBytes_FromStringAndSize (ptr , size );
1552
1569
if (bytes == NULL )
1553
1570
return NULL ;
1554
- res = PyObject_CallMethodOneArg (f , & _Py_ID ( write ) , bytes );
1571
+ res = PyObject_CallMethodOneArg (f , state -> str_write , bytes );
1555
1572
Py_DECREF (bytes );
1556
1573
if (res == NULL )
1557
1574
return NULL ;
@@ -2174,15 +2191,17 @@ array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
2174
2191
/*[clinic input]
2175
2192
array.array.__reduce_ex__
2176
2193
2194
+ cls: defining_class
2177
2195
value: object
2178
2196
/
2179
2197
2180
2198
Return state information for pickling.
2181
2199
[clinic start generated code]*/
2182
2200
2183
2201
static PyObject *
2184
- array_array___reduce_ex__ (arrayobject * self , PyObject * value )
2185
- /*[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]*/
2186
2205
{
2187
2206
PyObject * dict ;
2188
2207
PyObject * result ;
@@ -2192,13 +2211,16 @@ array_array___reduce_ex__(arrayobject *self, PyObject *value)
2192
2211
static PyObject * array_reconstructor = NULL ;
2193
2212
long protocol ;
2194
2213
2214
+ array_state * state = get_array_state_by_class (cls );
2215
+ assert (state != NULL );
2216
+
2195
2217
if (array_reconstructor == NULL ) {
2196
2218
PyObject * array_module = PyImport_ImportModule ("array" );
2197
2219
if (array_module == NULL )
2198
2220
return NULL ;
2199
2221
array_reconstructor = PyObject_GetAttr (
2200
2222
array_module ,
2201
- & _Py_ID ( _array_reconstructor ) );
2223
+ state -> str__array_reconstructor );
2202
2224
Py_DECREF (array_module );
2203
2225
if (array_reconstructor == NULL )
2204
2226
return NULL ;
@@ -2213,7 +2235,7 @@ array_array___reduce_ex__(arrayobject *self, PyObject *value)
2213
2235
if (protocol == -1 && PyErr_Occurred ())
2214
2236
return NULL ;
2215
2237
2216
- if (_PyObject_LookupAttr ((PyObject * )self , & _Py_ID ( __dict__ ) , & dict ) < 0 ) {
2238
+ if (_PyObject_LookupAttr ((PyObject * )self , state -> str___dict__ , & dict ) < 0 ) {
2217
2239
return NULL ;
2218
2240
}
2219
2241
if (dict == NULL ) {
@@ -2935,14 +2957,20 @@ arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2935
2957
/*[clinic input]
2936
2958
array.arrayiterator.__reduce__
2937
2959
2960
+ cls: defining_class
2961
+ /
2962
+
2938
2963
Return state information for pickling.
2939
2964
[clinic start generated code]*/
2940
2965
2941
2966
static PyObject *
2942
- array_arrayiterator___reduce___impl (arrayiterobject * self )
2943
- /*[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 ]*/
2944
2969
{
2945
- PyObject * func = _PyEval_GetBuiltin (& _Py_ID (iter ));
2970
+
2971
+ array_state * state = get_array_state_by_class (cls );
2972
+ assert (state != NULL );
2973
+ PyObject * func = _PyEval_GetBuiltin (state -> str_iter );
2946
2974
if (self -> ao == NULL ) {
2947
2975
return Py_BuildValue ("N(())" , func );
2948
2976
}
@@ -3006,6 +3034,11 @@ array_traverse(PyObject *module, visitproc visit, void *arg)
3006
3034
array_state * state = get_array_state (module );
3007
3035
Py_VISIT (state -> ArrayType );
3008
3036
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 );
3009
3042
return 0 ;
3010
3043
}
3011
3044
@@ -3015,6 +3048,11 @@ array_clear(PyObject *module)
3015
3048
array_state * state = get_array_state (module );
3016
3049
Py_CLEAR (state -> ArrayType );
3017
3050
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 );
3018
3056
return 0 ;
3019
3057
}
3020
3058
@@ -3038,6 +3076,15 @@ do { \
3038
3076
} \
3039
3077
} while (0)
3040
3078
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
+
3041
3088
static int
3042
3089
array_modexec (PyObject * m )
3043
3090
{
@@ -3046,6 +3093,13 @@ array_modexec(PyObject *m)
3046
3093
PyObject * typecodes ;
3047
3094
const struct arraydescr * descr ;
3048
3095
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
+
3049
3103
CREATE_TYPE (m , state -> ArrayType , & array_spec );
3050
3104
CREATE_TYPE (m , state -> ArrayIterType , & arrayiter_spec );
3051
3105
Py_SET_TYPE (state -> ArrayIterType , & PyType_Type );
0 commit comments