blob: c997bc6fa2d0cf03afb97902b14f6ae54e3abed1 [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 Stinner27e2d1f2018-11-01 00:52:28 +01005#include "pycore_state.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01006#include "pycore_accu.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Serhiy Storchaka0b561592017-03-19 08:47:58 +02008/*[clinic input]
9class tuple "PyTupleObject *" "&PyTuple_Type"
10[clinic start generated code]*/
11/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/
12
13#include "clinic/tupleobject.c.h"
14
Guido van Rossum5ce78f82000-04-21 21:15:05 +000015/* Speed optimization to avoid frequent malloc/free of small tuples */
Christian Heimes2202f872008-02-06 14:31:34 +000016#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +000018#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#ifndef PyTuple_MAXFREELIST
Christian Heimes2202f872008-02-06 14:31:34 +000020#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000021#endif
22
Christian Heimes2202f872008-02-06 14:31:34 +000023#if PyTuple_MAXSAVESIZE > 0
24/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025 tuple () of which at most one instance will be allocated.
26*/
Christian Heimes2202f872008-02-06 14:31:34 +000027static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
28static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000029#endif
30#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +000031Py_ssize_t _Py_fast_tuple_allocs;
32Py_ssize_t _Py_tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000033#endif
34
Antoine Pitrou3a652b12009-03-23 18:52:06 +000035/* Debug statistic to count GC tracking of tuples.
36 Please note that tuples are only untracked when considered by the GC, and
37 many of them will be dead before. Therefore, a tracking rate close to 100%
38 does not necessarily prove that the heuristic is inefficient.
39*/
40#ifdef SHOW_TRACK_COUNT
41static Py_ssize_t count_untracked = 0;
42static Py_ssize_t count_tracked = 0;
43
44static void
45show_track(void)
46{
Victor Stinnercaba55b2018-08-03 15:33:52 +020047 PyInterpreterState *interp = _PyInterpreterState_Get();
Eddie Elizondo745dc652018-02-21 20:55:18 -080048 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030049 return;
Victor Stinner25420fe2017-11-20 18:12:22 -080050 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
53 count_tracked + count_untracked);
54 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
55 "d\n", count_tracked);
56 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
57 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000058}
59#endif
60
David Malcolm49526f42012-06-22 14:55:41 -040061/* Print summary info about the state of the optimized allocator */
62void
63_PyTuple_DebugMallocStats(FILE *out)
64{
65#if PyTuple_MAXSAVESIZE > 0
66 int i;
67 char buf[128];
68 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
69 PyOS_snprintf(buf, sizeof(buf),
70 "free %d-sized PyTupleObject", i);
71 _PyDebugAllocatorStats(out,
72 buf,
73 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
74 }
75#endif
76}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000077
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020079PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020081 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 Py_ssize_t i;
83 if (size < 0) {
84 PyErr_BadInternalCall();
85 return NULL;
86 }
Christian Heimes2202f872008-02-06 14:31:34 +000087#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (size == 0 && free_list[0]) {
89 op = free_list[0];
90 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000091#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +000092 _Py_tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 return (PyObject *) op;
95 }
96 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
97 free_list[size] = (PyTupleObject *) op->ob_item[0];
98 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +0000100 _Py_fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000101#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000103#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 Py_SIZE(op) = size;
105 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 _Py_NewReference((PyObject *)op);
108 }
109 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200113 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100114 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 return PyErr_NoMemory();
116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
118 if (op == NULL)
119 return NULL;
120 }
121 for (i=0; i < size; i++)
122 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000123#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 if (size == 0) {
125 free_list[0] = op;
126 ++numfree[0];
127 Py_INCREF(op); /* extra INCREF so that this is never freed */
128 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000129#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000130#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000132#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 _PyObject_GC_TRACK(op);
134 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Martin v. Löwis18e16552006-02-15 17:27:45 +0000137Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200138PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (!PyTuple_Check(op)) {
141 PyErr_BadInternalCall();
142 return -1;
143 }
144 else
145 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146}
147
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200149PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (!PyTuple_Check(op)) {
152 PyErr_BadInternalCall();
153 return NULL;
154 }
155 if (i < 0 || i >= Py_SIZE(op)) {
156 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
157 return NULL;
158 }
159 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
162int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200163PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200165 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
167 Py_XDECREF(newitem);
168 PyErr_BadInternalCall();
169 return -1;
170 }
171 if (i < 0 || i >= Py_SIZE(op)) {
172 Py_XDECREF(newitem);
173 PyErr_SetString(PyExc_IndexError,
174 "tuple assignment index out of range");
175 return -1;
176 }
177 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300178 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000182void
183_PyTuple_MaybeUntrack(PyObject *op)
184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyTupleObject *t;
186 Py_ssize_t i, n;
187
188 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
189 return;
190 t = (PyTupleObject *) op;
191 n = Py_SIZE(t);
192 for (i = 0; i < n; i++) {
193 PyObject *elt = PyTuple_GET_ITEM(t, i);
194 /* Tuple with NULL elements aren't
195 fully constructed, don't untrack
196 them yet. */
197 if (!elt ||
198 _PyObject_GC_MAY_BE_TRACKED(elt))
199 return;
200 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000201#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 count_tracked--;
203 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000204#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000206}
207
Raymond Hettingercb2da432003-10-12 18:24:34 +0000208PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 Py_ssize_t i;
212 PyObject *o;
213 PyObject *result;
214 PyObject **items;
215 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 va_start(vargs, n);
218 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200219 if (result == NULL) {
220 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200222 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 items = ((PyTupleObject *)result)->ob_item;
224 for (i = 0; i < n; i++) {
225 o = va_arg(vargs, PyObject *);
226 Py_INCREF(o);
227 items[i] = o;
228 }
229 va_end(vargs);
230 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000231}
232
233
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234/* Methods */
235
236static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200237tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200239 Py_ssize_t i;
240 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyObject_GC_UnTrack(op);
242 Py_TRASHCAN_SAFE_BEGIN(op)
243 if (len > 0) {
244 i = len;
245 while (--i >= 0)
246 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000247#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (len < PyTuple_MAXSAVESIZE &&
249 numfree[len] < PyTuple_MAXFREELIST &&
250 Py_TYPE(op) == &PyTuple_Type)
251 {
252 op->ob_item[0] = (PyObject *) free_list[len];
253 numfree[len]++;
254 free_list[len] = op;
255 goto done; /* return */
256 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 }
259 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000260done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000265tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100268 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 n = Py_SIZE(v);
271 if (n == 0)
272 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* While not mutable, it is still possible to end up with a cycle in a
275 tuple through an object that stores itself within a tuple (and thus
276 infinitely asks for the repr of itself). This should only be
277 possible within a type. */
278 i = Py_ReprEnter((PyObject *)v);
279 if (i != 0) {
280 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
281 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000282
Victor Stinner88a9cd92013-11-19 12:59:46 +0100283 _PyUnicodeWriter_Init(&writer);
284 writer.overallocate = 1;
285 if (Py_SIZE(v) > 1) {
286 /* "(" + "1" + ", 2" * (len - 1) + ")" */
287 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
288 }
289 else {
290 /* "(1,)" */
291 writer.min_length = 4;
292 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200293
Victor Stinner88a9cd92013-11-19 12:59:46 +0100294 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200295 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Do repr() on each element. */
298 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100299 PyObject *s;
300
301 if (i > 0) {
302 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
303 goto error;
304 }
305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100307 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200308 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100309
310 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
311 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200312 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100313 }
314 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100316
317 writer.overallocate = 0;
318 if (n > 1) {
319 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
320 goto error;
321 }
322 else {
323 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
324 goto error;
325 }
Tim Petersa7259592001-06-16 05:11:17 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100328 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200329
330error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100331 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200332 Py_ReprLeave((PyObject *)v);
333 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334}
335
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000336
jdemeyeraeb1be52018-10-28 02:06:38 +0200337/* Hash for tuples. This is a slightly simplified version of the xxHash
338 non-cryptographic hash:
339 - we do not use any parallellism, there is only 1 accumulator.
340 - we drop the final mixing since this is just a permutation of the
341 output space: it does not help against collisions.
342 - at the end, we mangle the length with a single constant.
343 For the xxHash specification, see
344 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100345
jdemeyeraeb1be52018-10-28 02:06:38 +0200346 Below are the official constants from the xxHash specification. Optimizing
347 compilers should emit a single "rotate" instruction for the
348 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
349 platform, the macro could be changed to expand to a platform-specific rotate
350 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000351*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200352#if SIZEOF_PY_UHASH_T > 4
353#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
354#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
355#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
356#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
357#else
358#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
359#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
360#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
361#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
362#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000363
jdemeyeraeb1be52018-10-28 02:06:38 +0200364/* Tests have shown that it's not worth to cache the hash value, see
365 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000366static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000367tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000368{
jdemeyeraeb1be52018-10-28 02:06:38 +0200369 Py_ssize_t i, len = Py_SIZE(v);
370 PyObject **item = v->ob_item;
371
372 Py_uhash_t acc = _PyHASH_XXPRIME_5;
373 for (i = 0; i < len; i++) {
374 Py_uhash_t lane = PyObject_Hash(item[i]);
375 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200377 }
378 acc += lane * _PyHASH_XXPRIME_2;
379 acc = _PyHASH_XXROTATE(acc);
380 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200382
383 /* Add input length, mangled to keep the historical value of hash(()). */
384 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
385
386 if (acc == (Py_uhash_t)-1) {
387 return 1546275796;
388 }
389 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000390}
391
Martin v. Löwis18e16552006-02-15 17:27:45 +0000392static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000393tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396}
397
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000398static int
Fred Drakeba096332000-07-09 07:04:36 +0000399tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_ssize_t i;
402 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
405 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
406 Py_EQ);
407 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000408}
409
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200411tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (i < 0 || i >= Py_SIZE(a)) {
414 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
415 return NULL;
416 }
417 Py_INCREF(a->ob_item[i]);
418 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
420
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200422tupleslice(PyTupleObject *a, Py_ssize_t ilow,
423 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200425 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200427 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 Py_ssize_t len;
429 if (ilow < 0)
430 ilow = 0;
431 if (ihigh > Py_SIZE(a))
432 ihigh = Py_SIZE(a);
433 if (ihigh < ilow)
434 ihigh = ilow;
435 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
436 Py_INCREF(a);
437 return (PyObject *)a;
438 }
439 len = ihigh - ilow;
440 np = (PyTupleObject *)PyTuple_New(len);
441 if (np == NULL)
442 return NULL;
443 src = a->ob_item + ilow;
444 dest = np->ob_item;
445 for (i = 0; i < len; i++) {
446 PyObject *v = src[i];
447 Py_INCREF(v);
448 dest[i] = v;
449 }
450 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451}
452
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000454PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 if (op == NULL || !PyTuple_Check(op)) {
457 PyErr_BadInternalCall();
458 return NULL;
459 }
460 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000461}
462
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200464tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200466 Py_ssize_t size;
467 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyObject **src, **dest;
469 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200470 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
471 Py_INCREF(bb);
472 return bb;
473 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (!PyTuple_Check(bb)) {
475 PyErr_Format(PyExc_TypeError,
476 "can only concatenate tuple (not \"%.200s\") to tuple",
477 Py_TYPE(bb)->tp_name);
478 return NULL;
479 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200481 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
482 Py_INCREF(a);
483 return (PyObject *)a;
484 }
Martin Panterb93d8632016-07-25 02:39:20 +0000485 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000487 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 np = (PyTupleObject *) PyTuple_New(size);
489 if (np == NULL) {
490 return NULL;
491 }
492 src = a->ob_item;
493 dest = np->ob_item;
494 for (i = 0; i < Py_SIZE(a); i++) {
495 PyObject *v = src[i];
496 Py_INCREF(v);
497 dest[i] = v;
498 }
499 src = b->ob_item;
500 dest = np->ob_item + Py_SIZE(a);
501 for (i = 0; i < Py_SIZE(b); i++) {
502 PyObject *v = src[i];
503 Py_INCREF(v);
504 dest[i] = v;
505 }
506 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507#undef b
508}
509
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000511tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 Py_ssize_t i, j;
514 Py_ssize_t size;
515 PyTupleObject *np;
516 PyObject **p, **items;
517 if (n < 0)
518 n = 0;
519 if (Py_SIZE(a) == 0 || n == 1) {
520 if (PyTuple_CheckExact(a)) {
521 /* Since tuples are immutable, we can return a shared
522 copy in this case */
523 Py_INCREF(a);
524 return (PyObject *)a;
525 }
526 if (Py_SIZE(a) == 0)
527 return PyTuple_New(0);
528 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100529 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100531 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 np = (PyTupleObject *) PyTuple_New(size);
533 if (np == NULL)
534 return NULL;
535 p = np->ob_item;
536 items = a->ob_item;
537 for (i = 0; i < n; i++) {
538 for (j = 0; j < Py_SIZE(a); j++) {
539 *p = items[j];
540 Py_INCREF(*p);
541 p++;
542 }
543 }
544 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000545}
546
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200547/*[clinic input]
548tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000549
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200550 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300551 start: slice_index(accept={int}) = 0
552 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200553 /
554
555Return first index of value.
556
557Raises ValueError if the value is not present.
558[clinic start generated code]*/
559
560static PyObject *
561tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
562 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300563/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200564{
565 Py_ssize_t i;
566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (start < 0) {
568 start += Py_SIZE(self);
569 if (start < 0)
570 start = 0;
571 }
572 if (stop < 0) {
573 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200575 else if (stop > Py_SIZE(self)) {
576 stop = Py_SIZE(self);
577 }
578 for (i = start; i < stop; i++) {
579 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (cmp > 0)
581 return PyLong_FromSsize_t(i);
582 else if (cmp < 0)
583 return NULL;
584 }
585 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
586 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000587}
588
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200589/*[clinic input]
590tuple.count
591
592 value: object
593 /
594
595Return number of occurrences of value.
596[clinic start generated code]*/
597
Raymond Hettinger65baa342008-02-07 00:41:02 +0000598static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200599tuple_count(PyTupleObject *self, PyObject *value)
600/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 Py_ssize_t count = 0;
603 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200606 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (cmp > 0)
608 count++;
609 else if (cmp < 0)
610 return NULL;
611 }
612 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000613}
614
Jeremy Hylton8caad492000-06-23 14:18:11 +0000615static int
616tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 for (i = Py_SIZE(o); --i >= 0; )
621 Py_VISIT(o->ob_item[i]);
622 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000623}
624
Guido van Rossumf77bc622001-01-18 00:00:53 +0000625static PyObject *
626tuplerichcompare(PyObject *v, PyObject *w, int op)
627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyTupleObject *vt, *wt;
629 Py_ssize_t i;
630 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000631
Brian Curtindfc80e32011-08-10 20:28:54 -0500632 if (!PyTuple_Check(v) || !PyTuple_Check(w))
633 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 vt = (PyTupleObject *)v;
636 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 vlen = Py_SIZE(vt);
639 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 /* Note: the corresponding code for lists has an "early out" test
642 * here when op is EQ or NE and the lengths differ. That pays there,
643 * but Tim was unable to find any real code where EQ/NE tuple
644 * compares don't have the same length, so testing for it here would
645 * have cost without benefit.
646 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 /* Search for the first index where items are different.
649 * Note that because tuples are immutable, it's safe to reuse
650 * vlen and wlen across the comparison calls.
651 */
652 for (i = 0; i < vlen && i < wlen; i++) {
653 int k = PyObject_RichCompareBool(vt->ob_item[i],
654 wt->ob_item[i], Py_EQ);
655 if (k < 0)
656 return NULL;
657 if (!k)
658 break;
659 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 if (i >= vlen || i >= wlen) {
662 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100663 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 /* We have an item that differs -- shortcuts for EQ/NE */
667 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200668 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
670 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200671 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 /* Compare the final item again using the proper operator */
675 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000676}
677
Jeremy Hylton938ace62002-07-17 16:30:39 +0000678static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200679tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
680
681/*[clinic input]
682@classmethod
683tuple.__new__ as tuple_new
684 iterable: object(c_default="NULL") = ()
685 /
686
687Built-in immutable sequence.
688
689If no argument is given, the constructor returns an empty tuple.
690If iterable is specified the tuple is initialized from iterable's items.
691
692If the argument is a tuple, the return value is the same object.
693[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000694
Tim Peters6d6c1a32001-08-02 04:15:00 +0000695static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200696tuple_new_impl(PyTypeObject *type, PyObject *iterable)
697/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200700 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200702 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 return PyTuple_New(0);
704 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200705 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706}
707
Guido van Rossumae960af2001-08-30 03:11:59 +0000708static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200709tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PyObject *tmp, *newobj, *item;
712 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200715 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (tmp == NULL)
717 return NULL;
718 assert(PyTuple_Check(tmp));
719 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
720 if (newobj == NULL)
721 return NULL;
722 for (i = 0; i < n; i++) {
723 item = PyTuple_GET_ITEM(tmp, i);
724 Py_INCREF(item);
725 PyTuple_SET_ITEM(newobj, i, item);
726 }
727 Py_DECREF(tmp);
728 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000729}
730
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 (lenfunc)tuplelength, /* sq_length */
733 (binaryfunc)tupleconcat, /* sq_concat */
734 (ssizeargfunc)tuplerepeat, /* sq_repeat */
735 (ssizeargfunc)tupleitem, /* sq_item */
736 0, /* sq_slice */
737 0, /* sq_ass_item */
738 0, /* sq_ass_slice */
739 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000740};
741
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000742static PyObject*
743tuplesubscript(PyTupleObject* self, PyObject* item)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 if (PyIndex_Check(item)) {
746 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
747 if (i == -1 && PyErr_Occurred())
748 return NULL;
749 if (i < 0)
750 i += PyTuple_GET_SIZE(self);
751 return tupleitem(self, i);
752 }
753 else if (PySlice_Check(item)) {
754 Py_ssize_t start, stop, step, slicelength, cur, i;
755 PyObject* result;
756 PyObject* it;
757 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000758
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300759 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 return NULL;
761 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300762 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
763 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 if (slicelength <= 0) {
766 return PyTuple_New(0);
767 }
768 else if (start == 0 && step == 1 &&
769 slicelength == PyTuple_GET_SIZE(self) &&
770 PyTuple_CheckExact(self)) {
771 Py_INCREF(self);
772 return (PyObject *)self;
773 }
774 else {
775 result = PyTuple_New(slicelength);
776 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 src = self->ob_item;
779 dest = ((PyTupleObject *)result)->ob_item;
780 for (cur = start, i = 0; i < slicelength;
781 cur += step, i++) {
782 it = src[cur];
783 Py_INCREF(it);
784 dest[i] = it;
785 }
786
787 return result;
788 }
789 }
790 else {
791 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400792 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 Py_TYPE(item)->tp_name);
794 return NULL;
795 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000796}
797
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200798/*[clinic input]
799tuple.__getnewargs__
800[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200802static PyObject *
803tuple___getnewargs___impl(PyTupleObject *self)
804/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
805{
806 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000807}
808
809static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200810 TUPLE___GETNEWARGS___METHODDEF
811 TUPLE_INDEX_METHODDEF
812 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000814};
815
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000816static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 (lenfunc)tuplelength,
818 (binaryfunc)tuplesubscript,
819 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000820};
821
Raymond Hettinger48923c52002-08-09 01:30:17 +0000822static PyObject *tuple_iter(PyObject *seq);
823
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 PyVarObject_HEAD_INIT(&PyType_Type, 0)
826 "tuple",
827 sizeof(PyTupleObject) - sizeof(PyObject *),
828 sizeof(PyObject *),
829 (destructor)tupledealloc, /* tp_dealloc */
830 0, /* tp_print */
831 0, /* tp_getattr */
832 0, /* tp_setattr */
833 0, /* tp_reserved */
834 (reprfunc)tuplerepr, /* tp_repr */
835 0, /* tp_as_number */
836 &tuple_as_sequence, /* tp_as_sequence */
837 &tuple_as_mapping, /* tp_as_mapping */
838 (hashfunc)tuplehash, /* tp_hash */
839 0, /* tp_call */
840 0, /* tp_str */
841 PyObject_GenericGetAttr, /* tp_getattro */
842 0, /* tp_setattro */
843 0, /* tp_as_buffer */
844 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
845 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200846 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 (traverseproc)tupletraverse, /* tp_traverse */
848 0, /* tp_clear */
849 tuplerichcompare, /* tp_richcompare */
850 0, /* tp_weaklistoffset */
851 tuple_iter, /* tp_iter */
852 0, /* tp_iternext */
853 tuple_methods, /* tp_methods */
854 0, /* tp_members */
855 0, /* tp_getset */
856 0, /* tp_base */
857 0, /* tp_dict */
858 0, /* tp_descr_get */
859 0, /* tp_descr_set */
860 0, /* tp_dictoffset */
861 0, /* tp_init */
862 0, /* tp_alloc */
863 tuple_new, /* tp_new */
864 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000866
867/* The following function breaks the notion that tuples are immutable:
868 it changes the size of a tuple. We get away with this only if there
869 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000870 as creating a new tuple object and destroying the old one, only more
871 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000872 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000873
874int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000875_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000876{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200877 PyTupleObject *v;
878 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_ssize_t i;
880 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 v = (PyTupleObject *) *pv;
883 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
884 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
885 *pv = 0;
886 Py_XDECREF(v);
887 PyErr_BadInternalCall();
888 return -1;
889 }
890 oldsize = Py_SIZE(v);
891 if (oldsize == newsize)
892 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (oldsize == 0) {
895 /* Empty tuples are often shared, so we should never
896 resize them in-place even if we do own the only
897 (current) reference */
898 Py_DECREF(v);
899 *pv = PyTuple_New(newsize);
900 return *pv == NULL ? -1 : 0;
901 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 /* XXX UNREF/NEWREF interface should be more symmetrical */
904 _Py_DEC_REFTOTAL;
905 if (_PyObject_GC_IS_TRACKED(v))
906 _PyObject_GC_UNTRACK(v);
907 _Py_ForgetReference((PyObject *) v);
908 /* DECREF items deleted by shrinkage */
909 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200910 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
912 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
913 if (sv == NULL) {
914 *pv = NULL;
915 PyObject_GC_Del(v);
916 return -1;
917 }
918 _Py_NewReference((PyObject *) sv);
919 /* Zero out items added by growing */
920 if (newsize > oldsize)
921 memset(&sv->ob_item[oldsize], 0,
922 sizeof(*sv->ob_item) * (newsize - oldsize));
923 *pv = (PyObject *) sv;
924 _PyObject_GC_TRACK(sv);
925 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000926}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000927
Christian Heimesa156e092008-02-16 07:38:31 +0000928int
929PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000932#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 int i;
934 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
935 PyTupleObject *p, *q;
936 p = free_list[i];
937 freelist_size += numfree[i];
938 free_list[i] = NULL;
939 numfree[i] = 0;
940 while (p) {
941 q = p;
942 p = (PyTupleObject *)(p->ob_item[0]);
943 PyObject_GC_Del(q);
944 }
945 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000946#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000948}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949
Christian Heimesa156e092008-02-16 07:38:31 +0000950void
951PyTuple_Fini(void)
952{
953#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 /* empty tuples are used all over the place and applications may
955 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200956 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000959#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000960#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000962#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000963}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000964
965/*********************** Tuple Iterator **************************/
966
967typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200969 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000971} tupleiterobject;
972
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973static void
974tupleiter_dealloc(tupleiterobject *it)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 _PyObject_GC_UNTRACK(it);
977 Py_XDECREF(it->it_seq);
978 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000979}
980
981static int
982tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 Py_VISIT(it->it_seq);
985 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000986}
987
Raymond Hettinger48923c52002-08-09 01:30:17 +0000988static PyObject *
989tupleiter_next(tupleiterobject *it)
990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyTupleObject *seq;
992 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 assert(it != NULL);
995 seq = it->it_seq;
996 if (seq == NULL)
997 return NULL;
998 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1001 item = PyTuple_GET_ITEM(seq, it->it_index);
1002 ++it->it_index;
1003 Py_INCREF(item);
1004 return item;
1005 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001008 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001010}
1011
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001012static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301013tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 Py_ssize_t len = 0;
1016 if (it->it_seq)
1017 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1018 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001019}
1020
Armin Rigof5b3e362006-02-11 21:32:43 +00001021PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001022
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001023static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301024tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001025{
1026 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001027 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001028 it->it_seq, it->it_index);
1029 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001030 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001031}
1032
1033static PyObject *
1034tupleiter_setstate(tupleiterobject *it, PyObject *state)
1035{
Victor Stinner7660b882013-06-24 23:59:24 +02001036 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001037 if (index == -1 && PyErr_Occurred())
1038 return NULL;
1039 if (it->it_seq != NULL) {
1040 if (index < 0)
1041 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001042 else if (index > PyTuple_GET_SIZE(it->it_seq))
1043 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001044 it->it_index = index;
1045 }
1046 Py_RETURN_NONE;
1047}
1048
1049PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1050PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1051
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001052static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001054 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1055 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001057};
1058
Raymond Hettinger48923c52002-08-09 01:30:17 +00001059PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1061 "tuple_iterator", /* tp_name */
1062 sizeof(tupleiterobject), /* tp_basicsize */
1063 0, /* tp_itemsize */
1064 /* methods */
1065 (destructor)tupleiter_dealloc, /* tp_dealloc */
1066 0, /* tp_print */
1067 0, /* tp_getattr */
1068 0, /* tp_setattr */
1069 0, /* tp_reserved */
1070 0, /* tp_repr */
1071 0, /* tp_as_number */
1072 0, /* tp_as_sequence */
1073 0, /* tp_as_mapping */
1074 0, /* tp_hash */
1075 0, /* tp_call */
1076 0, /* tp_str */
1077 PyObject_GenericGetAttr, /* tp_getattro */
1078 0, /* tp_setattro */
1079 0, /* tp_as_buffer */
1080 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1081 0, /* tp_doc */
1082 (traverseproc)tupleiter_traverse, /* tp_traverse */
1083 0, /* tp_clear */
1084 0, /* tp_richcompare */
1085 0, /* tp_weaklistoffset */
1086 PyObject_SelfIter, /* tp_iter */
1087 (iternextfunc)tupleiter_next, /* tp_iternext */
1088 tupleiter_methods, /* tp_methods */
1089 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001090};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001091
1092static PyObject *
1093tuple_iter(PyObject *seq)
1094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (!PyTuple_Check(seq)) {
1098 PyErr_BadInternalCall();
1099 return NULL;
1100 }
1101 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1102 if (it == NULL)
1103 return NULL;
1104 it->it_index = 0;
1105 Py_INCREF(seq);
1106 it->it_seq = (PyTupleObject *)seq;
1107 _PyObject_GC_TRACK(it);
1108 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001109}