blob: 35decd8bb802f6721f39c4acfc03c54560a75769 [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 */
stratakise8b19652017-11-02 11:32:54 +0100650 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* We have an item that differs -- shortcuts for EQ/NE */
654 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200655 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 }
657 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200658 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 /* Compare the final item again using the proper operator */
662 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000663}
664
Jeremy Hylton938ace62002-07-17 16:30:39 +0000665static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200666tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
667
668/*[clinic input]
669@classmethod
670tuple.__new__ as tuple_new
671 iterable: object(c_default="NULL") = ()
672 /
673
674Built-in immutable sequence.
675
676If no argument is given, the constructor returns an empty tuple.
677If iterable is specified the tuple is initialized from iterable's items.
678
679If the argument is a tuple, the return value is the same object.
680[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000681
Tim Peters6d6c1a32001-08-02 04:15:00 +0000682static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200683tuple_new_impl(PyTypeObject *type, PyObject *iterable)
684/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200687 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000688
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200689 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 return PyTuple_New(0);
691 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200692 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000693}
694
Guido van Rossumae960af2001-08-30 03:11:59 +0000695static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200696tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyObject *tmp, *newobj, *item;
699 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200702 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 if (tmp == NULL)
704 return NULL;
705 assert(PyTuple_Check(tmp));
706 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
707 if (newobj == NULL)
708 return NULL;
709 for (i = 0; i < n; i++) {
710 item = PyTuple_GET_ITEM(tmp, i);
711 Py_INCREF(item);
712 PyTuple_SET_ITEM(newobj, i, item);
713 }
714 Py_DECREF(tmp);
715 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000716}
717
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 (lenfunc)tuplelength, /* sq_length */
720 (binaryfunc)tupleconcat, /* sq_concat */
721 (ssizeargfunc)tuplerepeat, /* sq_repeat */
722 (ssizeargfunc)tupleitem, /* sq_item */
723 0, /* sq_slice */
724 0, /* sq_ass_item */
725 0, /* sq_ass_slice */
726 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727};
728
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000729static PyObject*
730tuplesubscript(PyTupleObject* self, PyObject* item)
731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (PyIndex_Check(item)) {
733 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
734 if (i == -1 && PyErr_Occurred())
735 return NULL;
736 if (i < 0)
737 i += PyTuple_GET_SIZE(self);
738 return tupleitem(self, i);
739 }
740 else if (PySlice_Check(item)) {
741 Py_ssize_t start, stop, step, slicelength, cur, i;
742 PyObject* result;
743 PyObject* it;
744 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000745
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300746 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return NULL;
748 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300749 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
750 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (slicelength <= 0) {
753 return PyTuple_New(0);
754 }
755 else if (start == 0 && step == 1 &&
756 slicelength == PyTuple_GET_SIZE(self) &&
757 PyTuple_CheckExact(self)) {
758 Py_INCREF(self);
759 return (PyObject *)self;
760 }
761 else {
762 result = PyTuple_New(slicelength);
763 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 src = self->ob_item;
766 dest = ((PyTupleObject *)result)->ob_item;
767 for (cur = start, i = 0; i < slicelength;
768 cur += step, i++) {
769 it = src[cur];
770 Py_INCREF(it);
771 dest[i] = it;
772 }
773
774 return result;
775 }
776 }
777 else {
778 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400779 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 Py_TYPE(item)->tp_name);
781 return NULL;
782 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000783}
784
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200785/*[clinic input]
786tuple.__getnewargs__
787[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200789static PyObject *
790tuple___getnewargs___impl(PyTupleObject *self)
791/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
792{
793 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000794}
795
796static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200797 TUPLE___GETNEWARGS___METHODDEF
798 TUPLE_INDEX_METHODDEF
799 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000801};
802
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000803static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 (lenfunc)tuplelength,
805 (binaryfunc)tuplesubscript,
806 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000807};
808
Raymond Hettinger48923c52002-08-09 01:30:17 +0000809static PyObject *tuple_iter(PyObject *seq);
810
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000811PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 PyVarObject_HEAD_INIT(&PyType_Type, 0)
813 "tuple",
814 sizeof(PyTupleObject) - sizeof(PyObject *),
815 sizeof(PyObject *),
816 (destructor)tupledealloc, /* tp_dealloc */
817 0, /* tp_print */
818 0, /* tp_getattr */
819 0, /* tp_setattr */
820 0, /* tp_reserved */
821 (reprfunc)tuplerepr, /* tp_repr */
822 0, /* tp_as_number */
823 &tuple_as_sequence, /* tp_as_sequence */
824 &tuple_as_mapping, /* tp_as_mapping */
825 (hashfunc)tuplehash, /* tp_hash */
826 0, /* tp_call */
827 0, /* tp_str */
828 PyObject_GenericGetAttr, /* tp_getattro */
829 0, /* tp_setattro */
830 0, /* tp_as_buffer */
831 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
832 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200833 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 (traverseproc)tupletraverse, /* tp_traverse */
835 0, /* tp_clear */
836 tuplerichcompare, /* tp_richcompare */
837 0, /* tp_weaklistoffset */
838 tuple_iter, /* tp_iter */
839 0, /* tp_iternext */
840 tuple_methods, /* tp_methods */
841 0, /* tp_members */
842 0, /* tp_getset */
843 0, /* tp_base */
844 0, /* tp_dict */
845 0, /* tp_descr_get */
846 0, /* tp_descr_set */
847 0, /* tp_dictoffset */
848 0, /* tp_init */
849 0, /* tp_alloc */
850 tuple_new, /* tp_new */
851 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000852};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000853
854/* The following function breaks the notion that tuples are immutable:
855 it changes the size of a tuple. We get away with this only if there
856 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000857 as creating a new tuple object and destroying the old one, only more
858 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000859 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000860
861int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000862_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000863{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200864 PyTupleObject *v;
865 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 Py_ssize_t i;
867 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 v = (PyTupleObject *) *pv;
870 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
871 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
872 *pv = 0;
873 Py_XDECREF(v);
874 PyErr_BadInternalCall();
875 return -1;
876 }
877 oldsize = Py_SIZE(v);
878 if (oldsize == newsize)
879 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (oldsize == 0) {
882 /* Empty tuples are often shared, so we should never
883 resize them in-place even if we do own the only
884 (current) reference */
885 Py_DECREF(v);
886 *pv = PyTuple_New(newsize);
887 return *pv == NULL ? -1 : 0;
888 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 /* XXX UNREF/NEWREF interface should be more symmetrical */
891 _Py_DEC_REFTOTAL;
892 if (_PyObject_GC_IS_TRACKED(v))
893 _PyObject_GC_UNTRACK(v);
894 _Py_ForgetReference((PyObject *) v);
895 /* DECREF items deleted by shrinkage */
896 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200897 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 }
899 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
900 if (sv == NULL) {
901 *pv = NULL;
902 PyObject_GC_Del(v);
903 return -1;
904 }
905 _Py_NewReference((PyObject *) sv);
906 /* Zero out items added by growing */
907 if (newsize > oldsize)
908 memset(&sv->ob_item[oldsize], 0,
909 sizeof(*sv->ob_item) * (newsize - oldsize));
910 *pv = (PyObject *) sv;
911 _PyObject_GC_TRACK(sv);
912 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000914
Christian Heimesa156e092008-02-16 07:38:31 +0000915int
916PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000919#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 int i;
921 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
922 PyTupleObject *p, *q;
923 p = free_list[i];
924 freelist_size += numfree[i];
925 free_list[i] = NULL;
926 numfree[i] = 0;
927 while (p) {
928 q = p;
929 p = (PyTupleObject *)(p->ob_item[0]);
930 PyObject_GC_Del(q);
931 }
932 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000933#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000935}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936
Christian Heimesa156e092008-02-16 07:38:31 +0000937void
938PyTuple_Fini(void)
939{
940#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 /* empty tuples are used all over the place and applications may
942 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200943 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000946#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000947#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000949#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000950}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000951
952/*********************** Tuple Iterator **************************/
953
954typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200956 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000958} tupleiterobject;
959
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960static void
961tupleiter_dealloc(tupleiterobject *it)
962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 _PyObject_GC_UNTRACK(it);
964 Py_XDECREF(it->it_seq);
965 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000966}
967
968static int
969tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 Py_VISIT(it->it_seq);
972 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973}
974
Raymond Hettinger48923c52002-08-09 01:30:17 +0000975static PyObject *
976tupleiter_next(tupleiterobject *it)
977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 PyTupleObject *seq;
979 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 assert(it != NULL);
982 seq = it->it_seq;
983 if (seq == NULL)
984 return NULL;
985 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 if (it->it_index < PyTuple_GET_SIZE(seq)) {
988 item = PyTuple_GET_ITEM(seq, it->it_index);
989 ++it->it_index;
990 Py_INCREF(item);
991 return item;
992 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300995 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000997}
998
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000999static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +00001000tupleiter_len(tupleiterobject *it)
1001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Py_ssize_t len = 0;
1003 if (it->it_seq)
1004 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1005 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001006}
1007
Armin Rigof5b3e362006-02-11 21:32:43 +00001008PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001009
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001010static PyObject *
1011tupleiter_reduce(tupleiterobject *it)
1012{
1013 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001014 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001015 it->it_seq, it->it_index);
1016 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001017 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001018}
1019
1020static PyObject *
1021tupleiter_setstate(tupleiterobject *it, PyObject *state)
1022{
Victor Stinner7660b882013-06-24 23:59:24 +02001023 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001024 if (index == -1 && PyErr_Occurred())
1025 return NULL;
1026 if (it->it_seq != NULL) {
1027 if (index < 0)
1028 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001029 else if (index > PyTuple_GET_SIZE(it->it_seq))
1030 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001031 it->it_index = index;
1032 }
1033 Py_RETURN_NONE;
1034}
1035
1036PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1037PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1038
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001039static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001041 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1042 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001044};
1045
Raymond Hettinger48923c52002-08-09 01:30:17 +00001046PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1048 "tuple_iterator", /* tp_name */
1049 sizeof(tupleiterobject), /* tp_basicsize */
1050 0, /* tp_itemsize */
1051 /* methods */
1052 (destructor)tupleiter_dealloc, /* tp_dealloc */
1053 0, /* tp_print */
1054 0, /* tp_getattr */
1055 0, /* tp_setattr */
1056 0, /* tp_reserved */
1057 0, /* tp_repr */
1058 0, /* tp_as_number */
1059 0, /* tp_as_sequence */
1060 0, /* tp_as_mapping */
1061 0, /* tp_hash */
1062 0, /* tp_call */
1063 0, /* tp_str */
1064 PyObject_GenericGetAttr, /* tp_getattro */
1065 0, /* tp_setattro */
1066 0, /* tp_as_buffer */
1067 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1068 0, /* tp_doc */
1069 (traverseproc)tupleiter_traverse, /* tp_traverse */
1070 0, /* tp_clear */
1071 0, /* tp_richcompare */
1072 0, /* tp_weaklistoffset */
1073 PyObject_SelfIter, /* tp_iter */
1074 (iternextfunc)tupleiter_next, /* tp_iternext */
1075 tupleiter_methods, /* tp_methods */
1076 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001077};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001078
1079static PyObject *
1080tuple_iter(PyObject *seq)
1081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 if (!PyTuple_Check(seq)) {
1085 PyErr_BadInternalCall();
1086 return NULL;
1087 }
1088 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1089 if (it == NULL)
1090 return NULL;
1091 it->it_index = 0;
1092 Py_INCREF(seq);
1093 it->it_seq = (PyTupleObject *)seq;
1094 _PyObject_GC_TRACK(it);
1095 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001096}