6
6
#ifndef Py_BUILD_CORE_BUILTIN
7
7
# define Py_BUILD_CORE_MODULE 1
8
8
#endif
9
- #define NEEDS_PY_IDENTIFIER
10
9
11
10
#define PY_SSIZE_T_CLEAN
12
11
#include "Python.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,13 +1478,13 @@ 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 ;
1480
1487
Py_ssize_t nbytes ;
1481
- _Py_IDENTIFIER (read );
1482
1488
int not_enough_bytes ;
1483
1489
1484
1490
if (n < 0 ) {
@@ -1489,9 +1495,14 @@ array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1489
1495
PyErr_NoMemory ();
1490
1496
return NULL ;
1491
1497
}
1498
+
1499
+
1500
+ array_state * state = get_array_state_by_class (cls );
1501
+ assert (state != NULL );
1502
+
1492
1503
nbytes = n * itemsize ;
1493
1504
1494
- b = _PyObject_CallMethodId (f , & PyId_read , "n" , nbytes );
1505
+ b = _PyObject_CallMethod (f , state -> str_read , "n" , nbytes );
1495
1506
if (b == NULL )
1496
1507
return NULL ;
1497
1508
@@ -1522,15 +1533,16 @@ array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1522
1533
/*[clinic input]
1523
1534
array.array.tofile
1524
1535
1536
+ cls: defining_class
1525
1537
f: object
1526
1538
/
1527
1539
1528
1540
Write all items (as machine values) to the file object f.
1529
1541
[clinic start generated code]*/
1530
1542
1531
1543
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 ]*/
1534
1546
{
1535
1547
Py_ssize_t nbytes = Py_SIZE (self ) * self -> ob_descr -> itemsize ;
1536
1548
/* Write 64K blocks at a time */
@@ -1542,18 +1554,21 @@ array_array_tofile(arrayobject *self, PyObject *f)
1542
1554
if (Py_SIZE (self ) == 0 )
1543
1555
goto done ;
1544
1556
1557
+
1558
+ array_state * state = get_array_state_by_class (cls );
1559
+ assert (state != NULL );
1560
+
1545
1561
for (i = 0 ; i < nblocks ; i ++ ) {
1546
1562
char * ptr = self -> ob_item + i * BLOCKSIZE ;
1547
1563
Py_ssize_t size = BLOCKSIZE ;
1548
1564
PyObject * bytes , * res ;
1549
- _Py_IDENTIFIER (write );
1550
1565
1551
1566
if (i * BLOCKSIZE + size > nbytes )
1552
1567
size = nbytes - i * BLOCKSIZE ;
1553
1568
bytes = PyBytes_FromStringAndSize (ptr , size );
1554
1569
if (bytes == NULL )
1555
1570
return NULL ;
1556
- res = _PyObject_CallMethodIdOneArg (f , & PyId_write , bytes );
1571
+ res = PyObject_CallMethodOneArg (f , state -> str_write , bytes );
1557
1572
Py_DECREF (bytes );
1558
1573
if (res == NULL )
1559
1574
return NULL ;
@@ -2176,15 +2191,17 @@ array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
2176
2191
/*[clinic input]
2177
2192
array.array.__reduce_ex__
2178
2193
2194
+ cls: defining_class
2179
2195
value: object
2180
2196
/
2181
2197
2182
2198
Return state information for pickling.
2183
2199
[clinic start generated code]*/
2184
2200
2185
2201
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]*/
2188
2205
{
2189
2206
PyObject * dict ;
2190
2207
PyObject * result ;
@@ -2193,16 +2210,17 @@ array_array___reduce_ex__(arrayobject *self, PyObject *value)
2193
2210
int mformat_code ;
2194
2211
static PyObject * array_reconstructor = NULL ;
2195
2212
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 );
2198
2216
2199
2217
if (array_reconstructor == NULL ) {
2200
2218
PyObject * array_module = PyImport_ImportModule ("array" );
2201
2219
if (array_module == NULL )
2202
2220
return NULL ;
2203
- array_reconstructor = _PyObject_GetAttrId (
2221
+ array_reconstructor = PyObject_GetAttr (
2204
2222
array_module ,
2205
- & PyId__array_reconstructor );
2223
+ state -> str__array_reconstructor );
2206
2224
Py_DECREF (array_module );
2207
2225
if (array_reconstructor == NULL )
2208
2226
return NULL ;
@@ -2217,7 +2235,7 @@ array_array___reduce_ex__(arrayobject *self, PyObject *value)
2217
2235
if (protocol == -1 && PyErr_Occurred ())
2218
2236
return NULL ;
2219
2237
2220
- if (_PyObject_LookupAttrId ((PyObject * )self , & PyId___dict__ , & dict ) < 0 ) {
2238
+ if (_PyObject_LookupAttr ((PyObject * )self , state -> str___dict__ , & dict ) < 0 ) {
2221
2239
return NULL ;
2222
2240
}
2223
2241
if (dict == NULL ) {
@@ -2939,15 +2957,20 @@ arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2939
2957
/*[clinic input]
2940
2958
array.arrayiterator.__reduce__
2941
2959
2960
+ cls: defining_class
2961
+ /
2962
+
2942
2963
Return state information for pickling.
2943
2964
[clinic start generated code]*/
2944
2965
2945
2966
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 ]*/
2948
2969
{
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 );
2951
2974
if (self -> ao == NULL ) {
2952
2975
return Py_BuildValue ("N(())" , func );
2953
2976
}
@@ -3011,6 +3034,11 @@ array_traverse(PyObject *module, visitproc visit, void *arg)
3011
3034
array_state * state = get_array_state (module );
3012
3035
Py_VISIT (state -> ArrayType );
3013
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 );
3014
3042
return 0 ;
3015
3043
}
3016
3044
@@ -3020,6 +3048,11 @@ array_clear(PyObject *module)
3020
3048
array_state * state = get_array_state (module );
3021
3049
Py_CLEAR (state -> ArrayType );
3022
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 );
3023
3056
return 0 ;
3024
3057
}
3025
3058
@@ -3043,6 +3076,15 @@ do { \
3043
3076
} \
3044
3077
} while (0)
3045
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
+
3046
3088
static int
3047
3089
array_modexec (PyObject * m )
3048
3090
{
@@ -3051,6 +3093,13 @@ array_modexec(PyObject *m)
3051
3093
PyObject * typecodes ;
3052
3094
const struct arraydescr * descr ;
3053
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
+
3054
3103
CREATE_TYPE (m , state -> ArrayType , & array_spec );
3055
3104
CREATE_TYPE (m , state -> ArrayIterType , & arrayiter_spec );
3056
3105
Py_SET_TYPE (state -> ArrayIterType , & PyType_Type );
0 commit comments