blob: c150af8ed1acb8dbdae9ef09e34e9046d99000ab [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{
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030047 PyObject *xoptions, *value;
48 _Py_IDENTIFIER(showalloccount);
49
50 xoptions = PySys_GetXOptions();
51 if (xoptions == NULL)
52 return;
53 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
54 if (value != Py_True)
55 return;
56
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
58 count_tracked + count_untracked);
59 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
60 "d\n", count_tracked);
61 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
62 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000063}
64#endif
65
David Malcolm49526f42012-06-22 14:55:41 -040066/* Print summary info about the state of the optimized allocator */
67void
68_PyTuple_DebugMallocStats(FILE *out)
69{
70#if PyTuple_MAXSAVESIZE > 0
71 int i;
72 char buf[128];
73 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
74 PyOS_snprintf(buf, sizeof(buf),
75 "free %d-sized PyTupleObject", i);
76 _PyDebugAllocatorStats(out,
77 buf,
78 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
79 }
80#endif
81}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000082
Guido van Rossumc0b618a1997-05-02 03:12:38 +000083PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020084PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020086 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 Py_ssize_t i;
88 if (size < 0) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
Christian Heimes2202f872008-02-06 14:31:34 +000092#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 if (size == 0 && free_list[0]) {
94 op = free_list[0];
95 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000096#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 return (PyObject *) op;
100 }
101 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
102 free_list[size] = (PyTupleObject *) op->ob_item[0];
103 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000104#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000108#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 Py_SIZE(op) = size;
110 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000111#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 _Py_NewReference((PyObject *)op);
113 }
114 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000115#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200118 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100119 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 return PyErr_NoMemory();
121 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
123 if (op == NULL)
124 return NULL;
125 }
126 for (i=0; i < size; i++)
127 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000128#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 if (size == 0) {
130 free_list[0] = op;
131 ++numfree[0];
132 Py_INCREF(op); /* extra INCREF so that this is never freed */
133 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000134#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000135#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000137#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 _PyObject_GC_TRACK(op);
139 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140}
141
Martin v. Löwis18e16552006-02-15 17:27:45 +0000142Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200143PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 if (!PyTuple_Check(op)) {
146 PyErr_BadInternalCall();
147 return -1;
148 }
149 else
150 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151}
152
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000153PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200154PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (!PyTuple_Check(op)) {
157 PyErr_BadInternalCall();
158 return NULL;
159 }
160 if (i < 0 || i >= Py_SIZE(op)) {
161 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
162 return NULL;
163 }
164 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
166
167int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200168PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200170 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
172 Py_XDECREF(newitem);
173 PyErr_BadInternalCall();
174 return -1;
175 }
176 if (i < 0 || i >= Py_SIZE(op)) {
177 Py_XDECREF(newitem);
178 PyErr_SetString(PyExc_IndexError,
179 "tuple assignment index out of range");
180 return -1;
181 }
182 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300183 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185}
186
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000187void
188_PyTuple_MaybeUntrack(PyObject *op)
189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 PyTupleObject *t;
191 Py_ssize_t i, n;
192
193 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
194 return;
195 t = (PyTupleObject *) op;
196 n = Py_SIZE(t);
197 for (i = 0; i < n; i++) {
198 PyObject *elt = PyTuple_GET_ITEM(t, i);
199 /* Tuple with NULL elements aren't
200 fully constructed, don't untrack
201 them yet. */
202 if (!elt ||
203 _PyObject_GC_MAY_BE_TRACKED(elt))
204 return;
205 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000206#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 count_tracked--;
208 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000209#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000211}
212
Raymond Hettingercb2da432003-10-12 18:24:34 +0000213PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000214PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 Py_ssize_t i;
217 PyObject *o;
218 PyObject *result;
219 PyObject **items;
220 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 va_start(vargs, n);
223 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200224 if (result == NULL) {
225 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 items = ((PyTupleObject *)result)->ob_item;
229 for (i = 0; i < n; i++) {
230 o = va_arg(vargs, PyObject *);
231 Py_INCREF(o);
232 items[i] = o;
233 }
234 va_end(vargs);
235 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000236}
237
238
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239/* Methods */
240
241static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200242tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200244 Py_ssize_t i;
245 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PyObject_GC_UnTrack(op);
247 Py_TRASHCAN_SAFE_BEGIN(op)
248 if (len > 0) {
249 i = len;
250 while (--i >= 0)
251 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000252#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 if (len < PyTuple_MAXSAVESIZE &&
254 numfree[len] < PyTuple_MAXFREELIST &&
255 Py_TYPE(op) == &PyTuple_Type)
256 {
257 op->ob_item[0] = (PyObject *) free_list[len];
258 numfree[len]++;
259 free_list[len] = op;
260 goto done; /* return */
261 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000262#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 }
264 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000265done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267}
268
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000269static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000270tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100273 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 n = Py_SIZE(v);
276 if (n == 0)
277 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 /* While not mutable, it is still possible to end up with a cycle in a
280 tuple through an object that stores itself within a tuple (and thus
281 infinitely asks for the repr of itself). This should only be
282 possible within a type. */
283 i = Py_ReprEnter((PyObject *)v);
284 if (i != 0) {
285 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
286 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000287
Victor Stinner88a9cd92013-11-19 12:59:46 +0100288 _PyUnicodeWriter_Init(&writer);
289 writer.overallocate = 1;
290 if (Py_SIZE(v) > 1) {
291 /* "(" + "1" + ", 2" * (len - 1) + ")" */
292 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
293 }
294 else {
295 /* "(1,)" */
296 writer.min_length = 4;
297 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200298
Victor Stinner88a9cd92013-11-19 12:59:46 +0100299 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200300 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* Do repr() on each element. */
303 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100304 PyObject *s;
305
306 if (i > 0) {
307 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
308 goto error;
309 }
310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200312 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 s = PyObject_Repr(v->ob_item[i]);
314 Py_LeaveRecursiveCall();
Victor Stinner88a9cd92013-11-19 12:59:46 +0100315 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200316 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100317
318 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
319 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200320 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100321 }
322 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100324
325 writer.overallocate = 0;
326 if (n > 1) {
327 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
328 goto error;
329 }
330 else {
331 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
332 goto error;
333 }
Tim Petersa7259592001-06-16 05:11:17 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100336 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200337
338error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100339 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200340 Py_ReprLeave((PyObject *)v);
341 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342}
343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344/* The addend 82520, was selected from the range(0, 1000000) for
345 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000346 upto length eight:
347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000349 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100350
351 Tests have shown that it's not worth to cache the hash value, see
352 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000353*/
354
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000355static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000356tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000357{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200358 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
359 Py_hash_t y;
360 Py_ssize_t len = Py_SIZE(v);
361 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800362 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800363 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 p = v->ob_item;
365 while (--len >= 0) {
366 y = PyObject_Hash(*p++);
367 if (y == -1)
368 return -1;
369 x = (x ^ y) * mult;
370 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800371 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800373 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100374 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 x = -2;
376 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000377}
378
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000380tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383}
384
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000385static int
Fred Drakeba096332000-07-09 07:04:36 +0000386tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 Py_ssize_t i;
389 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
392 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
393 Py_EQ);
394 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000395}
396
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200398tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (i < 0 || i >= Py_SIZE(a)) {
401 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
402 return NULL;
403 }
404 Py_INCREF(a->ob_item[i]);
405 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406}
407
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200409tupleslice(PyTupleObject *a, Py_ssize_t ilow,
410 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200412 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200414 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 Py_ssize_t len;
416 if (ilow < 0)
417 ilow = 0;
418 if (ihigh > Py_SIZE(a))
419 ihigh = Py_SIZE(a);
420 if (ihigh < ilow)
421 ihigh = ilow;
422 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
423 Py_INCREF(a);
424 return (PyObject *)a;
425 }
426 len = ihigh - ilow;
427 np = (PyTupleObject *)PyTuple_New(len);
428 if (np == NULL)
429 return NULL;
430 src = a->ob_item + ilow;
431 dest = np->ob_item;
432 for (i = 0; i < len; i++) {
433 PyObject *v = src[i];
434 Py_INCREF(v);
435 dest[i] = v;
436 }
437 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438}
439
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (op == NULL || !PyTuple_Check(op)) {
444 PyErr_BadInternalCall();
445 return NULL;
446 }
447 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000448}
449
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000450static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200451tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200453 Py_ssize_t size;
454 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 PyObject **src, **dest;
456 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200457 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
458 Py_INCREF(bb);
459 return bb;
460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (!PyTuple_Check(bb)) {
462 PyErr_Format(PyExc_TypeError,
463 "can only concatenate tuple (not \"%.200s\") to tuple",
464 Py_TYPE(bb)->tp_name);
465 return NULL;
466 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200468 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
469 Py_INCREF(a);
470 return (PyObject *)a;
471 }
Martin Panterb93d8632016-07-25 02:39:20 +0000472 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000474 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 np = (PyTupleObject *) PyTuple_New(size);
476 if (np == NULL) {
477 return NULL;
478 }
479 src = a->ob_item;
480 dest = np->ob_item;
481 for (i = 0; i < Py_SIZE(a); i++) {
482 PyObject *v = src[i];
483 Py_INCREF(v);
484 dest[i] = v;
485 }
486 src = b->ob_item;
487 dest = np->ob_item + Py_SIZE(a);
488 for (i = 0; i < Py_SIZE(b); i++) {
489 PyObject *v = src[i];
490 Py_INCREF(v);
491 dest[i] = v;
492 }
493 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494#undef b
495}
496
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000498tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 Py_ssize_t i, j;
501 Py_ssize_t size;
502 PyTupleObject *np;
503 PyObject **p, **items;
504 if (n < 0)
505 n = 0;
506 if (Py_SIZE(a) == 0 || n == 1) {
507 if (PyTuple_CheckExact(a)) {
508 /* Since tuples are immutable, we can return a shared
509 copy in this case */
510 Py_INCREF(a);
511 return (PyObject *)a;
512 }
513 if (Py_SIZE(a) == 0)
514 return PyTuple_New(0);
515 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100516 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100518 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 np = (PyTupleObject *) PyTuple_New(size);
520 if (np == NULL)
521 return NULL;
522 p = np->ob_item;
523 items = a->ob_item;
524 for (i = 0; i < n; i++) {
525 for (j = 0; j < Py_SIZE(a); j++) {
526 *p = items[j];
527 Py_INCREF(*p);
528 p++;
529 }
530 }
531 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000532}
533
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200534/*[clinic input]
535tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000536
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200537 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300538 start: slice_index(accept={int}) = 0
539 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200540 /
541
542Return first index of value.
543
544Raises ValueError if the value is not present.
545[clinic start generated code]*/
546
547static PyObject *
548tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
549 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300550/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200551{
552 Py_ssize_t i;
553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (start < 0) {
555 start += Py_SIZE(self);
556 if (start < 0)
557 start = 0;
558 }
559 if (stop < 0) {
560 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200562 else if (stop > Py_SIZE(self)) {
563 stop = Py_SIZE(self);
564 }
565 for (i = start; i < stop; i++) {
566 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (cmp > 0)
568 return PyLong_FromSsize_t(i);
569 else if (cmp < 0)
570 return NULL;
571 }
572 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
573 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000574}
575
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200576/*[clinic input]
577tuple.count
578
579 value: object
580 /
581
582Return number of occurrences of value.
583[clinic start generated code]*/
584
Raymond Hettinger65baa342008-02-07 00:41:02 +0000585static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200586tuple_count(PyTupleObject *self, PyObject *value)
587/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 Py_ssize_t count = 0;
590 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200593 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 if (cmp > 0)
595 count++;
596 else if (cmp < 0)
597 return NULL;
598 }
599 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000600}
601
Jeremy Hylton8caad492000-06-23 14:18:11 +0000602static int
603tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 for (i = Py_SIZE(o); --i >= 0; )
608 Py_VISIT(o->ob_item[i]);
609 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000610}
611
Guido van Rossumf77bc622001-01-18 00:00:53 +0000612static PyObject *
613tuplerichcompare(PyObject *v, PyObject *w, int op)
614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 PyTupleObject *vt, *wt;
616 Py_ssize_t i;
617 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000618
Brian Curtindfc80e32011-08-10 20:28:54 -0500619 if (!PyTuple_Check(v) || !PyTuple_Check(w))
620 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 vt = (PyTupleObject *)v;
623 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 vlen = Py_SIZE(vt);
626 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 /* Note: the corresponding code for lists has an "early out" test
629 * here when op is EQ or NE and the lengths differ. That pays there,
630 * but Tim was unable to find any real code where EQ/NE tuple
631 * compares don't have the same length, so testing for it here would
632 * have cost without benefit.
633 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 /* Search for the first index where items are different.
636 * Note that because tuples are immutable, it's safe to reuse
637 * vlen and wlen across the comparison calls.
638 */
639 for (i = 0; i < vlen && i < wlen; i++) {
640 int k = PyObject_RichCompareBool(vt->ob_item[i],
641 wt->ob_item[i], Py_EQ);
642 if (k < 0)
643 return NULL;
644 if (!k)
645 break;
646 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 if (i >= vlen || i >= wlen) {
649 /* No more items to compare -- compare sizes */
650 int cmp;
651 PyObject *res;
652 switch (op) {
653 case Py_LT: cmp = vlen < wlen; break;
654 case Py_LE: cmp = vlen <= wlen; break;
655 case Py_EQ: cmp = vlen == wlen; break;
656 case Py_NE: cmp = vlen != wlen; break;
657 case Py_GT: cmp = vlen > wlen; break;
658 case Py_GE: cmp = vlen >= wlen; break;
659 default: return NULL; /* cannot happen */
660 }
661 if (cmp)
662 res = Py_True;
663 else
664 res = Py_False;
665 Py_INCREF(res);
666 return res;
667 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 /* We have an item that differs -- shortcuts for EQ/NE */
670 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200671 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 }
673 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200674 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 /* Compare the final item again using the proper operator */
678 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000679}
680
Jeremy Hylton938ace62002-07-17 16:30:39 +0000681static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200682tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
683
684/*[clinic input]
685@classmethod
686tuple.__new__ as tuple_new
687 iterable: object(c_default="NULL") = ()
688 /
689
690Built-in immutable sequence.
691
692If no argument is given, the constructor returns an empty tuple.
693If iterable is specified the tuple is initialized from iterable's items.
694
695If the argument is a tuple, the return value is the same object.
696[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000697
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200699tuple_new_impl(PyTypeObject *type, PyObject *iterable)
700/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200703 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200705 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return PyTuple_New(0);
707 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200708 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709}
710
Guido van Rossumae960af2001-08-30 03:11:59 +0000711static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200712tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyObject *tmp, *newobj, *item;
715 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200718 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 if (tmp == NULL)
720 return NULL;
721 assert(PyTuple_Check(tmp));
722 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
723 if (newobj == NULL)
724 return NULL;
725 for (i = 0; i < n; i++) {
726 item = PyTuple_GET_ITEM(tmp, i);
727 Py_INCREF(item);
728 PyTuple_SET_ITEM(newobj, i, item);
729 }
730 Py_DECREF(tmp);
731 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000732}
733
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 (lenfunc)tuplelength, /* sq_length */
736 (binaryfunc)tupleconcat, /* sq_concat */
737 (ssizeargfunc)tuplerepeat, /* sq_repeat */
738 (ssizeargfunc)tupleitem, /* sq_item */
739 0, /* sq_slice */
740 0, /* sq_ass_item */
741 0, /* sq_ass_slice */
742 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000743};
744
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000745static PyObject*
746tuplesubscript(PyTupleObject* self, PyObject* item)
747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 if (PyIndex_Check(item)) {
749 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
750 if (i == -1 && PyErr_Occurred())
751 return NULL;
752 if (i < 0)
753 i += PyTuple_GET_SIZE(self);
754 return tupleitem(self, i);
755 }
756 else if (PySlice_Check(item)) {
757 Py_ssize_t start, stop, step, slicelength, cur, i;
758 PyObject* result;
759 PyObject* it;
760 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000761
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300762 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 return NULL;
764 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300765 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
766 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 if (slicelength <= 0) {
769 return PyTuple_New(0);
770 }
771 else if (start == 0 && step == 1 &&
772 slicelength == PyTuple_GET_SIZE(self) &&
773 PyTuple_CheckExact(self)) {
774 Py_INCREF(self);
775 return (PyObject *)self;
776 }
777 else {
778 result = PyTuple_New(slicelength);
779 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 src = self->ob_item;
782 dest = ((PyTupleObject *)result)->ob_item;
783 for (cur = start, i = 0; i < slicelength;
784 cur += step, i++) {
785 it = src[cur];
786 Py_INCREF(it);
787 dest[i] = it;
788 }
789
790 return result;
791 }
792 }
793 else {
794 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400795 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 Py_TYPE(item)->tp_name);
797 return NULL;
798 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000799}
800
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200801/*[clinic input]
802tuple.__getnewargs__
803[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200805static PyObject *
806tuple___getnewargs___impl(PyTupleObject *self)
807/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
808{
809 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000810}
811
812static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200813 TUPLE___GETNEWARGS___METHODDEF
814 TUPLE_INDEX_METHODDEF
815 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000817};
818
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000819static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 (lenfunc)tuplelength,
821 (binaryfunc)tuplesubscript,
822 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000823};
824
Raymond Hettinger48923c52002-08-09 01:30:17 +0000825static PyObject *tuple_iter(PyObject *seq);
826
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 PyVarObject_HEAD_INIT(&PyType_Type, 0)
829 "tuple",
830 sizeof(PyTupleObject) - sizeof(PyObject *),
831 sizeof(PyObject *),
832 (destructor)tupledealloc, /* tp_dealloc */
833 0, /* tp_print */
834 0, /* tp_getattr */
835 0, /* tp_setattr */
836 0, /* tp_reserved */
837 (reprfunc)tuplerepr, /* tp_repr */
838 0, /* tp_as_number */
839 &tuple_as_sequence, /* tp_as_sequence */
840 &tuple_as_mapping, /* tp_as_mapping */
841 (hashfunc)tuplehash, /* tp_hash */
842 0, /* tp_call */
843 0, /* tp_str */
844 PyObject_GenericGetAttr, /* tp_getattro */
845 0, /* tp_setattro */
846 0, /* tp_as_buffer */
847 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
848 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200849 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 (traverseproc)tupletraverse, /* tp_traverse */
851 0, /* tp_clear */
852 tuplerichcompare, /* tp_richcompare */
853 0, /* tp_weaklistoffset */
854 tuple_iter, /* tp_iter */
855 0, /* tp_iternext */
856 tuple_methods, /* tp_methods */
857 0, /* tp_members */
858 0, /* tp_getset */
859 0, /* tp_base */
860 0, /* tp_dict */
861 0, /* tp_descr_get */
862 0, /* tp_descr_set */
863 0, /* tp_dictoffset */
864 0, /* tp_init */
865 0, /* tp_alloc */
866 tuple_new, /* tp_new */
867 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000869
870/* The following function breaks the notion that tuples are immutable:
871 it changes the size of a tuple. We get away with this only if there
872 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000873 as creating a new tuple object and destroying the old one, only more
874 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000875 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000876
877int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000878_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000879{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200880 PyTupleObject *v;
881 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 Py_ssize_t i;
883 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 v = (PyTupleObject *) *pv;
886 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
887 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
888 *pv = 0;
889 Py_XDECREF(v);
890 PyErr_BadInternalCall();
891 return -1;
892 }
893 oldsize = Py_SIZE(v);
894 if (oldsize == newsize)
895 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (oldsize == 0) {
898 /* Empty tuples are often shared, so we should never
899 resize them in-place even if we do own the only
900 (current) reference */
901 Py_DECREF(v);
902 *pv = PyTuple_New(newsize);
903 return *pv == NULL ? -1 : 0;
904 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* XXX UNREF/NEWREF interface should be more symmetrical */
907 _Py_DEC_REFTOTAL;
908 if (_PyObject_GC_IS_TRACKED(v))
909 _PyObject_GC_UNTRACK(v);
910 _Py_ForgetReference((PyObject *) v);
911 /* DECREF items deleted by shrinkage */
912 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200913 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 }
915 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
916 if (sv == NULL) {
917 *pv = NULL;
918 PyObject_GC_Del(v);
919 return -1;
920 }
921 _Py_NewReference((PyObject *) sv);
922 /* Zero out items added by growing */
923 if (newsize > oldsize)
924 memset(&sv->ob_item[oldsize], 0,
925 sizeof(*sv->ob_item) * (newsize - oldsize));
926 *pv = (PyObject *) sv;
927 _PyObject_GC_TRACK(sv);
928 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000929}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000930
Christian Heimesa156e092008-02-16 07:38:31 +0000931int
932PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000935#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 int i;
937 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
938 PyTupleObject *p, *q;
939 p = free_list[i];
940 freelist_size += numfree[i];
941 free_list[i] = NULL;
942 numfree[i] = 0;
943 while (p) {
944 q = p;
945 p = (PyTupleObject *)(p->ob_item[0]);
946 PyObject_GC_Del(q);
947 }
948 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000949#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000951}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952
Christian Heimesa156e092008-02-16 07:38:31 +0000953void
954PyTuple_Fini(void)
955{
956#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 /* empty tuples are used all over the place and applications may
958 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200959 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000962#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000963#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000965#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000966}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000967
968/*********************** Tuple Iterator **************************/
969
970typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200972 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000974} tupleiterobject;
975
Raymond Hettinger48923c52002-08-09 01:30:17 +0000976static void
977tupleiter_dealloc(tupleiterobject *it)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 _PyObject_GC_UNTRACK(it);
980 Py_XDECREF(it->it_seq);
981 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000982}
983
984static int
985tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 Py_VISIT(it->it_seq);
988 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000989}
990
Raymond Hettinger48923c52002-08-09 01:30:17 +0000991static PyObject *
992tupleiter_next(tupleiterobject *it)
993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyTupleObject *seq;
995 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 assert(it != NULL);
998 seq = it->it_seq;
999 if (seq == NULL)
1000 return NULL;
1001 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1004 item = PyTuple_GET_ITEM(seq, it->it_index);
1005 ++it->it_index;
1006 Py_INCREF(item);
1007 return item;
1008 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001011 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001013}
1014
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001015static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +00001016tupleiter_len(tupleiterobject *it)
1017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 Py_ssize_t len = 0;
1019 if (it->it_seq)
1020 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1021 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001022}
1023
Armin Rigof5b3e362006-02-11 21:32:43 +00001024PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001025
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001026static PyObject *
1027tupleiter_reduce(tupleiterobject *it)
1028{
1029 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001030 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001031 it->it_seq, it->it_index);
1032 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001033 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001034}
1035
1036static PyObject *
1037tupleiter_setstate(tupleiterobject *it, PyObject *state)
1038{
Victor Stinner7660b882013-06-24 23:59:24 +02001039 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001040 if (index == -1 && PyErr_Occurred())
1041 return NULL;
1042 if (it->it_seq != NULL) {
1043 if (index < 0)
1044 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001045 else if (index > PyTuple_GET_SIZE(it->it_seq))
1046 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001047 it->it_index = index;
1048 }
1049 Py_RETURN_NONE;
1050}
1051
1052PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1053PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1054
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001055static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001057 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1058 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001060};
1061
Raymond Hettinger48923c52002-08-09 01:30:17 +00001062PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1064 "tuple_iterator", /* tp_name */
1065 sizeof(tupleiterobject), /* tp_basicsize */
1066 0, /* tp_itemsize */
1067 /* methods */
1068 (destructor)tupleiter_dealloc, /* tp_dealloc */
1069 0, /* tp_print */
1070 0, /* tp_getattr */
1071 0, /* tp_setattr */
1072 0, /* tp_reserved */
1073 0, /* tp_repr */
1074 0, /* tp_as_number */
1075 0, /* tp_as_sequence */
1076 0, /* tp_as_mapping */
1077 0, /* tp_hash */
1078 0, /* tp_call */
1079 0, /* tp_str */
1080 PyObject_GenericGetAttr, /* tp_getattro */
1081 0, /* tp_setattro */
1082 0, /* tp_as_buffer */
1083 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1084 0, /* tp_doc */
1085 (traverseproc)tupleiter_traverse, /* tp_traverse */
1086 0, /* tp_clear */
1087 0, /* tp_richcompare */
1088 0, /* tp_weaklistoffset */
1089 PyObject_SelfIter, /* tp_iter */
1090 (iternextfunc)tupleiter_next, /* tp_iternext */
1091 tupleiter_methods, /* tp_methods */
1092 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001093};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001094
1095static PyObject *
1096tuple_iter(PyObject *seq)
1097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (!PyTuple_Check(seq)) {
1101 PyErr_BadInternalCall();
1102 return NULL;
1103 }
1104 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1105 if (it == NULL)
1106 return NULL;
1107 it->it_index = 0;
1108 Py_INCREF(seq);
1109 it->it_seq = (PyTupleObject *)seq;
1110 _PyObject_GC_TRACK(it);
1111 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001112}