blob: 2ff4c48111fe0b6c972422c5c722fae555239e55 [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
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050017static inline void
18tuple_gc_track(PyTupleObject *op)
19{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050020 _PyObject_GC_TRACK(op);
21}
22
David Malcolm49526f42012-06-22 14:55:41 -040023/* Print summary info about the state of the optimized allocator */
24void
25_PyTuple_DebugMallocStats(FILE *out)
26{
27#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +020028 PyInterpreterState *interp = _PyInterpreterState_GET();
29 struct _Py_tuple_state *state = &interp->tuple;
30 for (int i = 1; i < PyTuple_MAXSAVESIZE; i++) {
31 char buf[128];
David Malcolm49526f42012-06-22 14:55:41 -040032 PyOS_snprintf(buf, sizeof(buf),
33 "free %d-sized PyTupleObject", i);
Victor Stinner69ac6e52020-06-04 23:38:36 +020034 _PyDebugAllocatorStats(out, buf, state->numfree[i],
35 _PyObject_VAR_SIZE(&PyTuple_Type, i));
David Malcolm49526f42012-06-22 14:55:41 -040036 }
37#endif
38}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000039
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050040/* Allocate an uninitialized tuple object. Before making it public following
41 steps must be done:
42 - initialize its items
43 - call tuple_gc_track() on it
44 Because the empty tuple is always reused and it's already tracked by GC,
45 this function must not be called with size == 0 (unless from PyTuple_New()
46 which wraps this function).
47*/
48static PyTupleObject *
Victor Stinner69ac6e52020-06-04 23:38:36 +020049tuple_alloc(struct _Py_tuple_state *state, Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020051 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (size < 0) {
53 PyErr_BadInternalCall();
54 return NULL;
55 }
Christian Heimes2202f872008-02-06 14:31:34 +000056#if PyTuple_MAXSAVESIZE > 0
Victor Stinnerbcb19832020-06-08 02:14:47 +020057#ifdef Py_DEBUG
58 // tuple_alloc() must not be called after _PyTuple_Fini()
59 assert(state->numfree[0] != -1);
60#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +020061 if (size < PyTuple_MAXSAVESIZE && (op = state->free_list[size]) != NULL) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050062 assert(size != 0);
Victor Stinner69ac6e52020-06-04 23:38:36 +020063 state->free_list[size] = (PyTupleObject *) op->ob_item[0];
64 state->numfree[size]--;
Victor Stinner04fc4f22020-06-16 01:28:07 +020065 /* Inlined _PyObject_InitVar() without _PyType_HasFeature() test */
Guido van Rossum68055ce1998-12-11 14:56:38 +000066#ifdef Py_TRACE_REFS
Victor Stinnerfe2978b2020-05-27 14:55:10 +020067 Py_SET_SIZE(op, size);
Dong-hee Na7d847e22020-05-26 02:25:28 +090068 Py_SET_TYPE(op, &PyTuple_Type);
Guido van Rossum68055ce1998-12-11 14:56:38 +000069#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 _Py_NewReference((PyObject *)op);
71 }
72 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 /* Check for overflow */
Sergey Fedoseev755d4ef2019-09-10 01:40:58 +050076 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
77 sizeof(PyObject *))) / sizeof(PyObject *)) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050078 return (PyTupleObject *)PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
81 if (op == NULL)
82 return NULL;
83 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050084 return op;
85}
86
87PyObject *
88PyTuple_New(Py_ssize_t size)
89{
90 PyTupleObject *op;
91#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +020092 PyInterpreterState *interp = _PyInterpreterState_GET();
93 struct _Py_tuple_state *state = &interp->tuple;
94 if (size == 0 && state->free_list[0]) {
95 op = state->free_list[0];
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050096 Py_INCREF(op);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050097 return (PyObject *) op;
98 }
99#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +0200100 op = tuple_alloc(state, size);
Zackery Spytz60bd1f82019-09-04 07:58:05 -0600101 if (op == NULL) {
102 return NULL;
103 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500104 for (Py_ssize_t i = 0; i < size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 op->ob_item[i] = NULL;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500106 }
Christian Heimes2202f872008-02-06 14:31:34 +0000107#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (size == 0) {
Victor Stinnerbcb19832020-06-08 02:14:47 +0200109#ifdef Py_DEBUG
110 // PyTuple_New() must not be called after _PyTuple_Fini()
111 assert(state->numfree[0] != -1);
112#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +0200113 state->free_list[0] = op;
114 ++state->numfree[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 Py_INCREF(op); /* extra INCREF so that this is never freed */
116 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000117#endif
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500118 tuple_gc_track(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120}
121
Martin v. Löwis18e16552006-02-15 17:27:45 +0000122Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200123PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (!PyTuple_Check(op)) {
126 PyErr_BadInternalCall();
127 return -1;
128 }
129 else
130 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131}
132
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000133PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200134PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 if (!PyTuple_Check(op)) {
137 PyErr_BadInternalCall();
138 return NULL;
139 }
140 if (i < 0 || i >= Py_SIZE(op)) {
141 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
142 return NULL;
143 }
144 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145}
146
147int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200148PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200150 PyObject **p;
Victor Stinnera93c51e2020-02-07 00:38:59 +0100151 if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 Py_XDECREF(newitem);
153 PyErr_BadInternalCall();
154 return -1;
155 }
156 if (i < 0 || i >= Py_SIZE(op)) {
157 Py_XDECREF(newitem);
158 PyErr_SetString(PyExc_IndexError,
159 "tuple assignment index out of range");
160 return -1;
161 }
162 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300163 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
166
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000167void
168_PyTuple_MaybeUntrack(PyObject *op)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 PyTupleObject *t;
171 Py_ssize_t i, n;
172
173 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
174 return;
175 t = (PyTupleObject *) op;
176 n = Py_SIZE(t);
177 for (i = 0; i < n; i++) {
178 PyObject *elt = PyTuple_GET_ITEM(t, i);
179 /* Tuple with NULL elements aren't
180 fully constructed, don't untrack
181 them yet. */
182 if (!elt ||
183 _PyObject_GC_MAY_BE_TRACKED(elt))
184 return;
185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000187}
188
Raymond Hettingercb2da432003-10-12 18:24:34 +0000189PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000190PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 Py_ssize_t i;
193 PyObject *o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyObject **items;
195 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000196
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500197 if (n == 0) {
198 return PyTuple_New(0);
199 }
200
Victor Stinner69ac6e52020-06-04 23:38:36 +0200201 PyInterpreterState *interp = _PyInterpreterState_GET();
202 struct _Py_tuple_state *state = &interp->tuple;
203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 va_start(vargs, n);
Victor Stinner69ac6e52020-06-04 23:38:36 +0200205 PyTupleObject *result = tuple_alloc(state, n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200206 if (result == NULL) {
207 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200209 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500210 items = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 for (i = 0; i < n; i++) {
212 o = va_arg(vargs, PyObject *);
213 Py_INCREF(o);
214 items[i] = o;
215 }
216 va_end(vargs);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500217 tuple_gc_track(result);
218 return (PyObject *)result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000219}
220
221
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222/* Methods */
223
224static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200225tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200227 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200229 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (len > 0) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200231 Py_ssize_t i = len;
232 while (--i >= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 Py_XDECREF(op->ob_item[i]);
Victor Stinner69ac6e52020-06-04 23:38:36 +0200234 }
Christian Heimes2202f872008-02-06 14:31:34 +0000235#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200236 PyInterpreterState *interp = _PyInterpreterState_GET();
237 struct _Py_tuple_state *state = &interp->tuple;
Victor Stinnerbcb19832020-06-08 02:14:47 +0200238#ifdef Py_DEBUG
239 // tupledealloc() must not be called after _PyTuple_Fini()
240 assert(state->numfree[0] != -1);
241#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (len < PyTuple_MAXSAVESIZE &&
Victor Stinner69ac6e52020-06-04 23:38:36 +0200243 state->numfree[len] < PyTuple_MAXFREELIST &&
Andy Lesterdffe4c02020-03-04 07:15:20 -0600244 Py_IS_TYPE(op, &PyTuple_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200246 op->ob_item[0] = (PyObject *) state->free_list[len];
247 state->numfree[len]++;
248 state->free_list[len] = op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 goto done; /* return */
250 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 }
253 Py_TYPE(op)->tp_free((PyObject *)op);
Victor Stinnerb4b53862020-05-05 19:55:29 +0200254#if PyTuple_MAXSAVESIZE > 0
Guido van Rossumd724b232000-03-13 16:01:29 +0000255done:
Victor Stinnerb4b53862020-05-05 19:55:29 +0200256#endif
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200257 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258}
259
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000261tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100264 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 n = Py_SIZE(v);
267 if (n == 0)
268 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 /* While not mutable, it is still possible to end up with a cycle in a
271 tuple through an object that stores itself within a tuple (and thus
272 infinitely asks for the repr of itself). This should only be
273 possible within a type. */
274 i = Py_ReprEnter((PyObject *)v);
275 if (i != 0) {
276 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
277 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000278
Victor Stinner88a9cd92013-11-19 12:59:46 +0100279 _PyUnicodeWriter_Init(&writer);
280 writer.overallocate = 1;
281 if (Py_SIZE(v) > 1) {
282 /* "(" + "1" + ", 2" * (len - 1) + ")" */
283 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
284 }
285 else {
286 /* "(1,)" */
287 writer.min_length = 4;
288 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200289
Victor Stinner88a9cd92013-11-19 12:59:46 +0100290 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200291 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 /* Do repr() on each element. */
294 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100295 PyObject *s;
296
297 if (i > 0) {
298 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
299 goto error;
300 }
301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100303 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200304 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100305
306 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
307 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200308 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100309 }
310 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100312
313 writer.overallocate = 0;
314 if (n > 1) {
315 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
316 goto error;
317 }
318 else {
319 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
320 goto error;
321 }
Tim Petersa7259592001-06-16 05:11:17 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100324 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200325
326error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100327 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200328 Py_ReprLeave((PyObject *)v);
329 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330}
331
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000332
jdemeyeraeb1be52018-10-28 02:06:38 +0200333/* Hash for tuples. This is a slightly simplified version of the xxHash
334 non-cryptographic hash:
335 - we do not use any parallellism, there is only 1 accumulator.
336 - we drop the final mixing since this is just a permutation of the
337 output space: it does not help against collisions.
338 - at the end, we mangle the length with a single constant.
339 For the xxHash specification, see
340 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100341
jdemeyeraeb1be52018-10-28 02:06:38 +0200342 Below are the official constants from the xxHash specification. Optimizing
343 compilers should emit a single "rotate" instruction for the
344 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
345 platform, the macro could be changed to expand to a platform-specific rotate
346 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000347*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200348#if SIZEOF_PY_UHASH_T > 4
349#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
350#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
351#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
352#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
353#else
354#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
355#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
356#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
357#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
358#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000359
jdemeyeraeb1be52018-10-28 02:06:38 +0200360/* Tests have shown that it's not worth to cache the hash value, see
361 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000362static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000363tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000364{
jdemeyeraeb1be52018-10-28 02:06:38 +0200365 Py_ssize_t i, len = Py_SIZE(v);
366 PyObject **item = v->ob_item;
367
368 Py_uhash_t acc = _PyHASH_XXPRIME_5;
369 for (i = 0; i < len; i++) {
370 Py_uhash_t lane = PyObject_Hash(item[i]);
371 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200373 }
374 acc += lane * _PyHASH_XXPRIME_2;
375 acc = _PyHASH_XXROTATE(acc);
376 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200378
379 /* Add input length, mangled to keep the historical value of hash(()). */
380 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
381
382 if (acc == (Py_uhash_t)-1) {
383 return 1546275796;
384 }
385 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000386}
387
Martin v. Löwis18e16552006-02-15 17:27:45 +0000388static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000389tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392}
393
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000394static int
Fred Drakeba096332000-07-09 07:04:36 +0000395tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_ssize_t i;
398 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300401 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000403}
404
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200406tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (i < 0 || i >= Py_SIZE(a)) {
409 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
410 return NULL;
411 }
412 Py_INCREF(a->ob_item[i]);
413 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414}
415
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500416PyObject *
417_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
418{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500419 if (n == 0) {
420 return PyTuple_New(0);
421 }
422
Victor Stinner69ac6e52020-06-04 23:38:36 +0200423 PyInterpreterState *interp = _PyInterpreterState_GET();
424 struct _Py_tuple_state *state = &interp->tuple;
425 PyTupleObject *tuple = tuple_alloc(state, n);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500426 if (tuple == NULL) {
427 return NULL;
428 }
429 PyObject **dst = tuple->ob_item;
430 for (Py_ssize_t i = 0; i < n; i++) {
431 PyObject *item = src[i];
432 Py_INCREF(item);
433 dst[i] = item;
434 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500435 tuple_gc_track(tuple);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500436 return (PyObject *)tuple;
437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200440tupleslice(PyTupleObject *a, Py_ssize_t ilow,
441 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (ilow < 0)
444 ilow = 0;
445 if (ihigh > Py_SIZE(a))
446 ihigh = Py_SIZE(a);
447 if (ihigh < ilow)
448 ihigh = ilow;
449 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
450 Py_INCREF(a);
451 return (PyObject *)a;
452 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500453 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454}
455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (op == NULL || !PyTuple_Check(op)) {
460 PyErr_BadInternalCall();
461 return NULL;
462 }
463 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000464}
465
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200467tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200469 Py_ssize_t size;
470 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 PyObject **src, **dest;
472 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200473 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
474 Py_INCREF(bb);
475 return bb;
476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (!PyTuple_Check(bb)) {
478 PyErr_Format(PyExc_TypeError,
479 "can only concatenate tuple (not \"%.200s\") to tuple",
480 Py_TYPE(bb)->tp_name);
481 return NULL;
482 }
Victor Stinner69ac6e52020-06-04 23:38:36 +0200483 PyTupleObject *b = (PyTupleObject *)bb;
484
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200485 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
486 Py_INCREF(a);
487 return (PyObject *)a;
488 }
Sergey Fedoseeve682b262020-05-25 19:54:40 +0500489 assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
Martin Panterb93d8632016-07-25 02:39:20 +0000490 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500491 if (size == 0) {
492 return PyTuple_New(0);
493 }
494
Victor Stinner69ac6e52020-06-04 23:38:36 +0200495 PyInterpreterState *interp = _PyInterpreterState_GET();
496 struct _Py_tuple_state *state = &interp->tuple;
497 np = tuple_alloc(state, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (np == NULL) {
499 return NULL;
500 }
501 src = a->ob_item;
502 dest = np->ob_item;
503 for (i = 0; i < Py_SIZE(a); i++) {
504 PyObject *v = src[i];
505 Py_INCREF(v);
506 dest[i] = v;
507 }
508 src = b->ob_item;
509 dest = np->ob_item + Py_SIZE(a);
510 for (i = 0; i < Py_SIZE(b); i++) {
511 PyObject *v = src[i];
512 Py_INCREF(v);
513 dest[i] = v;
514 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500515 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517}
518
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000520tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_ssize_t i, j;
523 Py_ssize_t size;
524 PyTupleObject *np;
525 PyObject **p, **items;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (Py_SIZE(a) == 0 || n == 1) {
527 if (PyTuple_CheckExact(a)) {
528 /* Since tuples are immutable, we can return a shared
529 copy in this case */
530 Py_INCREF(a);
531 return (PyObject *)a;
532 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500533 }
534 if (Py_SIZE(a) == 0 || n <= 0) {
535 return PyTuple_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100537 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100539 size = Py_SIZE(a) * n;
Victor Stinner69ac6e52020-06-04 23:38:36 +0200540 PyInterpreterState *interp = _PyInterpreterState_GET();
541 struct _Py_tuple_state *state = &interp->tuple;
542 np = tuple_alloc(state, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (np == NULL)
544 return NULL;
545 p = np->ob_item;
546 items = a->ob_item;
547 for (i = 0; i < n; i++) {
548 for (j = 0; j < Py_SIZE(a); j++) {
549 *p = items[j];
550 Py_INCREF(*p);
551 p++;
552 }
553 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500554 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000556}
557
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200558/*[clinic input]
559tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000560
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200561 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300562 start: slice_index(accept={int}) = 0
563 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200564 /
565
566Return first index of value.
567
568Raises ValueError if the value is not present.
569[clinic start generated code]*/
570
571static PyObject *
572tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
573 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300574/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200575{
576 Py_ssize_t i;
577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (start < 0) {
579 start += Py_SIZE(self);
580 if (start < 0)
581 start = 0;
582 }
583 if (stop < 0) {
584 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200586 else if (stop > Py_SIZE(self)) {
587 stop = Py_SIZE(self);
588 }
589 for (i = start; i < stop; i++) {
590 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (cmp > 0)
592 return PyLong_FromSsize_t(i);
593 else if (cmp < 0)
594 return NULL;
595 }
596 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
597 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000598}
599
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200600/*[clinic input]
601tuple.count
602
603 value: object
604 /
605
606Return number of occurrences of value.
607[clinic start generated code]*/
608
Raymond Hettinger65baa342008-02-07 00:41:02 +0000609static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200610tuple_count(PyTupleObject *self, PyObject *value)
611/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 Py_ssize_t count = 0;
614 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200617 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 if (cmp > 0)
619 count++;
620 else if (cmp < 0)
621 return NULL;
622 }
623 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000624}
625
Jeremy Hylton8caad492000-06-23 14:18:11 +0000626static int
627tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 for (i = Py_SIZE(o); --i >= 0; )
632 Py_VISIT(o->ob_item[i]);
633 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000634}
635
Guido van Rossumf77bc622001-01-18 00:00:53 +0000636static PyObject *
637tuplerichcompare(PyObject *v, PyObject *w, int op)
638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyTupleObject *vt, *wt;
640 Py_ssize_t i;
641 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000642
Brian Curtindfc80e32011-08-10 20:28:54 -0500643 if (!PyTuple_Check(v) || !PyTuple_Check(w))
644 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 vt = (PyTupleObject *)v;
647 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 vlen = Py_SIZE(vt);
650 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 /* Note: the corresponding code for lists has an "early out" test
653 * here when op is EQ or NE and the lengths differ. That pays there,
654 * but Tim was unable to find any real code where EQ/NE tuple
655 * compares don't have the same length, so testing for it here would
656 * have cost without benefit.
657 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 /* Search for the first index where items are different.
660 * Note that because tuples are immutable, it's safe to reuse
661 * vlen and wlen across the comparison calls.
662 */
663 for (i = 0; i < vlen && i < wlen; i++) {
664 int k = PyObject_RichCompareBool(vt->ob_item[i],
665 wt->ob_item[i], Py_EQ);
666 if (k < 0)
667 return NULL;
668 if (!k)
669 break;
670 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (i >= vlen || i >= wlen) {
673 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100674 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 /* We have an item that differs -- shortcuts for EQ/NE */
678 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200679 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 }
681 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200682 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 /* Compare the final item again using the proper operator */
686 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000687}
688
Jeremy Hylton938ace62002-07-17 16:30:39 +0000689static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200690tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
691
692/*[clinic input]
693@classmethod
694tuple.__new__ as tuple_new
695 iterable: object(c_default="NULL") = ()
696 /
697
698Built-in immutable sequence.
699
700If no argument is given, the constructor returns an empty tuple.
701If iterable is specified the tuple is initialized from iterable's items.
702
703If the argument is a tuple, the return value is the same object.
704[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000705
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200707tuple_new_impl(PyTypeObject *type, PyObject *iterable)
708/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200711 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200713 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 return PyTuple_New(0);
715 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200716 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717}
718
Guido van Rossumae960af2001-08-30 03:11:59 +0000719static PyObject *
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900720tuple_vectorcall(PyObject *type, PyObject * const*args,
721 size_t nargsf, PyObject *kwnames)
722{
Dong-hee Na87ec86c2020-03-16 23:06:20 +0900723 if (!_PyArg_NoKwnames("tuple", kwnames)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900724 return NULL;
725 }
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900726
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900727 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900728 if (!_PyArg_CheckPositional("tuple", nargs, 0, 1)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900729 return NULL;
730 }
731
732 if (nargs) {
733 return tuple_new_impl((PyTypeObject *)type, args[0]);
734 }
735 return PyTuple_New(0);
736}
737
738static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200739tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PyObject *tmp, *newobj, *item;
742 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200745 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 if (tmp == NULL)
747 return NULL;
748 assert(PyTuple_Check(tmp));
749 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
Hai Shic81609e2020-03-16 03:37:49 +0800750 if (newobj == NULL) {
751 Py_DECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 return NULL;
Hai Shic81609e2020-03-16 03:37:49 +0800753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 for (i = 0; i < n; i++) {
755 item = PyTuple_GET_ITEM(tmp, i);
756 Py_INCREF(item);
757 PyTuple_SET_ITEM(newobj, i, item);
758 }
759 Py_DECREF(tmp);
760 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000761}
762
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 (lenfunc)tuplelength, /* sq_length */
765 (binaryfunc)tupleconcat, /* sq_concat */
766 (ssizeargfunc)tuplerepeat, /* sq_repeat */
767 (ssizeargfunc)tupleitem, /* sq_item */
768 0, /* sq_slice */
769 0, /* sq_ass_item */
770 0, /* sq_ass_slice */
771 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772};
773
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000774static PyObject*
775tuplesubscript(PyTupleObject* self, PyObject* item)
776{
Victor Stinnera15e2602020-04-08 02:01:56 +0200777 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
779 if (i == -1 && PyErr_Occurred())
780 return NULL;
781 if (i < 0)
782 i += PyTuple_GET_SIZE(self);
783 return tupleitem(self, i);
784 }
785 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600786 Py_ssize_t start, stop, step, slicelength, i;
787 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject* it;
789 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000790
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300791 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 return NULL;
793 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300794 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
795 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 if (slicelength <= 0) {
798 return PyTuple_New(0);
799 }
800 else if (start == 0 && step == 1 &&
801 slicelength == PyTuple_GET_SIZE(self) &&
802 PyTuple_CheckExact(self)) {
803 Py_INCREF(self);
804 return (PyObject *)self;
805 }
806 else {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200807 PyInterpreterState *interp = _PyInterpreterState_GET();
808 struct _Py_tuple_state *state = &interp->tuple;
809 PyTupleObject* result = tuple_alloc(state, slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 src = self->ob_item;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500813 dest = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 for (cur = start, i = 0; i < slicelength;
815 cur += step, i++) {
816 it = src[cur];
817 Py_INCREF(it);
818 dest[i] = it;
819 }
820
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500821 tuple_gc_track(result);
822 return (PyObject *)result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 }
824 }
825 else {
826 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400827 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 Py_TYPE(item)->tp_name);
829 return NULL;
830 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000831}
832
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200833/*[clinic input]
834tuple.__getnewargs__
835[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200837static PyObject *
838tuple___getnewargs___impl(PyTupleObject *self)
839/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
840{
841 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000842}
843
844static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200845 TUPLE___GETNEWARGS___METHODDEF
846 TUPLE_INDEX_METHODDEF
847 TUPLE_COUNT_METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -0700848 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000850};
851
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000852static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 (lenfunc)tuplelength,
854 (binaryfunc)tuplesubscript,
855 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000856};
857
Raymond Hettinger48923c52002-08-09 01:30:17 +0000858static PyObject *tuple_iter(PyObject *seq);
859
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 PyVarObject_HEAD_INIT(&PyType_Type, 0)
862 "tuple",
863 sizeof(PyTupleObject) - sizeof(PyObject *),
864 sizeof(PyObject *),
865 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200866 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 0, /* tp_getattr */
868 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200869 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 (reprfunc)tuplerepr, /* tp_repr */
871 0, /* tp_as_number */
872 &tuple_as_sequence, /* tp_as_sequence */
873 &tuple_as_mapping, /* tp_as_mapping */
874 (hashfunc)tuplehash, /* tp_hash */
875 0, /* tp_call */
876 0, /* tp_str */
877 PyObject_GenericGetAttr, /* tp_getattro */
878 0, /* tp_setattro */
879 0, /* tp_as_buffer */
880 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
881 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200882 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 (traverseproc)tupletraverse, /* tp_traverse */
884 0, /* tp_clear */
885 tuplerichcompare, /* tp_richcompare */
886 0, /* tp_weaklistoffset */
887 tuple_iter, /* tp_iter */
888 0, /* tp_iternext */
889 tuple_methods, /* tp_methods */
890 0, /* tp_members */
891 0, /* tp_getset */
892 0, /* tp_base */
893 0, /* tp_dict */
894 0, /* tp_descr_get */
895 0, /* tp_descr_set */
896 0, /* tp_dictoffset */
897 0, /* tp_init */
898 0, /* tp_alloc */
899 tuple_new, /* tp_new */
900 PyObject_GC_Del, /* tp_free */
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900901 .tp_vectorcall = tuple_vectorcall,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000903
904/* The following function breaks the notion that tuples are immutable:
905 it changes the size of a tuple. We get away with this only if there
906 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000907 as creating a new tuple object and destroying the old one, only more
908 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000909 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000910
911int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000912_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200914 PyTupleObject *v;
915 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 Py_ssize_t i;
917 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 v = (PyTupleObject *) *pv;
Andy Lester55728702020-03-06 16:53:17 -0600920 if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
922 *pv = 0;
923 Py_XDECREF(v);
924 PyErr_BadInternalCall();
925 return -1;
926 }
927 oldsize = Py_SIZE(v);
928 if (oldsize == newsize)
929 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 if (oldsize == 0) {
932 /* Empty tuples are often shared, so we should never
933 resize them in-place even if we do own the only
934 (current) reference */
935 Py_DECREF(v);
936 *pv = PyTuple_New(newsize);
937 return *pv == NULL ? -1 : 0;
938 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 /* XXX UNREF/NEWREF interface should be more symmetrical */
Victor Stinner49932fe2020-02-03 17:55:05 +0100941#ifdef Py_REF_DEBUG
942 _Py_RefTotal--;
943#endif
944 if (_PyObject_GC_IS_TRACKED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 _PyObject_GC_UNTRACK(v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100946 }
947#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 _Py_ForgetReference((PyObject *) v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100949#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* DECREF items deleted by shrinkage */
951 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200952 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 }
954 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
955 if (sv == NULL) {
956 *pv = NULL;
957 PyObject_GC_Del(v);
958 return -1;
959 }
960 _Py_NewReference((PyObject *) sv);
961 /* Zero out items added by growing */
962 if (newsize > oldsize)
963 memset(&sv->ob_item[oldsize], 0,
964 sizeof(*sv->ob_item) * (newsize - oldsize));
965 *pv = (PyObject *) sv;
966 _PyObject_GC_TRACK(sv);
967 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000968}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000969
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200970void
Victor Stinner69ac6e52020-06-04 23:38:36 +0200971_PyTuple_ClearFreeList(PyThreadState *tstate)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000972{
Christian Heimes2202f872008-02-06 14:31:34 +0000973#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200974 struct _Py_tuple_state *state = &tstate->interp->tuple;
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200975 for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200976 PyTupleObject *p = state->free_list[i];
977 state->free_list[i] = NULL;
978 state->numfree[i] = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 while (p) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200980 PyTupleObject *q = p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 p = (PyTupleObject *)(p->ob_item[0]);
982 PyObject_GC_Del(q);
983 }
984 }
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200985 // the empty tuple singleton is only cleared by _PyTuple_Fini()
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000986#endif
Christian Heimesa156e092008-02-16 07:38:31 +0000987}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988
Christian Heimesa156e092008-02-16 07:38:31 +0000989void
Victor Stinner69ac6e52020-06-04 23:38:36 +0200990_PyTuple_Fini(PyThreadState *tstate)
Christian Heimesa156e092008-02-16 07:38:31 +0000991{
992#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200993 struct _Py_tuple_state *state = &tstate->interp->tuple;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 /* empty tuples are used all over the place and applications may
995 * rely on the fact that an empty tuple is a singleton. */
Victor Stinner69ac6e52020-06-04 23:38:36 +0200996 Py_CLEAR(state->free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000997
Victor Stinner69ac6e52020-06-04 23:38:36 +0200998 _PyTuple_ClearFreeList(tstate);
Victor Stinnerbcb19832020-06-08 02:14:47 +0200999#ifdef Py_DEBUG
1000 state->numfree[0] = -1;
1001#endif
Christian Heimesa156e092008-02-16 07:38:31 +00001002#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001003}
Raymond Hettinger48923c52002-08-09 01:30:17 +00001004
1005/*********************** Tuple Iterator **************************/
1006
1007typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +02001009 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +00001011} tupleiterobject;
1012
Raymond Hettinger48923c52002-08-09 01:30:17 +00001013static void
1014tupleiter_dealloc(tupleiterobject *it)
1015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 _PyObject_GC_UNTRACK(it);
1017 Py_XDECREF(it->it_seq);
1018 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +00001019}
1020
1021static int
1022tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
1023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 Py_VISIT(it->it_seq);
1025 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001026}
1027
Raymond Hettinger48923c52002-08-09 01:30:17 +00001028static PyObject *
1029tupleiter_next(tupleiterobject *it)
1030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyTupleObject *seq;
1032 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 assert(it != NULL);
1035 seq = it->it_seq;
1036 if (seq == NULL)
1037 return NULL;
1038 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1041 item = PyTuple_GET_ITEM(seq, it->it_index);
1042 ++it->it_index;
1043 Py_INCREF(item);
1044 return item;
1045 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001048 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001050}
1051
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001052static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301053tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 Py_ssize_t len = 0;
1056 if (it->it_seq)
1057 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1058 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001059}
1060
Armin Rigof5b3e362006-02-11 21:32:43 +00001061PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001062
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001063static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301064tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001065{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001066 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001067 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001068 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001069 it->it_seq, it->it_index);
1070 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001071 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001072}
1073
1074static PyObject *
1075tupleiter_setstate(tupleiterobject *it, PyObject *state)
1076{
Victor Stinner7660b882013-06-24 23:59:24 +02001077 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001078 if (index == -1 && PyErr_Occurred())
1079 return NULL;
1080 if (it->it_seq != NULL) {
1081 if (index < 0)
1082 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001083 else if (index > PyTuple_GET_SIZE(it->it_seq))
1084 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001085 it->it_index = index;
1086 }
1087 Py_RETURN_NONE;
1088}
1089
1090PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1091PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1092
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001093static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001095 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1096 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001098};
1099
Raymond Hettinger48923c52002-08-09 01:30:17 +00001100PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1102 "tuple_iterator", /* tp_name */
1103 sizeof(tupleiterobject), /* tp_basicsize */
1104 0, /* tp_itemsize */
1105 /* methods */
1106 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001107 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 0, /* tp_getattr */
1109 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001110 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 0, /* tp_repr */
1112 0, /* tp_as_number */
1113 0, /* tp_as_sequence */
1114 0, /* tp_as_mapping */
1115 0, /* tp_hash */
1116 0, /* tp_call */
1117 0, /* tp_str */
1118 PyObject_GenericGetAttr, /* tp_getattro */
1119 0, /* tp_setattro */
1120 0, /* tp_as_buffer */
1121 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1122 0, /* tp_doc */
1123 (traverseproc)tupleiter_traverse, /* tp_traverse */
1124 0, /* tp_clear */
1125 0, /* tp_richcompare */
1126 0, /* tp_weaklistoffset */
1127 PyObject_SelfIter, /* tp_iter */
1128 (iternextfunc)tupleiter_next, /* tp_iternext */
1129 tupleiter_methods, /* tp_methods */
1130 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001131};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001132
1133static PyObject *
1134tuple_iter(PyObject *seq)
1135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (!PyTuple_Check(seq)) {
1139 PyErr_BadInternalCall();
1140 return NULL;
1141 }
1142 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1143 if (it == NULL)
1144 return NULL;
1145 it->it_index = 0;
1146 Py_INCREF(seq);
1147 it->it_seq = (PyTupleObject *)seq;
1148 _PyObject_GC_TRACK(it);
1149 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150}