blob: a72257f95b083be4e9f5d4246a32a3ef4a0e6028 [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);
Zackery Spytz60bd1f82019-09-04 07:58:05 -0600149 if (op == NULL) {
150 return NULL;
151 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500152 for (Py_ssize_t i = 0; i < size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 op->ob_item[i] = NULL;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500154 }
Christian Heimes2202f872008-02-06 14:31:34 +0000155#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (size == 0) {
157 free_list[0] = op;
158 ++numfree[0];
159 Py_INCREF(op); /* extra INCREF so that this is never freed */
160 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000161#endif
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500162 tuple_gc_track(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164}
165
Martin v. Löwis18e16552006-02-15 17:27:45 +0000166Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200167PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 if (!PyTuple_Check(op)) {
170 PyErr_BadInternalCall();
171 return -1;
172 }
173 else
174 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175}
176
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200178PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 if (!PyTuple_Check(op)) {
181 PyErr_BadInternalCall();
182 return NULL;
183 }
184 if (i < 0 || i >= Py_SIZE(op)) {
185 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
186 return NULL;
187 }
188 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189}
190
191int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200194 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
196 Py_XDECREF(newitem);
197 PyErr_BadInternalCall();
198 return -1;
199 }
200 if (i < 0 || i >= Py_SIZE(op)) {
201 Py_XDECREF(newitem);
202 PyErr_SetString(PyExc_IndexError,
203 "tuple assignment index out of range");
204 return -1;
205 }
206 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300207 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000211void
212_PyTuple_MaybeUntrack(PyObject *op)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 PyTupleObject *t;
215 Py_ssize_t i, n;
216
217 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
218 return;
219 t = (PyTupleObject *) op;
220 n = Py_SIZE(t);
221 for (i = 0; i < n; i++) {
222 PyObject *elt = PyTuple_GET_ITEM(t, i);
223 /* Tuple with NULL elements aren't
224 fully constructed, don't untrack
225 them yet. */
226 if (!elt ||
227 _PyObject_GC_MAY_BE_TRACKED(elt))
228 return;
229 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000230#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 count_tracked--;
232 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000235}
236
Raymond Hettingercb2da432003-10-12 18:24:34 +0000237PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000238PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 Py_ssize_t i;
241 PyObject *o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PyObject **items;
243 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000244
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500245 if (n == 0) {
246 return PyTuple_New(0);
247 }
248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 va_start(vargs, n);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500250 PyTupleObject *result = tuple_alloc(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200251 if (result == NULL) {
252 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200254 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500255 items = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 for (i = 0; i < n; i++) {
257 o = va_arg(vargs, PyObject *);
258 Py_INCREF(o);
259 items[i] = o;
260 }
261 va_end(vargs);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500262 tuple_gc_track(result);
263 return (PyObject *)result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000264}
265
266
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267/* Methods */
268
269static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200270tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200272 Py_ssize_t i;
273 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200275 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (len > 0) {
277 i = len;
278 while (--i >= 0)
279 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000280#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (len < PyTuple_MAXSAVESIZE &&
282 numfree[len] < PyTuple_MAXFREELIST &&
283 Py_TYPE(op) == &PyTuple_Type)
284 {
285 op->ob_item[0] = (PyObject *) free_list[len];
286 numfree[len]++;
287 free_list[len] = op;
288 goto done; /* return */
289 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 }
292 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000293done:
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200294 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295}
296
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000298tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100301 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 n = Py_SIZE(v);
304 if (n == 0)
305 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 /* While not mutable, it is still possible to end up with a cycle in a
308 tuple through an object that stores itself within a tuple (and thus
309 infinitely asks for the repr of itself). This should only be
310 possible within a type. */
311 i = Py_ReprEnter((PyObject *)v);
312 if (i != 0) {
313 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
314 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000315
Victor Stinner88a9cd92013-11-19 12:59:46 +0100316 _PyUnicodeWriter_Init(&writer);
317 writer.overallocate = 1;
318 if (Py_SIZE(v) > 1) {
319 /* "(" + "1" + ", 2" * (len - 1) + ")" */
320 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
321 }
322 else {
323 /* "(1,)" */
324 writer.min_length = 4;
325 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200326
Victor Stinner88a9cd92013-11-19 12:59:46 +0100327 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200328 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 /* Do repr() on each element. */
331 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100332 PyObject *s;
333
334 if (i > 0) {
335 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
336 goto error;
337 }
338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100340 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200341 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100342
343 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
344 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200345 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100346 }
347 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100349
350 writer.overallocate = 0;
351 if (n > 1) {
352 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
353 goto error;
354 }
355 else {
356 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
357 goto error;
358 }
Tim Petersa7259592001-06-16 05:11:17 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100361 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200362
363error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100364 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200365 Py_ReprLeave((PyObject *)v);
366 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367}
368
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000369
jdemeyeraeb1be52018-10-28 02:06:38 +0200370/* Hash for tuples. This is a slightly simplified version of the xxHash
371 non-cryptographic hash:
372 - we do not use any parallellism, there is only 1 accumulator.
373 - we drop the final mixing since this is just a permutation of the
374 output space: it does not help against collisions.
375 - at the end, we mangle the length with a single constant.
376 For the xxHash specification, see
377 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100378
jdemeyeraeb1be52018-10-28 02:06:38 +0200379 Below are the official constants from the xxHash specification. Optimizing
380 compilers should emit a single "rotate" instruction for the
381 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
382 platform, the macro could be changed to expand to a platform-specific rotate
383 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000384*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200385#if SIZEOF_PY_UHASH_T > 4
386#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
387#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
388#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
389#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
390#else
391#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
392#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
393#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
394#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
395#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000396
jdemeyeraeb1be52018-10-28 02:06:38 +0200397/* Tests have shown that it's not worth to cache the hash value, see
398 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000399static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000400tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000401{
jdemeyeraeb1be52018-10-28 02:06:38 +0200402 Py_ssize_t i, len = Py_SIZE(v);
403 PyObject **item = v->ob_item;
404
405 Py_uhash_t acc = _PyHASH_XXPRIME_5;
406 for (i = 0; i < len; i++) {
407 Py_uhash_t lane = PyObject_Hash(item[i]);
408 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200410 }
411 acc += lane * _PyHASH_XXPRIME_2;
412 acc = _PyHASH_XXROTATE(acc);
413 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200415
416 /* Add input length, mangled to keep the historical value of hash(()). */
417 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
418
419 if (acc == (Py_uhash_t)-1) {
420 return 1546275796;
421 }
422 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000423}
424
Martin v. Löwis18e16552006-02-15 17:27:45 +0000425static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000426tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429}
430
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000431static int
Fred Drakeba096332000-07-09 07:04:36 +0000432tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 Py_ssize_t i;
435 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300438 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000440}
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200443tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if (i < 0 || i >= Py_SIZE(a)) {
446 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
447 return NULL;
448 }
449 Py_INCREF(a->ob_item[i]);
450 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451}
452
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500453PyObject *
454_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
455{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500456 if (n == 0) {
457 return PyTuple_New(0);
458 }
459
460 PyTupleObject *tuple = tuple_alloc(n);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500461 if (tuple == NULL) {
462 return NULL;
463 }
464 PyObject **dst = tuple->ob_item;
465 for (Py_ssize_t i = 0; i < n; i++) {
466 PyObject *item = src[i];
467 Py_INCREF(item);
468 dst[i] = item;
469 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500470 tuple_gc_track(tuple);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500471 return (PyObject *)tuple;
472}
473
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200475tupleslice(PyTupleObject *a, Py_ssize_t ilow,
476 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (ilow < 0)
479 ilow = 0;
480 if (ihigh > Py_SIZE(a))
481 ihigh = Py_SIZE(a);
482 if (ihigh < ilow)
483 ihigh = ilow;
484 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
485 Py_INCREF(a);
486 return (PyObject *)a;
487 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500488 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489}
490
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000492PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 if (op == NULL || !PyTuple_Check(op)) {
495 PyErr_BadInternalCall();
496 return NULL;
497 }
498 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000499}
500
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200502tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000503{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200504 Py_ssize_t size;
505 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyObject **src, **dest;
507 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200508 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
509 Py_INCREF(bb);
510 return bb;
511 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (!PyTuple_Check(bb)) {
513 PyErr_Format(PyExc_TypeError,
514 "can only concatenate tuple (not \"%.200s\") to tuple",
515 Py_TYPE(bb)->tp_name);
516 return NULL;
517 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200519 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
520 Py_INCREF(a);
521 return (PyObject *)a;
522 }
Martin Panterb93d8632016-07-25 02:39:20 +0000523 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000525 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500526 if (size == 0) {
527 return PyTuple_New(0);
528 }
529
530 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (np == NULL) {
532 return NULL;
533 }
534 src = a->ob_item;
535 dest = np->ob_item;
536 for (i = 0; i < Py_SIZE(a); i++) {
537 PyObject *v = src[i];
538 Py_INCREF(v);
539 dest[i] = v;
540 }
541 src = b->ob_item;
542 dest = np->ob_item + Py_SIZE(a);
543 for (i = 0; i < Py_SIZE(b); i++) {
544 PyObject *v = src[i];
545 Py_INCREF(v);
546 dest[i] = v;
547 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500548 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550#undef b
551}
552
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000554tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 Py_ssize_t i, j;
557 Py_ssize_t size;
558 PyTupleObject *np;
559 PyObject **p, **items;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 if (Py_SIZE(a) == 0 || n == 1) {
561 if (PyTuple_CheckExact(a)) {
562 /* Since tuples are immutable, we can return a shared
563 copy in this case */
564 Py_INCREF(a);
565 return (PyObject *)a;
566 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500567 }
568 if (Py_SIZE(a) == 0 || n <= 0) {
569 return PyTuple_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100571 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100573 size = Py_SIZE(a) * n;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500574 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (np == NULL)
576 return NULL;
577 p = np->ob_item;
578 items = a->ob_item;
579 for (i = 0; i < n; i++) {
580 for (j = 0; j < Py_SIZE(a); j++) {
581 *p = items[j];
582 Py_INCREF(*p);
583 p++;
584 }
585 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500586 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000588}
589
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200590/*[clinic input]
591tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000592
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200593 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300594 start: slice_index(accept={int}) = 0
595 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200596 /
597
598Return first index of value.
599
600Raises ValueError if the value is not present.
601[clinic start generated code]*/
602
603static PyObject *
604tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
605 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300606/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200607{
608 Py_ssize_t i;
609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (start < 0) {
611 start += Py_SIZE(self);
612 if (start < 0)
613 start = 0;
614 }
615 if (stop < 0) {
616 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200618 else if (stop > Py_SIZE(self)) {
619 stop = Py_SIZE(self);
620 }
621 for (i = start; i < stop; i++) {
622 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if (cmp > 0)
624 return PyLong_FromSsize_t(i);
625 else if (cmp < 0)
626 return NULL;
627 }
628 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
629 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000630}
631
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200632/*[clinic input]
633tuple.count
634
635 value: object
636 /
637
638Return number of occurrences of value.
639[clinic start generated code]*/
640
Raymond Hettinger65baa342008-02-07 00:41:02 +0000641static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200642tuple_count(PyTupleObject *self, PyObject *value)
643/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 Py_ssize_t count = 0;
646 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200649 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (cmp > 0)
651 count++;
652 else if (cmp < 0)
653 return NULL;
654 }
655 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000656}
657
Jeremy Hylton8caad492000-06-23 14:18:11 +0000658static int
659tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 for (i = Py_SIZE(o); --i >= 0; )
664 Py_VISIT(o->ob_item[i]);
665 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000666}
667
Guido van Rossumf77bc622001-01-18 00:00:53 +0000668static PyObject *
669tuplerichcompare(PyObject *v, PyObject *w, int op)
670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PyTupleObject *vt, *wt;
672 Py_ssize_t i;
673 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000674
Brian Curtindfc80e32011-08-10 20:28:54 -0500675 if (!PyTuple_Check(v) || !PyTuple_Check(w))
676 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 vt = (PyTupleObject *)v;
679 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 vlen = Py_SIZE(vt);
682 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 /* Note: the corresponding code for lists has an "early out" test
685 * here when op is EQ or NE and the lengths differ. That pays there,
686 * but Tim was unable to find any real code where EQ/NE tuple
687 * compares don't have the same length, so testing for it here would
688 * have cost without benefit.
689 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 /* Search for the first index where items are different.
692 * Note that because tuples are immutable, it's safe to reuse
693 * vlen and wlen across the comparison calls.
694 */
695 for (i = 0; i < vlen && i < wlen; i++) {
696 int k = PyObject_RichCompareBool(vt->ob_item[i],
697 wt->ob_item[i], Py_EQ);
698 if (k < 0)
699 return NULL;
700 if (!k)
701 break;
702 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (i >= vlen || i >= wlen) {
705 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100706 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 /* We have an item that differs -- shortcuts for EQ/NE */
710 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200711 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 }
713 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200714 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 /* Compare the final item again using the proper operator */
718 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000719}
720
Jeremy Hylton938ace62002-07-17 16:30:39 +0000721static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200722tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
723
724/*[clinic input]
725@classmethod
726tuple.__new__ as tuple_new
727 iterable: object(c_default="NULL") = ()
728 /
729
730Built-in immutable sequence.
731
732If no argument is given, the constructor returns an empty tuple.
733If iterable is specified the tuple is initialized from iterable's items.
734
735If the argument is a tuple, the return value is the same object.
736[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000737
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200739tuple_new_impl(PyTypeObject *type, PyObject *iterable)
740/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200743 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200745 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 return PyTuple_New(0);
747 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200748 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749}
750
Guido van Rossumae960af2001-08-30 03:11:59 +0000751static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200752tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 PyObject *tmp, *newobj, *item;
755 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200758 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 if (tmp == NULL)
760 return NULL;
761 assert(PyTuple_Check(tmp));
762 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
763 if (newobj == NULL)
764 return NULL;
765 for (i = 0; i < n; i++) {
766 item = PyTuple_GET_ITEM(tmp, i);
767 Py_INCREF(item);
768 PyTuple_SET_ITEM(newobj, i, item);
769 }
770 Py_DECREF(tmp);
771 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000772}
773
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 (lenfunc)tuplelength, /* sq_length */
776 (binaryfunc)tupleconcat, /* sq_concat */
777 (ssizeargfunc)tuplerepeat, /* sq_repeat */
778 (ssizeargfunc)tupleitem, /* sq_item */
779 0, /* sq_slice */
780 0, /* sq_ass_item */
781 0, /* sq_ass_slice */
782 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000783};
784
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000785static PyObject*
786tuplesubscript(PyTupleObject* self, PyObject* item)
787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (PyIndex_Check(item)) {
789 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
790 if (i == -1 && PyErr_Occurred())
791 return NULL;
792 if (i < 0)
793 i += PyTuple_GET_SIZE(self);
794 return tupleitem(self, i);
795 }
796 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600797 Py_ssize_t start, stop, step, slicelength, i;
798 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyObject* it;
800 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000801
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300802 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 return NULL;
804 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300805 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
806 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 if (slicelength <= 0) {
809 return PyTuple_New(0);
810 }
811 else if (start == 0 && step == 1 &&
812 slicelength == PyTuple_GET_SIZE(self) &&
813 PyTuple_CheckExact(self)) {
814 Py_INCREF(self);
815 return (PyObject *)self;
816 }
817 else {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500818 PyTupleObject* result = tuple_alloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 src = self->ob_item;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500822 dest = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 for (cur = start, i = 0; i < slicelength;
824 cur += step, i++) {
825 it = src[cur];
826 Py_INCREF(it);
827 dest[i] = it;
828 }
829
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500830 tuple_gc_track(result);
831 return (PyObject *)result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 }
833 }
834 else {
835 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400836 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 Py_TYPE(item)->tp_name);
838 return NULL;
839 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000840}
841
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200842/*[clinic input]
843tuple.__getnewargs__
844[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200846static PyObject *
847tuple___getnewargs___impl(PyTupleObject *self)
848/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
849{
850 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000851}
852
853static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200854 TUPLE___GETNEWARGS___METHODDEF
855 TUPLE_INDEX_METHODDEF
856 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000858};
859
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000860static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 (lenfunc)tuplelength,
862 (binaryfunc)tuplesubscript,
863 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000864};
865
Raymond Hettinger48923c52002-08-09 01:30:17 +0000866static PyObject *tuple_iter(PyObject *seq);
867
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 PyVarObject_HEAD_INIT(&PyType_Type, 0)
870 "tuple",
871 sizeof(PyTupleObject) - sizeof(PyObject *),
872 sizeof(PyObject *),
873 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200874 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 0, /* tp_getattr */
876 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200877 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 (reprfunc)tuplerepr, /* tp_repr */
879 0, /* tp_as_number */
880 &tuple_as_sequence, /* tp_as_sequence */
881 &tuple_as_mapping, /* tp_as_mapping */
882 (hashfunc)tuplehash, /* tp_hash */
883 0, /* tp_call */
884 0, /* tp_str */
885 PyObject_GenericGetAttr, /* tp_getattro */
886 0, /* tp_setattro */
887 0, /* tp_as_buffer */
888 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
889 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200890 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 (traverseproc)tupletraverse, /* tp_traverse */
892 0, /* tp_clear */
893 tuplerichcompare, /* tp_richcompare */
894 0, /* tp_weaklistoffset */
895 tuple_iter, /* tp_iter */
896 0, /* tp_iternext */
897 tuple_methods, /* tp_methods */
898 0, /* tp_members */
899 0, /* tp_getset */
900 0, /* tp_base */
901 0, /* tp_dict */
902 0, /* tp_descr_get */
903 0, /* tp_descr_set */
904 0, /* tp_dictoffset */
905 0, /* tp_init */
906 0, /* tp_alloc */
907 tuple_new, /* tp_new */
908 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000910
911/* The following function breaks the notion that tuples are immutable:
912 it changes the size of a tuple. We get away with this only if there
913 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000914 as creating a new tuple object and destroying the old one, only more
915 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000916 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000917
918int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200921 PyTupleObject *v;
922 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 Py_ssize_t i;
924 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 v = (PyTupleObject *) *pv;
927 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
928 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
929 *pv = 0;
930 Py_XDECREF(v);
931 PyErr_BadInternalCall();
932 return -1;
933 }
934 oldsize = Py_SIZE(v);
935 if (oldsize == newsize)
936 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 if (oldsize == 0) {
939 /* Empty tuples are often shared, so we should never
940 resize them in-place even if we do own the only
941 (current) reference */
942 Py_DECREF(v);
943 *pv = PyTuple_New(newsize);
944 return *pv == NULL ? -1 : 0;
945 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* XXX UNREF/NEWREF interface should be more symmetrical */
948 _Py_DEC_REFTOTAL;
949 if (_PyObject_GC_IS_TRACKED(v))
950 _PyObject_GC_UNTRACK(v);
951 _Py_ForgetReference((PyObject *) v);
952 /* DECREF items deleted by shrinkage */
953 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200954 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 }
956 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
957 if (sv == NULL) {
958 *pv = NULL;
959 PyObject_GC_Del(v);
960 return -1;
961 }
962 _Py_NewReference((PyObject *) sv);
963 /* Zero out items added by growing */
964 if (newsize > oldsize)
965 memset(&sv->ob_item[oldsize], 0,
966 sizeof(*sv->ob_item) * (newsize - oldsize));
967 *pv = (PyObject *) sv;
968 _PyObject_GC_TRACK(sv);
969 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000971
Christian Heimesa156e092008-02-16 07:38:31 +0000972int
973PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000976#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 int i;
978 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
979 PyTupleObject *p, *q;
980 p = free_list[i];
981 freelist_size += numfree[i];
982 free_list[i] = NULL;
983 numfree[i] = 0;
984 while (p) {
985 q = p;
986 p = (PyTupleObject *)(p->ob_item[0]);
987 PyObject_GC_Del(q);
988 }
989 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000990#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000992}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993
Christian Heimesa156e092008-02-16 07:38:31 +0000994void
Victor Stinnerbed48172019-08-27 00:12:32 +0200995_PyTuple_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000996{
997#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 /* empty tuples are used all over the place and applications may
999 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001000 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +00001003#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001004#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001006#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001007}
Raymond Hettinger48923c52002-08-09 01:30:17 +00001008
1009/*********************** Tuple Iterator **************************/
1010
1011typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +02001013 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +00001015} tupleiterobject;
1016
Raymond Hettinger48923c52002-08-09 01:30:17 +00001017static void
1018tupleiter_dealloc(tupleiterobject *it)
1019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 _PyObject_GC_UNTRACK(it);
1021 Py_XDECREF(it->it_seq);
1022 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +00001023}
1024
1025static int
1026tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
1027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 Py_VISIT(it->it_seq);
1029 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001030}
1031
Raymond Hettinger48923c52002-08-09 01:30:17 +00001032static PyObject *
1033tupleiter_next(tupleiterobject *it)
1034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyTupleObject *seq;
1036 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 assert(it != NULL);
1039 seq = it->it_seq;
1040 if (seq == NULL)
1041 return NULL;
1042 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1045 item = PyTuple_GET_ITEM(seq, it->it_index);
1046 ++it->it_index;
1047 Py_INCREF(item);
1048 return item;
1049 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001052 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001054}
1055
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001056static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301057tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 Py_ssize_t len = 0;
1060 if (it->it_seq)
1061 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1062 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001063}
1064
Armin Rigof5b3e362006-02-11 21:32:43 +00001065PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001066
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001067static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301068tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001069{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001070 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001071 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001072 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001073 it->it_seq, it->it_index);
1074 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001075 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001076}
1077
1078static PyObject *
1079tupleiter_setstate(tupleiterobject *it, PyObject *state)
1080{
Victor Stinner7660b882013-06-24 23:59:24 +02001081 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001082 if (index == -1 && PyErr_Occurred())
1083 return NULL;
1084 if (it->it_seq != NULL) {
1085 if (index < 0)
1086 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001087 else if (index > PyTuple_GET_SIZE(it->it_seq))
1088 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001089 it->it_index = index;
1090 }
1091 Py_RETURN_NONE;
1092}
1093
1094PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1095PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1096
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001097static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001099 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1100 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001102};
1103
Raymond Hettinger48923c52002-08-09 01:30:17 +00001104PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1106 "tuple_iterator", /* tp_name */
1107 sizeof(tupleiterobject), /* tp_basicsize */
1108 0, /* tp_itemsize */
1109 /* methods */
1110 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001111 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 0, /* tp_getattr */
1113 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001114 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 0, /* tp_repr */
1116 0, /* tp_as_number */
1117 0, /* tp_as_sequence */
1118 0, /* tp_as_mapping */
1119 0, /* tp_hash */
1120 0, /* tp_call */
1121 0, /* tp_str */
1122 PyObject_GenericGetAttr, /* tp_getattro */
1123 0, /* tp_setattro */
1124 0, /* tp_as_buffer */
1125 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1126 0, /* tp_doc */
1127 (traverseproc)tupleiter_traverse, /* tp_traverse */
1128 0, /* tp_clear */
1129 0, /* tp_richcompare */
1130 0, /* tp_weaklistoffset */
1131 PyObject_SelfIter, /* tp_iter */
1132 (iternextfunc)tupleiter_next, /* tp_iternext */
1133 tupleiter_methods, /* tp_methods */
1134 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001135};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001136
1137static PyObject *
1138tuple_iter(PyObject *seq)
1139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (!PyTuple_Check(seq)) {
1143 PyErr_BadInternalCall();
1144 return NULL;
1145 }
1146 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1147 if (it == NULL)
1148 return NULL;
1149 it->it_index = 0;
1150 Py_INCREF(seq);
1151 it->it_seq = (PyTupleObject *)seq;
1152 _PyObject_GC_TRACK(it);
1153 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001154}