blob: 9bb91a5e65a0a0da0e5c6c3bf6530e84a09dd630 [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;
Eddie Elizondo745dc652018-02-21 20:55:18 -080048 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030049 return;
Victor Stinner25420fe2017-11-20 18:12:22 -080050 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
53 count_tracked + count_untracked);
54 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
55 "d\n", count_tracked);
56 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
57 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000058}
59#endif
60
David Malcolm49526f42012-06-22 14:55:41 -040061/* Print summary info about the state of the optimized allocator */
62void
63_PyTuple_DebugMallocStats(FILE *out)
64{
65#if PyTuple_MAXSAVESIZE > 0
66 int i;
67 char buf[128];
68 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
69 PyOS_snprintf(buf, sizeof(buf),
70 "free %d-sized PyTupleObject", i);
71 _PyDebugAllocatorStats(out,
72 buf,
73 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
74 }
75#endif
76}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000077
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020079PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020081 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 Py_ssize_t i;
83 if (size < 0) {
84 PyErr_BadInternalCall();
85 return NULL;
86 }
Christian Heimes2202f872008-02-06 14:31:34 +000087#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (size == 0 && free_list[0]) {
89 op = free_list[0];
90 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000091#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 return (PyObject *) op;
95 }
96 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
97 free_list[size] = (PyTupleObject *) op->ob_item[0];
98 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000101#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000103#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 Py_SIZE(op) = size;
105 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 _Py_NewReference((PyObject *)op);
108 }
109 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200113 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100114 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 return PyErr_NoMemory();
116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
118 if (op == NULL)
119 return NULL;
120 }
121 for (i=0; i < size; i++)
122 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000123#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 if (size == 0) {
125 free_list[0] = op;
126 ++numfree[0];
127 Py_INCREF(op); /* extra INCREF so that this is never freed */
128 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000129#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000130#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000132#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 _PyObject_GC_TRACK(op);
134 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Martin v. Löwis18e16552006-02-15 17:27:45 +0000137Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200138PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (!PyTuple_Check(op)) {
141 PyErr_BadInternalCall();
142 return -1;
143 }
144 else
145 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146}
147
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200149PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (!PyTuple_Check(op)) {
152 PyErr_BadInternalCall();
153 return NULL;
154 }
155 if (i < 0 || i >= Py_SIZE(op)) {
156 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
157 return NULL;
158 }
159 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
162int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200163PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200165 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
167 Py_XDECREF(newitem);
168 PyErr_BadInternalCall();
169 return -1;
170 }
171 if (i < 0 || i >= Py_SIZE(op)) {
172 Py_XDECREF(newitem);
173 PyErr_SetString(PyExc_IndexError,
174 "tuple assignment index out of range");
175 return -1;
176 }
177 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300178 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000182void
183_PyTuple_MaybeUntrack(PyObject *op)
184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyTupleObject *t;
186 Py_ssize_t i, n;
187
188 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
189 return;
190 t = (PyTupleObject *) op;
191 n = Py_SIZE(t);
192 for (i = 0; i < n; i++) {
193 PyObject *elt = PyTuple_GET_ITEM(t, i);
194 /* Tuple with NULL elements aren't
195 fully constructed, don't untrack
196 them yet. */
197 if (!elt ||
198 _PyObject_GC_MAY_BE_TRACKED(elt))
199 return;
200 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000201#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 count_tracked--;
203 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000204#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000206}
207
Raymond Hettingercb2da432003-10-12 18:24:34 +0000208PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 Py_ssize_t i;
212 PyObject *o;
213 PyObject *result;
214 PyObject **items;
215 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 va_start(vargs, n);
218 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200219 if (result == NULL) {
220 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200222 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 items = ((PyTupleObject *)result)->ob_item;
224 for (i = 0; i < n; i++) {
225 o = va_arg(vargs, PyObject *);
226 Py_INCREF(o);
227 items[i] = o;
228 }
229 va_end(vargs);
230 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000231}
232
233
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234/* Methods */
235
236static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200237tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200239 Py_ssize_t i;
240 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyObject_GC_UnTrack(op);
242 Py_TRASHCAN_SAFE_BEGIN(op)
243 if (len > 0) {
244 i = len;
245 while (--i >= 0)
246 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000247#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (len < PyTuple_MAXSAVESIZE &&
249 numfree[len] < PyTuple_MAXFREELIST &&
250 Py_TYPE(op) == &PyTuple_Type)
251 {
252 op->ob_item[0] = (PyObject *) free_list[len];
253 numfree[len]++;
254 free_list[len] = op;
255 goto done; /* return */
256 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 }
259 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000260done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000265tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100268 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 n = Py_SIZE(v);
271 if (n == 0)
272 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* While not mutable, it is still possible to end up with a cycle in a
275 tuple through an object that stores itself within a tuple (and thus
276 infinitely asks for the repr of itself). This should only be
277 possible within a type. */
278 i = Py_ReprEnter((PyObject *)v);
279 if (i != 0) {
280 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
281 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000282
Victor Stinner88a9cd92013-11-19 12:59:46 +0100283 _PyUnicodeWriter_Init(&writer);
284 writer.overallocate = 1;
285 if (Py_SIZE(v) > 1) {
286 /* "(" + "1" + ", 2" * (len - 1) + ")" */
287 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
288 }
289 else {
290 /* "(1,)" */
291 writer.min_length = 4;
292 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200293
Victor Stinner88a9cd92013-11-19 12:59:46 +0100294 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200295 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Do repr() on each element. */
298 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100299 PyObject *s;
300
301 if (i > 0) {
302 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
303 goto error;
304 }
305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100307 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200308 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100309
310 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
311 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200312 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100313 }
314 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100316
317 writer.overallocate = 0;
318 if (n > 1) {
319 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
320 goto error;
321 }
322 else {
323 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
324 goto error;
325 }
Tim Petersa7259592001-06-16 05:11:17 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100328 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200329
330error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100331 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200332 Py_ReprLeave((PyObject *)v);
333 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334}
335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336/* The addend 82520, was selected from the range(0, 1000000) for
337 generating the greatest number of prime multipliers for tuples
Leo Ariasc3d95082018-02-03 18:36:10 -0600338 up to length eight:
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000341 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100342
343 Tests have shown that it's not worth to cache the hash value, see
344 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000345*/
346
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000347static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000348tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000349{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200350 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
351 Py_hash_t y;
352 Py_ssize_t len = Py_SIZE(v);
353 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800354 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800355 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 p = v->ob_item;
357 while (--len >= 0) {
358 y = PyObject_Hash(*p++);
359 if (y == -1)
360 return -1;
361 x = (x ^ y) * mult;
362 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800363 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800365 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100366 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 x = -2;
368 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000369}
370
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000372tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
376
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000377static int
Fred Drakeba096332000-07-09 07:04:36 +0000378tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Py_ssize_t i;
381 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
384 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
385 Py_EQ);
386 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000387}
388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200390tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (i < 0 || i >= Py_SIZE(a)) {
393 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
394 return NULL;
395 }
396 Py_INCREF(a->ob_item[i]);
397 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200401tupleslice(PyTupleObject *a, Py_ssize_t ilow,
402 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200404 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200406 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 Py_ssize_t len;
408 if (ilow < 0)
409 ilow = 0;
410 if (ihigh > Py_SIZE(a))
411 ihigh = Py_SIZE(a);
412 if (ihigh < ilow)
413 ihigh = ilow;
414 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
415 Py_INCREF(a);
416 return (PyObject *)a;
417 }
418 len = ihigh - ilow;
419 np = (PyTupleObject *)PyTuple_New(len);
420 if (np == NULL)
421 return NULL;
422 src = a->ob_item + ilow;
423 dest = np->ob_item;
424 for (i = 0; i < len; i++) {
425 PyObject *v = src[i];
426 Py_INCREF(v);
427 dest[i] = v;
428 }
429 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430}
431
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000433PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (op == NULL || !PyTuple_Check(op)) {
436 PyErr_BadInternalCall();
437 return NULL;
438 }
439 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000440}
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200443tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200445 Py_ssize_t size;
446 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 PyObject **src, **dest;
448 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200449 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
450 Py_INCREF(bb);
451 return bb;
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (!PyTuple_Check(bb)) {
454 PyErr_Format(PyExc_TypeError,
455 "can only concatenate tuple (not \"%.200s\") to tuple",
456 Py_TYPE(bb)->tp_name);
457 return NULL;
458 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459#define b ((PyTupleObject *)bb)
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200460 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
461 Py_INCREF(a);
462 return (PyObject *)a;
463 }
Martin Panterb93d8632016-07-25 02:39:20 +0000464 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000466 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 np = (PyTupleObject *) PyTuple_New(size);
468 if (np == NULL) {
469 return NULL;
470 }
471 src = a->ob_item;
472 dest = np->ob_item;
473 for (i = 0; i < Py_SIZE(a); i++) {
474 PyObject *v = src[i];
475 Py_INCREF(v);
476 dest[i] = v;
477 }
478 src = b->ob_item;
479 dest = np->ob_item + Py_SIZE(a);
480 for (i = 0; i < Py_SIZE(b); i++) {
481 PyObject *v = src[i];
482 Py_INCREF(v);
483 dest[i] = v;
484 }
485 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486#undef b
487}
488
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000490tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_ssize_t i, j;
493 Py_ssize_t size;
494 PyTupleObject *np;
495 PyObject **p, **items;
496 if (n < 0)
497 n = 0;
498 if (Py_SIZE(a) == 0 || n == 1) {
499 if (PyTuple_CheckExact(a)) {
500 /* Since tuples are immutable, we can return a shared
501 copy in this case */
502 Py_INCREF(a);
503 return (PyObject *)a;
504 }
505 if (Py_SIZE(a) == 0)
506 return PyTuple_New(0);
507 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100508 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100510 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 np = (PyTupleObject *) PyTuple_New(size);
512 if (np == NULL)
513 return NULL;
514 p = np->ob_item;
515 items = a->ob_item;
516 for (i = 0; i < n; i++) {
517 for (j = 0; j < Py_SIZE(a); j++) {
518 *p = items[j];
519 Py_INCREF(*p);
520 p++;
521 }
522 }
523 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000524}
525
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200526/*[clinic input]
527tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000528
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200529 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300530 start: slice_index(accept={int}) = 0
531 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200532 /
533
534Return first index of value.
535
536Raises ValueError if the value is not present.
537[clinic start generated code]*/
538
539static PyObject *
540tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
541 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300542/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200543{
544 Py_ssize_t i;
545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (start < 0) {
547 start += Py_SIZE(self);
548 if (start < 0)
549 start = 0;
550 }
551 if (stop < 0) {
552 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200554 else if (stop > Py_SIZE(self)) {
555 stop = Py_SIZE(self);
556 }
557 for (i = start; i < stop; i++) {
558 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (cmp > 0)
560 return PyLong_FromSsize_t(i);
561 else if (cmp < 0)
562 return NULL;
563 }
564 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
565 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000566}
567
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200568/*[clinic input]
569tuple.count
570
571 value: object
572 /
573
574Return number of occurrences of value.
575[clinic start generated code]*/
576
Raymond Hettinger65baa342008-02-07 00:41:02 +0000577static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200578tuple_count(PyTupleObject *self, PyObject *value)
579/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 Py_ssize_t count = 0;
582 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200585 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (cmp > 0)
587 count++;
588 else if (cmp < 0)
589 return NULL;
590 }
591 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000592}
593
Jeremy Hylton8caad492000-06-23 14:18:11 +0000594static int
595tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 for (i = Py_SIZE(o); --i >= 0; )
600 Py_VISIT(o->ob_item[i]);
601 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000602}
603
Guido van Rossumf77bc622001-01-18 00:00:53 +0000604static PyObject *
605tuplerichcompare(PyObject *v, PyObject *w, int op)
606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 PyTupleObject *vt, *wt;
608 Py_ssize_t i;
609 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000610
Brian Curtindfc80e32011-08-10 20:28:54 -0500611 if (!PyTuple_Check(v) || !PyTuple_Check(w))
612 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 vt = (PyTupleObject *)v;
615 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 vlen = Py_SIZE(vt);
618 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 /* Note: the corresponding code for lists has an "early out" test
621 * here when op is EQ or NE and the lengths differ. That pays there,
622 * but Tim was unable to find any real code where EQ/NE tuple
623 * compares don't have the same length, so testing for it here would
624 * have cost without benefit.
625 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Search for the first index where items are different.
628 * Note that because tuples are immutable, it's safe to reuse
629 * vlen and wlen across the comparison calls.
630 */
631 for (i = 0; i < vlen && i < wlen; i++) {
632 int k = PyObject_RichCompareBool(vt->ob_item[i],
633 wt->ob_item[i], Py_EQ);
634 if (k < 0)
635 return NULL;
636 if (!k)
637 break;
638 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (i >= vlen || i >= wlen) {
641 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100642 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 /* We have an item that differs -- shortcuts for EQ/NE */
646 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200647 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 }
649 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200650 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* Compare the final item again using the proper operator */
654 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000655}
656
Jeremy Hylton938ace62002-07-17 16:30:39 +0000657static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200658tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
659
660/*[clinic input]
661@classmethod
662tuple.__new__ as tuple_new
663 iterable: object(c_default="NULL") = ()
664 /
665
666Built-in immutable sequence.
667
668If no argument is given, the constructor returns an empty tuple.
669If iterable is specified the tuple is initialized from iterable's items.
670
671If the argument is a tuple, the return value is the same object.
672[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000673
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200675tuple_new_impl(PyTypeObject *type, PyObject *iterable)
676/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200679 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200681 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return PyTuple_New(0);
683 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200684 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685}
686
Guido van Rossumae960af2001-08-30 03:11:59 +0000687static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200688tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyObject *tmp, *newobj, *item;
691 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200694 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 if (tmp == NULL)
696 return NULL;
697 assert(PyTuple_Check(tmp));
698 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
699 if (newobj == NULL)
700 return NULL;
701 for (i = 0; i < n; i++) {
702 item = PyTuple_GET_ITEM(tmp, i);
703 Py_INCREF(item);
704 PyTuple_SET_ITEM(newobj, i, item);
705 }
706 Py_DECREF(tmp);
707 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000708}
709
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 (lenfunc)tuplelength, /* sq_length */
712 (binaryfunc)tupleconcat, /* sq_concat */
713 (ssizeargfunc)tuplerepeat, /* sq_repeat */
714 (ssizeargfunc)tupleitem, /* sq_item */
715 0, /* sq_slice */
716 0, /* sq_ass_item */
717 0, /* sq_ass_slice */
718 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719};
720
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000721static PyObject*
722tuplesubscript(PyTupleObject* self, PyObject* item)
723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 if (PyIndex_Check(item)) {
725 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
726 if (i == -1 && PyErr_Occurred())
727 return NULL;
728 if (i < 0)
729 i += PyTuple_GET_SIZE(self);
730 return tupleitem(self, i);
731 }
732 else if (PySlice_Check(item)) {
733 Py_ssize_t start, stop, step, slicelength, cur, i;
734 PyObject* result;
735 PyObject* it;
736 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000737
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300738 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 return NULL;
740 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300741 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
742 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 if (slicelength <= 0) {
745 return PyTuple_New(0);
746 }
747 else if (start == 0 && step == 1 &&
748 slicelength == PyTuple_GET_SIZE(self) &&
749 PyTuple_CheckExact(self)) {
750 Py_INCREF(self);
751 return (PyObject *)self;
752 }
753 else {
754 result = PyTuple_New(slicelength);
755 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 src = self->ob_item;
758 dest = ((PyTupleObject *)result)->ob_item;
759 for (cur = start, i = 0; i < slicelength;
760 cur += step, i++) {
761 it = src[cur];
762 Py_INCREF(it);
763 dest[i] = it;
764 }
765
766 return result;
767 }
768 }
769 else {
770 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400771 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 Py_TYPE(item)->tp_name);
773 return NULL;
774 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000775}
776
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200777/*[clinic input]
778tuple.__getnewargs__
779[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200781static PyObject *
782tuple___getnewargs___impl(PyTupleObject *self)
783/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
784{
785 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000786}
787
788static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200789 TUPLE___GETNEWARGS___METHODDEF
790 TUPLE_INDEX_METHODDEF
791 TUPLE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000793};
794
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000795static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 (lenfunc)tuplelength,
797 (binaryfunc)tuplesubscript,
798 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000799};
800
Raymond Hettinger48923c52002-08-09 01:30:17 +0000801static PyObject *tuple_iter(PyObject *seq);
802
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyVarObject_HEAD_INIT(&PyType_Type, 0)
805 "tuple",
806 sizeof(PyTupleObject) - sizeof(PyObject *),
807 sizeof(PyObject *),
808 (destructor)tupledealloc, /* tp_dealloc */
809 0, /* tp_print */
810 0, /* tp_getattr */
811 0, /* tp_setattr */
812 0, /* tp_reserved */
813 (reprfunc)tuplerepr, /* tp_repr */
814 0, /* tp_as_number */
815 &tuple_as_sequence, /* tp_as_sequence */
816 &tuple_as_mapping, /* tp_as_mapping */
817 (hashfunc)tuplehash, /* tp_hash */
818 0, /* tp_call */
819 0, /* tp_str */
820 PyObject_GenericGetAttr, /* tp_getattro */
821 0, /* tp_setattro */
822 0, /* tp_as_buffer */
823 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
824 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200825 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 (traverseproc)tupletraverse, /* tp_traverse */
827 0, /* tp_clear */
828 tuplerichcompare, /* tp_richcompare */
829 0, /* tp_weaklistoffset */
830 tuple_iter, /* tp_iter */
831 0, /* tp_iternext */
832 tuple_methods, /* tp_methods */
833 0, /* tp_members */
834 0, /* tp_getset */
835 0, /* tp_base */
836 0, /* tp_dict */
837 0, /* tp_descr_get */
838 0, /* tp_descr_set */
839 0, /* tp_dictoffset */
840 0, /* tp_init */
841 0, /* tp_alloc */
842 tuple_new, /* tp_new */
843 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000845
846/* The following function breaks the notion that tuples are immutable:
847 it changes the size of a tuple. We get away with this only if there
848 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000849 as creating a new tuple object and destroying the old one, only more
850 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000851 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000852
853int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000854_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000855{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200856 PyTupleObject *v;
857 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 Py_ssize_t i;
859 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 v = (PyTupleObject *) *pv;
862 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
863 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
864 *pv = 0;
865 Py_XDECREF(v);
866 PyErr_BadInternalCall();
867 return -1;
868 }
869 oldsize = Py_SIZE(v);
870 if (oldsize == newsize)
871 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (oldsize == 0) {
874 /* Empty tuples are often shared, so we should never
875 resize them in-place even if we do own the only
876 (current) reference */
877 Py_DECREF(v);
878 *pv = PyTuple_New(newsize);
879 return *pv == NULL ? -1 : 0;
880 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* XXX UNREF/NEWREF interface should be more symmetrical */
883 _Py_DEC_REFTOTAL;
884 if (_PyObject_GC_IS_TRACKED(v))
885 _PyObject_GC_UNTRACK(v);
886 _Py_ForgetReference((PyObject *) v);
887 /* DECREF items deleted by shrinkage */
888 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200889 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 }
891 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
892 if (sv == NULL) {
893 *pv = NULL;
894 PyObject_GC_Del(v);
895 return -1;
896 }
897 _Py_NewReference((PyObject *) sv);
898 /* Zero out items added by growing */
899 if (newsize > oldsize)
900 memset(&sv->ob_item[oldsize], 0,
901 sizeof(*sv->ob_item) * (newsize - oldsize));
902 *pv = (PyObject *) sv;
903 _PyObject_GC_TRACK(sv);
904 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000905}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000906
Christian Heimesa156e092008-02-16 07:38:31 +0000907int
908PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000911#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 int i;
913 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
914 PyTupleObject *p, *q;
915 p = free_list[i];
916 freelist_size += numfree[i];
917 free_list[i] = NULL;
918 numfree[i] = 0;
919 while (p) {
920 q = p;
921 p = (PyTupleObject *)(p->ob_item[0]);
922 PyObject_GC_Del(q);
923 }
924 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000925#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000927}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928
Christian Heimesa156e092008-02-16 07:38:31 +0000929void
930PyTuple_Fini(void)
931{
932#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* empty tuples are used all over the place and applications may
934 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200935 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000938#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000939#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000941#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000942}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000943
944/*********************** Tuple Iterator **************************/
945
946typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200948 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000950} tupleiterobject;
951
Raymond Hettinger48923c52002-08-09 01:30:17 +0000952static void
953tupleiter_dealloc(tupleiterobject *it)
954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 _PyObject_GC_UNTRACK(it);
956 Py_XDECREF(it->it_seq);
957 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000958}
959
960static int
961tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 Py_VISIT(it->it_seq);
964 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000965}
966
Raymond Hettinger48923c52002-08-09 01:30:17 +0000967static PyObject *
968tupleiter_next(tupleiterobject *it)
969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyTupleObject *seq;
971 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 assert(it != NULL);
974 seq = it->it_seq;
975 if (seq == NULL)
976 return NULL;
977 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 if (it->it_index < PyTuple_GET_SIZE(seq)) {
980 item = PyTuple_GET_ITEM(seq, it->it_index);
981 ++it->it_index;
982 Py_INCREF(item);
983 return item;
984 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300987 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000989}
990
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000991static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000992tupleiter_len(tupleiterobject *it)
993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 Py_ssize_t len = 0;
995 if (it->it_seq)
996 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
997 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000998}
999
Armin Rigof5b3e362006-02-11 21:32:43 +00001000PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001001
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001002static PyObject *
1003tupleiter_reduce(tupleiterobject *it)
1004{
1005 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001006 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001007 it->it_seq, it->it_index);
1008 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001009 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001010}
1011
1012static PyObject *
1013tupleiter_setstate(tupleiterobject *it, PyObject *state)
1014{
Victor Stinner7660b882013-06-24 23:59:24 +02001015 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001016 if (index == -1 && PyErr_Occurred())
1017 return NULL;
1018 if (it->it_seq != NULL) {
1019 if (index < 0)
1020 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001021 else if (index > PyTuple_GET_SIZE(it->it_seq))
1022 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001023 it->it_index = index;
1024 }
1025 Py_RETURN_NONE;
1026}
1027
1028PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1029PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1030
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001031static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001033 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1034 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001036};
1037
Raymond Hettinger48923c52002-08-09 01:30:17 +00001038PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1040 "tuple_iterator", /* tp_name */
1041 sizeof(tupleiterobject), /* tp_basicsize */
1042 0, /* tp_itemsize */
1043 /* methods */
1044 (destructor)tupleiter_dealloc, /* tp_dealloc */
1045 0, /* tp_print */
1046 0, /* tp_getattr */
1047 0, /* tp_setattr */
1048 0, /* tp_reserved */
1049 0, /* tp_repr */
1050 0, /* tp_as_number */
1051 0, /* tp_as_sequence */
1052 0, /* tp_as_mapping */
1053 0, /* tp_hash */
1054 0, /* tp_call */
1055 0, /* tp_str */
1056 PyObject_GenericGetAttr, /* tp_getattro */
1057 0, /* tp_setattro */
1058 0, /* tp_as_buffer */
1059 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1060 0, /* tp_doc */
1061 (traverseproc)tupleiter_traverse, /* tp_traverse */
1062 0, /* tp_clear */
1063 0, /* tp_richcompare */
1064 0, /* tp_weaklistoffset */
1065 PyObject_SelfIter, /* tp_iter */
1066 (iternextfunc)tupleiter_next, /* tp_iternext */
1067 tupleiter_methods, /* tp_methods */
1068 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001069};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070
1071static PyObject *
1072tuple_iter(PyObject *seq)
1073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 if (!PyTuple_Check(seq)) {
1077 PyErr_BadInternalCall();
1078 return NULL;
1079 }
1080 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1081 if (it == NULL)
1082 return NULL;
1083 it->it_index = 0;
1084 Py_INCREF(seq);
1085 it->it_seq = (PyTupleObject *)seq;
1086 _PyObject_GC_TRACK(it);
1087 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001088}