blob: c2696c7dfb799aae71ad2e75e97e81dfeb1c85bd [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;
449 if (!PyTuple_Check(bb)) {
450 PyErr_Format(PyExc_TypeError,
451 "can only concatenate tuple (not \"%.200s\") to tuple",
452 Py_TYPE(bb)->tp_name);
453 return NULL;
454 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455#define b ((PyTupleObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000456 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000458 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 np = (PyTupleObject *) PyTuple_New(size);
460 if (np == NULL) {
461 return NULL;
462 }
463 src = a->ob_item;
464 dest = np->ob_item;
465 for (i = 0; i < Py_SIZE(a); i++) {
466 PyObject *v = src[i];
467 Py_INCREF(v);
468 dest[i] = v;
469 }
470 src = b->ob_item;
471 dest = np->ob_item + Py_SIZE(a);
472 for (i = 0; i < Py_SIZE(b); i++) {
473 PyObject *v = src[i];
474 Py_INCREF(v);
475 dest[i] = v;
476 }
477 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478#undef b
479}
480
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 Py_ssize_t i, j;
485 Py_ssize_t size;
486 PyTupleObject *np;
487 PyObject **p, **items;
488 if (n < 0)
489 n = 0;
490 if (Py_SIZE(a) == 0 || n == 1) {
491 if (PyTuple_CheckExact(a)) {
492 /* Since tuples are immutable, we can return a shared
493 copy in this case */
494 Py_INCREF(a);
495 return (PyObject *)a;
496 }
497 if (Py_SIZE(a) == 0)
498 return PyTuple_New(0);
499 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100500 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100502 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 np = (PyTupleObject *) PyTuple_New(size);
504 if (np == NULL)
505 return NULL;
506 p = np->ob_item;
507 items = a->ob_item;
508 for (i = 0; i < n; i++) {
509 for (j = 0; j < Py_SIZE(a); j++) {
510 *p = items[j];
511 Py_INCREF(*p);
512 p++;
513 }
514 }
515 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000516}
517
Raymond Hettinger65baa342008-02-07 00:41:02 +0000518static PyObject *
519tupleindex(PyTupleObject *self, PyObject *args)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200522 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000523
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200524 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
525 _PyEval_SliceIndex, &start,
526 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return NULL;
528 if (start < 0) {
529 start += Py_SIZE(self);
530 if (start < 0)
531 start = 0;
532 }
533 if (stop < 0) {
534 stop += Py_SIZE(self);
535 if (stop < 0)
536 stop = 0;
537 }
538 for (i = start; i < stop && i < Py_SIZE(self); i++) {
539 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
540 if (cmp > 0)
541 return PyLong_FromSsize_t(i);
542 else if (cmp < 0)
543 return NULL;
544 }
545 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
546 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000547}
548
549static PyObject *
550tuplecount(PyTupleObject *self, PyObject *v)
551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_ssize_t count = 0;
553 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 for (i = 0; i < Py_SIZE(self); i++) {
556 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
557 if (cmp > 0)
558 count++;
559 else if (cmp < 0)
560 return NULL;
561 }
562 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000563}
564
Jeremy Hylton8caad492000-06-23 14:18:11 +0000565static int
566tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 for (i = Py_SIZE(o); --i >= 0; )
571 Py_VISIT(o->ob_item[i]);
572 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000573}
574
Guido van Rossumf77bc622001-01-18 00:00:53 +0000575static PyObject *
576tuplerichcompare(PyObject *v, PyObject *w, int op)
577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyTupleObject *vt, *wt;
579 Py_ssize_t i;
580 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000581
Brian Curtindfc80e32011-08-10 20:28:54 -0500582 if (!PyTuple_Check(v) || !PyTuple_Check(w))
583 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 vt = (PyTupleObject *)v;
586 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 vlen = Py_SIZE(vt);
589 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Note: the corresponding code for lists has an "early out" test
592 * here when op is EQ or NE and the lengths differ. That pays there,
593 * but Tim was unable to find any real code where EQ/NE tuple
594 * compares don't have the same length, so testing for it here would
595 * have cost without benefit.
596 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* Search for the first index where items are different.
599 * Note that because tuples are immutable, it's safe to reuse
600 * vlen and wlen across the comparison calls.
601 */
602 for (i = 0; i < vlen && i < wlen; i++) {
603 int k = PyObject_RichCompareBool(vt->ob_item[i],
604 wt->ob_item[i], Py_EQ);
605 if (k < 0)
606 return NULL;
607 if (!k)
608 break;
609 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (i >= vlen || i >= wlen) {
612 /* No more items to compare -- compare sizes */
613 int cmp;
614 PyObject *res;
615 switch (op) {
616 case Py_LT: cmp = vlen < wlen; break;
617 case Py_LE: cmp = vlen <= wlen; break;
618 case Py_EQ: cmp = vlen == wlen; break;
619 case Py_NE: cmp = vlen != wlen; break;
620 case Py_GT: cmp = vlen > wlen; break;
621 case Py_GE: cmp = vlen >= wlen; break;
622 default: return NULL; /* cannot happen */
623 }
624 if (cmp)
625 res = Py_True;
626 else
627 res = Py_False;
628 Py_INCREF(res);
629 return res;
630 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* We have an item that differs -- shortcuts for EQ/NE */
633 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200634 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 }
636 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200637 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /* Compare the final item again using the proper operator */
641 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000642}
643
Jeremy Hylton938ace62002-07-17 16:30:39 +0000644static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000645tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
646
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647static PyObject *
648tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyObject *arg = NULL;
651 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (type != &PyTuple_Type)
654 return tuple_subtype_new(type, args, kwds);
655 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
656 return NULL;
Serhiy Storchaka58d23e62017-03-06 00:53:39 +0200657 if (arg != NULL && PyTuple_GET_SIZE(args) == 0) {
658 if (PyErr_Warn(PyExc_DeprecationWarning,
659 "Using 'sequence' as a keyword argument is deprecated; "
660 "specify the value as a positional argument instead") < 0)
661 return NULL;
662 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 if (arg == NULL)
665 return PyTuple_New(0);
666 else
667 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668}
669
Guido van Rossumae960af2001-08-30 03:11:59 +0000670static PyObject *
671tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 PyObject *tmp, *newobj, *item;
674 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 assert(PyType_IsSubtype(type, &PyTuple_Type));
677 tmp = tuple_new(&PyTuple_Type, args, kwds);
678 if (tmp == NULL)
679 return NULL;
680 assert(PyTuple_Check(tmp));
681 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
682 if (newobj == NULL)
683 return NULL;
684 for (i = 0; i < n; i++) {
685 item = PyTuple_GET_ITEM(tmp, i);
686 Py_INCREF(item);
687 PyTuple_SET_ITEM(newobj, i, item);
688 }
689 Py_DECREF(tmp);
690 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000691}
692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000693PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000694"tuple() -> empty tuple\n\
695tuple(iterable) -> tuple initialized from iterable's items\n\
696\n\
697If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 (lenfunc)tuplelength, /* sq_length */
701 (binaryfunc)tupleconcat, /* sq_concat */
702 (ssizeargfunc)tuplerepeat, /* sq_repeat */
703 (ssizeargfunc)tupleitem, /* sq_item */
704 0, /* sq_slice */
705 0, /* sq_ass_item */
706 0, /* sq_ass_slice */
707 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000708};
709
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000710static PyObject*
711tuplesubscript(PyTupleObject* self, PyObject* item)
712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (PyIndex_Check(item)) {
714 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
715 if (i == -1 && PyErr_Occurred())
716 return NULL;
717 if (i < 0)
718 i += PyTuple_GET_SIZE(self);
719 return tupleitem(self, i);
720 }
721 else if (PySlice_Check(item)) {
722 Py_ssize_t start, stop, step, slicelength, cur, i;
723 PyObject* result;
724 PyObject* it;
725 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000726
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000727 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 PyTuple_GET_SIZE(self),
729 &start, &stop, &step, &slicelength) < 0) {
730 return NULL;
731 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (slicelength <= 0) {
734 return PyTuple_New(0);
735 }
736 else if (start == 0 && step == 1 &&
737 slicelength == PyTuple_GET_SIZE(self) &&
738 PyTuple_CheckExact(self)) {
739 Py_INCREF(self);
740 return (PyObject *)self;
741 }
742 else {
743 result = PyTuple_New(slicelength);
744 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 src = self->ob_item;
747 dest = ((PyTupleObject *)result)->ob_item;
748 for (cur = start, i = 0; i < slicelength;
749 cur += step, i++) {
750 it = src[cur];
751 Py_INCREF(it);
752 dest[i] = it;
753 }
754
755 return result;
756 }
757 }
758 else {
759 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400760 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 Py_TYPE(item)->tp_name);
762 return NULL;
763 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000764}
765
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000766static PyObject *
767tuple_getnewargs(PyTupleObject *v)
768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
770
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000771}
772
Raymond Hettinger65baa342008-02-07 00:41:02 +0000773PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000774"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
775"Raises ValueError if the value is not present."
776);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000777PyDoc_STRVAR(count_doc,
778"T.count(value) -> integer -- return number of occurrences of value");
779
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000780static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
783 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
784 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000785};
786
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000787static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 (lenfunc)tuplelength,
789 (binaryfunc)tuplesubscript,
790 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000791};
792
Raymond Hettinger48923c52002-08-09 01:30:17 +0000793static PyObject *tuple_iter(PyObject *seq);
794
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyVarObject_HEAD_INIT(&PyType_Type, 0)
797 "tuple",
798 sizeof(PyTupleObject) - sizeof(PyObject *),
799 sizeof(PyObject *),
800 (destructor)tupledealloc, /* tp_dealloc */
801 0, /* tp_print */
802 0, /* tp_getattr */
803 0, /* tp_setattr */
804 0, /* tp_reserved */
805 (reprfunc)tuplerepr, /* tp_repr */
806 0, /* tp_as_number */
807 &tuple_as_sequence, /* tp_as_sequence */
808 &tuple_as_mapping, /* tp_as_mapping */
809 (hashfunc)tuplehash, /* tp_hash */
810 0, /* tp_call */
811 0, /* tp_str */
812 PyObject_GenericGetAttr, /* tp_getattro */
813 0, /* tp_setattro */
814 0, /* tp_as_buffer */
815 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
816 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
817 tuple_doc, /* tp_doc */
818 (traverseproc)tupletraverse, /* tp_traverse */
819 0, /* tp_clear */
820 tuplerichcompare, /* tp_richcompare */
821 0, /* tp_weaklistoffset */
822 tuple_iter, /* tp_iter */
823 0, /* tp_iternext */
824 tuple_methods, /* tp_methods */
825 0, /* tp_members */
826 0, /* tp_getset */
827 0, /* tp_base */
828 0, /* tp_dict */
829 0, /* tp_descr_get */
830 0, /* tp_descr_set */
831 0, /* tp_dictoffset */
832 0, /* tp_init */
833 0, /* tp_alloc */
834 tuple_new, /* tp_new */
835 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000837
838/* The following function breaks the notion that tuples are immutable:
839 it changes the size of a tuple. We get away with this only if there
840 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000841 as creating a new tuple object and destroying the old one, only more
842 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000843 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000844
845int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000846_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000847{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200848 PyTupleObject *v;
849 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 Py_ssize_t i;
851 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 v = (PyTupleObject *) *pv;
854 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
855 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
856 *pv = 0;
857 Py_XDECREF(v);
858 PyErr_BadInternalCall();
859 return -1;
860 }
861 oldsize = Py_SIZE(v);
862 if (oldsize == newsize)
863 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (oldsize == 0) {
866 /* Empty tuples are often shared, so we should never
867 resize them in-place even if we do own the only
868 (current) reference */
869 Py_DECREF(v);
870 *pv = PyTuple_New(newsize);
871 return *pv == NULL ? -1 : 0;
872 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 /* XXX UNREF/NEWREF interface should be more symmetrical */
875 _Py_DEC_REFTOTAL;
876 if (_PyObject_GC_IS_TRACKED(v))
877 _PyObject_GC_UNTRACK(v);
878 _Py_ForgetReference((PyObject *) v);
879 /* DECREF items deleted by shrinkage */
880 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200881 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 }
883 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
884 if (sv == NULL) {
885 *pv = NULL;
886 PyObject_GC_Del(v);
887 return -1;
888 }
889 _Py_NewReference((PyObject *) sv);
890 /* Zero out items added by growing */
891 if (newsize > oldsize)
892 memset(&sv->ob_item[oldsize], 0,
893 sizeof(*sv->ob_item) * (newsize - oldsize));
894 *pv = (PyObject *) sv;
895 _PyObject_GC_TRACK(sv);
896 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000898
Christian Heimesa156e092008-02-16 07:38:31 +0000899int
900PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000903#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 int i;
905 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
906 PyTupleObject *p, *q;
907 p = free_list[i];
908 freelist_size += numfree[i];
909 free_list[i] = NULL;
910 numfree[i] = 0;
911 while (p) {
912 q = p;
913 p = (PyTupleObject *)(p->ob_item[0]);
914 PyObject_GC_Del(q);
915 }
916 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000917#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000919}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920
Christian Heimesa156e092008-02-16 07:38:31 +0000921void
922PyTuple_Fini(void)
923{
924#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 /* empty tuples are used all over the place and applications may
926 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200927 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000930#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000931#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000933#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000934}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000935
936/*********************** Tuple Iterator **************************/
937
938typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200940 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000942} tupleiterobject;
943
Raymond Hettinger48923c52002-08-09 01:30:17 +0000944static void
945tupleiter_dealloc(tupleiterobject *it)
946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 _PyObject_GC_UNTRACK(it);
948 Py_XDECREF(it->it_seq);
949 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000950}
951
952static int
953tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 Py_VISIT(it->it_seq);
956 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000957}
958
Raymond Hettinger48923c52002-08-09 01:30:17 +0000959static PyObject *
960tupleiter_next(tupleiterobject *it)
961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 PyTupleObject *seq;
963 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 assert(it != NULL);
966 seq = it->it_seq;
967 if (seq == NULL)
968 return NULL;
969 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 if (it->it_index < PyTuple_GET_SIZE(seq)) {
972 item = PyTuple_GET_ITEM(seq, it->it_index);
973 ++it->it_index;
974 Py_INCREF(item);
975 return item;
976 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300979 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000981}
982
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000983static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000984tupleiter_len(tupleiterobject *it)
985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_ssize_t len = 0;
987 if (it->it_seq)
988 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
989 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000990}
991
Armin Rigof5b3e362006-02-11 21:32:43 +0000992PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000993
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000994static PyObject *
995tupleiter_reduce(tupleiterobject *it)
996{
997 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +0200998 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000999 it->it_seq, it->it_index);
1000 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001001 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001002}
1003
1004static PyObject *
1005tupleiter_setstate(tupleiterobject *it, PyObject *state)
1006{
Victor Stinner7660b882013-06-24 23:59:24 +02001007 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001008 if (index == -1 && PyErr_Occurred())
1009 return NULL;
1010 if (it->it_seq != NULL) {
1011 if (index < 0)
1012 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001013 else if (index > PyTuple_GET_SIZE(it->it_seq))
1014 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001015 it->it_index = index;
1016 }
1017 Py_RETURN_NONE;
1018}
1019
1020PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1021PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1022
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001023static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001025 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1026 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001028};
1029
Raymond Hettinger48923c52002-08-09 01:30:17 +00001030PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1032 "tuple_iterator", /* tp_name */
1033 sizeof(tupleiterobject), /* tp_basicsize */
1034 0, /* tp_itemsize */
1035 /* methods */
1036 (destructor)tupleiter_dealloc, /* tp_dealloc */
1037 0, /* tp_print */
1038 0, /* tp_getattr */
1039 0, /* tp_setattr */
1040 0, /* tp_reserved */
1041 0, /* tp_repr */
1042 0, /* tp_as_number */
1043 0, /* tp_as_sequence */
1044 0, /* tp_as_mapping */
1045 0, /* tp_hash */
1046 0, /* tp_call */
1047 0, /* tp_str */
1048 PyObject_GenericGetAttr, /* tp_getattro */
1049 0, /* tp_setattro */
1050 0, /* tp_as_buffer */
1051 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1052 0, /* tp_doc */
1053 (traverseproc)tupleiter_traverse, /* tp_traverse */
1054 0, /* tp_clear */
1055 0, /* tp_richcompare */
1056 0, /* tp_weaklistoffset */
1057 PyObject_SelfIter, /* tp_iter */
1058 (iternextfunc)tupleiter_next, /* tp_iternext */
1059 tupleiter_methods, /* tp_methods */
1060 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001061};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001062
1063static PyObject *
1064tuple_iter(PyObject *seq)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!PyTuple_Check(seq)) {
1069 PyErr_BadInternalCall();
1070 return NULL;
1071 }
1072 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1073 if (it == NULL)
1074 return NULL;
1075 it->it_index = 0;
1076 Py_INCREF(seq);
1077 it->it_seq = (PyTupleObject *)seq;
1078 _PyObject_GC_TRACK(it);
1079 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080}