blob: 839667ad6f1730c6d123cbd95d9e0ce1e7d5b831 [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 Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pystate.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01007#include "pycore_accu.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Serhiy Storchaka0b561592017-03-19 08:47:58 +02009/*[clinic input]
10class tuple "PyTupleObject *" "&PyTuple_Type"
11[clinic start generated code]*/
12/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/
13
14#include "clinic/tupleobject.c.h"
15
Guido van Rossum5ce78f82000-04-21 21:15:05 +000016/* Speed optimization to avoid frequent malloc/free of small tuples */
Christian Heimes2202f872008-02-06 14:31:34 +000017#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +000019#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020#ifndef PyTuple_MAXFREELIST
Christian Heimes2202f872008-02-06 14:31:34 +000021#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000022#endif
23
Christian Heimes2202f872008-02-06 14:31:34 +000024#if PyTuple_MAXSAVESIZE > 0
25/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000026 tuple () of which at most one instance will be allocated.
27*/
Christian Heimes2202f872008-02-06 14:31:34 +000028static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
29static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000030#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +000031
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050032static inline void
33tuple_gc_track(PyTupleObject *op)
34{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050035 _PyObject_GC_TRACK(op);
36}
37
David Malcolm49526f42012-06-22 14:55:41 -040038/* Print summary info about the state of the optimized allocator */
39void
40_PyTuple_DebugMallocStats(FILE *out)
41{
42#if PyTuple_MAXSAVESIZE > 0
43 int i;
44 char buf[128];
45 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
46 PyOS_snprintf(buf, sizeof(buf),
47 "free %d-sized PyTupleObject", i);
48 _PyDebugAllocatorStats(out,
49 buf,
50 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
51 }
52#endif
53}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000054
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050055/* Allocate an uninitialized tuple object. Before making it public following
56 steps must be done:
57 - initialize its items
58 - call tuple_gc_track() on it
59 Because the empty tuple is always reused and it's already tracked by GC,
60 this function must not be called with size == 0 (unless from PyTuple_New()
61 which wraps this function).
62*/
63static PyTupleObject *
64tuple_alloc(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020066 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (size < 0) {
68 PyErr_BadInternalCall();
69 return NULL;
70 }
Christian Heimes2202f872008-02-06 14:31:34 +000071#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050073 assert(size != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 free_list[size] = (PyTupleObject *) op->ob_item[0];
75 numfree[size]--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000077#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 Py_SIZE(op) = size;
79 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000080#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 _Py_NewReference((PyObject *)op);
82 }
83 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000084#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 /* Check for overflow */
Sergey Fedoseev755d4ef2019-09-10 01:40:58 +050087 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
88 sizeof(PyObject *))) / sizeof(PyObject *)) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050089 return (PyTupleObject *)PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
92 if (op == NULL)
93 return NULL;
94 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050095 return op;
96}
97
98PyObject *
99PyTuple_New(Py_ssize_t size)
100{
101 PyTupleObject *op;
102#if PyTuple_MAXSAVESIZE > 0
103 if (size == 0 && free_list[0]) {
104 op = free_list[0];
105 Py_INCREF(op);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500106 return (PyObject *) op;
107 }
108#endif
109 op = tuple_alloc(size);
Zackery Spytz60bd1f82019-09-04 07:58:05 -0600110 if (op == NULL) {
111 return NULL;
112 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500113 for (Py_ssize_t i = 0; i < size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 op->ob_item[i] = NULL;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500115 }
Christian Heimes2202f872008-02-06 14:31:34 +0000116#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (size == 0) {
118 free_list[0] = op;
119 ++numfree[0];
120 Py_INCREF(op); /* extra INCREF so that this is never freed */
121 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000122#endif
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500123 tuple_gc_track(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125}
126
Martin v. Löwis18e16552006-02-15 17:27:45 +0000127Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200128PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 if (!PyTuple_Check(op)) {
131 PyErr_BadInternalCall();
132 return -1;
133 }
134 else
135 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200139PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (!PyTuple_Check(op)) {
142 PyErr_BadInternalCall();
143 return NULL;
144 }
145 if (i < 0 || i >= Py_SIZE(op)) {
146 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
147 return NULL;
148 }
149 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150}
151
152int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200153PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200155 PyObject **p;
Victor Stinnera93c51e2020-02-07 00:38:59 +0100156 if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 Py_XDECREF(newitem);
158 PyErr_BadInternalCall();
159 return -1;
160 }
161 if (i < 0 || i >= Py_SIZE(op)) {
162 Py_XDECREF(newitem);
163 PyErr_SetString(PyExc_IndexError,
164 "tuple assignment index out of range");
165 return -1;
166 }
167 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300168 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000172void
173_PyTuple_MaybeUntrack(PyObject *op)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 PyTupleObject *t;
176 Py_ssize_t i, n;
177
178 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
179 return;
180 t = (PyTupleObject *) op;
181 n = Py_SIZE(t);
182 for (i = 0; i < n; i++) {
183 PyObject *elt = PyTuple_GET_ITEM(t, i);
184 /* Tuple with NULL elements aren't
185 fully constructed, don't untrack
186 them yet. */
187 if (!elt ||
188 _PyObject_GC_MAY_BE_TRACKED(elt))
189 return;
190 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000192}
193
Raymond Hettingercb2da432003-10-12 18:24:34 +0000194PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000195PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 Py_ssize_t i;
198 PyObject *o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 PyObject **items;
200 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000201
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500202 if (n == 0) {
203 return PyTuple_New(0);
204 }
205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 va_start(vargs, n);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500207 PyTupleObject *result = tuple_alloc(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200208 if (result == NULL) {
209 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200211 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500212 items = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 for (i = 0; i < n; i++) {
214 o = va_arg(vargs, PyObject *);
215 Py_INCREF(o);
216 items[i] = o;
217 }
218 va_end(vargs);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500219 tuple_gc_track(result);
220 return (PyObject *)result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000221}
222
223
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224/* Methods */
225
226static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200227tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200229 Py_ssize_t i;
230 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200232 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (len > 0) {
234 i = len;
235 while (--i >= 0)
236 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000237#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (len < PyTuple_MAXSAVESIZE &&
239 numfree[len] < PyTuple_MAXFREELIST &&
Andy Lesterdffe4c02020-03-04 07:15:20 -0600240 Py_IS_TYPE(op, &PyTuple_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 {
242 op->ob_item[0] = (PyObject *) free_list[len];
243 numfree[len]++;
244 free_list[len] = op;
245 goto done; /* return */
246 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 }
249 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000250done:
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200251 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000255tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100258 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 n = Py_SIZE(v);
261 if (n == 0)
262 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* While not mutable, it is still possible to end up with a cycle in a
265 tuple through an object that stores itself within a tuple (and thus
266 infinitely asks for the repr of itself). This should only be
267 possible within a type. */
268 i = Py_ReprEnter((PyObject *)v);
269 if (i != 0) {
270 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
271 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000272
Victor Stinner88a9cd92013-11-19 12:59:46 +0100273 _PyUnicodeWriter_Init(&writer);
274 writer.overallocate = 1;
275 if (Py_SIZE(v) > 1) {
276 /* "(" + "1" + ", 2" * (len - 1) + ")" */
277 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
278 }
279 else {
280 /* "(1,)" */
281 writer.min_length = 4;
282 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200283
Victor Stinner88a9cd92013-11-19 12:59:46 +0100284 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200285 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Do repr() on each element. */
288 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100289 PyObject *s;
290
291 if (i > 0) {
292 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
293 goto error;
294 }
295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100297 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200298 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100299
300 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
301 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200302 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100303 }
304 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100306
307 writer.overallocate = 0;
308 if (n > 1) {
309 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
310 goto error;
311 }
312 else {
313 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
314 goto error;
315 }
Tim Petersa7259592001-06-16 05:11:17 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100318 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200319
320error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100321 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200322 Py_ReprLeave((PyObject *)v);
323 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000326
jdemeyeraeb1be52018-10-28 02:06:38 +0200327/* Hash for tuples. This is a slightly simplified version of the xxHash
328 non-cryptographic hash:
329 - we do not use any parallellism, there is only 1 accumulator.
330 - we drop the final mixing since this is just a permutation of the
331 output space: it does not help against collisions.
332 - at the end, we mangle the length with a single constant.
333 For the xxHash specification, see
334 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100335
jdemeyeraeb1be52018-10-28 02:06:38 +0200336 Below are the official constants from the xxHash specification. Optimizing
337 compilers should emit a single "rotate" instruction for the
338 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
339 platform, the macro could be changed to expand to a platform-specific rotate
340 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000341*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200342#if SIZEOF_PY_UHASH_T > 4
343#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
344#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
345#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
346#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
347#else
348#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
349#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
350#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
351#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
352#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000353
jdemeyeraeb1be52018-10-28 02:06:38 +0200354/* Tests have shown that it's not worth to cache the hash value, see
355 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000356static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000357tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000358{
jdemeyeraeb1be52018-10-28 02:06:38 +0200359 Py_ssize_t i, len = Py_SIZE(v);
360 PyObject **item = v->ob_item;
361
362 Py_uhash_t acc = _PyHASH_XXPRIME_5;
363 for (i = 0; i < len; i++) {
364 Py_uhash_t lane = PyObject_Hash(item[i]);
365 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200367 }
368 acc += lane * _PyHASH_XXPRIME_2;
369 acc = _PyHASH_XXROTATE(acc);
370 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200372
373 /* Add input length, mangled to keep the historical value of hash(()). */
374 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
375
376 if (acc == (Py_uhash_t)-1) {
377 return 1546275796;
378 }
379 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000380}
381
Martin v. Löwis18e16552006-02-15 17:27:45 +0000382static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000383tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386}
387
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000388static int
Fred Drakeba096332000-07-09 07:04:36 +0000389tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 Py_ssize_t i;
392 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300395 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000397}
398
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200400tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (i < 0 || i >= Py_SIZE(a)) {
403 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
404 return NULL;
405 }
406 Py_INCREF(a->ob_item[i]);
407 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408}
409
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500410PyObject *
411_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
412{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500413 if (n == 0) {
414 return PyTuple_New(0);
415 }
416
417 PyTupleObject *tuple = tuple_alloc(n);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500418 if (tuple == NULL) {
419 return NULL;
420 }
421 PyObject **dst = tuple->ob_item;
422 for (Py_ssize_t i = 0; i < n; i++) {
423 PyObject *item = src[i];
424 Py_INCREF(item);
425 dst[i] = item;
426 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500427 tuple_gc_track(tuple);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500428 return (PyObject *)tuple;
429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200432tupleslice(PyTupleObject *a, Py_ssize_t ilow,
433 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (ilow < 0)
436 ilow = 0;
437 if (ihigh > Py_SIZE(a))
438 ihigh = Py_SIZE(a);
439 if (ihigh < ilow)
440 ihigh = ilow;
441 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
442 Py_INCREF(a);
443 return (PyObject *)a;
444 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500445 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446}
447
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000449PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (op == NULL || !PyTuple_Check(op)) {
452 PyErr_BadInternalCall();
453 return NULL;
454 }
455 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000456}
457
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200459tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200461 Py_ssize_t size;
462 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 PyObject **src, **dest;
464 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200465 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
466 Py_INCREF(bb);
467 return bb;
468 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (!PyTuple_Check(bb)) {
470 PyErr_Format(PyExc_TypeError,
471 "can only concatenate tuple (not \"%.200s\") to tuple",
472 Py_TYPE(bb)->tp_name);
473 return NULL;
474 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200476 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
477 Py_INCREF(a);
478 return (PyObject *)a;
479 }
Martin Panterb93d8632016-07-25 02:39:20 +0000480 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000482 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500483 if (size == 0) {
484 return PyTuple_New(0);
485 }
486
487 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (np == NULL) {
489 return NULL;
490 }
491 src = a->ob_item;
492 dest = np->ob_item;
493 for (i = 0; i < Py_SIZE(a); i++) {
494 PyObject *v = src[i];
495 Py_INCREF(v);
496 dest[i] = v;
497 }
498 src = b->ob_item;
499 dest = np->ob_item + Py_SIZE(a);
500 for (i = 0; i < Py_SIZE(b); i++) {
501 PyObject *v = src[i];
502 Py_INCREF(v);
503 dest[i] = v;
504 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500505 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507#undef b
508}
509
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000511tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 Py_ssize_t i, j;
514 Py_ssize_t size;
515 PyTupleObject *np;
516 PyObject **p, **items;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if (Py_SIZE(a) == 0 || n == 1) {
518 if (PyTuple_CheckExact(a)) {
519 /* Since tuples are immutable, we can return a shared
520 copy in this case */
521 Py_INCREF(a);
522 return (PyObject *)a;
523 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500524 }
525 if (Py_SIZE(a) == 0 || n <= 0) {
526 return PyTuple_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100528 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100530 size = Py_SIZE(a) * n;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500531 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (np == NULL)
533 return NULL;
534 p = np->ob_item;
535 items = a->ob_item;
536 for (i = 0; i < n; i++) {
537 for (j = 0; j < Py_SIZE(a); j++) {
538 *p = items[j];
539 Py_INCREF(*p);
540 p++;
541 }
542 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500543 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000545}
546
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200547/*[clinic input]
548tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000549
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200550 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300551 start: slice_index(accept={int}) = 0
552 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200553 /
554
555Return first index of value.
556
557Raises ValueError if the value is not present.
558[clinic start generated code]*/
559
560static PyObject *
561tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
562 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300563/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200564{
565 Py_ssize_t i;
566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (start < 0) {
568 start += Py_SIZE(self);
569 if (start < 0)
570 start = 0;
571 }
572 if (stop < 0) {
573 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200575 else if (stop > Py_SIZE(self)) {
576 stop = Py_SIZE(self);
577 }
578 for (i = start; i < stop; i++) {
579 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (cmp > 0)
581 return PyLong_FromSsize_t(i);
582 else if (cmp < 0)
583 return NULL;
584 }
585 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
586 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000587}
588
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200589/*[clinic input]
590tuple.count
591
592 value: object
593 /
594
595Return number of occurrences of value.
596[clinic start generated code]*/
597
Raymond Hettinger65baa342008-02-07 00:41:02 +0000598static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200599tuple_count(PyTupleObject *self, PyObject *value)
600/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 Py_ssize_t count = 0;
603 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200606 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (cmp > 0)
608 count++;
609 else if (cmp < 0)
610 return NULL;
611 }
612 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000613}
614
Jeremy Hylton8caad492000-06-23 14:18:11 +0000615static int
616tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 for (i = Py_SIZE(o); --i >= 0; )
621 Py_VISIT(o->ob_item[i]);
622 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000623}
624
Guido van Rossumf77bc622001-01-18 00:00:53 +0000625static PyObject *
626tuplerichcompare(PyObject *v, PyObject *w, int op)
627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyTupleObject *vt, *wt;
629 Py_ssize_t i;
630 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000631
Brian Curtindfc80e32011-08-10 20:28:54 -0500632 if (!PyTuple_Check(v) || !PyTuple_Check(w))
633 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 vt = (PyTupleObject *)v;
636 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 vlen = Py_SIZE(vt);
639 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 /* Note: the corresponding code for lists has an "early out" test
642 * here when op is EQ or NE and the lengths differ. That pays there,
643 * but Tim was unable to find any real code where EQ/NE tuple
644 * compares don't have the same length, so testing for it here would
645 * have cost without benefit.
646 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 /* Search for the first index where items are different.
649 * Note that because tuples are immutable, it's safe to reuse
650 * vlen and wlen across the comparison calls.
651 */
652 for (i = 0; i < vlen && i < wlen; i++) {
653 int k = PyObject_RichCompareBool(vt->ob_item[i],
654 wt->ob_item[i], Py_EQ);
655 if (k < 0)
656 return NULL;
657 if (!k)
658 break;
659 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 if (i >= vlen || i >= wlen) {
662 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100663 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 /* We have an item that differs -- shortcuts for EQ/NE */
667 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200668 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
670 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200671 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 /* Compare the final item again using the proper operator */
675 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000676}
677
Jeremy Hylton938ace62002-07-17 16:30:39 +0000678static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200679tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
680
681/*[clinic input]
682@classmethod
683tuple.__new__ as tuple_new
684 iterable: object(c_default="NULL") = ()
685 /
686
687Built-in immutable sequence.
688
689If no argument is given, the constructor returns an empty tuple.
690If iterable is specified the tuple is initialized from iterable's items.
691
692If the argument is a tuple, the return value is the same object.
693[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000694
Tim Peters6d6c1a32001-08-02 04:15:00 +0000695static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200696tuple_new_impl(PyTypeObject *type, PyObject *iterable)
697/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200700 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200702 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 return PyTuple_New(0);
704 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200705 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706}
707
Guido van Rossumae960af2001-08-30 03:11:59 +0000708static PyObject *
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900709tuple_vectorcall(PyObject *type, PyObject * const*args,
710 size_t nargsf, PyObject *kwnames)
711{
712 if (kwnames && PyTuple_GET_SIZE(kwnames) != 0) {
713 PyErr_Format(PyExc_TypeError, "tuple() takes no keyword arguments");
714 return NULL;
715 }
716 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
717 if (nargs > 1) {
718 PyErr_Format(PyExc_TypeError, "tuple() expected at most 1 argument, got %zd", nargs);
719 return NULL;
720 }
721
722 if (nargs) {
723 return tuple_new_impl((PyTypeObject *)type, args[0]);
724 }
725 return PyTuple_New(0);
726}
727
728static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200729tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyObject *tmp, *newobj, *item;
732 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200735 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 if (tmp == NULL)
737 return NULL;
738 assert(PyTuple_Check(tmp));
739 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
Hai Shic81609e2020-03-16 03:37:49 +0800740 if (newobj == NULL) {
741 Py_DECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 return NULL;
Hai Shic81609e2020-03-16 03:37:49 +0800743 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 for (i = 0; i < n; i++) {
745 item = PyTuple_GET_ITEM(tmp, i);
746 Py_INCREF(item);
747 PyTuple_SET_ITEM(newobj, i, item);
748 }
749 Py_DECREF(tmp);
750 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000751}
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 (lenfunc)tuplelength, /* sq_length */
755 (binaryfunc)tupleconcat, /* sq_concat */
756 (ssizeargfunc)tuplerepeat, /* sq_repeat */
757 (ssizeargfunc)tupleitem, /* sq_item */
758 0, /* sq_slice */
759 0, /* sq_ass_item */
760 0, /* sq_ass_slice */
761 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762};
763
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000764static PyObject*
765tuplesubscript(PyTupleObject* self, PyObject* item)
766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (PyIndex_Check(item)) {
768 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
769 if (i == -1 && PyErr_Occurred())
770 return NULL;
771 if (i < 0)
772 i += PyTuple_GET_SIZE(self);
773 return tupleitem(self, i);
774 }
775 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600776 Py_ssize_t start, stop, step, slicelength, i;
777 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 PyObject* it;
779 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000780
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300781 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 return NULL;
783 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300784 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
785 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 if (slicelength <= 0) {
788 return PyTuple_New(0);
789 }
790 else if (start == 0 && step == 1 &&
791 slicelength == PyTuple_GET_SIZE(self) &&
792 PyTuple_CheckExact(self)) {
793 Py_INCREF(self);
794 return (PyObject *)self;
795 }
796 else {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500797 PyTupleObject* result = tuple_alloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 src = self->ob_item;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500801 dest = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 for (cur = start, i = 0; i < slicelength;
803 cur += step, i++) {
804 it = src[cur];
805 Py_INCREF(it);
806 dest[i] = it;
807 }
808
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500809 tuple_gc_track(result);
810 return (PyObject *)result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 }
812 }
813 else {
814 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400815 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_TYPE(item)->tp_name);
817 return NULL;
818 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000819}
820
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200821/*[clinic input]
822tuple.__getnewargs__
823[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200825static PyObject *
826tuple___getnewargs___impl(PyTupleObject *self)
827/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
828{
829 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000830}
831
832static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200833 TUPLE___GETNEWARGS___METHODDEF
834 TUPLE_INDEX_METHODDEF
835 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000837};
838
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000839static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 (lenfunc)tuplelength,
841 (binaryfunc)tuplesubscript,
842 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000843};
844
Raymond Hettinger48923c52002-08-09 01:30:17 +0000845static PyObject *tuple_iter(PyObject *seq);
846
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 PyVarObject_HEAD_INIT(&PyType_Type, 0)
849 "tuple",
850 sizeof(PyTupleObject) - sizeof(PyObject *),
851 sizeof(PyObject *),
852 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200853 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 0, /* tp_getattr */
855 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200856 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 (reprfunc)tuplerepr, /* tp_repr */
858 0, /* tp_as_number */
859 &tuple_as_sequence, /* tp_as_sequence */
860 &tuple_as_mapping, /* tp_as_mapping */
861 (hashfunc)tuplehash, /* tp_hash */
862 0, /* tp_call */
863 0, /* tp_str */
864 PyObject_GenericGetAttr, /* tp_getattro */
865 0, /* tp_setattro */
866 0, /* tp_as_buffer */
867 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
868 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200869 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 (traverseproc)tupletraverse, /* tp_traverse */
871 0, /* tp_clear */
872 tuplerichcompare, /* tp_richcompare */
873 0, /* tp_weaklistoffset */
874 tuple_iter, /* tp_iter */
875 0, /* tp_iternext */
876 tuple_methods, /* tp_methods */
877 0, /* tp_members */
878 0, /* tp_getset */
879 0, /* tp_base */
880 0, /* tp_dict */
881 0, /* tp_descr_get */
882 0, /* tp_descr_set */
883 0, /* tp_dictoffset */
884 0, /* tp_init */
885 0, /* tp_alloc */
886 tuple_new, /* tp_new */
887 PyObject_GC_Del, /* tp_free */
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900888 .tp_vectorcall = tuple_vectorcall,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000890
891/* The following function breaks the notion that tuples are immutable:
892 it changes the size of a tuple. We get away with this only if there
893 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000894 as creating a new tuple object and destroying the old one, only more
895 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000896 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897
898int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000899_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000900{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200901 PyTupleObject *v;
902 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Py_ssize_t i;
904 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 v = (PyTupleObject *) *pv;
Andy Lester55728702020-03-06 16:53:17 -0600907 if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
909 *pv = 0;
910 Py_XDECREF(v);
911 PyErr_BadInternalCall();
912 return -1;
913 }
914 oldsize = Py_SIZE(v);
915 if (oldsize == newsize)
916 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (oldsize == 0) {
919 /* Empty tuples are often shared, so we should never
920 resize them in-place even if we do own the only
921 (current) reference */
922 Py_DECREF(v);
923 *pv = PyTuple_New(newsize);
924 return *pv == NULL ? -1 : 0;
925 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 /* XXX UNREF/NEWREF interface should be more symmetrical */
Victor Stinner49932fe2020-02-03 17:55:05 +0100928#ifdef Py_REF_DEBUG
929 _Py_RefTotal--;
930#endif
931 if (_PyObject_GC_IS_TRACKED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 _PyObject_GC_UNTRACK(v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100933 }
934#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 _Py_ForgetReference((PyObject *) v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100936#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 /* DECREF items deleted by shrinkage */
938 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200939 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 }
941 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
942 if (sv == NULL) {
943 *pv = NULL;
944 PyObject_GC_Del(v);
945 return -1;
946 }
947 _Py_NewReference((PyObject *) sv);
948 /* Zero out items added by growing */
949 if (newsize > oldsize)
950 memset(&sv->ob_item[oldsize], 0,
951 sizeof(*sv->ob_item) * (newsize - oldsize));
952 *pv = (PyObject *) sv;
953 _PyObject_GC_TRACK(sv);
954 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000955}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000956
Christian Heimesa156e092008-02-16 07:38:31 +0000957int
958PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000961#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 int i;
963 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
964 PyTupleObject *p, *q;
965 p = free_list[i];
966 freelist_size += numfree[i];
967 free_list[i] = NULL;
968 numfree[i] = 0;
969 while (p) {
970 q = p;
971 p = (PyTupleObject *)(p->ob_item[0]);
972 PyObject_GC_Del(q);
973 }
974 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000975#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000977}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978
Christian Heimesa156e092008-02-16 07:38:31 +0000979void
Victor Stinnerbed48172019-08-27 00:12:32 +0200980_PyTuple_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000981{
982#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 /* empty tuples are used all over the place and applications may
984 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200985 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000988#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000989}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000990
991/*********************** Tuple Iterator **************************/
992
993typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200995 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000997} tupleiterobject;
998
Raymond Hettinger48923c52002-08-09 01:30:17 +0000999static void
1000tupleiter_dealloc(tupleiterobject *it)
1001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 _PyObject_GC_UNTRACK(it);
1003 Py_XDECREF(it->it_seq);
1004 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +00001005}
1006
1007static int
1008tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
1009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Py_VISIT(it->it_seq);
1011 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001012}
1013
Raymond Hettinger48923c52002-08-09 01:30:17 +00001014static PyObject *
1015tupleiter_next(tupleiterobject *it)
1016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 PyTupleObject *seq;
1018 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 assert(it != NULL);
1021 seq = it->it_seq;
1022 if (seq == NULL)
1023 return NULL;
1024 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1027 item = PyTuple_GET_ITEM(seq, it->it_index);
1028 ++it->it_index;
1029 Py_INCREF(item);
1030 return item;
1031 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001034 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001036}
1037
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001038static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301039tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 Py_ssize_t len = 0;
1042 if (it->it_seq)
1043 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1044 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001045}
1046
Armin Rigof5b3e362006-02-11 21:32:43 +00001047PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001048
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001049static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301050tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001051{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001052 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001053 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001054 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001055 it->it_seq, it->it_index);
1056 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001057 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001058}
1059
1060static PyObject *
1061tupleiter_setstate(tupleiterobject *it, PyObject *state)
1062{
Victor Stinner7660b882013-06-24 23:59:24 +02001063 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001064 if (index == -1 && PyErr_Occurred())
1065 return NULL;
1066 if (it->it_seq != NULL) {
1067 if (index < 0)
1068 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001069 else if (index > PyTuple_GET_SIZE(it->it_seq))
1070 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001071 it->it_index = index;
1072 }
1073 Py_RETURN_NONE;
1074}
1075
1076PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1077PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1078
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001079static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001081 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1082 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001084};
1085
Raymond Hettinger48923c52002-08-09 01:30:17 +00001086PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1088 "tuple_iterator", /* tp_name */
1089 sizeof(tupleiterobject), /* tp_basicsize */
1090 0, /* tp_itemsize */
1091 /* methods */
1092 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001093 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 0, /* tp_getattr */
1095 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001096 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 0, /* tp_repr */
1098 0, /* tp_as_number */
1099 0, /* tp_as_sequence */
1100 0, /* tp_as_mapping */
1101 0, /* tp_hash */
1102 0, /* tp_call */
1103 0, /* tp_str */
1104 PyObject_GenericGetAttr, /* tp_getattro */
1105 0, /* tp_setattro */
1106 0, /* tp_as_buffer */
1107 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1108 0, /* tp_doc */
1109 (traverseproc)tupleiter_traverse, /* tp_traverse */
1110 0, /* tp_clear */
1111 0, /* tp_richcompare */
1112 0, /* tp_weaklistoffset */
1113 PyObject_SelfIter, /* tp_iter */
1114 (iternextfunc)tupleiter_next, /* tp_iternext */
1115 tupleiter_methods, /* tp_methods */
1116 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001117};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118
1119static PyObject *
1120tuple_iter(PyObject *seq)
1121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (!PyTuple_Check(seq)) {
1125 PyErr_BadInternalCall();
1126 return NULL;
1127 }
1128 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1129 if (it == NULL)
1130 return NULL;
1131 it->it_index = 0;
1132 Py_INCREF(seq);
1133 it->it_seq = (PyTupleObject *)seq;
1134 _PyObject_GC_TRACK(it);
1135 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001136}