blob: 964db3bb8de8b03657d0ddc2ff2bd851d563ebfe [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Tuple object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/pystate.h"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01006#include "accu.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Serhiy Storchaka0b561592017-03-19 08:47:58 +02008/*[clinic input]
9class tuple "PyTupleObject *" "&PyTuple_Type"
10[clinic start generated code]*/
11/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/
12
13#include "clinic/tupleobject.c.h"
14
Guido van Rossum5ce78f82000-04-21 21:15:05 +000015/* Speed optimization to avoid frequent malloc/free of small tuples */
Christian Heimes2202f872008-02-06 14:31:34 +000016#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +000018#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#ifndef PyTuple_MAXFREELIST
Christian Heimes2202f872008-02-06 14:31:34 +000020#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000021#endif
22
Christian Heimes2202f872008-02-06 14:31:34 +000023#if PyTuple_MAXSAVESIZE > 0
24/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025 tuple () of which at most one instance will be allocated.
26*/
Christian Heimes2202f872008-02-06 14:31:34 +000027static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
28static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000029#endif
30#ifdef COUNT_ALLOCS
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000031Py_ssize_t fast_tuple_allocs;
32Py_ssize_t tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000033#endif
34
Antoine Pitrou3a652b12009-03-23 18:52:06 +000035/* Debug statistic to count GC tracking of tuples.
36 Please note that tuples are only untracked when considered by the GC, and
37 many of them will be dead before. Therefore, a tracking rate close to 100%
38 does not necessarily prove that the heuristic is inefficient.
39*/
40#ifdef SHOW_TRACK_COUNT
41static Py_ssize_t count_untracked = 0;
42static Py_ssize_t count_tracked = 0;
43
44static void
45show_track(void)
46{
Victor Stinner25420fe2017-11-20 18:12:22 -080047 PyInterpreterState *interp = PyThreadState_GET()->interp;
48 if (!inter->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030049 return;
Victor Stinner25420fe2017-11-20 18:12:22 -080050 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
53 count_tracked + count_untracked);
54 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
55 "d\n", count_tracked);
56 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
57 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000058}
59#endif
60
David Malcolm49526f42012-06-22 14:55:41 -040061/* Print summary info about the state of the optimized allocator */
62void
63_PyTuple_DebugMallocStats(FILE *out)
64{
65#if PyTuple_MAXSAVESIZE > 0
66 int i;
67 char buf[128];
68 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
69 PyOS_snprintf(buf, sizeof(buf),
70 "free %d-sized PyTupleObject", i);
71 _PyDebugAllocatorStats(out,
72 buf,
73 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
74 }
75#endif
76}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000077
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020079PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020081 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 Py_ssize_t i;
83 if (size < 0) {
84 PyErr_BadInternalCall();
85 return NULL;
86 }
Christian Heimes2202f872008-02-06 14:31:34 +000087#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (size == 0 && free_list[0]) {
89 op = free_list[0];
90 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000091#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 return (PyObject *) op;
95 }
96 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
97 free_list[size] = (PyTupleObject *) op->ob_item[0];
98 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000101#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000103#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 Py_SIZE(op) = size;
105 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 _Py_NewReference((PyObject *)op);
108 }
109 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200113 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100114 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 return PyErr_NoMemory();
116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
118 if (op == NULL)
119 return NULL;
120 }
121 for (i=0; i < size; i++)
122 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000123#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 if (size == 0) {
125 free_list[0] = op;
126 ++numfree[0];
127 Py_INCREF(op); /* extra INCREF so that this is never freed */
128 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000129#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000130#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000132#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 _PyObject_GC_TRACK(op);
134 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Martin v. Löwis18e16552006-02-15 17:27:45 +0000137Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200138PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (!PyTuple_Check(op)) {
141 PyErr_BadInternalCall();
142 return -1;
143 }
144 else
145 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146}
147
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200149PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (!PyTuple_Check(op)) {
152 PyErr_BadInternalCall();
153 return NULL;
154 }
155 if (i < 0 || i >= Py_SIZE(op)) {
156 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
157 return NULL;
158 }
159 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
162int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200163PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200165 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
167 Py_XDECREF(newitem);
168 PyErr_BadInternalCall();
169 return -1;
170 }
171 if (i < 0 || i >= Py_SIZE(op)) {
172 Py_XDECREF(newitem);
173 PyErr_SetString(PyExc_IndexError,
174 "tuple assignment index out of range");
175 return -1;
176 }
177 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300178 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000182void
183_PyTuple_MaybeUntrack(PyObject *op)
184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyTupleObject *t;
186 Py_ssize_t i, n;
187
188 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
189 return;
190 t = (PyTupleObject *) op;
191 n = Py_SIZE(t);
192 for (i = 0; i < n; i++) {
193 PyObject *elt = PyTuple_GET_ITEM(t, i);
194 /* Tuple with NULL elements aren't
195 fully constructed, don't untrack
196 them yet. */
197 if (!elt ||
198 _PyObject_GC_MAY_BE_TRACKED(elt))
199 return;
200 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000201#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 count_tracked--;
203 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000204#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000206}
207
Raymond Hettingercb2da432003-10-12 18:24:34 +0000208PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 Py_ssize_t i;
212 PyObject *o;
213 PyObject *result;
214 PyObject **items;
215 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 va_start(vargs, n);
218 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200219 if (result == NULL) {
220 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200222 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 items = ((PyTupleObject *)result)->ob_item;
224 for (i = 0; i < n; i++) {
225 o = va_arg(vargs, PyObject *);
226 Py_INCREF(o);
227 items[i] = o;
228 }
229 va_end(vargs);
230 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000231}
232
233
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234/* Methods */
235
236static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200237tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200239 Py_ssize_t i;
240 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyObject_GC_UnTrack(op);
242 Py_TRASHCAN_SAFE_BEGIN(op)
243 if (len > 0) {
244 i = len;
245 while (--i >= 0)
246 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000247#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (len < PyTuple_MAXSAVESIZE &&
249 numfree[len] < PyTuple_MAXFREELIST &&
250 Py_TYPE(op) == &PyTuple_Type)
251 {
252 op->ob_item[0] = (PyObject *) free_list[len];
253 numfree[len]++;
254 free_list[len] = op;
255 goto done; /* return */
256 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 }
259 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000260done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000265tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100268 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 n = Py_SIZE(v);
271 if (n == 0)
272 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* While not mutable, it is still possible to end up with a cycle in a
275 tuple through an object that stores itself within a tuple (and thus
276 infinitely asks for the repr of itself). This should only be
277 possible within a type. */
278 i = Py_ReprEnter((PyObject *)v);
279 if (i != 0) {
280 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
281 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000282
Victor Stinner88a9cd92013-11-19 12:59:46 +0100283 _PyUnicodeWriter_Init(&writer);
284 writer.overallocate = 1;
285 if (Py_SIZE(v) > 1) {
286 /* "(" + "1" + ", 2" * (len - 1) + ")" */
287 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
288 }
289 else {
290 /* "(1,)" */
291 writer.min_length = 4;
292 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200293
Victor Stinner88a9cd92013-11-19 12:59:46 +0100294 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200295 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Do repr() on each element. */
298 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100299 PyObject *s;
300
301 if (i > 0) {
302 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
303 goto error;
304 }
305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200307 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 s = PyObject_Repr(v->ob_item[i]);
309 Py_LeaveRecursiveCall();
Victor Stinner88a9cd92013-11-19 12:59:46 +0100310 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200311 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100312
313 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
314 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200315 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100316 }
317 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100319
320 writer.overallocate = 0;
321 if (n > 1) {
322 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
323 goto error;
324 }
325 else {
326 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
327 goto error;
328 }
Tim Petersa7259592001-06-16 05:11:17 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100331 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200332
333error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100334 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200335 Py_ReprLeave((PyObject *)v);
336 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337}
338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339/* The addend 82520, was selected from the range(0, 1000000) for
340 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000341 upto length eight:
342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000344 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100345
346 Tests have shown that it's not worth to cache the hash value, see
347 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000348*/
349
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000350static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000351tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000352{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200353 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
354 Py_hash_t y;
355 Py_ssize_t len = Py_SIZE(v);
356 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800357 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800358 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 p = v->ob_item;
360 while (--len >= 0) {
361 y = PyObject_Hash(*p++);
362 if (y == -1)
363 return -1;
364 x = (x ^ y) * mult;
365 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800366 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800368 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100369 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 x = -2;
371 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000372}
373
Martin v. Löwis18e16552006-02-15 17:27:45 +0000374static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000375tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378}
379
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000380static int
Fred Drakeba096332000-07-09 07:04:36 +0000381tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_ssize_t i;
384 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
387 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
388 Py_EQ);
389 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000390}
391
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200393tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (i < 0 || i >= Py_SIZE(a)) {
396 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
397 return NULL;
398 }
399 Py_INCREF(a->ob_item[i]);
400 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401}
402
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200404tupleslice(PyTupleObject *a, Py_ssize_t ilow,
405 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200407 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200409 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 Py_ssize_t len;
411 if (ilow < 0)
412 ilow = 0;
413 if (ihigh > Py_SIZE(a))
414 ihigh = Py_SIZE(a);
415 if (ihigh < ilow)
416 ihigh = ilow;
417 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
418 Py_INCREF(a);
419 return (PyObject *)a;
420 }
421 len = ihigh - ilow;
422 np = (PyTupleObject *)PyTuple_New(len);
423 if (np == NULL)
424 return NULL;
425 src = a->ob_item + ilow;
426 dest = np->ob_item;
427 for (i = 0; i < len; i++) {
428 PyObject *v = src[i];
429 Py_INCREF(v);
430 dest[i] = v;
431 }
432 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433}
434
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000436PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (op == NULL || !PyTuple_Check(op)) {
439 PyErr_BadInternalCall();
440 return NULL;
441 }
442 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000443}
444
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200446tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200448 Py_ssize_t size;
449 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyObject **src, **dest;
451 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200452 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
453 Py_INCREF(bb);
454 return bb;
455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 if (!PyTuple_Check(bb)) {
457 PyErr_Format(PyExc_TypeError,
458 "can only concatenate tuple (not \"%.200s\") to tuple",
459 Py_TYPE(bb)->tp_name);
460 return NULL;
461 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200463 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
464 Py_INCREF(a);
465 return (PyObject *)a;
466 }
Martin Panterb93d8632016-07-25 02:39:20 +0000467 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000469 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 np = (PyTupleObject *) PyTuple_New(size);
471 if (np == NULL) {
472 return NULL;
473 }
474 src = a->ob_item;
475 dest = np->ob_item;
476 for (i = 0; i < Py_SIZE(a); i++) {
477 PyObject *v = src[i];
478 Py_INCREF(v);
479 dest[i] = v;
480 }
481 src = b->ob_item;
482 dest = np->ob_item + Py_SIZE(a);
483 for (i = 0; i < Py_SIZE(b); i++) {
484 PyObject *v = src[i];
485 Py_INCREF(v);
486 dest[i] = v;
487 }
488 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489#undef b
490}
491
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000493tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 Py_ssize_t i, j;
496 Py_ssize_t size;
497 PyTupleObject *np;
498 PyObject **p, **items;
499 if (n < 0)
500 n = 0;
501 if (Py_SIZE(a) == 0 || n == 1) {
502 if (PyTuple_CheckExact(a)) {
503 /* Since tuples are immutable, we can return a shared
504 copy in this case */
505 Py_INCREF(a);
506 return (PyObject *)a;
507 }
508 if (Py_SIZE(a) == 0)
509 return PyTuple_New(0);
510 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100511 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100513 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 np = (PyTupleObject *) PyTuple_New(size);
515 if (np == NULL)
516 return NULL;
517 p = np->ob_item;
518 items = a->ob_item;
519 for (i = 0; i < n; i++) {
520 for (j = 0; j < Py_SIZE(a); j++) {
521 *p = items[j];
522 Py_INCREF(*p);
523 p++;
524 }
525 }
526 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000527}
528
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200529/*[clinic input]
530tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000531
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200532 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300533 start: slice_index(accept={int}) = 0
534 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200535 /
536
537Return first index of value.
538
539Raises ValueError if the value is not present.
540[clinic start generated code]*/
541
542static PyObject *
543tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
544 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300545/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200546{
547 Py_ssize_t i;
548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (start < 0) {
550 start += Py_SIZE(self);
551 if (start < 0)
552 start = 0;
553 }
554 if (stop < 0) {
555 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200557 else if (stop > Py_SIZE(self)) {
558 stop = Py_SIZE(self);
559 }
560 for (i = start; i < stop; i++) {
561 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if (cmp > 0)
563 return PyLong_FromSsize_t(i);
564 else if (cmp < 0)
565 return NULL;
566 }
567 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
568 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000569}
570
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200571/*[clinic input]
572tuple.count
573
574 value: object
575 /
576
577Return number of occurrences of value.
578[clinic start generated code]*/
579
Raymond Hettinger65baa342008-02-07 00:41:02 +0000580static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200581tuple_count(PyTupleObject *self, PyObject *value)
582/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 Py_ssize_t count = 0;
585 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200588 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (cmp > 0)
590 count++;
591 else if (cmp < 0)
592 return NULL;
593 }
594 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000595}
596
Jeremy Hylton8caad492000-06-23 14:18:11 +0000597static int
598tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 for (i = Py_SIZE(o); --i >= 0; )
603 Py_VISIT(o->ob_item[i]);
604 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000605}
606
Guido van Rossumf77bc622001-01-18 00:00:53 +0000607static PyObject *
608tuplerichcompare(PyObject *v, PyObject *w, int op)
609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 PyTupleObject *vt, *wt;
611 Py_ssize_t i;
612 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000613
Brian Curtindfc80e32011-08-10 20:28:54 -0500614 if (!PyTuple_Check(v) || !PyTuple_Check(w))
615 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 vt = (PyTupleObject *)v;
618 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 vlen = Py_SIZE(vt);
621 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Note: the corresponding code for lists has an "early out" test
624 * here when op is EQ or NE and the lengths differ. That pays there,
625 * but Tim was unable to find any real code where EQ/NE tuple
626 * compares don't have the same length, so testing for it here would
627 * have cost without benefit.
628 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 /* Search for the first index where items are different.
631 * Note that because tuples are immutable, it's safe to reuse
632 * vlen and wlen across the comparison calls.
633 */
634 for (i = 0; i < vlen && i < wlen; i++) {
635 int k = PyObject_RichCompareBool(vt->ob_item[i],
636 wt->ob_item[i], Py_EQ);
637 if (k < 0)
638 return NULL;
639 if (!k)
640 break;
641 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if (i >= vlen || i >= wlen) {
644 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100645 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 /* We have an item that differs -- shortcuts for EQ/NE */
649 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200650 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 }
652 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200653 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 /* Compare the final item again using the proper operator */
657 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000658}
659
Jeremy Hylton938ace62002-07-17 16:30:39 +0000660static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200661tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
662
663/*[clinic input]
664@classmethod
665tuple.__new__ as tuple_new
666 iterable: object(c_default="NULL") = ()
667 /
668
669Built-in immutable sequence.
670
671If no argument is given, the constructor returns an empty tuple.
672If iterable is specified the tuple is initialized from iterable's items.
673
674If the argument is a tuple, the return value is the same object.
675[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000676
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200678tuple_new_impl(PyTypeObject *type, PyObject *iterable)
679/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200682 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200684 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 return PyTuple_New(0);
686 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200687 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000688}
689
Guido van Rossumae960af2001-08-30 03:11:59 +0000690static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200691tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *tmp, *newobj, *item;
694 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200697 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (tmp == NULL)
699 return NULL;
700 assert(PyTuple_Check(tmp));
701 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
702 if (newobj == NULL)
703 return NULL;
704 for (i = 0; i < n; i++) {
705 item = PyTuple_GET_ITEM(tmp, i);
706 Py_INCREF(item);
707 PyTuple_SET_ITEM(newobj, i, item);
708 }
709 Py_DECREF(tmp);
710 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000711}
712
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 (lenfunc)tuplelength, /* sq_length */
715 (binaryfunc)tupleconcat, /* sq_concat */
716 (ssizeargfunc)tuplerepeat, /* sq_repeat */
717 (ssizeargfunc)tupleitem, /* sq_item */
718 0, /* sq_slice */
719 0, /* sq_ass_item */
720 0, /* sq_ass_slice */
721 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722};
723
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000724static PyObject*
725tuplesubscript(PyTupleObject* self, PyObject* item)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (PyIndex_Check(item)) {
728 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
729 if (i == -1 && PyErr_Occurred())
730 return NULL;
731 if (i < 0)
732 i += PyTuple_GET_SIZE(self);
733 return tupleitem(self, i);
734 }
735 else if (PySlice_Check(item)) {
736 Py_ssize_t start, stop, step, slicelength, cur, i;
737 PyObject* result;
738 PyObject* it;
739 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000740
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300741 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 return NULL;
743 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300744 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
745 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (slicelength <= 0) {
748 return PyTuple_New(0);
749 }
750 else if (start == 0 && step == 1 &&
751 slicelength == PyTuple_GET_SIZE(self) &&
752 PyTuple_CheckExact(self)) {
753 Py_INCREF(self);
754 return (PyObject *)self;
755 }
756 else {
757 result = PyTuple_New(slicelength);
758 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 src = self->ob_item;
761 dest = ((PyTupleObject *)result)->ob_item;
762 for (cur = start, i = 0; i < slicelength;
763 cur += step, i++) {
764 it = src[cur];
765 Py_INCREF(it);
766 dest[i] = it;
767 }
768
769 return result;
770 }
771 }
772 else {
773 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400774 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 Py_TYPE(item)->tp_name);
776 return NULL;
777 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000778}
779
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200780/*[clinic input]
781tuple.__getnewargs__
782[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200784static PyObject *
785tuple___getnewargs___impl(PyTupleObject *self)
786/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
787{
788 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000789}
790
791static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200792 TUPLE___GETNEWARGS___METHODDEF
793 TUPLE_INDEX_METHODDEF
794 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000796};
797
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000798static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 (lenfunc)tuplelength,
800 (binaryfunc)tuplesubscript,
801 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000802};
803
Raymond Hettinger48923c52002-08-09 01:30:17 +0000804static PyObject *tuple_iter(PyObject *seq);
805
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyVarObject_HEAD_INIT(&PyType_Type, 0)
808 "tuple",
809 sizeof(PyTupleObject) - sizeof(PyObject *),
810 sizeof(PyObject *),
811 (destructor)tupledealloc, /* tp_dealloc */
812 0, /* tp_print */
813 0, /* tp_getattr */
814 0, /* tp_setattr */
815 0, /* tp_reserved */
816 (reprfunc)tuplerepr, /* tp_repr */
817 0, /* tp_as_number */
818 &tuple_as_sequence, /* tp_as_sequence */
819 &tuple_as_mapping, /* tp_as_mapping */
820 (hashfunc)tuplehash, /* tp_hash */
821 0, /* tp_call */
822 0, /* tp_str */
823 PyObject_GenericGetAttr, /* tp_getattro */
824 0, /* tp_setattro */
825 0, /* tp_as_buffer */
826 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
827 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200828 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 (traverseproc)tupletraverse, /* tp_traverse */
830 0, /* tp_clear */
831 tuplerichcompare, /* tp_richcompare */
832 0, /* tp_weaklistoffset */
833 tuple_iter, /* tp_iter */
834 0, /* tp_iternext */
835 tuple_methods, /* tp_methods */
836 0, /* tp_members */
837 0, /* tp_getset */
838 0, /* tp_base */
839 0, /* tp_dict */
840 0, /* tp_descr_get */
841 0, /* tp_descr_set */
842 0, /* tp_dictoffset */
843 0, /* tp_init */
844 0, /* tp_alloc */
845 tuple_new, /* tp_new */
846 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848
849/* The following function breaks the notion that tuples are immutable:
850 it changes the size of a tuple. We get away with this only if there
851 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000852 as creating a new tuple object and destroying the old one, only more
853 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000854 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000855
856int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000857_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000858{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200859 PyTupleObject *v;
860 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 Py_ssize_t i;
862 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 v = (PyTupleObject *) *pv;
865 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
866 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
867 *pv = 0;
868 Py_XDECREF(v);
869 PyErr_BadInternalCall();
870 return -1;
871 }
872 oldsize = Py_SIZE(v);
873 if (oldsize == newsize)
874 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (oldsize == 0) {
877 /* Empty tuples are often shared, so we should never
878 resize them in-place even if we do own the only
879 (current) reference */
880 Py_DECREF(v);
881 *pv = PyTuple_New(newsize);
882 return *pv == NULL ? -1 : 0;
883 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 /* XXX UNREF/NEWREF interface should be more symmetrical */
886 _Py_DEC_REFTOTAL;
887 if (_PyObject_GC_IS_TRACKED(v))
888 _PyObject_GC_UNTRACK(v);
889 _Py_ForgetReference((PyObject *) v);
890 /* DECREF items deleted by shrinkage */
891 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200892 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 }
894 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
895 if (sv == NULL) {
896 *pv = NULL;
897 PyObject_GC_Del(v);
898 return -1;
899 }
900 _Py_NewReference((PyObject *) sv);
901 /* Zero out items added by growing */
902 if (newsize > oldsize)
903 memset(&sv->ob_item[oldsize], 0,
904 sizeof(*sv->ob_item) * (newsize - oldsize));
905 *pv = (PyObject *) sv;
906 _PyObject_GC_TRACK(sv);
907 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000908}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000909
Christian Heimesa156e092008-02-16 07:38:31 +0000910int
911PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000914#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 int i;
916 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
917 PyTupleObject *p, *q;
918 p = free_list[i];
919 freelist_size += numfree[i];
920 free_list[i] = NULL;
921 numfree[i] = 0;
922 while (p) {
923 q = p;
924 p = (PyTupleObject *)(p->ob_item[0]);
925 PyObject_GC_Del(q);
926 }
927 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000928#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000930}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931
Christian Heimesa156e092008-02-16 07:38:31 +0000932void
933PyTuple_Fini(void)
934{
935#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 /* empty tuples are used all over the place and applications may
937 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200938 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000941#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000942#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000944#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000945}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000946
947/*********************** Tuple Iterator **************************/
948
949typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200951 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000953} tupleiterobject;
954
Raymond Hettinger48923c52002-08-09 01:30:17 +0000955static void
956tupleiter_dealloc(tupleiterobject *it)
957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 _PyObject_GC_UNTRACK(it);
959 Py_XDECREF(it->it_seq);
960 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000961}
962
963static int
964tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_VISIT(it->it_seq);
967 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000968}
969
Raymond Hettinger48923c52002-08-09 01:30:17 +0000970static PyObject *
971tupleiter_next(tupleiterobject *it)
972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 PyTupleObject *seq;
974 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 assert(it != NULL);
977 seq = it->it_seq;
978 if (seq == NULL)
979 return NULL;
980 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (it->it_index < PyTuple_GET_SIZE(seq)) {
983 item = PyTuple_GET_ITEM(seq, it->it_index);
984 ++it->it_index;
985 Py_INCREF(item);
986 return item;
987 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300990 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000992}
993
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000994static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000995tupleiter_len(tupleiterobject *it)
996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Py_ssize_t len = 0;
998 if (it->it_seq)
999 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1000 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001001}
1002
Armin Rigof5b3e362006-02-11 21:32:43 +00001003PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001004
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001005static PyObject *
1006tupleiter_reduce(tupleiterobject *it)
1007{
1008 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001009 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001010 it->it_seq, it->it_index);
1011 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001012 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001013}
1014
1015static PyObject *
1016tupleiter_setstate(tupleiterobject *it, PyObject *state)
1017{
Victor Stinner7660b882013-06-24 23:59:24 +02001018 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001019 if (index == -1 && PyErr_Occurred())
1020 return NULL;
1021 if (it->it_seq != NULL) {
1022 if (index < 0)
1023 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001024 else if (index > PyTuple_GET_SIZE(it->it_seq))
1025 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001026 it->it_index = index;
1027 }
1028 Py_RETURN_NONE;
1029}
1030
1031PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1032PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1033
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001034static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001036 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1037 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001039};
1040
Raymond Hettinger48923c52002-08-09 01:30:17 +00001041PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1043 "tuple_iterator", /* tp_name */
1044 sizeof(tupleiterobject), /* tp_basicsize */
1045 0, /* tp_itemsize */
1046 /* methods */
1047 (destructor)tupleiter_dealloc, /* tp_dealloc */
1048 0, /* tp_print */
1049 0, /* tp_getattr */
1050 0, /* tp_setattr */
1051 0, /* tp_reserved */
1052 0, /* tp_repr */
1053 0, /* tp_as_number */
1054 0, /* tp_as_sequence */
1055 0, /* tp_as_mapping */
1056 0, /* tp_hash */
1057 0, /* tp_call */
1058 0, /* tp_str */
1059 PyObject_GenericGetAttr, /* tp_getattro */
1060 0, /* tp_setattro */
1061 0, /* tp_as_buffer */
1062 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1063 0, /* tp_doc */
1064 (traverseproc)tupleiter_traverse, /* tp_traverse */
1065 0, /* tp_clear */
1066 0, /* tp_richcompare */
1067 0, /* tp_weaklistoffset */
1068 PyObject_SelfIter, /* tp_iter */
1069 (iternextfunc)tupleiter_next, /* tp_iternext */
1070 tupleiter_methods, /* tp_methods */
1071 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001072};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001073
1074static PyObject *
1075tuple_iter(PyObject *seq)
1076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (!PyTuple_Check(seq)) {
1080 PyErr_BadInternalCall();
1081 return NULL;
1082 }
1083 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1084 if (it == NULL)
1085 return NULL;
1086 it->it_index = 0;
1087 Py_INCREF(seq);
1088 it->it_seq = (PyTupleObject *)seq;
1089 _PyObject_GC_TRACK(it);
1090 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001091}