blob: 3419baa529a6f031f0ceffbfaa60eda431a7382f [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
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050062static inline void
63tuple_gc_track(PyTupleObject *op)
64{
65#ifdef SHOW_TRACK_COUNT
66 count_tracked++;
67#endif
68 _PyObject_GC_TRACK(op);
69}
70
David Malcolm49526f42012-06-22 14:55:41 -040071/* Print summary info about the state of the optimized allocator */
72void
73_PyTuple_DebugMallocStats(FILE *out)
74{
75#if PyTuple_MAXSAVESIZE > 0
76 int i;
77 char buf[128];
78 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
79 PyOS_snprintf(buf, sizeof(buf),
80 "free %d-sized PyTupleObject", i);
81 _PyDebugAllocatorStats(out,
82 buf,
83 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
84 }
85#endif
86}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000087
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050088/* Allocate an uninitialized tuple object. Before making it public following
89 steps must be done:
90 - initialize its items
91 - call tuple_gc_track() on it
92 Because the empty tuple is always reused and it's already tracked by GC,
93 this function must not be called with size == 0 (unless from PyTuple_New()
94 which wraps this function).
95*/
96static PyTupleObject *
97tuple_alloc(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020099 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (size < 0) {
101 PyErr_BadInternalCall();
102 return NULL;
103 }
Christian Heimes2202f872008-02-06 14:31:34 +0000104#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500106 assert(size != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 free_list[size] = (PyTupleObject *) op->ob_item[0];
108 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000109#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +0000110 _Py_fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000111#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000113#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 Py_SIZE(op) = size;
115 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000116#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 _Py_NewReference((PyObject *)op);
118 }
119 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000120#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200123 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100124 sizeof(PyObject *)) / sizeof(PyObject *)) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500125 return (PyTupleObject *)PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
128 if (op == NULL)
129 return NULL;
130 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500131 return op;
132}
133
134PyObject *
135PyTuple_New(Py_ssize_t size)
136{
137 PyTupleObject *op;
138#if PyTuple_MAXSAVESIZE > 0
139 if (size == 0 && free_list[0]) {
140 op = free_list[0];
141 Py_INCREF(op);
142#ifdef COUNT_ALLOCS
143 _Py_tuple_zero_allocs++;
144#endif
145 return (PyObject *) op;
146 }
147#endif
148 op = tuple_alloc(size);
149 for (Py_ssize_t i = 0; i < size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 op->ob_item[i] = NULL;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500151 }
Christian Heimes2202f872008-02-06 14:31:34 +0000152#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (size == 0) {
154 free_list[0] = op;
155 ++numfree[0];
156 Py_INCREF(op); /* extra INCREF so that this is never freed */
157 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000158#endif
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500159 tuple_gc_track(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161}
162
Martin v. Löwis18e16552006-02-15 17:27:45 +0000163Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200164PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 if (!PyTuple_Check(op)) {
167 PyErr_BadInternalCall();
168 return -1;
169 }
170 else
171 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172}
173
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200175PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (!PyTuple_Check(op)) {
178 PyErr_BadInternalCall();
179 return NULL;
180 }
181 if (i < 0 || i >= Py_SIZE(op)) {
182 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
183 return NULL;
184 }
185 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186}
187
188int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200189PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200191 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
193 Py_XDECREF(newitem);
194 PyErr_BadInternalCall();
195 return -1;
196 }
197 if (i < 0 || i >= Py_SIZE(op)) {
198 Py_XDECREF(newitem);
199 PyErr_SetString(PyExc_IndexError,
200 "tuple assignment index out of range");
201 return -1;
202 }
203 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300204 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206}
207
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000208void
209_PyTuple_MaybeUntrack(PyObject *op)
210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 PyTupleObject *t;
212 Py_ssize_t i, n;
213
214 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
215 return;
216 t = (PyTupleObject *) op;
217 n = Py_SIZE(t);
218 for (i = 0; i < n; i++) {
219 PyObject *elt = PyTuple_GET_ITEM(t, i);
220 /* Tuple with NULL elements aren't
221 fully constructed, don't untrack
222 them yet. */
223 if (!elt ||
224 _PyObject_GC_MAY_BE_TRACKED(elt))
225 return;
226 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000227#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 count_tracked--;
229 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000230#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000232}
233
Raymond Hettingercb2da432003-10-12 18:24:34 +0000234PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000235PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 Py_ssize_t i;
238 PyObject *o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyObject **items;
240 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000241
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500242 if (n == 0) {
243 return PyTuple_New(0);
244 }
245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 va_start(vargs, n);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500247 PyTupleObject *result = tuple_alloc(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200248 if (result == NULL) {
249 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200251 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500252 items = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 for (i = 0; i < n; i++) {
254 o = va_arg(vargs, PyObject *);
255 Py_INCREF(o);
256 items[i] = o;
257 }
258 va_end(vargs);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500259 tuple_gc_track(result);
260 return (PyObject *)result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000261}
262
263
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264/* Methods */
265
266static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200267tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200269 Py_ssize_t i;
270 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200272 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (len > 0) {
274 i = len;
275 while (--i >= 0)
276 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000277#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (len < PyTuple_MAXSAVESIZE &&
279 numfree[len] < PyTuple_MAXFREELIST &&
280 Py_TYPE(op) == &PyTuple_Type)
281 {
282 op->ob_item[0] = (PyObject *) free_list[len];
283 numfree[len]++;
284 free_list[len] = op;
285 goto done; /* return */
286 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000287#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
289 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000290done:
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200291 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292}
293
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000295tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100298 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 n = Py_SIZE(v);
301 if (n == 0)
302 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 /* While not mutable, it is still possible to end up with a cycle in a
305 tuple through an object that stores itself within a tuple (and thus
306 infinitely asks for the repr of itself). This should only be
307 possible within a type. */
308 i = Py_ReprEnter((PyObject *)v);
309 if (i != 0) {
310 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
311 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000312
Victor Stinner88a9cd92013-11-19 12:59:46 +0100313 _PyUnicodeWriter_Init(&writer);
314 writer.overallocate = 1;
315 if (Py_SIZE(v) > 1) {
316 /* "(" + "1" + ", 2" * (len - 1) + ")" */
317 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
318 }
319 else {
320 /* "(1,)" */
321 writer.min_length = 4;
322 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200323
Victor Stinner88a9cd92013-11-19 12:59:46 +0100324 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200325 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* Do repr() on each element. */
328 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100329 PyObject *s;
330
331 if (i > 0) {
332 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
333 goto error;
334 }
335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100337 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200338 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100339
340 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
341 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200342 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100343 }
344 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100346
347 writer.overallocate = 0;
348 if (n > 1) {
349 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
350 goto error;
351 }
352 else {
353 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
354 goto error;
355 }
Tim Petersa7259592001-06-16 05:11:17 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100358 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200359
360error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100361 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200362 Py_ReprLeave((PyObject *)v);
363 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364}
365
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000366
jdemeyeraeb1be52018-10-28 02:06:38 +0200367/* Hash for tuples. This is a slightly simplified version of the xxHash
368 non-cryptographic hash:
369 - we do not use any parallellism, there is only 1 accumulator.
370 - we drop the final mixing since this is just a permutation of the
371 output space: it does not help against collisions.
372 - at the end, we mangle the length with a single constant.
373 For the xxHash specification, see
374 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100375
jdemeyeraeb1be52018-10-28 02:06:38 +0200376 Below are the official constants from the xxHash specification. Optimizing
377 compilers should emit a single "rotate" instruction for the
378 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
379 platform, the macro could be changed to expand to a platform-specific rotate
380 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000381*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200382#if SIZEOF_PY_UHASH_T > 4
383#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
384#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
385#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
386#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
387#else
388#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
389#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
390#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
391#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
392#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000393
jdemeyeraeb1be52018-10-28 02:06:38 +0200394/* Tests have shown that it's not worth to cache the hash value, see
395 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000396static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000397tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000398{
jdemeyeraeb1be52018-10-28 02:06:38 +0200399 Py_ssize_t i, len = Py_SIZE(v);
400 PyObject **item = v->ob_item;
401
402 Py_uhash_t acc = _PyHASH_XXPRIME_5;
403 for (i = 0; i < len; i++) {
404 Py_uhash_t lane = PyObject_Hash(item[i]);
405 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200407 }
408 acc += lane * _PyHASH_XXPRIME_2;
409 acc = _PyHASH_XXROTATE(acc);
410 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200412
413 /* Add input length, mangled to keep the historical value of hash(()). */
414 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
415
416 if (acc == (Py_uhash_t)-1) {
417 return 1546275796;
418 }
419 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000420}
421
Martin v. Löwis18e16552006-02-15 17:27:45 +0000422static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000423tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426}
427
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000428static int
Fred Drakeba096332000-07-09 07:04:36 +0000429tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 Py_ssize_t i;
432 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300435 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200440tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (i < 0 || i >= Py_SIZE(a)) {
443 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
444 return NULL;
445 }
446 Py_INCREF(a->ob_item[i]);
447 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448}
449
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500450PyObject *
451_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
452{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500453 if (n == 0) {
454 return PyTuple_New(0);
455 }
456
457 PyTupleObject *tuple = tuple_alloc(n);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500458 if (tuple == NULL) {
459 return NULL;
460 }
461 PyObject **dst = tuple->ob_item;
462 for (Py_ssize_t i = 0; i < n; i++) {
463 PyObject *item = src[i];
464 Py_INCREF(item);
465 dst[i] = item;
466 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500467 tuple_gc_track(tuple);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500468 return (PyObject *)tuple;
469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200472tupleslice(PyTupleObject *a, Py_ssize_t ilow,
473 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (ilow < 0)
476 ilow = 0;
477 if (ihigh > Py_SIZE(a))
478 ihigh = Py_SIZE(a);
479 if (ihigh < ilow)
480 ihigh = ilow;
481 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
482 Py_INCREF(a);
483 return (PyObject *)a;
484 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500485 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486}
487
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000489PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (op == NULL || !PyTuple_Check(op)) {
492 PyErr_BadInternalCall();
493 return NULL;
494 }
495 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000496}
497
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000498static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200499tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200501 Py_ssize_t size;
502 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 PyObject **src, **dest;
504 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200505 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
506 Py_INCREF(bb);
507 return bb;
508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 if (!PyTuple_Check(bb)) {
510 PyErr_Format(PyExc_TypeError,
511 "can only concatenate tuple (not \"%.200s\") to tuple",
512 Py_TYPE(bb)->tp_name);
513 return NULL;
514 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200516 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
517 Py_INCREF(a);
518 return (PyObject *)a;
519 }
Martin Panterb93d8632016-07-25 02:39:20 +0000520 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000522 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500523 if (size == 0) {
524 return PyTuple_New(0);
525 }
526
527 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (np == NULL) {
529 return NULL;
530 }
531 src = a->ob_item;
532 dest = np->ob_item;
533 for (i = 0; i < Py_SIZE(a); i++) {
534 PyObject *v = src[i];
535 Py_INCREF(v);
536 dest[i] = v;
537 }
538 src = b->ob_item;
539 dest = np->ob_item + Py_SIZE(a);
540 for (i = 0; i < Py_SIZE(b); i++) {
541 PyObject *v = src[i];
542 Py_INCREF(v);
543 dest[i] = v;
544 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500545 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547#undef b
548}
549
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000551tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 Py_ssize_t i, j;
554 Py_ssize_t size;
555 PyTupleObject *np;
556 PyObject **p, **items;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 if (Py_SIZE(a) == 0 || n == 1) {
558 if (PyTuple_CheckExact(a)) {
559 /* Since tuples are immutable, we can return a shared
560 copy in this case */
561 Py_INCREF(a);
562 return (PyObject *)a;
563 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500564 }
565 if (Py_SIZE(a) == 0 || n <= 0) {
566 return PyTuple_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100568 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100570 size = Py_SIZE(a) * n;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500571 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (np == NULL)
573 return NULL;
574 p = np->ob_item;
575 items = a->ob_item;
576 for (i = 0; i < n; i++) {
577 for (j = 0; j < Py_SIZE(a); j++) {
578 *p = items[j];
579 Py_INCREF(*p);
580 p++;
581 }
582 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500583 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000585}
586
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200587/*[clinic input]
588tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000589
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200590 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300591 start: slice_index(accept={int}) = 0
592 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200593 /
594
595Return first index of value.
596
597Raises ValueError if the value is not present.
598[clinic start generated code]*/
599
600static PyObject *
601tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
602 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300603/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200604{
605 Py_ssize_t i;
606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (start < 0) {
608 start += Py_SIZE(self);
609 if (start < 0)
610 start = 0;
611 }
612 if (stop < 0) {
613 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200615 else if (stop > Py_SIZE(self)) {
616 stop = Py_SIZE(self);
617 }
618 for (i = start; i < stop; i++) {
619 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 if (cmp > 0)
621 return PyLong_FromSsize_t(i);
622 else if (cmp < 0)
623 return NULL;
624 }
625 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
626 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000627}
628
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200629/*[clinic input]
630tuple.count
631
632 value: object
633 /
634
635Return number of occurrences of value.
636[clinic start generated code]*/
637
Raymond Hettinger65baa342008-02-07 00:41:02 +0000638static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200639tuple_count(PyTupleObject *self, PyObject *value)
640/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 Py_ssize_t count = 0;
643 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200646 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (cmp > 0)
648 count++;
649 else if (cmp < 0)
650 return NULL;
651 }
652 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000653}
654
Jeremy Hylton8caad492000-06-23 14:18:11 +0000655static int
656tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 for (i = Py_SIZE(o); --i >= 0; )
661 Py_VISIT(o->ob_item[i]);
662 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000663}
664
Guido van Rossumf77bc622001-01-18 00:00:53 +0000665static PyObject *
666tuplerichcompare(PyObject *v, PyObject *w, int op)
667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 PyTupleObject *vt, *wt;
669 Py_ssize_t i;
670 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000671
Brian Curtindfc80e32011-08-10 20:28:54 -0500672 if (!PyTuple_Check(v) || !PyTuple_Check(w))
673 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 vt = (PyTupleObject *)v;
676 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 vlen = Py_SIZE(vt);
679 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* Note: the corresponding code for lists has an "early out" test
682 * here when op is EQ or NE and the lengths differ. That pays there,
683 * but Tim was unable to find any real code where EQ/NE tuple
684 * compares don't have the same length, so testing for it here would
685 * have cost without benefit.
686 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 /* Search for the first index where items are different.
689 * Note that because tuples are immutable, it's safe to reuse
690 * vlen and wlen across the comparison calls.
691 */
692 for (i = 0; i < vlen && i < wlen; i++) {
693 int k = PyObject_RichCompareBool(vt->ob_item[i],
694 wt->ob_item[i], Py_EQ);
695 if (k < 0)
696 return NULL;
697 if (!k)
698 break;
699 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (i >= vlen || i >= wlen) {
702 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100703 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 /* We have an item that differs -- shortcuts for EQ/NE */
707 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200708 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 }
710 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200711 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* Compare the final item again using the proper operator */
715 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000716}
717
Jeremy Hylton938ace62002-07-17 16:30:39 +0000718static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200719tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
720
721/*[clinic input]
722@classmethod
723tuple.__new__ as tuple_new
724 iterable: object(c_default="NULL") = ()
725 /
726
727Built-in immutable sequence.
728
729If no argument is given, the constructor returns an empty tuple.
730If iterable is specified the tuple is initialized from iterable's items.
731
732If the argument is a tuple, the return value is the same object.
733[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000734
Tim Peters6d6c1a32001-08-02 04:15:00 +0000735static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200736tuple_new_impl(PyTypeObject *type, PyObject *iterable)
737/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200740 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000741
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200742 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 return PyTuple_New(0);
744 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200745 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746}
747
Guido van Rossumae960af2001-08-30 03:11:59 +0000748static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200749tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyObject *tmp, *newobj, *item;
752 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200755 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 if (tmp == NULL)
757 return NULL;
758 assert(PyTuple_Check(tmp));
759 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
760 if (newobj == NULL)
761 return NULL;
762 for (i = 0; i < n; i++) {
763 item = PyTuple_GET_ITEM(tmp, i);
764 Py_INCREF(item);
765 PyTuple_SET_ITEM(newobj, i, item);
766 }
767 Py_DECREF(tmp);
768 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000769}
770
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 (lenfunc)tuplelength, /* sq_length */
773 (binaryfunc)tupleconcat, /* sq_concat */
774 (ssizeargfunc)tuplerepeat, /* sq_repeat */
775 (ssizeargfunc)tupleitem, /* sq_item */
776 0, /* sq_slice */
777 0, /* sq_ass_item */
778 0, /* sq_ass_slice */
779 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780};
781
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000782static PyObject*
783tuplesubscript(PyTupleObject* self, PyObject* item)
784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (PyIndex_Check(item)) {
786 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
787 if (i == -1 && PyErr_Occurred())
788 return NULL;
789 if (i < 0)
790 i += PyTuple_GET_SIZE(self);
791 return tupleitem(self, i);
792 }
793 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600794 Py_ssize_t start, stop, step, slicelength, i;
795 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyObject* it;
797 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000798
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300799 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return NULL;
801 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300802 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
803 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 if (slicelength <= 0) {
806 return PyTuple_New(0);
807 }
808 else if (start == 0 && step == 1 &&
809 slicelength == PyTuple_GET_SIZE(self) &&
810 PyTuple_CheckExact(self)) {
811 Py_INCREF(self);
812 return (PyObject *)self;
813 }
814 else {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500815 PyTupleObject* result = tuple_alloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 src = self->ob_item;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500819 dest = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 for (cur = start, i = 0; i < slicelength;
821 cur += step, i++) {
822 it = src[cur];
823 Py_INCREF(it);
824 dest[i] = it;
825 }
826
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500827 tuple_gc_track(result);
828 return (PyObject *)result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 }
830 }
831 else {
832 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400833 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 Py_TYPE(item)->tp_name);
835 return NULL;
836 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000837}
838
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200839/*[clinic input]
840tuple.__getnewargs__
841[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200843static PyObject *
844tuple___getnewargs___impl(PyTupleObject *self)
845/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
846{
847 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000848}
849
850static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200851 TUPLE___GETNEWARGS___METHODDEF
852 TUPLE_INDEX_METHODDEF
853 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000855};
856
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000857static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 (lenfunc)tuplelength,
859 (binaryfunc)tuplesubscript,
860 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000861};
862
Raymond Hettinger48923c52002-08-09 01:30:17 +0000863static PyObject *tuple_iter(PyObject *seq);
864
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000865PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
867 "tuple",
868 sizeof(PyTupleObject) - sizeof(PyObject *),
869 sizeof(PyObject *),
870 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200871 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 0, /* tp_getattr */
873 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200874 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 (reprfunc)tuplerepr, /* tp_repr */
876 0, /* tp_as_number */
877 &tuple_as_sequence, /* tp_as_sequence */
878 &tuple_as_mapping, /* tp_as_mapping */
879 (hashfunc)tuplehash, /* tp_hash */
880 0, /* tp_call */
881 0, /* tp_str */
882 PyObject_GenericGetAttr, /* tp_getattro */
883 0, /* tp_setattro */
884 0, /* tp_as_buffer */
885 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
886 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200887 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 (traverseproc)tupletraverse, /* tp_traverse */
889 0, /* tp_clear */
890 tuplerichcompare, /* tp_richcompare */
891 0, /* tp_weaklistoffset */
892 tuple_iter, /* tp_iter */
893 0, /* tp_iternext */
894 tuple_methods, /* tp_methods */
895 0, /* tp_members */
896 0, /* tp_getset */
897 0, /* tp_base */
898 0, /* tp_dict */
899 0, /* tp_descr_get */
900 0, /* tp_descr_set */
901 0, /* tp_dictoffset */
902 0, /* tp_init */
903 0, /* tp_alloc */
904 tuple_new, /* tp_new */
905 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000906};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907
908/* The following function breaks the notion that tuples are immutable:
909 it changes the size of a tuple. We get away with this only if there
910 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000911 as creating a new tuple object and destroying the old one, only more
912 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000913 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000914
915int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000916_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000917{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200918 PyTupleObject *v;
919 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_ssize_t i;
921 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 v = (PyTupleObject *) *pv;
924 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
925 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
926 *pv = 0;
927 Py_XDECREF(v);
928 PyErr_BadInternalCall();
929 return -1;
930 }
931 oldsize = Py_SIZE(v);
932 if (oldsize == newsize)
933 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (oldsize == 0) {
936 /* Empty tuples are often shared, so we should never
937 resize them in-place even if we do own the only
938 (current) reference */
939 Py_DECREF(v);
940 *pv = PyTuple_New(newsize);
941 return *pv == NULL ? -1 : 0;
942 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 /* XXX UNREF/NEWREF interface should be more symmetrical */
945 _Py_DEC_REFTOTAL;
946 if (_PyObject_GC_IS_TRACKED(v))
947 _PyObject_GC_UNTRACK(v);
948 _Py_ForgetReference((PyObject *) v);
949 /* DECREF items deleted by shrinkage */
950 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200951 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 }
953 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
954 if (sv == NULL) {
955 *pv = NULL;
956 PyObject_GC_Del(v);
957 return -1;
958 }
959 _Py_NewReference((PyObject *) sv);
960 /* Zero out items added by growing */
961 if (newsize > oldsize)
962 memset(&sv->ob_item[oldsize], 0,
963 sizeof(*sv->ob_item) * (newsize - oldsize));
964 *pv = (PyObject *) sv;
965 _PyObject_GC_TRACK(sv);
966 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000967}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000968
Christian Heimesa156e092008-02-16 07:38:31 +0000969int
970PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000973#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 int i;
975 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
976 PyTupleObject *p, *q;
977 p = free_list[i];
978 freelist_size += numfree[i];
979 free_list[i] = NULL;
980 numfree[i] = 0;
981 while (p) {
982 q = p;
983 p = (PyTupleObject *)(p->ob_item[0]);
984 PyObject_GC_Del(q);
985 }
986 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000987#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000989}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990
Christian Heimesa156e092008-02-16 07:38:31 +0000991void
Victor Stinnerbed48172019-08-27 00:12:32 +0200992_PyTuple_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000993{
994#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 /* empty tuples are used all over the place and applications may
996 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200997 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +00001000#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001001#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001003#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001004}
Raymond Hettinger48923c52002-08-09 01:30:17 +00001005
1006/*********************** Tuple Iterator **************************/
1007
1008typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +02001010 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +00001012} tupleiterobject;
1013
Raymond Hettinger48923c52002-08-09 01:30:17 +00001014static void
1015tupleiter_dealloc(tupleiterobject *it)
1016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 _PyObject_GC_UNTRACK(it);
1018 Py_XDECREF(it->it_seq);
1019 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +00001020}
1021
1022static int
1023tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
1024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 Py_VISIT(it->it_seq);
1026 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001027}
1028
Raymond Hettinger48923c52002-08-09 01:30:17 +00001029static PyObject *
1030tupleiter_next(tupleiterobject *it)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyTupleObject *seq;
1033 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 assert(it != NULL);
1036 seq = it->it_seq;
1037 if (seq == NULL)
1038 return NULL;
1039 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1042 item = PyTuple_GET_ITEM(seq, it->it_index);
1043 ++it->it_index;
1044 Py_INCREF(item);
1045 return item;
1046 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001049 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001051}
1052
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001053static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301054tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 Py_ssize_t len = 0;
1057 if (it->it_seq)
1058 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1059 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001060}
1061
Armin Rigof5b3e362006-02-11 21:32:43 +00001062PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001063
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001064static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301065tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001066{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001067 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001068 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001069 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001070 it->it_seq, it->it_index);
1071 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001072 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001073}
1074
1075static PyObject *
1076tupleiter_setstate(tupleiterobject *it, PyObject *state)
1077{
Victor Stinner7660b882013-06-24 23:59:24 +02001078 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001079 if (index == -1 && PyErr_Occurred())
1080 return NULL;
1081 if (it->it_seq != NULL) {
1082 if (index < 0)
1083 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001084 else if (index > PyTuple_GET_SIZE(it->it_seq))
1085 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001086 it->it_index = index;
1087 }
1088 Py_RETURN_NONE;
1089}
1090
1091PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1092PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1093
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001094static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001096 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1097 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001099};
1100
Raymond Hettinger48923c52002-08-09 01:30:17 +00001101PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1103 "tuple_iterator", /* tp_name */
1104 sizeof(tupleiterobject), /* tp_basicsize */
1105 0, /* tp_itemsize */
1106 /* methods */
1107 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001108 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 0, /* tp_getattr */
1110 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001111 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 0, /* tp_repr */
1113 0, /* tp_as_number */
1114 0, /* tp_as_sequence */
1115 0, /* tp_as_mapping */
1116 0, /* tp_hash */
1117 0, /* tp_call */
1118 0, /* tp_str */
1119 PyObject_GenericGetAttr, /* tp_getattro */
1120 0, /* tp_setattro */
1121 0, /* tp_as_buffer */
1122 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1123 0, /* tp_doc */
1124 (traverseproc)tupleiter_traverse, /* tp_traverse */
1125 0, /* tp_clear */
1126 0, /* tp_richcompare */
1127 0, /* tp_weaklistoffset */
1128 PyObject_SelfIter, /* tp_iter */
1129 (iternextfunc)tupleiter_next, /* tp_iternext */
1130 tupleiter_methods, /* tp_methods */
1131 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001132};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001133
1134static PyObject *
1135tuple_iter(PyObject *seq)
1136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (!PyTuple_Check(seq)) {
1140 PyErr_BadInternalCall();
1141 return NULL;
1142 }
1143 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1144 if (it == NULL)
1145 return NULL;
1146 it->it_index = 0;
1147 Py_INCREF(seq);
1148 it->it_seq = (PyTupleObject *)seq;
1149 _PyObject_GC_TRACK(it);
1150 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001151}