blob: 7ee06e20e4a9131ed1e2820bc24b606bcd517c10 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/pystate.h"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01006#include "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
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000031Py_ssize_t fast_tuple_allocs;
32Py_ssize_t 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 Stinner25420fe2017-11-20 18:12:22 -080047 PyInterpreterState *interp = PyThreadState_GET()->interp;
Miss Islington (bot)bc2e1102018-02-21 21:44:08 -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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336/* The addend 82520, was selected from the range(0, 1000000) for
337 generating the greatest number of prime multipliers for tuples
Miss Islington (bot)e86db342018-02-03 17:41:43 -0800338 up to length eight:
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000341 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100342
343 Tests have shown that it's not worth to cache the hash value, see
344 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000345*/
346
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000347static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000348tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000349{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200350 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
351 Py_hash_t y;
352 Py_ssize_t len = Py_SIZE(v);
353 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800354 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800355 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 p = v->ob_item;
357 while (--len >= 0) {
358 y = PyObject_Hash(*p++);
359 if (y == -1)
360 return -1;
361 x = (x ^ y) * mult;
362 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800363 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800365 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100366 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 x = -2;
368 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000369}
370
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000372tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
376
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000377static int
Fred Drakeba096332000-07-09 07:04:36 +0000378tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Py_ssize_t i;
381 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
384 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
385 Py_EQ);
386 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000387}
388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200390tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (i < 0 || i >= Py_SIZE(a)) {
393 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
394 return NULL;
395 }
396 Py_INCREF(a->ob_item[i]);
397 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200401tupleslice(PyTupleObject *a, Py_ssize_t ilow,
402 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200404 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200406 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 Py_ssize_t len;
408 if (ilow < 0)
409 ilow = 0;
410 if (ihigh > Py_SIZE(a))
411 ihigh = Py_SIZE(a);
412 if (ihigh < ilow)
413 ihigh = ilow;
414 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
415 Py_INCREF(a);
416 return (PyObject *)a;
417 }
418 len = ihigh - ilow;
419 np = (PyTupleObject *)PyTuple_New(len);
420 if (np == NULL)
421 return NULL;
422 src = a->ob_item + ilow;
423 dest = np->ob_item;
424 for (i = 0; i < len; i++) {
425 PyObject *v = src[i];
426 Py_INCREF(v);
427 dest[i] = v;
428 }
429 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430}
431
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000433PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (op == NULL || !PyTuple_Check(op)) {
436 PyErr_BadInternalCall();
437 return NULL;
438 }
439 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000440}
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200443tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200445 Py_ssize_t size;
446 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 PyObject **src, **dest;
448 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200449 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
450 Py_INCREF(bb);
451 return bb;
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (!PyTuple_Check(bb)) {
454 PyErr_Format(PyExc_TypeError,
455 "can only concatenate tuple (not \"%.200s\") to tuple",
456 Py_TYPE(bb)->tp_name);
457 return NULL;
458 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200460 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
461 Py_INCREF(a);
462 return (PyObject *)a;
463 }
Martin Panterb93d8632016-07-25 02:39:20 +0000464 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000466 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 np = (PyTupleObject *) PyTuple_New(size);
468 if (np == NULL) {
469 return NULL;
470 }
471 src = a->ob_item;
472 dest = np->ob_item;
473 for (i = 0; i < Py_SIZE(a); i++) {
474 PyObject *v = src[i];
475 Py_INCREF(v);
476 dest[i] = v;
477 }
478 src = b->ob_item;
479 dest = np->ob_item + Py_SIZE(a);
480 for (i = 0; i < Py_SIZE(b); i++) {
481 PyObject *v = src[i];
482 Py_INCREF(v);
483 dest[i] = v;
484 }
485 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486#undef b
487}
488
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000490tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_ssize_t i, j;
493 Py_ssize_t size;
494 PyTupleObject *np;
495 PyObject **p, **items;
496 if (n < 0)
497 n = 0;
498 if (Py_SIZE(a) == 0 || n == 1) {
499 if (PyTuple_CheckExact(a)) {
500 /* Since tuples are immutable, we can return a shared
501 copy in this case */
502 Py_INCREF(a);
503 return (PyObject *)a;
504 }
505 if (Py_SIZE(a) == 0)
506 return PyTuple_New(0);
507 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100508 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100510 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 np = (PyTupleObject *) PyTuple_New(size);
512 if (np == NULL)
513 return NULL;
514 p = np->ob_item;
515 items = a->ob_item;
516 for (i = 0; i < n; i++) {
517 for (j = 0; j < Py_SIZE(a); j++) {
518 *p = items[j];
519 Py_INCREF(*p);
520 p++;
521 }
522 }
523 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000524}
525
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200526/*[clinic input]
527tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000528
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200529 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300530 start: slice_index(accept={int}) = 0
531 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200532 /
533
534Return first index of value.
535
536Raises ValueError if the value is not present.
537[clinic start generated code]*/
538
539static PyObject *
540tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
541 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300542/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200543{
544 Py_ssize_t i;
545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (start < 0) {
547 start += Py_SIZE(self);
548 if (start < 0)
549 start = 0;
550 }
551 if (stop < 0) {
552 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200554 else if (stop > Py_SIZE(self)) {
555 stop = Py_SIZE(self);
556 }
557 for (i = start; i < stop; i++) {
558 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (cmp > 0)
560 return PyLong_FromSsize_t(i);
561 else if (cmp < 0)
562 return NULL;
563 }
564 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
565 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000566}
567
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200568/*[clinic input]
569tuple.count
570
571 value: object
572 /
573
574Return number of occurrences of value.
575[clinic start generated code]*/
576
Raymond Hettinger65baa342008-02-07 00:41:02 +0000577static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200578tuple_count(PyTupleObject *self, PyObject *value)
579/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 Py_ssize_t count = 0;
582 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200585 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (cmp > 0)
587 count++;
588 else if (cmp < 0)
589 return NULL;
590 }
591 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000592}
593
Jeremy Hylton8caad492000-06-23 14:18:11 +0000594static int
595tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 for (i = Py_SIZE(o); --i >= 0; )
600 Py_VISIT(o->ob_item[i]);
601 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000602}
603
Guido van Rossumf77bc622001-01-18 00:00:53 +0000604static PyObject *
605tuplerichcompare(PyObject *v, PyObject *w, int op)
606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 PyTupleObject *vt, *wt;
608 Py_ssize_t i;
609 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000610
Brian Curtindfc80e32011-08-10 20:28:54 -0500611 if (!PyTuple_Check(v) || !PyTuple_Check(w))
612 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 vt = (PyTupleObject *)v;
615 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 vlen = Py_SIZE(vt);
618 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 /* Note: the corresponding code for lists has an "early out" test
621 * here when op is EQ or NE and the lengths differ. That pays there,
622 * but Tim was unable to find any real code where EQ/NE tuple
623 * compares don't have the same length, so testing for it here would
624 * have cost without benefit.
625 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Search for the first index where items are different.
628 * Note that because tuples are immutable, it's safe to reuse
629 * vlen and wlen across the comparison calls.
630 */
631 for (i = 0; i < vlen && i < wlen; i++) {
632 int k = PyObject_RichCompareBool(vt->ob_item[i],
633 wt->ob_item[i], Py_EQ);
634 if (k < 0)
635 return NULL;
636 if (!k)
637 break;
638 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (i >= vlen || i >= wlen) {
641 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100642 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 /* We have an item that differs -- shortcuts for EQ/NE */
646 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200647 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 }
649 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200650 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* Compare the final item again using the proper operator */
654 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000655}
656
Jeremy Hylton938ace62002-07-17 16:30:39 +0000657static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200658tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
659
660/*[clinic input]
661@classmethod
662tuple.__new__ as tuple_new
663 iterable: object(c_default="NULL") = ()
664 /
665
666Built-in immutable sequence.
667
668If no argument is given, the constructor returns an empty tuple.
669If iterable is specified the tuple is initialized from iterable's items.
670
671If the argument is a tuple, the return value is the same object.
672[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000673
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200675tuple_new_impl(PyTypeObject *type, PyObject *iterable)
676/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200679 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200681 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return PyTuple_New(0);
683 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200684 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685}
686
Guido van Rossumae960af2001-08-30 03:11:59 +0000687static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200688tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyObject *tmp, *newobj, *item;
691 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200694 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 if (tmp == NULL)
696 return NULL;
697 assert(PyTuple_Check(tmp));
698 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
699 if (newobj == NULL)
700 return NULL;
701 for (i = 0; i < n; i++) {
702 item = PyTuple_GET_ITEM(tmp, i);
703 Py_INCREF(item);
704 PyTuple_SET_ITEM(newobj, i, item);
705 }
706 Py_DECREF(tmp);
707 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000708}
709
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 (lenfunc)tuplelength, /* sq_length */
712 (binaryfunc)tupleconcat, /* sq_concat */
713 (ssizeargfunc)tuplerepeat, /* sq_repeat */
714 (ssizeargfunc)tupleitem, /* sq_item */
715 0, /* sq_slice */
716 0, /* sq_ass_item */
717 0, /* sq_ass_slice */
718 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719};
720
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000721static PyObject*
722tuplesubscript(PyTupleObject* self, PyObject* item)
723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 if (PyIndex_Check(item)) {
725 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
726 if (i == -1 && PyErr_Occurred())
727 return NULL;
728 if (i < 0)
729 i += PyTuple_GET_SIZE(self);
730 return tupleitem(self, i);
731 }
732 else if (PySlice_Check(item)) {
Miss Islington (bot)f02d1a42019-05-17 00:33:10 -0700733 Py_ssize_t start, stop, step, slicelength, i;
734 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyObject* result;
736 PyObject* it;
737 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000738
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300739 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return NULL;
741 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300742 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
743 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 if (slicelength <= 0) {
746 return PyTuple_New(0);
747 }
748 else if (start == 0 && step == 1 &&
749 slicelength == PyTuple_GET_SIZE(self) &&
750 PyTuple_CheckExact(self)) {
751 Py_INCREF(self);
752 return (PyObject *)self;
753 }
754 else {
755 result = PyTuple_New(slicelength);
756 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 src = self->ob_item;
759 dest = ((PyTupleObject *)result)->ob_item;
760 for (cur = start, i = 0; i < slicelength;
761 cur += step, i++) {
762 it = src[cur];
763 Py_INCREF(it);
764 dest[i] = it;
765 }
766
767 return result;
768 }
769 }
770 else {
771 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400772 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 Py_TYPE(item)->tp_name);
774 return NULL;
775 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000776}
777
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200778/*[clinic input]
779tuple.__getnewargs__
780[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200782static PyObject *
783tuple___getnewargs___impl(PyTupleObject *self)
784/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
785{
786 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000787}
788
789static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200790 TUPLE___GETNEWARGS___METHODDEF
791 TUPLE_INDEX_METHODDEF
792 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000794};
795
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000796static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 (lenfunc)tuplelength,
798 (binaryfunc)tuplesubscript,
799 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000800};
801
Raymond Hettinger48923c52002-08-09 01:30:17 +0000802static PyObject *tuple_iter(PyObject *seq);
803
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyVarObject_HEAD_INIT(&PyType_Type, 0)
806 "tuple",
807 sizeof(PyTupleObject) - sizeof(PyObject *),
808 sizeof(PyObject *),
809 (destructor)tupledealloc, /* tp_dealloc */
810 0, /* tp_print */
811 0, /* tp_getattr */
812 0, /* tp_setattr */
813 0, /* tp_reserved */
814 (reprfunc)tuplerepr, /* tp_repr */
815 0, /* tp_as_number */
816 &tuple_as_sequence, /* tp_as_sequence */
817 &tuple_as_mapping, /* tp_as_mapping */
818 (hashfunc)tuplehash, /* tp_hash */
819 0, /* tp_call */
820 0, /* tp_str */
821 PyObject_GenericGetAttr, /* tp_getattro */
822 0, /* tp_setattro */
823 0, /* tp_as_buffer */
824 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
825 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200826 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 (traverseproc)tupletraverse, /* tp_traverse */
828 0, /* tp_clear */
829 tuplerichcompare, /* tp_richcompare */
830 0, /* tp_weaklistoffset */
831 tuple_iter, /* tp_iter */
832 0, /* tp_iternext */
833 tuple_methods, /* tp_methods */
834 0, /* tp_members */
835 0, /* tp_getset */
836 0, /* tp_base */
837 0, /* tp_dict */
838 0, /* tp_descr_get */
839 0, /* tp_descr_set */
840 0, /* tp_dictoffset */
841 0, /* tp_init */
842 0, /* tp_alloc */
843 tuple_new, /* tp_new */
844 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000845};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000846
847/* The following function breaks the notion that tuples are immutable:
848 it changes the size of a tuple. We get away with this only if there
849 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000850 as creating a new tuple object and destroying the old one, only more
851 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000852 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000853
854int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000855_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200857 PyTupleObject *v;
858 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 Py_ssize_t i;
860 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 v = (PyTupleObject *) *pv;
863 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
864 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
865 *pv = 0;
866 Py_XDECREF(v);
867 PyErr_BadInternalCall();
868 return -1;
869 }
870 oldsize = Py_SIZE(v);
871 if (oldsize == newsize)
872 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (oldsize == 0) {
875 /* Empty tuples are often shared, so we should never
876 resize them in-place even if we do own the only
877 (current) reference */
878 Py_DECREF(v);
879 *pv = PyTuple_New(newsize);
880 return *pv == NULL ? -1 : 0;
881 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 /* XXX UNREF/NEWREF interface should be more symmetrical */
884 _Py_DEC_REFTOTAL;
885 if (_PyObject_GC_IS_TRACKED(v))
886 _PyObject_GC_UNTRACK(v);
887 _Py_ForgetReference((PyObject *) v);
888 /* DECREF items deleted by shrinkage */
889 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200890 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 }
892 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
893 if (sv == NULL) {
894 *pv = NULL;
895 PyObject_GC_Del(v);
896 return -1;
897 }
898 _Py_NewReference((PyObject *) sv);
899 /* Zero out items added by growing */
900 if (newsize > oldsize)
901 memset(&sv->ob_item[oldsize], 0,
902 sizeof(*sv->ob_item) * (newsize - oldsize));
903 *pv = (PyObject *) sv;
904 _PyObject_GC_TRACK(sv);
905 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000906}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000907
Christian Heimesa156e092008-02-16 07:38:31 +0000908int
909PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000912#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 int i;
914 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
915 PyTupleObject *p, *q;
916 p = free_list[i];
917 freelist_size += numfree[i];
918 free_list[i] = NULL;
919 numfree[i] = 0;
920 while (p) {
921 q = p;
922 p = (PyTupleObject *)(p->ob_item[0]);
923 PyObject_GC_Del(q);
924 }
925 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000926#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000928}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929
Christian Heimesa156e092008-02-16 07:38:31 +0000930void
931PyTuple_Fini(void)
932{
933#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 /* empty tuples are used all over the place and applications may
935 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200936 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000939#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000940#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000942#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000943}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000944
945/*********************** Tuple Iterator **************************/
946
947typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200949 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000951} tupleiterobject;
952
Raymond Hettinger48923c52002-08-09 01:30:17 +0000953static void
954tupleiter_dealloc(tupleiterobject *it)
955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 _PyObject_GC_UNTRACK(it);
957 Py_XDECREF(it->it_seq);
958 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000959}
960
961static int
962tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 Py_VISIT(it->it_seq);
965 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000966}
967
Raymond Hettinger48923c52002-08-09 01:30:17 +0000968static PyObject *
969tupleiter_next(tupleiterobject *it)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyTupleObject *seq;
972 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 assert(it != NULL);
975 seq = it->it_seq;
976 if (seq == NULL)
977 return NULL;
978 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if (it->it_index < PyTuple_GET_SIZE(seq)) {
981 item = PyTuple_GET_ITEM(seq, it->it_index);
982 ++it->it_index;
983 Py_INCREF(item);
984 return item;
985 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300988 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000990}
991
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000992static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000993tupleiter_len(tupleiterobject *it)
994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 Py_ssize_t len = 0;
996 if (it->it_seq)
997 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
998 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000999}
1000
Armin Rigof5b3e362006-02-11 21:32:43 +00001001PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001002
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001003static PyObject *
1004tupleiter_reduce(tupleiterobject *it)
1005{
1006 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001007 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001008 it->it_seq, it->it_index);
1009 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001010 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001011}
1012
1013static PyObject *
1014tupleiter_setstate(tupleiterobject *it, PyObject *state)
1015{
Victor Stinner7660b882013-06-24 23:59:24 +02001016 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001017 if (index == -1 && PyErr_Occurred())
1018 return NULL;
1019 if (it->it_seq != NULL) {
1020 if (index < 0)
1021 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001022 else if (index > PyTuple_GET_SIZE(it->it_seq))
1023 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001024 it->it_index = index;
1025 }
1026 Py_RETURN_NONE;
1027}
1028
1029PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1030PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1031
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001032static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001034 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1035 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001037};
1038
Raymond Hettinger48923c52002-08-09 01:30:17 +00001039PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1041 "tuple_iterator", /* tp_name */
1042 sizeof(tupleiterobject), /* tp_basicsize */
1043 0, /* tp_itemsize */
1044 /* methods */
1045 (destructor)tupleiter_dealloc, /* tp_dealloc */
1046 0, /* tp_print */
1047 0, /* tp_getattr */
1048 0, /* tp_setattr */
1049 0, /* tp_reserved */
1050 0, /* tp_repr */
1051 0, /* tp_as_number */
1052 0, /* tp_as_sequence */
1053 0, /* tp_as_mapping */
1054 0, /* tp_hash */
1055 0, /* tp_call */
1056 0, /* tp_str */
1057 PyObject_GenericGetAttr, /* tp_getattro */
1058 0, /* tp_setattro */
1059 0, /* tp_as_buffer */
1060 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1061 0, /* tp_doc */
1062 (traverseproc)tupleiter_traverse, /* tp_traverse */
1063 0, /* tp_clear */
1064 0, /* tp_richcompare */
1065 0, /* tp_weaklistoffset */
1066 PyObject_SelfIter, /* tp_iter */
1067 (iternextfunc)tupleiter_next, /* tp_iternext */
1068 tupleiter_methods, /* tp_methods */
1069 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001070};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001071
1072static PyObject *
1073tuple_iter(PyObject *seq)
1074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (!PyTuple_Check(seq)) {
1078 PyErr_BadInternalCall();
1079 return NULL;
1080 }
1081 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1082 if (it == NULL)
1083 return NULL;
1084 it->it_index = 0;
1085 Py_INCREF(seq);
1086 it->it_seq = (PyTupleObject *)seq;
1087 _PyObject_GC_TRACK(it);
1088 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089}