blob: f4f9aa259e8b2134c96e297722ef965162fa2b96 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Tuple object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinnera15e2602020-04-08 02:01:56 +02005#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinnere281f7d2018-11-01 02:30:36 +01006#include "pycore_accu.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02007#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
8#include "pycore_object.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Serhiy Storchaka0b561592017-03-19 08:47:58 +020010/*[clinic input]
11class tuple "PyTupleObject *" "&PyTuple_Type"
12[clinic start generated code]*/
13/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/
14
15#include "clinic/tupleobject.c.h"
16
Victor Stinner522691c2020-06-23 16:40:40 +020017
18static struct _Py_tuple_state *
19get_tuple_state(void)
20{
21 PyInterpreterState *interp = _PyInterpreterState_GET();
22 return &interp->tuple;
23}
24
25
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050026static inline void
27tuple_gc_track(PyTupleObject *op)
28{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050029 _PyObject_GC_TRACK(op);
30}
31
Victor Stinner522691c2020-06-23 16:40:40 +020032
David Malcolm49526f42012-06-22 14:55:41 -040033/* Print summary info about the state of the optimized allocator */
34void
35_PyTuple_DebugMallocStats(FILE *out)
36{
37#if PyTuple_MAXSAVESIZE > 0
Victor Stinner522691c2020-06-23 16:40:40 +020038 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +020039 for (int i = 1; i < PyTuple_MAXSAVESIZE; i++) {
40 char buf[128];
David Malcolm49526f42012-06-22 14:55:41 -040041 PyOS_snprintf(buf, sizeof(buf),
42 "free %d-sized PyTupleObject", i);
Victor Stinner69ac6e52020-06-04 23:38:36 +020043 _PyDebugAllocatorStats(out, buf, state->numfree[i],
44 _PyObject_VAR_SIZE(&PyTuple_Type, i));
David Malcolm49526f42012-06-22 14:55:41 -040045 }
46#endif
47}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000048
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050049/* Allocate an uninitialized tuple object. Before making it public following
50 steps must be done:
51 - initialize its items
52 - call tuple_gc_track() on it
53 Because the empty tuple is always reused and it's already tracked by GC,
54 this function must not be called with size == 0 (unless from PyTuple_New()
55 which wraps this function).
56*/
57static PyTupleObject *
Victor Stinner69ac6e52020-06-04 23:38:36 +020058tuple_alloc(struct _Py_tuple_state *state, Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020060 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 if (size < 0) {
62 PyErr_BadInternalCall();
63 return NULL;
64 }
Christian Heimes2202f872008-02-06 14:31:34 +000065#if PyTuple_MAXSAVESIZE > 0
Victor Stinnerbcb19832020-06-08 02:14:47 +020066#ifdef Py_DEBUG
67 // tuple_alloc() must not be called after _PyTuple_Fini()
68 assert(state->numfree[0] != -1);
69#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +020070 if (size < PyTuple_MAXSAVESIZE && (op = state->free_list[size]) != NULL) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050071 assert(size != 0);
Victor Stinner69ac6e52020-06-04 23:38:36 +020072 state->free_list[size] = (PyTupleObject *) op->ob_item[0];
73 state->numfree[size]--;
Victor Stinner04fc4f22020-06-16 01:28:07 +020074 /* Inlined _PyObject_InitVar() without _PyType_HasFeature() test */
Guido van Rossum68055ce1998-12-11 14:56:38 +000075#ifdef Py_TRACE_REFS
Victor Stinnerfe2978b2020-05-27 14:55:10 +020076 Py_SET_SIZE(op, size);
Dong-hee Na7d847e22020-05-26 02:25:28 +090077 Py_SET_TYPE(op, &PyTuple_Type);
Guido van Rossum68055ce1998-12-11 14:56:38 +000078#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 _Py_NewReference((PyObject *)op);
80 }
81 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000082#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 /* Check for overflow */
Sergey Fedoseev755d4ef2019-09-10 01:40:58 +050085 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
86 sizeof(PyObject *))) / sizeof(PyObject *)) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050087 return (PyTupleObject *)PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
90 if (op == NULL)
91 return NULL;
92 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050093 return op;
94}
95
96PyObject *
97PyTuple_New(Py_ssize_t size)
98{
99 PyTupleObject *op;
100#if PyTuple_MAXSAVESIZE > 0
Victor Stinner522691c2020-06-23 16:40:40 +0200101 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +0200102 if (size == 0 && state->free_list[0]) {
103 op = state->free_list[0];
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500104 Py_INCREF(op);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500105 return (PyObject *) op;
106 }
107#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +0200108 op = tuple_alloc(state, size);
Zackery Spytz60bd1f82019-09-04 07:58:05 -0600109 if (op == NULL) {
110 return NULL;
111 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500112 for (Py_ssize_t i = 0; i < size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 op->ob_item[i] = NULL;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500114 }
Christian Heimes2202f872008-02-06 14:31:34 +0000115#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (size == 0) {
Victor Stinnerbcb19832020-06-08 02:14:47 +0200117#ifdef Py_DEBUG
118 // PyTuple_New() must not be called after _PyTuple_Fini()
119 assert(state->numfree[0] != -1);
120#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +0200121 state->free_list[0] = op;
122 ++state->numfree[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 Py_INCREF(op); /* extra INCREF so that this is never freed */
124 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000125#endif
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500126 tuple_gc_track(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128}
129
Martin v. Löwis18e16552006-02-15 17:27:45 +0000130Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200131PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 if (!PyTuple_Check(op)) {
134 PyErr_BadInternalCall();
135 return -1;
136 }
137 else
138 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139}
140
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (!PyTuple_Check(op)) {
145 PyErr_BadInternalCall();
146 return NULL;
147 }
148 if (i < 0 || i >= Py_SIZE(op)) {
149 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
150 return NULL;
151 }
152 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153}
154
155int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200156PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200158 PyObject **p;
Victor Stinnera93c51e2020-02-07 00:38:59 +0100159 if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 Py_XDECREF(newitem);
161 PyErr_BadInternalCall();
162 return -1;
163 }
164 if (i < 0 || i >= Py_SIZE(op)) {
165 Py_XDECREF(newitem);
166 PyErr_SetString(PyExc_IndexError,
167 "tuple assignment index out of range");
168 return -1;
169 }
170 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300171 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173}
174
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000175void
176_PyTuple_MaybeUntrack(PyObject *op)
177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 PyTupleObject *t;
179 Py_ssize_t i, n;
180
181 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
182 return;
183 t = (PyTupleObject *) op;
184 n = Py_SIZE(t);
185 for (i = 0; i < n; i++) {
186 PyObject *elt = PyTuple_GET_ITEM(t, i);
187 /* Tuple with NULL elements aren't
188 fully constructed, don't untrack
189 them yet. */
190 if (!elt ||
191 _PyObject_GC_MAY_BE_TRACKED(elt))
192 return;
193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000195}
196
Raymond Hettingercb2da432003-10-12 18:24:34 +0000197PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000198PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 Py_ssize_t i;
201 PyObject *o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyObject **items;
203 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000204
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500205 if (n == 0) {
206 return PyTuple_New(0);
207 }
208
Victor Stinner522691c2020-06-23 16:40:40 +0200209 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +0200210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 va_start(vargs, n);
Victor Stinner69ac6e52020-06-04 23:38:36 +0200212 PyTupleObject *result = tuple_alloc(state, n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200213 if (result == NULL) {
214 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200216 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500217 items = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 for (i = 0; i < n; i++) {
219 o = va_arg(vargs, PyObject *);
220 Py_INCREF(o);
221 items[i] = o;
222 }
223 va_end(vargs);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500224 tuple_gc_track(result);
225 return (PyObject *)result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000226}
227
228
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229/* Methods */
230
231static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200232tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200234 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200236 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if (len > 0) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200238 Py_ssize_t i = len;
239 while (--i >= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 Py_XDECREF(op->ob_item[i]);
Victor Stinner69ac6e52020-06-04 23:38:36 +0200241 }
Christian Heimes2202f872008-02-06 14:31:34 +0000242#if PyTuple_MAXSAVESIZE > 0
Victor Stinner522691c2020-06-23 16:40:40 +0200243 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinnerbcb19832020-06-08 02:14:47 +0200244#ifdef Py_DEBUG
245 // tupledealloc() must not be called after _PyTuple_Fini()
246 assert(state->numfree[0] != -1);
247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (len < PyTuple_MAXSAVESIZE &&
Victor Stinner69ac6e52020-06-04 23:38:36 +0200249 state->numfree[len] < PyTuple_MAXFREELIST &&
Andy Lesterdffe4c02020-03-04 07:15:20 -0600250 Py_IS_TYPE(op, &PyTuple_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200252 op->ob_item[0] = (PyObject *) state->free_list[len];
253 state->numfree[len]++;
254 state->free_list[len] = op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 goto done; /* return */
256 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 }
259 Py_TYPE(op)->tp_free((PyObject *)op);
Victor Stinnerb4b53862020-05-05 19:55:29 +0200260#if PyTuple_MAXSAVESIZE > 0
Guido van Rossumd724b232000-03-13 16:01:29 +0000261done:
Victor Stinnerb4b53862020-05-05 19:55:29 +0200262#endif
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200263 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264}
265
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000267tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100270 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 n = Py_SIZE(v);
273 if (n == 0)
274 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 /* While not mutable, it is still possible to end up with a cycle in a
277 tuple through an object that stores itself within a tuple (and thus
278 infinitely asks for the repr of itself). This should only be
279 possible within a type. */
280 i = Py_ReprEnter((PyObject *)v);
281 if (i != 0) {
282 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
283 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000284
Victor Stinner88a9cd92013-11-19 12:59:46 +0100285 _PyUnicodeWriter_Init(&writer);
286 writer.overallocate = 1;
287 if (Py_SIZE(v) > 1) {
288 /* "(" + "1" + ", 2" * (len - 1) + ")" */
289 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
290 }
291 else {
292 /* "(1,)" */
293 writer.min_length = 4;
294 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200295
Victor Stinner88a9cd92013-11-19 12:59:46 +0100296 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200297 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 /* Do repr() on each element. */
300 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100301 PyObject *s;
302
303 if (i > 0) {
304 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
305 goto error;
306 }
307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100309 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200310 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100311
312 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
313 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200314 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100315 }
316 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100318
319 writer.overallocate = 0;
320 if (n > 1) {
321 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
322 goto error;
323 }
324 else {
325 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
326 goto error;
327 }
Tim Petersa7259592001-06-16 05:11:17 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100330 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200331
332error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100333 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200334 Py_ReprLeave((PyObject *)v);
335 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336}
337
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000338
jdemeyeraeb1be52018-10-28 02:06:38 +0200339/* Hash for tuples. This is a slightly simplified version of the xxHash
340 non-cryptographic hash:
341 - we do not use any parallellism, there is only 1 accumulator.
342 - we drop the final mixing since this is just a permutation of the
343 output space: it does not help against collisions.
344 - at the end, we mangle the length with a single constant.
345 For the xxHash specification, see
346 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100347
jdemeyeraeb1be52018-10-28 02:06:38 +0200348 Below are the official constants from the xxHash specification. Optimizing
349 compilers should emit a single "rotate" instruction for the
350 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
351 platform, the macro could be changed to expand to a platform-specific rotate
352 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000353*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200354#if SIZEOF_PY_UHASH_T > 4
355#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
356#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
357#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
358#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
359#else
360#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
361#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
362#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
363#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
364#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000365
jdemeyeraeb1be52018-10-28 02:06:38 +0200366/* Tests have shown that it's not worth to cache the hash value, see
367 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000368static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000369tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000370{
jdemeyeraeb1be52018-10-28 02:06:38 +0200371 Py_ssize_t i, len = Py_SIZE(v);
372 PyObject **item = v->ob_item;
373
374 Py_uhash_t acc = _PyHASH_XXPRIME_5;
375 for (i = 0; i < len; i++) {
376 Py_uhash_t lane = PyObject_Hash(item[i]);
377 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200379 }
380 acc += lane * _PyHASH_XXPRIME_2;
381 acc = _PyHASH_XXROTATE(acc);
382 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200384
385 /* Add input length, mangled to keep the historical value of hash(()). */
386 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
387
388 if (acc == (Py_uhash_t)-1) {
389 return 1546275796;
390 }
391 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000392}
393
Martin v. Löwis18e16552006-02-15 17:27:45 +0000394static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000395tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000400static int
Fred Drakeba096332000-07-09 07:04:36 +0000401tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 Py_ssize_t i;
404 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300407 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000409}
410
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000411static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200412tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (i < 0 || i >= Py_SIZE(a)) {
415 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
416 return NULL;
417 }
418 Py_INCREF(a->ob_item[i]);
419 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420}
421
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500422PyObject *
423_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
424{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500425 if (n == 0) {
426 return PyTuple_New(0);
427 }
428
Victor Stinner522691c2020-06-23 16:40:40 +0200429 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +0200430 PyTupleObject *tuple = tuple_alloc(state, n);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500431 if (tuple == NULL) {
432 return NULL;
433 }
434 PyObject **dst = tuple->ob_item;
435 for (Py_ssize_t i = 0; i < n; i++) {
436 PyObject *item = src[i];
437 Py_INCREF(item);
438 dst[i] = item;
439 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500440 tuple_gc_track(tuple);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500441 return (PyObject *)tuple;
442}
443
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200445tupleslice(PyTupleObject *a, Py_ssize_t ilow,
446 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (ilow < 0)
449 ilow = 0;
450 if (ihigh > Py_SIZE(a))
451 ihigh = Py_SIZE(a);
452 if (ihigh < ilow)
453 ihigh = ilow;
454 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
455 Py_INCREF(a);
456 return (PyObject *)a;
457 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500458 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459}
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (op == NULL || !PyTuple_Check(op)) {
465 PyErr_BadInternalCall();
466 return NULL;
467 }
468 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200472tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200474 Py_ssize_t size;
475 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyObject **src, **dest;
477 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200478 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
479 Py_INCREF(bb);
480 return bb;
481 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (!PyTuple_Check(bb)) {
483 PyErr_Format(PyExc_TypeError,
484 "can only concatenate tuple (not \"%.200s\") to tuple",
485 Py_TYPE(bb)->tp_name);
486 return NULL;
487 }
Victor Stinner69ac6e52020-06-04 23:38:36 +0200488 PyTupleObject *b = (PyTupleObject *)bb;
489
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200490 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
491 Py_INCREF(a);
492 return (PyObject *)a;
493 }
Sergey Fedoseeve682b262020-05-25 19:54:40 +0500494 assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
Martin Panterb93d8632016-07-25 02:39:20 +0000495 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500496 if (size == 0) {
497 return PyTuple_New(0);
498 }
499
Victor Stinner522691c2020-06-23 16:40:40 +0200500 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +0200501 np = tuple_alloc(state, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (np == NULL) {
503 return NULL;
504 }
505 src = a->ob_item;
506 dest = np->ob_item;
507 for (i = 0; i < Py_SIZE(a); i++) {
508 PyObject *v = src[i];
509 Py_INCREF(v);
510 dest[i] = v;
511 }
512 src = b->ob_item;
513 dest = np->ob_item + Py_SIZE(a);
514 for (i = 0; i < Py_SIZE(b); i++) {
515 PyObject *v = src[i];
516 Py_INCREF(v);
517 dest[i] = v;
518 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500519 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521}
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 Py_ssize_t i, j;
527 Py_ssize_t size;
528 PyTupleObject *np;
529 PyObject **p, **items;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (Py_SIZE(a) == 0 || n == 1) {
531 if (PyTuple_CheckExact(a)) {
532 /* Since tuples are immutable, we can return a shared
533 copy in this case */
534 Py_INCREF(a);
535 return (PyObject *)a;
536 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500537 }
538 if (Py_SIZE(a) == 0 || n <= 0) {
539 return PyTuple_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100541 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100543 size = Py_SIZE(a) * n;
Victor Stinner522691c2020-06-23 16:40:40 +0200544 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +0200545 np = tuple_alloc(state, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (np == NULL)
547 return NULL;
548 p = np->ob_item;
549 items = a->ob_item;
550 for (i = 0; i < n; i++) {
551 for (j = 0; j < Py_SIZE(a); j++) {
552 *p = items[j];
553 Py_INCREF(*p);
554 p++;
555 }
556 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500557 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000559}
560
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200561/*[clinic input]
562tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000563
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200564 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300565 start: slice_index(accept={int}) = 0
566 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200567 /
568
569Return first index of value.
570
571Raises ValueError if the value is not present.
572[clinic start generated code]*/
573
574static PyObject *
575tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
576 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300577/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200578{
579 Py_ssize_t i;
580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (start < 0) {
582 start += Py_SIZE(self);
583 if (start < 0)
584 start = 0;
585 }
586 if (stop < 0) {
587 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200589 else if (stop > Py_SIZE(self)) {
590 stop = Py_SIZE(self);
591 }
592 for (i = start; i < stop; i++) {
593 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 if (cmp > 0)
595 return PyLong_FromSsize_t(i);
596 else if (cmp < 0)
597 return NULL;
598 }
599 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
600 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000601}
602
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200603/*[clinic input]
604tuple.count
605
606 value: object
607 /
608
609Return number of occurrences of value.
610[clinic start generated code]*/
611
Raymond Hettinger65baa342008-02-07 00:41:02 +0000612static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200613tuple_count(PyTupleObject *self, PyObject *value)
614/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 Py_ssize_t count = 0;
617 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200620 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (cmp > 0)
622 count++;
623 else if (cmp < 0)
624 return NULL;
625 }
626 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000627}
628
Jeremy Hylton8caad492000-06-23 14:18:11 +0000629static int
630tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 for (i = Py_SIZE(o); --i >= 0; )
635 Py_VISIT(o->ob_item[i]);
636 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000637}
638
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639static PyObject *
640tuplerichcompare(PyObject *v, PyObject *w, int op)
641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyTupleObject *vt, *wt;
643 Py_ssize_t i;
644 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000645
Brian Curtindfc80e32011-08-10 20:28:54 -0500646 if (!PyTuple_Check(v) || !PyTuple_Check(w))
647 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 vt = (PyTupleObject *)v;
650 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 vlen = Py_SIZE(vt);
653 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 /* Note: the corresponding code for lists has an "early out" test
656 * here when op is EQ or NE and the lengths differ. That pays there,
657 * but Tim was unable to find any real code where EQ/NE tuple
658 * compares don't have the same length, so testing for it here would
659 * have cost without benefit.
660 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 /* Search for the first index where items are different.
663 * Note that because tuples are immutable, it's safe to reuse
664 * vlen and wlen across the comparison calls.
665 */
666 for (i = 0; i < vlen && i < wlen; i++) {
667 int k = PyObject_RichCompareBool(vt->ob_item[i],
668 wt->ob_item[i], Py_EQ);
669 if (k < 0)
670 return NULL;
671 if (!k)
672 break;
673 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 if (i >= vlen || i >= wlen) {
676 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100677 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 /* We have an item that differs -- shortcuts for EQ/NE */
681 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200682 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 }
684 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200685 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 /* Compare the final item again using the proper operator */
689 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000690}
691
Jeremy Hylton938ace62002-07-17 16:30:39 +0000692static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200693tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
694
695/*[clinic input]
696@classmethod
697tuple.__new__ as tuple_new
698 iterable: object(c_default="NULL") = ()
699 /
700
701Built-in immutable sequence.
702
703If no argument is given, the constructor returns an empty tuple.
704If iterable is specified the tuple is initialized from iterable's items.
705
706If the argument is a tuple, the return value is the same object.
707[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000708
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200710tuple_new_impl(PyTypeObject *type, PyObject *iterable)
711/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200714 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000715
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200716 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 return PyTuple_New(0);
718 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200719 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720}
721
Guido van Rossumae960af2001-08-30 03:11:59 +0000722static PyObject *
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900723tuple_vectorcall(PyObject *type, PyObject * const*args,
724 size_t nargsf, PyObject *kwnames)
725{
Dong-hee Na87ec86c2020-03-16 23:06:20 +0900726 if (!_PyArg_NoKwnames("tuple", kwnames)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900727 return NULL;
728 }
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900729
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900730 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900731 if (!_PyArg_CheckPositional("tuple", nargs, 0, 1)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900732 return NULL;
733 }
734
735 if (nargs) {
736 return tuple_new_impl((PyTypeObject *)type, args[0]);
737 }
738 return PyTuple_New(0);
739}
740
741static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200742tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 PyObject *tmp, *newobj, *item;
745 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200748 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 if (tmp == NULL)
750 return NULL;
751 assert(PyTuple_Check(tmp));
752 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
Hai Shic81609e2020-03-16 03:37:49 +0800753 if (newobj == NULL) {
754 Py_DECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 return NULL;
Hai Shic81609e2020-03-16 03:37:49 +0800756 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 for (i = 0; i < n; i++) {
758 item = PyTuple_GET_ITEM(tmp, i);
759 Py_INCREF(item);
760 PyTuple_SET_ITEM(newobj, i, item);
761 }
762 Py_DECREF(tmp);
763 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000764}
765
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 (lenfunc)tuplelength, /* sq_length */
768 (binaryfunc)tupleconcat, /* sq_concat */
769 (ssizeargfunc)tuplerepeat, /* sq_repeat */
770 (ssizeargfunc)tupleitem, /* sq_item */
771 0, /* sq_slice */
772 0, /* sq_ass_item */
773 0, /* sq_ass_slice */
774 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775};
776
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000777static PyObject*
778tuplesubscript(PyTupleObject* self, PyObject* item)
779{
Victor Stinnera15e2602020-04-08 02:01:56 +0200780 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
782 if (i == -1 && PyErr_Occurred())
783 return NULL;
784 if (i < 0)
785 i += PyTuple_GET_SIZE(self);
786 return tupleitem(self, i);
787 }
788 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600789 Py_ssize_t start, stop, step, slicelength, i;
790 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 PyObject* it;
792 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000793
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300794 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return NULL;
796 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300797 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
798 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 if (slicelength <= 0) {
801 return PyTuple_New(0);
802 }
803 else if (start == 0 && step == 1 &&
804 slicelength == PyTuple_GET_SIZE(self) &&
805 PyTuple_CheckExact(self)) {
806 Py_INCREF(self);
807 return (PyObject *)self;
808 }
809 else {
Victor Stinner522691c2020-06-23 16:40:40 +0200810 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +0200811 PyTupleObject* result = tuple_alloc(state, slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 src = self->ob_item;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500815 dest = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 for (cur = start, i = 0; i < slicelength;
817 cur += step, i++) {
818 it = src[cur];
819 Py_INCREF(it);
820 dest[i] = it;
821 }
822
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500823 tuple_gc_track(result);
824 return (PyObject *)result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 }
826 }
827 else {
828 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400829 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 Py_TYPE(item)->tp_name);
831 return NULL;
832 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000833}
834
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200835/*[clinic input]
836tuple.__getnewargs__
837[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200839static PyObject *
840tuple___getnewargs___impl(PyTupleObject *self)
841/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
842{
843 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000844}
845
846static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200847 TUPLE___GETNEWARGS___METHODDEF
848 TUPLE_INDEX_METHODDEF
849 TUPLE_COUNT_METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -0700850 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000852};
853
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000854static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 (lenfunc)tuplelength,
856 (binaryfunc)tuplesubscript,
857 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000858};
859
Raymond Hettinger48923c52002-08-09 01:30:17 +0000860static PyObject *tuple_iter(PyObject *seq);
861
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 PyVarObject_HEAD_INIT(&PyType_Type, 0)
864 "tuple",
865 sizeof(PyTupleObject) - sizeof(PyObject *),
866 sizeof(PyObject *),
867 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200868 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 0, /* tp_getattr */
870 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200871 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 (reprfunc)tuplerepr, /* tp_repr */
873 0, /* tp_as_number */
874 &tuple_as_sequence, /* tp_as_sequence */
875 &tuple_as_mapping, /* tp_as_mapping */
876 (hashfunc)tuplehash, /* tp_hash */
877 0, /* tp_call */
878 0, /* tp_str */
879 PyObject_GenericGetAttr, /* tp_getattro */
880 0, /* tp_setattro */
881 0, /* tp_as_buffer */
882 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
883 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200884 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 (traverseproc)tupletraverse, /* tp_traverse */
886 0, /* tp_clear */
887 tuplerichcompare, /* tp_richcompare */
888 0, /* tp_weaklistoffset */
889 tuple_iter, /* tp_iter */
890 0, /* tp_iternext */
891 tuple_methods, /* tp_methods */
892 0, /* tp_members */
893 0, /* tp_getset */
894 0, /* tp_base */
895 0, /* tp_dict */
896 0, /* tp_descr_get */
897 0, /* tp_descr_set */
898 0, /* tp_dictoffset */
899 0, /* tp_init */
900 0, /* tp_alloc */
901 tuple_new, /* tp_new */
902 PyObject_GC_Del, /* tp_free */
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900903 .tp_vectorcall = tuple_vectorcall,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000905
906/* The following function breaks the notion that tuples are immutable:
907 it changes the size of a tuple. We get away with this only if there
908 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000909 as creating a new tuple object and destroying the old one, only more
910 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000911 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000912
913int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000914_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000915{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200916 PyTupleObject *v;
917 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 Py_ssize_t i;
919 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 v = (PyTupleObject *) *pv;
Andy Lester55728702020-03-06 16:53:17 -0600922 if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
924 *pv = 0;
925 Py_XDECREF(v);
926 PyErr_BadInternalCall();
927 return -1;
928 }
929 oldsize = Py_SIZE(v);
930 if (oldsize == newsize)
931 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 if (oldsize == 0) {
934 /* Empty tuples are often shared, so we should never
935 resize them in-place even if we do own the only
936 (current) reference */
937 Py_DECREF(v);
938 *pv = PyTuple_New(newsize);
939 return *pv == NULL ? -1 : 0;
940 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 /* XXX UNREF/NEWREF interface should be more symmetrical */
Victor Stinner49932fe2020-02-03 17:55:05 +0100943#ifdef Py_REF_DEBUG
944 _Py_RefTotal--;
945#endif
946 if (_PyObject_GC_IS_TRACKED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 _PyObject_GC_UNTRACK(v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100948 }
949#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 _Py_ForgetReference((PyObject *) v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100951#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 /* DECREF items deleted by shrinkage */
953 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200954 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 }
956 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
957 if (sv == NULL) {
958 *pv = NULL;
959 PyObject_GC_Del(v);
960 return -1;
961 }
962 _Py_NewReference((PyObject *) sv);
963 /* Zero out items added by growing */
964 if (newsize > oldsize)
965 memset(&sv->ob_item[oldsize], 0,
966 sizeof(*sv->ob_item) * (newsize - oldsize));
967 *pv = (PyObject *) sv;
968 _PyObject_GC_TRACK(sv);
969 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000971
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200972void
Victor Stinner69ac6e52020-06-04 23:38:36 +0200973_PyTuple_ClearFreeList(PyThreadState *tstate)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000974{
Christian Heimes2202f872008-02-06 14:31:34 +0000975#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200976 struct _Py_tuple_state *state = &tstate->interp->tuple;
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200977 for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200978 PyTupleObject *p = state->free_list[i];
979 state->free_list[i] = NULL;
980 state->numfree[i] = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 while (p) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200982 PyTupleObject *q = p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 p = (PyTupleObject *)(p->ob_item[0]);
984 PyObject_GC_Del(q);
985 }
986 }
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200987 // the empty tuple singleton is only cleared by _PyTuple_Fini()
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000988#endif
Christian Heimesa156e092008-02-16 07:38:31 +0000989}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990
Christian Heimesa156e092008-02-16 07:38:31 +0000991void
Victor Stinner69ac6e52020-06-04 23:38:36 +0200992_PyTuple_Fini(PyThreadState *tstate)
Christian Heimesa156e092008-02-16 07:38:31 +0000993{
994#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200995 struct _Py_tuple_state *state = &tstate->interp->tuple;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 /* empty tuples are used all over the place and applications may
997 * rely on the fact that an empty tuple is a singleton. */
Victor Stinner69ac6e52020-06-04 23:38:36 +0200998 Py_CLEAR(state->free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000999
Victor Stinner69ac6e52020-06-04 23:38:36 +02001000 _PyTuple_ClearFreeList(tstate);
Victor Stinnerbcb19832020-06-08 02:14:47 +02001001#ifdef Py_DEBUG
1002 state->numfree[0] = -1;
1003#endif
Christian Heimesa156e092008-02-16 07:38:31 +00001004#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001005}
Raymond Hettinger48923c52002-08-09 01:30:17 +00001006
1007/*********************** Tuple Iterator **************************/
1008
1009typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +02001011 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +00001013} tupleiterobject;
1014
Raymond Hettinger48923c52002-08-09 01:30:17 +00001015static void
1016tupleiter_dealloc(tupleiterobject *it)
1017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 _PyObject_GC_UNTRACK(it);
1019 Py_XDECREF(it->it_seq);
1020 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +00001021}
1022
1023static int
1024tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
1025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 Py_VISIT(it->it_seq);
1027 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001028}
1029
Raymond Hettinger48923c52002-08-09 01:30:17 +00001030static PyObject *
1031tupleiter_next(tupleiterobject *it)
1032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PyTupleObject *seq;
1034 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 assert(it != NULL);
1037 seq = it->it_seq;
1038 if (seq == NULL)
1039 return NULL;
1040 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1043 item = PyTuple_GET_ITEM(seq, it->it_index);
1044 ++it->it_index;
1045 Py_INCREF(item);
1046 return item;
1047 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001050 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001052}
1053
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001054static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301055tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 Py_ssize_t len = 0;
1058 if (it->it_seq)
1059 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1060 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001061}
1062
Armin Rigof5b3e362006-02-11 21:32:43 +00001063PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001064
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001065static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301066tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001067{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001068 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001069 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001070 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001071 it->it_seq, it->it_index);
1072 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001073 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001074}
1075
1076static PyObject *
1077tupleiter_setstate(tupleiterobject *it, PyObject *state)
1078{
Victor Stinner7660b882013-06-24 23:59:24 +02001079 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001080 if (index == -1 && PyErr_Occurred())
1081 return NULL;
1082 if (it->it_seq != NULL) {
1083 if (index < 0)
1084 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001085 else if (index > PyTuple_GET_SIZE(it->it_seq))
1086 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001087 it->it_index = index;
1088 }
1089 Py_RETURN_NONE;
1090}
1091
1092PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1093PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1094
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001095static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001097 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1098 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001100};
1101
Raymond Hettinger48923c52002-08-09 01:30:17 +00001102PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1104 "tuple_iterator", /* tp_name */
1105 sizeof(tupleiterobject), /* tp_basicsize */
1106 0, /* tp_itemsize */
1107 /* methods */
1108 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001109 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 0, /* tp_getattr */
1111 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001112 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 0, /* tp_repr */
1114 0, /* tp_as_number */
1115 0, /* tp_as_sequence */
1116 0, /* tp_as_mapping */
1117 0, /* tp_hash */
1118 0, /* tp_call */
1119 0, /* tp_str */
1120 PyObject_GenericGetAttr, /* tp_getattro */
1121 0, /* tp_setattro */
1122 0, /* tp_as_buffer */
1123 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1124 0, /* tp_doc */
1125 (traverseproc)tupleiter_traverse, /* tp_traverse */
1126 0, /* tp_clear */
1127 0, /* tp_richcompare */
1128 0, /* tp_weaklistoffset */
1129 PyObject_SelfIter, /* tp_iter */
1130 (iternextfunc)tupleiter_next, /* tp_iternext */
1131 tupleiter_methods, /* tp_methods */
1132 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001133};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001134
1135static PyObject *
1136tuple_iter(PyObject *seq)
1137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (!PyTuple_Check(seq)) {
1141 PyErr_BadInternalCall();
1142 return NULL;
1143 }
1144 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1145 if (it == NULL)
1146 return NULL;
1147 it->it_index = 0;
1148 Py_INCREF(seq);
1149 it->it_seq = (PyTupleObject *)seq;
1150 _PyObject_GC_TRACK(it);
1151 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152}