blob: 4c6bbe71a194051efe3e0ad7e5df5e3934cc84a4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Tuple object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01005#include "accu.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum5ce78f82000-04-21 21:15:05 +00007/* Speed optimization to avoid frequent malloc/free of small tuples */
Christian Heimes2202f872008-02-06 14:31:34 +00008#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +000010#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011#ifndef PyTuple_MAXFREELIST
Christian Heimes2202f872008-02-06 14:31:34 +000012#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000013#endif
14
Christian Heimes2202f872008-02-06 14:31:34 +000015#if PyTuple_MAXSAVESIZE > 0
16/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000017 tuple () of which at most one instance will be allocated.
18*/
Christian Heimes2202f872008-02-06 14:31:34 +000019static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
20static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000021#endif
22#ifdef COUNT_ALLOCS
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000023Py_ssize_t fast_tuple_allocs;
24Py_ssize_t tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025#endif
26
Antoine Pitrou3a652b12009-03-23 18:52:06 +000027/* Debug statistic to count GC tracking of tuples.
28 Please note that tuples are only untracked when considered by the GC, and
29 many of them will be dead before. Therefore, a tracking rate close to 100%
30 does not necessarily prove that the heuristic is inefficient.
31*/
32#ifdef SHOW_TRACK_COUNT
33static Py_ssize_t count_untracked = 0;
34static Py_ssize_t count_tracked = 0;
35
36static void
37show_track(void)
38{
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030039 PyObject *xoptions, *value;
40 _Py_IDENTIFIER(showalloccount);
41
42 xoptions = PySys_GetXOptions();
43 if (xoptions == NULL)
44 return;
45 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
46 if (value != Py_True)
47 return;
48
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
50 count_tracked + count_untracked);
51 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
52 "d\n", count_tracked);
53 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
54 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000055}
56#endif
57
David Malcolm49526f42012-06-22 14:55:41 -040058/* Print summary info about the state of the optimized allocator */
59void
60_PyTuple_DebugMallocStats(FILE *out)
61{
62#if PyTuple_MAXSAVESIZE > 0
63 int i;
64 char buf[128];
65 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
66 PyOS_snprintf(buf, sizeof(buf),
67 "free %d-sized PyTupleObject", i);
68 _PyDebugAllocatorStats(out,
69 buf,
70 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
71 }
72#endif
73}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000074
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020076PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020078 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 Py_ssize_t i;
80 if (size < 0) {
81 PyErr_BadInternalCall();
82 return NULL;
83 }
Christian Heimes2202f872008-02-06 14:31:34 +000084#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (size == 0 && free_list[0]) {
86 op = free_list[0];
87 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000088#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return (PyObject *) op;
92 }
93 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
94 free_list[size] = (PyTupleObject *) op->ob_item[0];
95 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000096#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000100#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 Py_SIZE(op) = size;
102 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000103#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 _Py_NewReference((PyObject *)op);
105 }
106 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200110 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100111 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 return PyErr_NoMemory();
113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
115 if (op == NULL)
116 return NULL;
117 }
118 for (i=0; i < size; i++)
119 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000120#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (size == 0) {
122 free_list[0] = op;
123 ++numfree[0];
124 Py_INCREF(op); /* extra INCREF so that this is never freed */
125 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000126#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000127#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000129#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 _PyObject_GC_TRACK(op);
131 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200135PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 if (!PyTuple_Check(op)) {
138 PyErr_BadInternalCall();
139 return -1;
140 }
141 else
142 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143}
144
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200146PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (!PyTuple_Check(op)) {
149 PyErr_BadInternalCall();
150 return NULL;
151 }
152 if (i < 0 || i >= Py_SIZE(op)) {
153 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
154 return NULL;
155 }
156 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157}
158
159int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200160PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200162 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
164 Py_XDECREF(newitem);
165 PyErr_BadInternalCall();
166 return -1;
167 }
168 if (i < 0 || i >= Py_SIZE(op)) {
169 Py_XDECREF(newitem);
170 PyErr_SetString(PyExc_IndexError,
171 "tuple assignment index out of range");
172 return -1;
173 }
174 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300175 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
178
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000179void
180_PyTuple_MaybeUntrack(PyObject *op)
181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 PyTupleObject *t;
183 Py_ssize_t i, n;
184
185 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
186 return;
187 t = (PyTupleObject *) op;
188 n = Py_SIZE(t);
189 for (i = 0; i < n; i++) {
190 PyObject *elt = PyTuple_GET_ITEM(t, i);
191 /* Tuple with NULL elements aren't
192 fully constructed, don't untrack
193 them yet. */
194 if (!elt ||
195 _PyObject_GC_MAY_BE_TRACKED(elt))
196 return;
197 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000198#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 count_tracked--;
200 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000201#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000203}
204
Raymond Hettingercb2da432003-10-12 18:24:34 +0000205PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 Py_ssize_t i;
209 PyObject *o;
210 PyObject *result;
211 PyObject **items;
212 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 va_start(vargs, n);
215 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200216 if (result == NULL) {
217 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 items = ((PyTupleObject *)result)->ob_item;
221 for (i = 0; i < n; i++) {
222 o = va_arg(vargs, PyObject *);
223 Py_INCREF(o);
224 items[i] = o;
225 }
226 va_end(vargs);
227 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000228}
229
230
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231/* Methods */
232
233static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200234tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200236 Py_ssize_t i;
237 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 PyObject_GC_UnTrack(op);
239 Py_TRASHCAN_SAFE_BEGIN(op)
240 if (len > 0) {
241 i = len;
242 while (--i >= 0)
243 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000244#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (len < PyTuple_MAXSAVESIZE &&
246 numfree[len] < PyTuple_MAXFREELIST &&
247 Py_TYPE(op) == &PyTuple_Type)
248 {
249 op->ob_item[0] = (PyObject *) free_list[len];
250 numfree[len]++;
251 free_list[len] = op;
252 goto done; /* return */
253 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 }
256 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000257done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259}
260
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000262tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100265 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 n = Py_SIZE(v);
268 if (n == 0)
269 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* While not mutable, it is still possible to end up with a cycle in a
272 tuple through an object that stores itself within a tuple (and thus
273 infinitely asks for the repr of itself). This should only be
274 possible within a type. */
275 i = Py_ReprEnter((PyObject *)v);
276 if (i != 0) {
277 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
278 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000279
Victor Stinner88a9cd92013-11-19 12:59:46 +0100280 _PyUnicodeWriter_Init(&writer);
281 writer.overallocate = 1;
282 if (Py_SIZE(v) > 1) {
283 /* "(" + "1" + ", 2" * (len - 1) + ")" */
284 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
285 }
286 else {
287 /* "(1,)" */
288 writer.min_length = 4;
289 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200290
Victor Stinner88a9cd92013-11-19 12:59:46 +0100291 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200292 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* Do repr() on each element. */
295 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100296 PyObject *s;
297
298 if (i > 0) {
299 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
300 goto error;
301 }
302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200304 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 s = PyObject_Repr(v->ob_item[i]);
306 Py_LeaveRecursiveCall();
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
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000338 upto length eight:
339
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
Raymond Hettinger65baa342008-02-07 00:41:02 +0000526static PyObject *
527tupleindex(PyTupleObject *self, PyObject *args)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200530 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000531
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200532 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
533 _PyEval_SliceIndex, &start,
534 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return NULL;
536 if (start < 0) {
537 start += Py_SIZE(self);
538 if (start < 0)
539 start = 0;
540 }
541 if (stop < 0) {
542 stop += Py_SIZE(self);
543 if (stop < 0)
544 stop = 0;
545 }
546 for (i = start; i < stop && i < Py_SIZE(self); i++) {
547 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
548 if (cmp > 0)
549 return PyLong_FromSsize_t(i);
550 else if (cmp < 0)
551 return NULL;
552 }
553 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
554 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000555}
556
557static PyObject *
558tuplecount(PyTupleObject *self, PyObject *v)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 Py_ssize_t count = 0;
561 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 for (i = 0; i < Py_SIZE(self); i++) {
564 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
565 if (cmp > 0)
566 count++;
567 else if (cmp < 0)
568 return NULL;
569 }
570 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000571}
572
Jeremy Hylton8caad492000-06-23 14:18:11 +0000573static int
574tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 for (i = Py_SIZE(o); --i >= 0; )
579 Py_VISIT(o->ob_item[i]);
580 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000581}
582
Guido van Rossumf77bc622001-01-18 00:00:53 +0000583static PyObject *
584tuplerichcompare(PyObject *v, PyObject *w, int op)
585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 PyTupleObject *vt, *wt;
587 Py_ssize_t i;
588 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000589
Brian Curtindfc80e32011-08-10 20:28:54 -0500590 if (!PyTuple_Check(v) || !PyTuple_Check(w))
591 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 vt = (PyTupleObject *)v;
594 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 vlen = Py_SIZE(vt);
597 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 /* Note: the corresponding code for lists has an "early out" test
600 * here when op is EQ or NE and the lengths differ. That pays there,
601 * but Tim was unable to find any real code where EQ/NE tuple
602 * compares don't have the same length, so testing for it here would
603 * have cost without benefit.
604 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 /* Search for the first index where items are different.
607 * Note that because tuples are immutable, it's safe to reuse
608 * vlen and wlen across the comparison calls.
609 */
610 for (i = 0; i < vlen && i < wlen; i++) {
611 int k = PyObject_RichCompareBool(vt->ob_item[i],
612 wt->ob_item[i], Py_EQ);
613 if (k < 0)
614 return NULL;
615 if (!k)
616 break;
617 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (i >= vlen || i >= wlen) {
620 /* No more items to compare -- compare sizes */
621 int cmp;
622 PyObject *res;
623 switch (op) {
624 case Py_LT: cmp = vlen < wlen; break;
625 case Py_LE: cmp = vlen <= wlen; break;
626 case Py_EQ: cmp = vlen == wlen; break;
627 case Py_NE: cmp = vlen != wlen; break;
628 case Py_GT: cmp = vlen > wlen; break;
629 case Py_GE: cmp = vlen >= wlen; break;
630 default: return NULL; /* cannot happen */
631 }
632 if (cmp)
633 res = Py_True;
634 else
635 res = Py_False;
636 Py_INCREF(res);
637 return res;
638 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /* We have an item that differs -- shortcuts for EQ/NE */
641 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200642 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 }
644 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200645 Py_RETURN_TRUE;
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 /* Compare the final item again using the proper operator */
649 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000650}
651
Jeremy Hylton938ace62002-07-17 16:30:39 +0000652static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000653tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
654
Tim Peters6d6c1a32001-08-02 04:15:00 +0000655static PyObject *
656tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 PyObject *arg = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (type != &PyTuple_Type)
661 return tuple_subtype_new(type, args, kwds);
Serhiy Storchaka2e564242017-03-06 17:01:06 +0200662 if (!_PyArg_NoKeywords("tuple()", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 return NULL;
Serhiy Storchaka2e564242017-03-06 17:01:06 +0200664 if (!PyArg_UnpackTuple(args, "tuple", 0, 1, &arg))
665 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 if (arg == NULL)
668 return PyTuple_New(0);
669 else
670 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000671}
672
Guido van Rossumae960af2001-08-30 03:11:59 +0000673static PyObject *
674tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 PyObject *tmp, *newobj, *item;
677 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 assert(PyType_IsSubtype(type, &PyTuple_Type));
680 tmp = tuple_new(&PyTuple_Type, args, kwds);
681 if (tmp == NULL)
682 return NULL;
683 assert(PyTuple_Check(tmp));
684 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
685 if (newobj == NULL)
686 return NULL;
687 for (i = 0; i < n; i++) {
688 item = PyTuple_GET_ITEM(tmp, i);
689 Py_INCREF(item);
690 PyTuple_SET_ITEM(newobj, i, item);
691 }
692 Py_DECREF(tmp);
693 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000694}
695
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000696PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000697"tuple() -> empty tuple\n\
698tuple(iterable) -> tuple initialized from iterable's items\n\
699\n\
700If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 (lenfunc)tuplelength, /* sq_length */
704 (binaryfunc)tupleconcat, /* sq_concat */
705 (ssizeargfunc)tuplerepeat, /* sq_repeat */
706 (ssizeargfunc)tupleitem, /* sq_item */
707 0, /* sq_slice */
708 0, /* sq_ass_item */
709 0, /* sq_ass_slice */
710 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711};
712
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000713static PyObject*
714tuplesubscript(PyTupleObject* self, PyObject* item)
715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (PyIndex_Check(item)) {
717 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
718 if (i == -1 && PyErr_Occurred())
719 return NULL;
720 if (i < 0)
721 i += PyTuple_GET_SIZE(self);
722 return tupleitem(self, i);
723 }
724 else if (PySlice_Check(item)) {
725 Py_ssize_t start, stop, step, slicelength, cur, i;
726 PyObject* result;
727 PyObject* it;
728 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000729
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000730 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyTuple_GET_SIZE(self),
732 &start, &stop, &step, &slicelength) < 0) {
733 return NULL;
734 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 if (slicelength <= 0) {
737 return PyTuple_New(0);
738 }
739 else if (start == 0 && step == 1 &&
740 slicelength == PyTuple_GET_SIZE(self) &&
741 PyTuple_CheckExact(self)) {
742 Py_INCREF(self);
743 return (PyObject *)self;
744 }
745 else {
746 result = PyTuple_New(slicelength);
747 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 src = self->ob_item;
750 dest = ((PyTupleObject *)result)->ob_item;
751 for (cur = start, i = 0; i < slicelength;
752 cur += step, i++) {
753 it = src[cur];
754 Py_INCREF(it);
755 dest[i] = it;
756 }
757
758 return result;
759 }
760 }
761 else {
762 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400763 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 Py_TYPE(item)->tp_name);
765 return NULL;
766 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000767}
768
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000769static PyObject *
770tuple_getnewargs(PyTupleObject *v)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
773
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000774}
775
Raymond Hettinger65baa342008-02-07 00:41:02 +0000776PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000777"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
778"Raises ValueError if the value is not present."
779);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000780PyDoc_STRVAR(count_doc,
781"T.count(value) -> integer -- return number of occurrences of value");
782
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000783static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
786 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
787 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000788};
789
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000790static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 (lenfunc)tuplelength,
792 (binaryfunc)tuplesubscript,
793 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000794};
795
Raymond Hettinger48923c52002-08-09 01:30:17 +0000796static PyObject *tuple_iter(PyObject *seq);
797
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyVarObject_HEAD_INIT(&PyType_Type, 0)
800 "tuple",
801 sizeof(PyTupleObject) - sizeof(PyObject *),
802 sizeof(PyObject *),
803 (destructor)tupledealloc, /* tp_dealloc */
804 0, /* tp_print */
805 0, /* tp_getattr */
806 0, /* tp_setattr */
807 0, /* tp_reserved */
808 (reprfunc)tuplerepr, /* tp_repr */
809 0, /* tp_as_number */
810 &tuple_as_sequence, /* tp_as_sequence */
811 &tuple_as_mapping, /* tp_as_mapping */
812 (hashfunc)tuplehash, /* tp_hash */
813 0, /* tp_call */
814 0, /* tp_str */
815 PyObject_GenericGetAttr, /* tp_getattro */
816 0, /* tp_setattro */
817 0, /* tp_as_buffer */
818 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
819 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
820 tuple_doc, /* tp_doc */
821 (traverseproc)tupletraverse, /* tp_traverse */
822 0, /* tp_clear */
823 tuplerichcompare, /* tp_richcompare */
824 0, /* tp_weaklistoffset */
825 tuple_iter, /* tp_iter */
826 0, /* tp_iternext */
827 tuple_methods, /* tp_methods */
828 0, /* tp_members */
829 0, /* tp_getset */
830 0, /* tp_base */
831 0, /* tp_dict */
832 0, /* tp_descr_get */
833 0, /* tp_descr_set */
834 0, /* tp_dictoffset */
835 0, /* tp_init */
836 0, /* tp_alloc */
837 tuple_new, /* tp_new */
838 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000840
841/* The following function breaks the notion that tuples are immutable:
842 it changes the size of a tuple. We get away with this only if there
843 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000844 as creating a new tuple object and destroying the old one, only more
845 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000846 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000847
848int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000850{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200851 PyTupleObject *v;
852 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 Py_ssize_t i;
854 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 v = (PyTupleObject *) *pv;
857 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
858 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
859 *pv = 0;
860 Py_XDECREF(v);
861 PyErr_BadInternalCall();
862 return -1;
863 }
864 oldsize = Py_SIZE(v);
865 if (oldsize == newsize)
866 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (oldsize == 0) {
869 /* Empty tuples are often shared, so we should never
870 resize them in-place even if we do own the only
871 (current) reference */
872 Py_DECREF(v);
873 *pv = PyTuple_New(newsize);
874 return *pv == NULL ? -1 : 0;
875 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 /* XXX UNREF/NEWREF interface should be more symmetrical */
878 _Py_DEC_REFTOTAL;
879 if (_PyObject_GC_IS_TRACKED(v))
880 _PyObject_GC_UNTRACK(v);
881 _Py_ForgetReference((PyObject *) v);
882 /* DECREF items deleted by shrinkage */
883 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200884 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 }
886 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
887 if (sv == NULL) {
888 *pv = NULL;
889 PyObject_GC_Del(v);
890 return -1;
891 }
892 _Py_NewReference((PyObject *) sv);
893 /* Zero out items added by growing */
894 if (newsize > oldsize)
895 memset(&sv->ob_item[oldsize], 0,
896 sizeof(*sv->ob_item) * (newsize - oldsize));
897 *pv = (PyObject *) sv;
898 _PyObject_GC_TRACK(sv);
899 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000900}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000901
Christian Heimesa156e092008-02-16 07:38:31 +0000902int
903PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000906#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 int i;
908 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
909 PyTupleObject *p, *q;
910 p = free_list[i];
911 freelist_size += numfree[i];
912 free_list[i] = NULL;
913 numfree[i] = 0;
914 while (p) {
915 q = p;
916 p = (PyTupleObject *)(p->ob_item[0]);
917 PyObject_GC_Del(q);
918 }
919 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000920#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000922}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923
Christian Heimesa156e092008-02-16 07:38:31 +0000924void
925PyTuple_Fini(void)
926{
927#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* empty tuples are used all over the place and applications may
929 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200930 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000933#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000934#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000936#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000937}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000938
939/*********************** Tuple Iterator **************************/
940
941typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200943 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000945} tupleiterobject;
946
Raymond Hettinger48923c52002-08-09 01:30:17 +0000947static void
948tupleiter_dealloc(tupleiterobject *it)
949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 _PyObject_GC_UNTRACK(it);
951 Py_XDECREF(it->it_seq);
952 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000953}
954
955static int
956tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 Py_VISIT(it->it_seq);
959 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960}
961
Raymond Hettinger48923c52002-08-09 01:30:17 +0000962static PyObject *
963tupleiter_next(tupleiterobject *it)
964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyTupleObject *seq;
966 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 assert(it != NULL);
969 seq = it->it_seq;
970 if (seq == NULL)
971 return NULL;
972 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 if (it->it_index < PyTuple_GET_SIZE(seq)) {
975 item = PyTuple_GET_ITEM(seq, it->it_index);
976 ++it->it_index;
977 Py_INCREF(item);
978 return item;
979 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300982 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000984}
985
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000986static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000987tupleiter_len(tupleiterobject *it)
988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 Py_ssize_t len = 0;
990 if (it->it_seq)
991 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
992 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000993}
994
Armin Rigof5b3e362006-02-11 21:32:43 +0000995PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000996
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000997static PyObject *
998tupleiter_reduce(tupleiterobject *it)
999{
1000 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02001001 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001002 it->it_seq, it->it_index);
1003 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001004 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001005}
1006
1007static PyObject *
1008tupleiter_setstate(tupleiterobject *it, PyObject *state)
1009{
Victor Stinner7660b882013-06-24 23:59:24 +02001010 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001011 if (index == -1 && PyErr_Occurred())
1012 return NULL;
1013 if (it->it_seq != NULL) {
1014 if (index < 0)
1015 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001016 else if (index > PyTuple_GET_SIZE(it->it_seq))
1017 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001018 it->it_index = index;
1019 }
1020 Py_RETURN_NONE;
1021}
1022
1023PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1024PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1025
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001026static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001028 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1029 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001031};
1032
Raymond Hettinger48923c52002-08-09 01:30:17 +00001033PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1035 "tuple_iterator", /* tp_name */
1036 sizeof(tupleiterobject), /* tp_basicsize */
1037 0, /* tp_itemsize */
1038 /* methods */
1039 (destructor)tupleiter_dealloc, /* tp_dealloc */
1040 0, /* tp_print */
1041 0, /* tp_getattr */
1042 0, /* tp_setattr */
1043 0, /* tp_reserved */
1044 0, /* tp_repr */
1045 0, /* tp_as_number */
1046 0, /* tp_as_sequence */
1047 0, /* tp_as_mapping */
1048 0, /* tp_hash */
1049 0, /* tp_call */
1050 0, /* tp_str */
1051 PyObject_GenericGetAttr, /* tp_getattro */
1052 0, /* tp_setattro */
1053 0, /* tp_as_buffer */
1054 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1055 0, /* tp_doc */
1056 (traverseproc)tupleiter_traverse, /* tp_traverse */
1057 0, /* tp_clear */
1058 0, /* tp_richcompare */
1059 0, /* tp_weaklistoffset */
1060 PyObject_SelfIter, /* tp_iter */
1061 (iternextfunc)tupleiter_next, /* tp_iternext */
1062 tupleiter_methods, /* tp_methods */
1063 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001064};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001065
1066static PyObject *
1067tuple_iter(PyObject *seq)
1068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 if (!PyTuple_Check(seq)) {
1072 PyErr_BadInternalCall();
1073 return NULL;
1074 }
1075 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1076 if (it == NULL)
1077 return NULL;
1078 it->it_index = 0;
1079 Py_INCREF(seq);
1080 it->it_seq = (PyTupleObject *)seq;
1081 _PyObject_GC_TRACK(it);
1082 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083}