blob: 2a9049049935e6bc22f7aeb1653b941ee2130c0f [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"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01005#include "accu.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Serhiy Storchaka0b561592017-03-19 08:47:58 +02007/*[clinic input]
8class tuple "PyTupleObject *" "&PyTuple_Type"
9[clinic start generated code]*/
10/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/
11
12#include "clinic/tupleobject.c.h"
13
Guido van Rossum5ce78f82000-04-21 21:15:05 +000014/* Speed optimization to avoid frequent malloc/free of small tuples */
Christian Heimes2202f872008-02-06 14:31:34 +000015#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +000017#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018#ifndef PyTuple_MAXFREELIST
Christian Heimes2202f872008-02-06 14:31:34 +000019#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000020#endif
21
Christian Heimes2202f872008-02-06 14:31:34 +000022#if PyTuple_MAXSAVESIZE > 0
23/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000024 tuple () of which at most one instance will be allocated.
25*/
Christian Heimes2202f872008-02-06 14:31:34 +000026static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
27static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000028#endif
29#ifdef COUNT_ALLOCS
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000030Py_ssize_t fast_tuple_allocs;
31Py_ssize_t tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000032#endif
33
Antoine Pitrou3a652b12009-03-23 18:52:06 +000034/* Debug statistic to count GC tracking of tuples.
35 Please note that tuples are only untracked when considered by the GC, and
36 many of them will be dead before. Therefore, a tracking rate close to 100%
37 does not necessarily prove that the heuristic is inefficient.
38*/
39#ifdef SHOW_TRACK_COUNT
40static Py_ssize_t count_untracked = 0;
41static Py_ssize_t count_tracked = 0;
42
43static void
44show_track(void)
45{
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030046 PyObject *xoptions, *value;
47 _Py_IDENTIFIER(showalloccount);
48
49 xoptions = PySys_GetXOptions();
50 if (xoptions == NULL)
51 return;
52 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
53 if (value != Py_True)
54 return;
55
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
57 count_tracked + count_untracked);
58 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
59 "d\n", count_tracked);
60 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
61 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000062}
63#endif
64
David Malcolm49526f42012-06-22 14:55:41 -040065/* Print summary info about the state of the optimized allocator */
66void
67_PyTuple_DebugMallocStats(FILE *out)
68{
69#if PyTuple_MAXSAVESIZE > 0
70 int i;
71 char buf[128];
72 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
73 PyOS_snprintf(buf, sizeof(buf),
74 "free %d-sized PyTupleObject", i);
75 _PyDebugAllocatorStats(out,
76 buf,
77 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
78 }
79#endif
80}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000081
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020083PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020085 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 Py_ssize_t i;
87 if (size < 0) {
88 PyErr_BadInternalCall();
89 return NULL;
90 }
Christian Heimes2202f872008-02-06 14:31:34 +000091#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (size == 0 && free_list[0]) {
93 op = free_list[0];
94 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000095#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000097#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 return (PyObject *) op;
99 }
100 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
101 free_list[size] = (PyTupleObject *) op->ob_item[0];
102 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000103#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000105#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000107#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 Py_SIZE(op) = size;
109 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 _Py_NewReference((PyObject *)op);
112 }
113 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000114#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200117 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100118 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 return PyErr_NoMemory();
120 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
122 if (op == NULL)
123 return NULL;
124 }
125 for (i=0; i < size; i++)
126 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000127#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (size == 0) {
129 free_list[0] = op;
130 ++numfree[0];
131 Py_INCREF(op); /* extra INCREF so that this is never freed */
132 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000133#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000134#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000136#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 _PyObject_GC_TRACK(op);
138 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139}
140
Martin v. Löwis18e16552006-02-15 17:27:45 +0000141Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (!PyTuple_Check(op)) {
145 PyErr_BadInternalCall();
146 return -1;
147 }
148 else
149 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150}
151
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200153PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 if (!PyTuple_Check(op)) {
156 PyErr_BadInternalCall();
157 return NULL;
158 }
159 if (i < 0 || i >= Py_SIZE(op)) {
160 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
161 return NULL;
162 }
163 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164}
165
166int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200167PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200169 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
171 Py_XDECREF(newitem);
172 PyErr_BadInternalCall();
173 return -1;
174 }
175 if (i < 0 || i >= Py_SIZE(op)) {
176 Py_XDECREF(newitem);
177 PyErr_SetString(PyExc_IndexError,
178 "tuple assignment index out of range");
179 return -1;
180 }
181 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300182 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184}
185
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000186void
187_PyTuple_MaybeUntrack(PyObject *op)
188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 PyTupleObject *t;
190 Py_ssize_t i, n;
191
192 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
193 return;
194 t = (PyTupleObject *) op;
195 n = Py_SIZE(t);
196 for (i = 0; i < n; i++) {
197 PyObject *elt = PyTuple_GET_ITEM(t, i);
198 /* Tuple with NULL elements aren't
199 fully constructed, don't untrack
200 them yet. */
201 if (!elt ||
202 _PyObject_GC_MAY_BE_TRACKED(elt))
203 return;
204 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000205#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 count_tracked--;
207 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000208#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000210}
211
Raymond Hettingercb2da432003-10-12 18:24:34 +0000212PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000213PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_ssize_t i;
216 PyObject *o;
217 PyObject *result;
218 PyObject **items;
219 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 va_start(vargs, n);
222 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200223 if (result == NULL) {
224 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 items = ((PyTupleObject *)result)->ob_item;
228 for (i = 0; i < n; i++) {
229 o = va_arg(vargs, PyObject *);
230 Py_INCREF(o);
231 items[i] = o;
232 }
233 va_end(vargs);
234 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000235}
236
237
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238/* Methods */
239
240static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200241tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200243 Py_ssize_t i;
244 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyObject_GC_UnTrack(op);
246 Py_TRASHCAN_SAFE_BEGIN(op)
247 if (len > 0) {
248 i = len;
249 while (--i >= 0)
250 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000251#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (len < PyTuple_MAXSAVESIZE &&
253 numfree[len] < PyTuple_MAXFREELIST &&
254 Py_TYPE(op) == &PyTuple_Type)
255 {
256 op->ob_item[0] = (PyObject *) free_list[len];
257 numfree[len]++;
258 free_list[len] = op;
259 goto done; /* return */
260 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000261#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 }
263 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000264done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266}
267
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000268static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000269tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100272 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 n = Py_SIZE(v);
275 if (n == 0)
276 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* While not mutable, it is still possible to end up with a cycle in a
279 tuple through an object that stores itself within a tuple (and thus
280 infinitely asks for the repr of itself). This should only be
281 possible within a type. */
282 i = Py_ReprEnter((PyObject *)v);
283 if (i != 0) {
284 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
285 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000286
Victor Stinner88a9cd92013-11-19 12:59:46 +0100287 _PyUnicodeWriter_Init(&writer);
288 writer.overallocate = 1;
289 if (Py_SIZE(v) > 1) {
290 /* "(" + "1" + ", 2" * (len - 1) + ")" */
291 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
292 }
293 else {
294 /* "(1,)" */
295 writer.min_length = 4;
296 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200297
Victor Stinner88a9cd92013-11-19 12:59:46 +0100298 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200299 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 /* Do repr() on each element. */
302 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100303 PyObject *s;
304
305 if (i > 0) {
306 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
307 goto error;
308 }
309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200311 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 s = PyObject_Repr(v->ob_item[i]);
313 Py_LeaveRecursiveCall();
Victor Stinner88a9cd92013-11-19 12:59:46 +0100314 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200315 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100316
317 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
318 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200319 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100320 }
321 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100323
324 writer.overallocate = 0;
325 if (n > 1) {
326 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
327 goto error;
328 }
329 else {
330 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
331 goto error;
332 }
Tim Petersa7259592001-06-16 05:11:17 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100335 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200336
337error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100338 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200339 Py_ReprLeave((PyObject *)v);
340 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341}
342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343/* The addend 82520, was selected from the range(0, 1000000) for
344 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000345 upto length eight:
346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000348 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100349
350 Tests have shown that it's not worth to cache the hash value, see
351 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000352*/
353
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000354static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000355tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000356{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200357 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
358 Py_hash_t y;
359 Py_ssize_t len = Py_SIZE(v);
360 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800361 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800362 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 p = v->ob_item;
364 while (--len >= 0) {
365 y = PyObject_Hash(*p++);
366 if (y == -1)
367 return -1;
368 x = (x ^ y) * mult;
369 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800370 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800372 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100373 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 x = -2;
375 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000376}
377
Martin v. Löwis18e16552006-02-15 17:27:45 +0000378static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000379tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382}
383
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000384static int
Fred Drakeba096332000-07-09 07:04:36 +0000385tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 Py_ssize_t i;
388 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
391 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
392 Py_EQ);
393 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000394}
395
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200397tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (i < 0 || i >= Py_SIZE(a)) {
400 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
401 return NULL;
402 }
403 Py_INCREF(a->ob_item[i]);
404 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405}
406
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200408tupleslice(PyTupleObject *a, Py_ssize_t ilow,
409 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200411 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200413 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 Py_ssize_t len;
415 if (ilow < 0)
416 ilow = 0;
417 if (ihigh > Py_SIZE(a))
418 ihigh = Py_SIZE(a);
419 if (ihigh < ilow)
420 ihigh = ilow;
421 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
422 Py_INCREF(a);
423 return (PyObject *)a;
424 }
425 len = ihigh - ilow;
426 np = (PyTupleObject *)PyTuple_New(len);
427 if (np == NULL)
428 return NULL;
429 src = a->ob_item + ilow;
430 dest = np->ob_item;
431 for (i = 0; i < len; i++) {
432 PyObject *v = src[i];
433 Py_INCREF(v);
434 dest[i] = v;
435 }
436 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000440PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (op == NULL || !PyTuple_Check(op)) {
443 PyErr_BadInternalCall();
444 return NULL;
445 }
446 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000447}
448
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200450tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200452 Py_ssize_t size;
453 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyObject **src, **dest;
455 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200456 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
457 Py_INCREF(bb);
458 return bb;
459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (!PyTuple_Check(bb)) {
461 PyErr_Format(PyExc_TypeError,
462 "can only concatenate tuple (not \"%.200s\") to tuple",
463 Py_TYPE(bb)->tp_name);
464 return NULL;
465 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200467 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
468 Py_INCREF(a);
469 return (PyObject *)a;
470 }
Martin Panterb93d8632016-07-25 02:39:20 +0000471 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000473 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 np = (PyTupleObject *) PyTuple_New(size);
475 if (np == NULL) {
476 return NULL;
477 }
478 src = a->ob_item;
479 dest = np->ob_item;
480 for (i = 0; i < Py_SIZE(a); i++) {
481 PyObject *v = src[i];
482 Py_INCREF(v);
483 dest[i] = v;
484 }
485 src = b->ob_item;
486 dest = np->ob_item + Py_SIZE(a);
487 for (i = 0; i < Py_SIZE(b); i++) {
488 PyObject *v = src[i];
489 Py_INCREF(v);
490 dest[i] = v;
491 }
492 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493#undef b
494}
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000497tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 Py_ssize_t i, j;
500 Py_ssize_t size;
501 PyTupleObject *np;
502 PyObject **p, **items;
503 if (n < 0)
504 n = 0;
505 if (Py_SIZE(a) == 0 || n == 1) {
506 if (PyTuple_CheckExact(a)) {
507 /* Since tuples are immutable, we can return a shared
508 copy in this case */
509 Py_INCREF(a);
510 return (PyObject *)a;
511 }
512 if (Py_SIZE(a) == 0)
513 return PyTuple_New(0);
514 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100515 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100517 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 np = (PyTupleObject *) PyTuple_New(size);
519 if (np == NULL)
520 return NULL;
521 p = np->ob_item;
522 items = a->ob_item;
523 for (i = 0; i < n; i++) {
524 for (j = 0; j < Py_SIZE(a); j++) {
525 *p = items[j];
526 Py_INCREF(*p);
527 p++;
528 }
529 }
530 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000531}
532
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200533/*[clinic input]
534tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000535
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200536 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300537 start: slice_index(accept={int}) = 0
538 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200539 /
540
541Return first index of value.
542
543Raises ValueError if the value is not present.
544[clinic start generated code]*/
545
546static PyObject *
547tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
548 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300549/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200550{
551 Py_ssize_t i;
552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (start < 0) {
554 start += Py_SIZE(self);
555 if (start < 0)
556 start = 0;
557 }
558 if (stop < 0) {
559 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200561 else if (stop > Py_SIZE(self)) {
562 stop = Py_SIZE(self);
563 }
564 for (i = start; i < stop; i++) {
565 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (cmp > 0)
567 return PyLong_FromSsize_t(i);
568 else if (cmp < 0)
569 return NULL;
570 }
571 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
572 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000573}
574
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200575/*[clinic input]
576tuple.count
577
578 value: object
579 /
580
581Return number of occurrences of value.
582[clinic start generated code]*/
583
Raymond Hettinger65baa342008-02-07 00:41:02 +0000584static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200585tuple_count(PyTupleObject *self, PyObject *value)
586/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 Py_ssize_t count = 0;
589 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200592 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (cmp > 0)
594 count++;
595 else if (cmp < 0)
596 return NULL;
597 }
598 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000599}
600
Jeremy Hylton8caad492000-06-23 14:18:11 +0000601static int
602tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 for (i = Py_SIZE(o); --i >= 0; )
607 Py_VISIT(o->ob_item[i]);
608 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000609}
610
Guido van Rossumf77bc622001-01-18 00:00:53 +0000611static PyObject *
612tuplerichcompare(PyObject *v, PyObject *w, int op)
613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PyTupleObject *vt, *wt;
615 Py_ssize_t i;
616 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000617
Brian Curtindfc80e32011-08-10 20:28:54 -0500618 if (!PyTuple_Check(v) || !PyTuple_Check(w))
619 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 vt = (PyTupleObject *)v;
622 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 vlen = Py_SIZE(vt);
625 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Note: the corresponding code for lists has an "early out" test
628 * here when op is EQ or NE and the lengths differ. That pays there,
629 * but Tim was unable to find any real code where EQ/NE tuple
630 * compares don't have the same length, so testing for it here would
631 * have cost without benefit.
632 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 /* Search for the first index where items are different.
635 * Note that because tuples are immutable, it's safe to reuse
636 * vlen and wlen across the comparison calls.
637 */
638 for (i = 0; i < vlen && i < wlen; i++) {
639 int k = PyObject_RichCompareBool(vt->ob_item[i],
640 wt->ob_item[i], Py_EQ);
641 if (k < 0)
642 return NULL;
643 if (!k)
644 break;
645 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (i >= vlen || i >= wlen) {
648 /* No more items to compare -- compare sizes */
649 int cmp;
650 PyObject *res;
651 switch (op) {
652 case Py_LT: cmp = vlen < wlen; break;
653 case Py_LE: cmp = vlen <= wlen; break;
654 case Py_EQ: cmp = vlen == wlen; break;
655 case Py_NE: cmp = vlen != wlen; break;
656 case Py_GT: cmp = vlen > wlen; break;
657 case Py_GE: cmp = vlen >= wlen; break;
658 default: return NULL; /* cannot happen */
659 }
660 if (cmp)
661 res = Py_True;
662 else
663 res = Py_False;
664 Py_INCREF(res);
665 return res;
666 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 /* We have an item that differs -- shortcuts for EQ/NE */
669 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200670 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 }
672 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200673 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 /* Compare the final item again using the proper operator */
677 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000678}
679
Jeremy Hylton938ace62002-07-17 16:30:39 +0000680static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200681tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
682
683/*[clinic input]
684@classmethod
685tuple.__new__ as tuple_new
686 iterable: object(c_default="NULL") = ()
687 /
688
689Built-in immutable sequence.
690
691If no argument is given, the constructor returns an empty tuple.
692If iterable is specified the tuple is initialized from iterable's items.
693
694If the argument is a tuple, the return value is the same object.
695[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000696
Tim Peters6d6c1a32001-08-02 04:15:00 +0000697static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200698tuple_new_impl(PyTypeObject *type, PyObject *iterable)
699/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200702 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200704 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return PyTuple_New(0);
706 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200707 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000708}
709
Guido van Rossumae960af2001-08-30 03:11:59 +0000710static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200711tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 PyObject *tmp, *newobj, *item;
714 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200717 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 if (tmp == NULL)
719 return NULL;
720 assert(PyTuple_Check(tmp));
721 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
722 if (newobj == NULL)
723 return NULL;
724 for (i = 0; i < n; i++) {
725 item = PyTuple_GET_ITEM(tmp, i);
726 Py_INCREF(item);
727 PyTuple_SET_ITEM(newobj, i, item);
728 }
729 Py_DECREF(tmp);
730 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000731}
732
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 (lenfunc)tuplelength, /* sq_length */
735 (binaryfunc)tupleconcat, /* sq_concat */
736 (ssizeargfunc)tuplerepeat, /* sq_repeat */
737 (ssizeargfunc)tupleitem, /* sq_item */
738 0, /* sq_slice */
739 0, /* sq_ass_item */
740 0, /* sq_ass_slice */
741 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742};
743
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000744static PyObject*
745tuplesubscript(PyTupleObject* self, PyObject* item)
746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (PyIndex_Check(item)) {
748 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
749 if (i == -1 && PyErr_Occurred())
750 return NULL;
751 if (i < 0)
752 i += PyTuple_GET_SIZE(self);
753 return tupleitem(self, i);
754 }
755 else if (PySlice_Check(item)) {
756 Py_ssize_t start, stop, step, slicelength, cur, i;
757 PyObject* result;
758 PyObject* it;
759 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000760
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300761 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 return NULL;
763 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300764 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
765 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (slicelength <= 0) {
768 return PyTuple_New(0);
769 }
770 else if (start == 0 && step == 1 &&
771 slicelength == PyTuple_GET_SIZE(self) &&
772 PyTuple_CheckExact(self)) {
773 Py_INCREF(self);
774 return (PyObject *)self;
775 }
776 else {
777 result = PyTuple_New(slicelength);
778 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 src = self->ob_item;
781 dest = ((PyTupleObject *)result)->ob_item;
782 for (cur = start, i = 0; i < slicelength;
783 cur += step, i++) {
784 it = src[cur];
785 Py_INCREF(it);
786 dest[i] = it;
787 }
788
789 return result;
790 }
791 }
792 else {
793 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400794 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 Py_TYPE(item)->tp_name);
796 return NULL;
797 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000798}
799
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200800/*[clinic input]
801tuple.__getnewargs__
802[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200804static PyObject *
805tuple___getnewargs___impl(PyTupleObject *self)
806/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
807{
808 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000809}
810
811static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200812 TUPLE___GETNEWARGS___METHODDEF
813 TUPLE_INDEX_METHODDEF
814 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000816};
817
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000818static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 (lenfunc)tuplelength,
820 (binaryfunc)tuplesubscript,
821 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000822};
823
Raymond Hettinger48923c52002-08-09 01:30:17 +0000824static PyObject *tuple_iter(PyObject *seq);
825
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 PyVarObject_HEAD_INIT(&PyType_Type, 0)
828 "tuple",
829 sizeof(PyTupleObject) - sizeof(PyObject *),
830 sizeof(PyObject *),
831 (destructor)tupledealloc, /* tp_dealloc */
832 0, /* tp_print */
833 0, /* tp_getattr */
834 0, /* tp_setattr */
835 0, /* tp_reserved */
836 (reprfunc)tuplerepr, /* tp_repr */
837 0, /* tp_as_number */
838 &tuple_as_sequence, /* tp_as_sequence */
839 &tuple_as_mapping, /* tp_as_mapping */
840 (hashfunc)tuplehash, /* tp_hash */
841 0, /* tp_call */
842 0, /* tp_str */
843 PyObject_GenericGetAttr, /* tp_getattro */
844 0, /* tp_setattro */
845 0, /* tp_as_buffer */
846 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
847 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200848 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 (traverseproc)tupletraverse, /* tp_traverse */
850 0, /* tp_clear */
851 tuplerichcompare, /* tp_richcompare */
852 0, /* tp_weaklistoffset */
853 tuple_iter, /* tp_iter */
854 0, /* tp_iternext */
855 tuple_methods, /* tp_methods */
856 0, /* tp_members */
857 0, /* tp_getset */
858 0, /* tp_base */
859 0, /* tp_dict */
860 0, /* tp_descr_get */
861 0, /* tp_descr_set */
862 0, /* tp_dictoffset */
863 0, /* tp_init */
864 0, /* tp_alloc */
865 tuple_new, /* tp_new */
866 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000867};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000868
869/* The following function breaks the notion that tuples are immutable:
870 it changes the size of a tuple. We get away with this only if there
871 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000872 as creating a new tuple object and destroying the old one, only more
873 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000874 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000875
876int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000877_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000878{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200879 PyTupleObject *v;
880 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_ssize_t i;
882 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 v = (PyTupleObject *) *pv;
885 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
886 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
887 *pv = 0;
888 Py_XDECREF(v);
889 PyErr_BadInternalCall();
890 return -1;
891 }
892 oldsize = Py_SIZE(v);
893 if (oldsize == newsize)
894 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 if (oldsize == 0) {
897 /* Empty tuples are often shared, so we should never
898 resize them in-place even if we do own the only
899 (current) reference */
900 Py_DECREF(v);
901 *pv = PyTuple_New(newsize);
902 return *pv == NULL ? -1 : 0;
903 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 /* XXX UNREF/NEWREF interface should be more symmetrical */
906 _Py_DEC_REFTOTAL;
907 if (_PyObject_GC_IS_TRACKED(v))
908 _PyObject_GC_UNTRACK(v);
909 _Py_ForgetReference((PyObject *) v);
910 /* DECREF items deleted by shrinkage */
911 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200912 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 }
914 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
915 if (sv == NULL) {
916 *pv = NULL;
917 PyObject_GC_Del(v);
918 return -1;
919 }
920 _Py_NewReference((PyObject *) sv);
921 /* Zero out items added by growing */
922 if (newsize > oldsize)
923 memset(&sv->ob_item[oldsize], 0,
924 sizeof(*sv->ob_item) * (newsize - oldsize));
925 *pv = (PyObject *) sv;
926 _PyObject_GC_TRACK(sv);
927 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000928}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000929
Christian Heimesa156e092008-02-16 07:38:31 +0000930int
931PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000934#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 int i;
936 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
937 PyTupleObject *p, *q;
938 p = free_list[i];
939 freelist_size += numfree[i];
940 free_list[i] = NULL;
941 numfree[i] = 0;
942 while (p) {
943 q = p;
944 p = (PyTupleObject *)(p->ob_item[0]);
945 PyObject_GC_Del(q);
946 }
947 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000948#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000950}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951
Christian Heimesa156e092008-02-16 07:38:31 +0000952void
953PyTuple_Fini(void)
954{
955#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 /* empty tuples are used all over the place and applications may
957 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200958 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000961#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000962#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000964#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000965}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000966
967/*********************** Tuple Iterator **************************/
968
969typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200971 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973} tupleiterobject;
974
Raymond Hettinger48923c52002-08-09 01:30:17 +0000975static void
976tupleiter_dealloc(tupleiterobject *it)
977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 _PyObject_GC_UNTRACK(it);
979 Py_XDECREF(it->it_seq);
980 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000981}
982
983static int
984tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_VISIT(it->it_seq);
987 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000988}
989
Raymond Hettinger48923c52002-08-09 01:30:17 +0000990static PyObject *
991tupleiter_next(tupleiterobject *it)
992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyTupleObject *seq;
994 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 assert(it != NULL);
997 seq = it->it_seq;
998 if (seq == NULL)
999 return NULL;
1000 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1003 item = PyTuple_GET_ITEM(seq, it->it_index);
1004 ++it->it_index;
1005 Py_INCREF(item);
1006 return item;
1007 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001010 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001012}
1013
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001014static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +00001015tupleiter_len(tupleiterobject *it)
1016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 Py_ssize_t len = 0;
1018 if (it->it_seq)
1019 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1020 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001021}
1022
Armin Rigof5b3e362006-02-11 21:32:43 +00001023PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001024
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001025static PyObject *
1026tupleiter_reduce(tupleiterobject *it)
1027{
1028 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001029 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001030 it->it_seq, it->it_index);
1031 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001032 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001033}
1034
1035static PyObject *
1036tupleiter_setstate(tupleiterobject *it, PyObject *state)
1037{
Victor Stinner7660b882013-06-24 23:59:24 +02001038 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001039 if (index == -1 && PyErr_Occurred())
1040 return NULL;
1041 if (it->it_seq != NULL) {
1042 if (index < 0)
1043 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001044 else if (index > PyTuple_GET_SIZE(it->it_seq))
1045 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001046 it->it_index = index;
1047 }
1048 Py_RETURN_NONE;
1049}
1050
1051PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1052PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1053
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001054static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001056 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1057 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001059};
1060
Raymond Hettinger48923c52002-08-09 01:30:17 +00001061PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1063 "tuple_iterator", /* tp_name */
1064 sizeof(tupleiterobject), /* tp_basicsize */
1065 0, /* tp_itemsize */
1066 /* methods */
1067 (destructor)tupleiter_dealloc, /* tp_dealloc */
1068 0, /* tp_print */
1069 0, /* tp_getattr */
1070 0, /* tp_setattr */
1071 0, /* tp_reserved */
1072 0, /* tp_repr */
1073 0, /* tp_as_number */
1074 0, /* tp_as_sequence */
1075 0, /* tp_as_mapping */
1076 0, /* tp_hash */
1077 0, /* tp_call */
1078 0, /* tp_str */
1079 PyObject_GenericGetAttr, /* tp_getattro */
1080 0, /* tp_setattro */
1081 0, /* tp_as_buffer */
1082 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1083 0, /* tp_doc */
1084 (traverseproc)tupleiter_traverse, /* tp_traverse */
1085 0, /* tp_clear */
1086 0, /* tp_richcompare */
1087 0, /* tp_weaklistoffset */
1088 PyObject_SelfIter, /* tp_iter */
1089 (iternextfunc)tupleiter_next, /* tp_iternext */
1090 tupleiter_methods, /* tp_methods */
1091 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001092};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001093
1094static PyObject *
1095tuple_iter(PyObject *seq)
1096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (!PyTuple_Check(seq)) {
1100 PyErr_BadInternalCall();
1101 return NULL;
1102 }
1103 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1104 if (it == NULL)
1105 return NULL;
1106 it->it_index = 0;
1107 Py_INCREF(seq);
1108 it->it_seq = (PyTupleObject *)seq;
1109 _PyObject_GC_TRACK(it);
1110 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111}