blob: aeaf845d74cfa1b348186e6e1a7159f5360de55a [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
31#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +000032Py_ssize_t _Py_fast_tuple_allocs;
33Py_ssize_t _Py_tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000034#endif
35
Antoine Pitrou3a652b12009-03-23 18:52:06 +000036/* Debug statistic to count GC tracking of tuples.
37 Please note that tuples are only untracked when considered by the GC, and
38 many of them will be dead before. Therefore, a tracking rate close to 100%
39 does not necessarily prove that the heuristic is inefficient.
40*/
41#ifdef SHOW_TRACK_COUNT
42static Py_ssize_t count_untracked = 0;
43static Py_ssize_t count_tracked = 0;
44
45static void
46show_track(void)
47{
Victor Stinnercaba55b2018-08-03 15:33:52 +020048 PyInterpreterState *interp = _PyInterpreterState_Get();
Victor Stinner331a6a52019-05-27 16:39:22 +020049 if (!interp->config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030050 return;
Victor Stinner25420fe2017-11-20 18:12:22 -080051 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
54 count_tracked + count_untracked);
55 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
56 "d\n", count_tracked);
57 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
58 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000059}
60#endif
61
David Malcolm49526f42012-06-22 14:55:41 -040062/* Print summary info about the state of the optimized allocator */
63void
64_PyTuple_DebugMallocStats(FILE *out)
65{
66#if PyTuple_MAXSAVESIZE > 0
67 int i;
68 char buf[128];
69 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
70 PyOS_snprintf(buf, sizeof(buf),
71 "free %d-sized PyTupleObject", i);
72 _PyDebugAllocatorStats(out,
73 buf,
74 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
75 }
76#endif
77}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000078
Guido van Rossumc0b618a1997-05-02 03:12:38 +000079PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020080PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020082 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 Py_ssize_t i;
84 if (size < 0) {
85 PyErr_BadInternalCall();
86 return NULL;
87 }
Christian Heimes2202f872008-02-06 14:31:34 +000088#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 if (size == 0 && free_list[0]) {
90 op = free_list[0];
91 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000092#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +000093 _Py_tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000094#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return (PyObject *) op;
96 }
97 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
98 free_list[size] = (PyTupleObject *) op->ob_item[0];
99 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000100#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +0000101 _Py_fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000102#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000104#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 Py_SIZE(op) = size;
106 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 _Py_NewReference((PyObject *)op);
109 }
110 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000111#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200114 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100115 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 return PyErr_NoMemory();
117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
119 if (op == NULL)
120 return NULL;
121 }
122 for (i=0; i < size; i++)
123 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000124#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (size == 0) {
126 free_list[0] = op;
127 ++numfree[0];
128 Py_INCREF(op); /* extra INCREF so that this is never freed */
129 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000130#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000131#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000133#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 _PyObject_GC_TRACK(op);
135 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200139PyTuple_Size(PyObject *op)
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 -1;
144 }
145 else
146 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147}
148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200150PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 if (!PyTuple_Check(op)) {
153 PyErr_BadInternalCall();
154 return NULL;
155 }
156 if (i < 0 || i >= Py_SIZE(op)) {
157 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
158 return NULL;
159 }
160 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161}
162
163int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200164PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200166 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
168 Py_XDECREF(newitem);
169 PyErr_BadInternalCall();
170 return -1;
171 }
172 if (i < 0 || i >= Py_SIZE(op)) {
173 Py_XDECREF(newitem);
174 PyErr_SetString(PyExc_IndexError,
175 "tuple assignment index out of range");
176 return -1;
177 }
178 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300179 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181}
182
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000183void
184_PyTuple_MaybeUntrack(PyObject *op)
185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 PyTupleObject *t;
187 Py_ssize_t i, n;
188
189 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
190 return;
191 t = (PyTupleObject *) op;
192 n = Py_SIZE(t);
193 for (i = 0; i < n; i++) {
194 PyObject *elt = PyTuple_GET_ITEM(t, i);
195 /* Tuple with NULL elements aren't
196 fully constructed, don't untrack
197 them yet. */
198 if (!elt ||
199 _PyObject_GC_MAY_BE_TRACKED(elt))
200 return;
201 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000202#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 count_tracked--;
204 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000205#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000207}
208
Raymond Hettingercb2da432003-10-12 18:24:34 +0000209PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000210PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 Py_ssize_t i;
213 PyObject *o;
214 PyObject *result;
215 PyObject **items;
216 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 va_start(vargs, n);
219 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200220 if (result == NULL) {
221 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200223 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 items = ((PyTupleObject *)result)->ob_item;
225 for (i = 0; i < n; i++) {
226 o = va_arg(vargs, PyObject *);
227 Py_INCREF(o);
228 items[i] = o;
229 }
230 va_end(vargs);
231 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000232}
233
234
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235/* Methods */
236
237static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200238tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200240 Py_ssize_t i;
241 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200243 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (len > 0) {
245 i = len;
246 while (--i >= 0)
247 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000248#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 if (len < PyTuple_MAXSAVESIZE &&
250 numfree[len] < PyTuple_MAXFREELIST &&
251 Py_TYPE(op) == &PyTuple_Type)
252 {
253 op->ob_item[0] = (PyObject *) free_list[len];
254 numfree[len]++;
255 free_list[len] = op;
256 goto done; /* return */
257 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000258#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 }
260 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000261done:
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200262 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263}
264
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000266tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100269 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 n = Py_SIZE(v);
272 if (n == 0)
273 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 /* While not mutable, it is still possible to end up with a cycle in a
276 tuple through an object that stores itself within a tuple (and thus
277 infinitely asks for the repr of itself). This should only be
278 possible within a type. */
279 i = Py_ReprEnter((PyObject *)v);
280 if (i != 0) {
281 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
282 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000283
Victor Stinner88a9cd92013-11-19 12:59:46 +0100284 _PyUnicodeWriter_Init(&writer);
285 writer.overallocate = 1;
286 if (Py_SIZE(v) > 1) {
287 /* "(" + "1" + ", 2" * (len - 1) + ")" */
288 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
289 }
290 else {
291 /* "(1,)" */
292 writer.min_length = 4;
293 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200294
Victor Stinner88a9cd92013-11-19 12:59:46 +0100295 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200296 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 /* Do repr() on each element. */
299 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100300 PyObject *s;
301
302 if (i > 0) {
303 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
304 goto error;
305 }
306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100308 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200309 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100310
311 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
312 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200313 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100314 }
315 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100317
318 writer.overallocate = 0;
319 if (n > 1) {
320 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
321 goto error;
322 }
323 else {
324 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
325 goto error;
326 }
Tim Petersa7259592001-06-16 05:11:17 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100329 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200330
331error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100332 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200333 Py_ReprLeave((PyObject *)v);
334 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335}
336
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000337
jdemeyeraeb1be52018-10-28 02:06:38 +0200338/* Hash for tuples. This is a slightly simplified version of the xxHash
339 non-cryptographic hash:
340 - we do not use any parallellism, there is only 1 accumulator.
341 - we drop the final mixing since this is just a permutation of the
342 output space: it does not help against collisions.
343 - at the end, we mangle the length with a single constant.
344 For the xxHash specification, see
345 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100346
jdemeyeraeb1be52018-10-28 02:06:38 +0200347 Below are the official constants from the xxHash specification. Optimizing
348 compilers should emit a single "rotate" instruction for the
349 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
350 platform, the macro could be changed to expand to a platform-specific rotate
351 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000352*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200353#if SIZEOF_PY_UHASH_T > 4
354#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
355#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
356#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
357#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
358#else
359#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
360#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
361#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
362#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
363#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000364
jdemeyeraeb1be52018-10-28 02:06:38 +0200365/* Tests have shown that it's not worth to cache the hash value, see
366 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000367static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000368tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000369{
jdemeyeraeb1be52018-10-28 02:06:38 +0200370 Py_ssize_t i, len = Py_SIZE(v);
371 PyObject **item = v->ob_item;
372
373 Py_uhash_t acc = _PyHASH_XXPRIME_5;
374 for (i = 0; i < len; i++) {
375 Py_uhash_t lane = PyObject_Hash(item[i]);
376 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200378 }
379 acc += lane * _PyHASH_XXPRIME_2;
380 acc = _PyHASH_XXROTATE(acc);
381 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200383
384 /* Add input length, mangled to keep the historical value of hash(()). */
385 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
386
387 if (acc == (Py_uhash_t)-1) {
388 return 1546275796;
389 }
390 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000391}
392
Martin v. Löwis18e16552006-02-15 17:27:45 +0000393static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000394tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397}
398
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000399static int
Fred Drakeba096332000-07-09 07:04:36 +0000400tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 Py_ssize_t i;
403 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300406 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000408}
409
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200411tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (i < 0 || i >= Py_SIZE(a)) {
414 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
415 return NULL;
416 }
417 Py_INCREF(a->ob_item[i]);
418 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
420
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500421PyObject *
422_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
423{
424 PyTupleObject *tuple = (PyTupleObject *)PyTuple_New(n);
425 if (tuple == NULL) {
426 return NULL;
427 }
428 PyObject **dst = tuple->ob_item;
429 for (Py_ssize_t i = 0; i < n; i++) {
430 PyObject *item = src[i];
431 Py_INCREF(item);
432 dst[i] = item;
433 }
434 return (PyObject *)tuple;
435}
436
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200438tupleslice(PyTupleObject *a, Py_ssize_t ilow,
439 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (ilow < 0)
442 ilow = 0;
443 if (ihigh > Py_SIZE(a))
444 ihigh = Py_SIZE(a);
445 if (ihigh < ilow)
446 ihigh = ilow;
447 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
448 Py_INCREF(a);
449 return (PyObject *)a;
450 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500451 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452}
453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (op == NULL || !PyTuple_Check(op)) {
458 PyErr_BadInternalCall();
459 return NULL;
460 }
461 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000462}
463
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200465tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200467 Py_ssize_t size;
468 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyObject **src, **dest;
470 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200471 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
472 Py_INCREF(bb);
473 return bb;
474 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (!PyTuple_Check(bb)) {
476 PyErr_Format(PyExc_TypeError,
477 "can only concatenate tuple (not \"%.200s\") to tuple",
478 Py_TYPE(bb)->tp_name);
479 return NULL;
480 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200482 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
483 Py_INCREF(a);
484 return (PyObject *)a;
485 }
Martin Panterb93d8632016-07-25 02:39:20 +0000486 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000488 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 np = (PyTupleObject *) PyTuple_New(size);
490 if (np == NULL) {
491 return NULL;
492 }
493 src = a->ob_item;
494 dest = np->ob_item;
495 for (i = 0; i < Py_SIZE(a); i++) {
496 PyObject *v = src[i];
497 Py_INCREF(v);
498 dest[i] = v;
499 }
500 src = b->ob_item;
501 dest = np->ob_item + Py_SIZE(a);
502 for (i = 0; i < Py_SIZE(b); i++) {
503 PyObject *v = src[i];
504 Py_INCREF(v);
505 dest[i] = v;
506 }
507 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000508#undef b
509}
510
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000512tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 Py_ssize_t i, j;
515 Py_ssize_t size;
516 PyTupleObject *np;
517 PyObject **p, **items;
518 if (n < 0)
519 n = 0;
520 if (Py_SIZE(a) == 0 || n == 1) {
521 if (PyTuple_CheckExact(a)) {
522 /* Since tuples are immutable, we can return a shared
523 copy in this case */
524 Py_INCREF(a);
525 return (PyObject *)a;
526 }
527 if (Py_SIZE(a) == 0)
528 return PyTuple_New(0);
529 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100530 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100532 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 np = (PyTupleObject *) PyTuple_New(size);
534 if (np == NULL)
535 return NULL;
536 p = np->ob_item;
537 items = a->ob_item;
538 for (i = 0; i < n; i++) {
539 for (j = 0; j < Py_SIZE(a); j++) {
540 *p = items[j];
541 Py_INCREF(*p);
542 p++;
543 }
544 }
545 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000546}
547
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200548/*[clinic input]
549tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000550
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200551 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300552 start: slice_index(accept={int}) = 0
553 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200554 /
555
556Return first index of value.
557
558Raises ValueError if the value is not present.
559[clinic start generated code]*/
560
561static PyObject *
562tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
563 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300564/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200565{
566 Py_ssize_t i;
567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (start < 0) {
569 start += Py_SIZE(self);
570 if (start < 0)
571 start = 0;
572 }
573 if (stop < 0) {
574 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200576 else if (stop > Py_SIZE(self)) {
577 stop = Py_SIZE(self);
578 }
579 for (i = start; i < stop; i++) {
580 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (cmp > 0)
582 return PyLong_FromSsize_t(i);
583 else if (cmp < 0)
584 return NULL;
585 }
586 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
587 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000588}
589
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200590/*[clinic input]
591tuple.count
592
593 value: object
594 /
595
596Return number of occurrences of value.
597[clinic start generated code]*/
598
Raymond Hettinger65baa342008-02-07 00:41:02 +0000599static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200600tuple_count(PyTupleObject *self, PyObject *value)
601/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_ssize_t count = 0;
604 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200607 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 if (cmp > 0)
609 count++;
610 else if (cmp < 0)
611 return NULL;
612 }
613 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000614}
615
Jeremy Hylton8caad492000-06-23 14:18:11 +0000616static int
617tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 for (i = Py_SIZE(o); --i >= 0; )
622 Py_VISIT(o->ob_item[i]);
623 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000624}
625
Guido van Rossumf77bc622001-01-18 00:00:53 +0000626static PyObject *
627tuplerichcompare(PyObject *v, PyObject *w, int op)
628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 PyTupleObject *vt, *wt;
630 Py_ssize_t i;
631 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000632
Brian Curtindfc80e32011-08-10 20:28:54 -0500633 if (!PyTuple_Check(v) || !PyTuple_Check(w))
634 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 vt = (PyTupleObject *)v;
637 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 vlen = Py_SIZE(vt);
640 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* Note: the corresponding code for lists has an "early out" test
643 * here when op is EQ or NE and the lengths differ. That pays there,
644 * but Tim was unable to find any real code where EQ/NE tuple
645 * compares don't have the same length, so testing for it here would
646 * have cost without benefit.
647 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 /* Search for the first index where items are different.
650 * Note that because tuples are immutable, it's safe to reuse
651 * vlen and wlen across the comparison calls.
652 */
653 for (i = 0; i < vlen && i < wlen; i++) {
654 int k = PyObject_RichCompareBool(vt->ob_item[i],
655 wt->ob_item[i], Py_EQ);
656 if (k < 0)
657 return NULL;
658 if (!k)
659 break;
660 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 if (i >= vlen || i >= wlen) {
663 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100664 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 /* We have an item that differs -- shortcuts for EQ/NE */
668 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200669 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 }
671 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200672 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 /* Compare the final item again using the proper operator */
676 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000677}
678
Jeremy Hylton938ace62002-07-17 16:30:39 +0000679static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200680tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
681
682/*[clinic input]
683@classmethod
684tuple.__new__ as tuple_new
685 iterable: object(c_default="NULL") = ()
686 /
687
688Built-in immutable sequence.
689
690If no argument is given, the constructor returns an empty tuple.
691If iterable is specified the tuple is initialized from iterable's items.
692
693If the argument is a tuple, the return value is the same object.
694[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000695
Tim Peters6d6c1a32001-08-02 04:15:00 +0000696static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200697tuple_new_impl(PyTypeObject *type, PyObject *iterable)
698/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200701 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000702
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200703 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 return PyTuple_New(0);
705 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200706 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707}
708
Guido van Rossumae960af2001-08-30 03:11:59 +0000709static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200710tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 PyObject *tmp, *newobj, *item;
713 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200716 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 if (tmp == NULL)
718 return NULL;
719 assert(PyTuple_Check(tmp));
720 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
721 if (newobj == NULL)
722 return NULL;
723 for (i = 0; i < n; i++) {
724 item = PyTuple_GET_ITEM(tmp, i);
725 Py_INCREF(item);
726 PyTuple_SET_ITEM(newobj, i, item);
727 }
728 Py_DECREF(tmp);
729 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000730}
731
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 (lenfunc)tuplelength, /* sq_length */
734 (binaryfunc)tupleconcat, /* sq_concat */
735 (ssizeargfunc)tuplerepeat, /* sq_repeat */
736 (ssizeargfunc)tupleitem, /* sq_item */
737 0, /* sq_slice */
738 0, /* sq_ass_item */
739 0, /* sq_ass_slice */
740 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000741};
742
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000743static PyObject*
744tuplesubscript(PyTupleObject* self, PyObject* item)
745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 if (PyIndex_Check(item)) {
747 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
748 if (i == -1 && PyErr_Occurred())
749 return NULL;
750 if (i < 0)
751 i += PyTuple_GET_SIZE(self);
752 return tupleitem(self, i);
753 }
754 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600755 Py_ssize_t start, stop, step, slicelength, i;
756 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyObject* result;
758 PyObject* it;
759 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000760
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300761 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 return NULL;
763 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300764 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
765 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (slicelength <= 0) {
768 return PyTuple_New(0);
769 }
770 else if (start == 0 && step == 1 &&
771 slicelength == PyTuple_GET_SIZE(self) &&
772 PyTuple_CheckExact(self)) {
773 Py_INCREF(self);
774 return (PyObject *)self;
775 }
776 else {
777 result = PyTuple_New(slicelength);
778 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 src = self->ob_item;
781 dest = ((PyTupleObject *)result)->ob_item;
782 for (cur = start, i = 0; i < slicelength;
783 cur += step, i++) {
784 it = src[cur];
785 Py_INCREF(it);
786 dest[i] = it;
787 }
788
789 return result;
790 }
791 }
792 else {
793 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400794 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 Py_TYPE(item)->tp_name);
796 return NULL;
797 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000798}
799
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200800/*[clinic input]
801tuple.__getnewargs__
802[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200804static PyObject *
805tuple___getnewargs___impl(PyTupleObject *self)
806/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
807{
808 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000809}
810
811static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200812 TUPLE___GETNEWARGS___METHODDEF
813 TUPLE_INDEX_METHODDEF
814 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000816};
817
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000818static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 (lenfunc)tuplelength,
820 (binaryfunc)tuplesubscript,
821 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000822};
823
Raymond Hettinger48923c52002-08-09 01:30:17 +0000824static PyObject *tuple_iter(PyObject *seq);
825
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 PyVarObject_HEAD_INIT(&PyType_Type, 0)
828 "tuple",
829 sizeof(PyTupleObject) - sizeof(PyObject *),
830 sizeof(PyObject *),
831 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200832 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 0, /* tp_getattr */
834 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200835 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 (reprfunc)tuplerepr, /* tp_repr */
837 0, /* tp_as_number */
838 &tuple_as_sequence, /* tp_as_sequence */
839 &tuple_as_mapping, /* tp_as_mapping */
840 (hashfunc)tuplehash, /* tp_hash */
841 0, /* tp_call */
842 0, /* tp_str */
843 PyObject_GenericGetAttr, /* tp_getattro */
844 0, /* tp_setattro */
845 0, /* tp_as_buffer */
846 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
847 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200848 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 (traverseproc)tupletraverse, /* tp_traverse */
850 0, /* tp_clear */
851 tuplerichcompare, /* tp_richcompare */
852 0, /* tp_weaklistoffset */
853 tuple_iter, /* tp_iter */
854 0, /* tp_iternext */
855 tuple_methods, /* tp_methods */
856 0, /* tp_members */
857 0, /* tp_getset */
858 0, /* tp_base */
859 0, /* tp_dict */
860 0, /* tp_descr_get */
861 0, /* tp_descr_set */
862 0, /* tp_dictoffset */
863 0, /* tp_init */
864 0, /* tp_alloc */
865 tuple_new, /* tp_new */
866 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000867};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000868
869/* The following function breaks the notion that tuples are immutable:
870 it changes the size of a tuple. We get away with this only if there
871 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000872 as creating a new tuple object and destroying the old one, only more
873 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000874 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000875
876int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000877_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000878{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200879 PyTupleObject *v;
880 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_ssize_t i;
882 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 v = (PyTupleObject *) *pv;
885 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
886 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
887 *pv = 0;
888 Py_XDECREF(v);
889 PyErr_BadInternalCall();
890 return -1;
891 }
892 oldsize = Py_SIZE(v);
893 if (oldsize == newsize)
894 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 if (oldsize == 0) {
897 /* Empty tuples are often shared, so we should never
898 resize them in-place even if we do own the only
899 (current) reference */
900 Py_DECREF(v);
901 *pv = PyTuple_New(newsize);
902 return *pv == NULL ? -1 : 0;
903 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 /* XXX UNREF/NEWREF interface should be more symmetrical */
906 _Py_DEC_REFTOTAL;
907 if (_PyObject_GC_IS_TRACKED(v))
908 _PyObject_GC_UNTRACK(v);
909 _Py_ForgetReference((PyObject *) v);
910 /* DECREF items deleted by shrinkage */
911 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200912 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 }
914 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
915 if (sv == NULL) {
916 *pv = NULL;
917 PyObject_GC_Del(v);
918 return -1;
919 }
920 _Py_NewReference((PyObject *) sv);
921 /* Zero out items added by growing */
922 if (newsize > oldsize)
923 memset(&sv->ob_item[oldsize], 0,
924 sizeof(*sv->ob_item) * (newsize - oldsize));
925 *pv = (PyObject *) sv;
926 _PyObject_GC_TRACK(sv);
927 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000928}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000929
Christian Heimesa156e092008-02-16 07:38:31 +0000930int
931PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000934#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 int i;
936 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
937 PyTupleObject *p, *q;
938 p = free_list[i];
939 freelist_size += numfree[i];
940 free_list[i] = NULL;
941 numfree[i] = 0;
942 while (p) {
943 q = p;
944 p = (PyTupleObject *)(p->ob_item[0]);
945 PyObject_GC_Del(q);
946 }
947 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000948#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000950}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951
Christian Heimesa156e092008-02-16 07:38:31 +0000952void
953PyTuple_Fini(void)
954{
955#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 /* empty tuples are used all over the place and applications may
957 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200958 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000961#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000962#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000964#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000965}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000966
967/*********************** Tuple Iterator **************************/
968
969typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200971 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973} tupleiterobject;
974
Raymond Hettinger48923c52002-08-09 01:30:17 +0000975static void
976tupleiter_dealloc(tupleiterobject *it)
977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 _PyObject_GC_UNTRACK(it);
979 Py_XDECREF(it->it_seq);
980 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000981}
982
983static int
984tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_VISIT(it->it_seq);
987 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000988}
989
Raymond Hettinger48923c52002-08-09 01:30:17 +0000990static PyObject *
991tupleiter_next(tupleiterobject *it)
992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyTupleObject *seq;
994 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 assert(it != NULL);
997 seq = it->it_seq;
998 if (seq == NULL)
999 return NULL;
1000 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1003 item = PyTuple_GET_ITEM(seq, it->it_index);
1004 ++it->it_index;
1005 Py_INCREF(item);
1006 return item;
1007 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001010 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001012}
1013
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001014static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301015tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 Py_ssize_t len = 0;
1018 if (it->it_seq)
1019 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1020 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001021}
1022
Armin Rigof5b3e362006-02-11 21:32:43 +00001023PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001024
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001025static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301026tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001027{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001028 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001029 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001030 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001031 it->it_seq, it->it_index);
1032 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001033 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001034}
1035
1036static PyObject *
1037tupleiter_setstate(tupleiterobject *it, PyObject *state)
1038{
Victor Stinner7660b882013-06-24 23:59:24 +02001039 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001040 if (index == -1 && PyErr_Occurred())
1041 return NULL;
1042 if (it->it_seq != NULL) {
1043 if (index < 0)
1044 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001045 else if (index > PyTuple_GET_SIZE(it->it_seq))
1046 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001047 it->it_index = index;
1048 }
1049 Py_RETURN_NONE;
1050}
1051
1052PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1053PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1054
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001055static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001057 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1058 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001060};
1061
Raymond Hettinger48923c52002-08-09 01:30:17 +00001062PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1064 "tuple_iterator", /* tp_name */
1065 sizeof(tupleiterobject), /* tp_basicsize */
1066 0, /* tp_itemsize */
1067 /* methods */
1068 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001069 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 0, /* tp_getattr */
1071 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001072 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 0, /* tp_repr */
1074 0, /* tp_as_number */
1075 0, /* tp_as_sequence */
1076 0, /* tp_as_mapping */
1077 0, /* tp_hash */
1078 0, /* tp_call */
1079 0, /* tp_str */
1080 PyObject_GenericGetAttr, /* tp_getattro */
1081 0, /* tp_setattro */
1082 0, /* tp_as_buffer */
1083 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1084 0, /* tp_doc */
1085 (traverseproc)tupleiter_traverse, /* tp_traverse */
1086 0, /* tp_clear */
1087 0, /* tp_richcompare */
1088 0, /* tp_weaklistoffset */
1089 PyObject_SelfIter, /* tp_iter */
1090 (iternextfunc)tupleiter_next, /* tp_iternext */
1091 tupleiter_methods, /* tp_methods */
1092 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001093};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001094
1095static PyObject *
1096tuple_iter(PyObject *seq)
1097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (!PyTuple_Check(seq)) {
1101 PyErr_BadInternalCall();
1102 return NULL;
1103 }
1104 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1105 if (it == NULL)
1106 return NULL;
1107 it->it_index = 0;
1108 Py_INCREF(seq);
1109 it->it_seq = (PyTupleObject *)seq;
1110 _PyObject_GC_TRACK(it);
1111 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001112}