2
2
/* Tuple object implementation */
3
3
4
4
#include "Python.h"
5
- #include "pycore_abstract.h" // _PyIndex_Check()
6
- #include "pycore_accu .h"
7
- #include "pycore_gc .h" // _PyObject_GC_IS_TRACKED ()
8
- #include "pycore_object.h"
5
+ #include "pycore_abstract.h" // _PyIndex_Check()
6
+ #include "pycore_gc .h" // _PyObject_GC_IS_TRACKED()
7
+ #include "pycore_initconfig .h" // _PyStatus_OK ()
8
+ #include "pycore_object.h" // _PyObject_GC_TRACK()
9
9
10
10
/*[clinic input]
11
11
class tuple "PyTupleObject *" "&PyTuple_Type"
@@ -15,12 +15,14 @@ class tuple "PyTupleObject *" "&PyTuple_Type"
15
15
#include "clinic/tupleobject.c.h"
16
16
17
17
18
+ #if PyTuple_MAXSAVESIZE > 0
18
19
static struct _Py_tuple_state *
19
20
get_tuple_state (void )
20
21
{
21
22
PyInterpreterState * interp = _PyInterpreterState_GET ();
22
23
return & interp -> tuple ;
23
24
}
25
+ #endif
24
26
25
27
26
28
static inline void
@@ -55,14 +57,21 @@ _PyTuple_DebugMallocStats(FILE *out)
55
57
which wraps this function).
56
58
*/
57
59
static PyTupleObject *
58
- tuple_alloc (struct _Py_tuple_state * state , Py_ssize_t size )
60
+ tuple_alloc (Py_ssize_t size )
59
61
{
60
62
PyTupleObject * op ;
63
+ #if PyTuple_MAXSAVESIZE > 0
64
+ // If Python is built with the empty tuple singleton,
65
+ // tuple_alloc(0) must not be called.
66
+ assert (size != 0 );
67
+ #endif
61
68
if (size < 0 ) {
62
69
PyErr_BadInternalCall ();
63
70
return NULL ;
64
71
}
72
+
65
73
#if PyTuple_MAXSAVESIZE > 0
74
+ struct _Py_tuple_state * state = get_tuple_state ();
66
75
#ifdef Py_DEBUG
67
76
// tuple_alloc() must not be called after _PyTuple_Fini()
68
77
assert (state -> numfree [0 ] != -1 );
@@ -93,36 +102,65 @@ tuple_alloc(struct _Py_tuple_state *state, Py_ssize_t size)
93
102
return op ;
94
103
}
95
104
105
+ static int
106
+ tuple_create_empty_tuple_singleton (struct _Py_tuple_state * state )
107
+ {
108
+ #if PyTuple_MAXSAVESIZE > 0
109
+ assert (state -> free_list [0 ] == NULL );
110
+
111
+ PyTupleObject * op = PyObject_GC_NewVar (PyTupleObject , & PyTuple_Type , 0 );
112
+ if (op == NULL ) {
113
+ return -1 ;
114
+ }
115
+ // The empty tuple singleton is not tracked by the GC.
116
+ // It does not contain any Python object.
117
+
118
+ state -> free_list [0 ] = op ;
119
+ state -> numfree [0 ]++ ;
120
+
121
+ assert (state -> numfree [0 ] == 1 );
122
+ #endif
123
+ return 0 ;
124
+ }
125
+
126
+
127
+ static PyObject *
128
+ tuple_get_empty (void )
129
+ {
130
+ #if PyTuple_MAXSAVESIZE > 0
131
+ struct _Py_tuple_state * state = get_tuple_state ();
132
+ PyTupleObject * op = state -> free_list [0 ];
133
+ // tuple_get_empty() must not be called before _PyTuple_Init()
134
+ // or after _PyTuple_Fini()
135
+ assert (op != NULL );
136
+ #ifdef Py_DEBUG
137
+ assert (state -> numfree [0 ] != -1 );
138
+ #endif
139
+
140
+ Py_INCREF (op );
141
+ return (PyObject * ) op ;
142
+ #else
143
+ return PyTuple_New (0 );
144
+ #endif
145
+ }
146
+
147
+
96
148
PyObject *
97
149
PyTuple_New (Py_ssize_t size )
98
150
{
99
151
PyTupleObject * op ;
100
152
#if PyTuple_MAXSAVESIZE > 0
101
- struct _Py_tuple_state * state = get_tuple_state ();
102
- if (size == 0 && state -> free_list [0 ]) {
103
- op = state -> free_list [0 ];
104
- Py_INCREF (op );
105
- return (PyObject * ) op ;
153
+ if (size == 0 ) {
154
+ return tuple_get_empty ();
106
155
}
107
156
#endif
108
- op = tuple_alloc (state , size );
157
+ op = tuple_alloc (size );
109
158
if (op == NULL ) {
110
159
return NULL ;
111
160
}
112
161
for (Py_ssize_t i = 0 ; i < size ; i ++ ) {
113
162
op -> ob_item [i ] = NULL ;
114
163
}
115
- #if PyTuple_MAXSAVESIZE > 0
116
- if (size == 0 ) {
117
- #ifdef Py_DEBUG
118
- // PyTuple_New() must not be called after _PyTuple_Fini()
119
- assert (state -> numfree [0 ] != -1 );
120
- #endif
121
- state -> free_list [0 ] = op ;
122
- ++ state -> numfree [0 ];
123
- Py_INCREF (op ); /* extra INCREF so that this is never freed */
124
- }
125
- #endif
126
164
tuple_gc_track (op );
127
165
return (PyObject * ) op ;
128
166
}
@@ -203,13 +241,11 @@ PyTuple_Pack(Py_ssize_t n, ...)
203
241
va_list vargs ;
204
242
205
243
if (n == 0 ) {
206
- return PyTuple_New ( 0 );
244
+ return tuple_get_empty ( );
207
245
}
208
246
209
- struct _Py_tuple_state * state = get_tuple_state ();
210
-
211
247
va_start (vargs , n );
212
- PyTupleObject * result = tuple_alloc (state , n );
248
+ PyTupleObject * result = tuple_alloc (n );
213
249
if (result == NULL ) {
214
250
va_end (vargs );
215
251
return NULL ;
@@ -245,9 +281,9 @@ tupledealloc(PyTupleObject *op)
245
281
// tupledealloc() must not be called after _PyTuple_Fini()
246
282
assert (state -> numfree [0 ] != -1 );
247
283
#endif
248
- if (len < PyTuple_MAXSAVESIZE &&
249
- state -> numfree [len ] < PyTuple_MAXFREELIST &&
250
- Py_IS_TYPE (op , & PyTuple_Type ))
284
+ if (len < PyTuple_MAXSAVESIZE
285
+ && state -> numfree [len ] < PyTuple_MAXFREELIST
286
+ && Py_IS_TYPE (op , & PyTuple_Type ))
251
287
{
252
288
op -> ob_item [0 ] = (PyObject * ) state -> free_list [len ];
253
289
state -> numfree [len ]++ ;
@@ -257,6 +293,7 @@ tupledealloc(PyTupleObject *op)
257
293
#endif
258
294
}
259
295
Py_TYPE (op )-> tp_free ((PyObject * )op );
296
+
260
297
#if PyTuple_MAXSAVESIZE > 0
261
298
done :
262
299
#endif
@@ -423,11 +460,10 @@ PyObject *
423
460
_PyTuple_FromArray (PyObject * const * src , Py_ssize_t n )
424
461
{
425
462
if (n == 0 ) {
426
- return PyTuple_New ( 0 );
463
+ return tuple_get_empty ( );
427
464
}
428
465
429
- struct _Py_tuple_state * state = get_tuple_state ();
430
- PyTupleObject * tuple = tuple_alloc (state , n );
466
+ PyTupleObject * tuple = tuple_alloc (n );
431
467
if (tuple == NULL ) {
432
468
return NULL ;
433
469
}
@@ -494,11 +530,10 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
494
530
assert ((size_t )Py_SIZE (a ) + (size_t )Py_SIZE (b ) < PY_SSIZE_T_MAX );
495
531
size = Py_SIZE (a ) + Py_SIZE (b );
496
532
if (size == 0 ) {
497
- return PyTuple_New ( 0 );
533
+ return tuple_get_empty ( );
498
534
}
499
535
500
- struct _Py_tuple_state * state = get_tuple_state ();
501
- np = tuple_alloc (state , size );
536
+ np = tuple_alloc (size );
502
537
if (np == NULL ) {
503
538
return NULL ;
504
539
}
@@ -536,13 +571,12 @@ tuplerepeat(PyTupleObject *a, Py_ssize_t n)
536
571
}
537
572
}
538
573
if (Py_SIZE (a ) == 0 || n <= 0 ) {
539
- return PyTuple_New ( 0 );
574
+ return tuple_get_empty ( );
540
575
}
541
576
if (n > PY_SSIZE_T_MAX / Py_SIZE (a ))
542
577
return PyErr_NoMemory ();
543
578
size = Py_SIZE (a ) * n ;
544
- struct _Py_tuple_state * state = get_tuple_state ();
545
- np = tuple_alloc (state , size );
579
+ np = tuple_alloc (size );
546
580
if (np == NULL )
547
581
return NULL ;
548
582
p = np -> ob_item ;
@@ -713,10 +747,12 @@ tuple_new_impl(PyTypeObject *type, PyObject *iterable)
713
747
if (type != & PyTuple_Type )
714
748
return tuple_subtype_new (type , iterable );
715
749
716
- if (iterable == NULL )
717
- return PyTuple_New (0 );
718
- else
750
+ if (iterable == NULL ) {
751
+ return tuple_get_empty ();
752
+ }
753
+ else {
719
754
return PySequence_Tuple (iterable );
755
+ }
720
756
}
721
757
722
758
static PyObject *
@@ -735,7 +771,9 @@ tuple_vectorcall(PyObject *type, PyObject * const*args,
735
771
if (nargs ) {
736
772
return tuple_new_impl ((PyTypeObject * )type , args [0 ]);
737
773
}
738
- return PyTuple_New (0 );
774
+ else {
775
+ return tuple_get_empty ();
776
+ }
739
777
}
740
778
741
779
static PyObject *
@@ -798,7 +836,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
798
836
& stop , step );
799
837
800
838
if (slicelength <= 0 ) {
801
- return PyTuple_New ( 0 );
839
+ return tuple_get_empty ( );
802
840
}
803
841
else if (start == 0 && step == 1 &&
804
842
slicelength == PyTuple_GET_SIZE (self ) &&
@@ -807,8 +845,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
807
845
return (PyObject * )self ;
808
846
}
809
847
else {
810
- struct _Py_tuple_state * state = get_tuple_state ();
811
- PyTupleObject * result = tuple_alloc (state , slicelength );
848
+ PyTupleObject * result = tuple_alloc (slicelength );
812
849
if (!result ) return NULL ;
813
850
814
851
src = self -> ob_item ;
@@ -988,15 +1025,26 @@ _PyTuple_ClearFreeList(PyThreadState *tstate)
988
1025
#endif
989
1026
}
990
1027
1028
+
1029
+ PyStatus
1030
+ _PyTuple_Init (PyThreadState * tstate )
1031
+ {
1032
+ struct _Py_tuple_state * state = & tstate -> interp -> tuple ;
1033
+ if (tuple_create_empty_tuple_singleton (state ) < 0 ) {
1034
+ return _PyStatus_NO_MEMORY ();
1035
+ }
1036
+ return _PyStatus_OK ();
1037
+ }
1038
+
1039
+
991
1040
void
992
1041
_PyTuple_Fini (PyThreadState * tstate )
993
1042
{
994
1043
#if PyTuple_MAXSAVESIZE > 0
995
1044
struct _Py_tuple_state * state = & tstate -> interp -> tuple ;
996
- /* empty tuples are used all over the place and applications may
997
- * rely on the fact that an empty tuple is a singleton. */
1045
+ // The empty tuple singleton must not be tracked by the GC
1046
+ assert (! _PyObject_GC_IS_TRACKED ( state -> free_list [ 0 ]));
998
1047
Py_CLEAR (state -> free_list [0 ]);
999
-
1000
1048
_PyTuple_ClearFreeList (tstate );
1001
1049
#ifdef Py_DEBUG
1002
1050
state -> numfree [0 ] = -1 ;
0 commit comments