blob: 8e1b00b63d18e416654a42f67810066437fe66cf [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
40 count_tracked + count_untracked);
41 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
42 "d\n", count_tracked);
43 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
44 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000045}
46#endif
47
David Malcolm49526f42012-06-22 14:55:41 -040048/* Print summary info about the state of the optimized allocator */
49void
50_PyTuple_DebugMallocStats(FILE *out)
51{
52#if PyTuple_MAXSAVESIZE > 0
53 int i;
54 char buf[128];
55 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
56 PyOS_snprintf(buf, sizeof(buf),
57 "free %d-sized PyTupleObject", i);
58 _PyDebugAllocatorStats(out,
59 buf,
60 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
61 }
62#endif
63}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000064
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020066PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020068 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 Py_ssize_t i;
70 if (size < 0) {
71 PyErr_BadInternalCall();
72 return NULL;
73 }
Christian Heimes2202f872008-02-06 14:31:34 +000074#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (size == 0 && free_list[0]) {
76 op = free_list[0];
77 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000078#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 return (PyObject *) op;
82 }
83 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
84 free_list[size] = (PyTupleObject *) op->ob_item[0];
85 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000086#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000088#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000090#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 Py_SIZE(op) = size;
92 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 _Py_NewReference((PyObject *)op);
95 }
96 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000097#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200100 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100101 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 return PyErr_NoMemory();
103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
105 if (op == NULL)
106 return NULL;
107 }
108 for (i=0; i < size; i++)
109 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000110#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 if (size == 0) {
112 free_list[0] = op;
113 ++numfree[0];
114 Py_INCREF(op); /* extra INCREF so that this is never freed */
115 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000116#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000117#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000119#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 _PyObject_GC_TRACK(op);
121 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122}
123
Martin v. Löwis18e16552006-02-15 17:27:45 +0000124Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200125PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 if (!PyTuple_Check(op)) {
128 PyErr_BadInternalCall();
129 return -1;
130 }
131 else
132 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200136PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (!PyTuple_Check(op)) {
139 PyErr_BadInternalCall();
140 return NULL;
141 }
142 if (i < 0 || i >= Py_SIZE(op)) {
143 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
144 return NULL;
145 }
146 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147}
148
149int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200150PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200152 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
154 Py_XDECREF(newitem);
155 PyErr_BadInternalCall();
156 return -1;
157 }
158 if (i < 0 || i >= Py_SIZE(op)) {
159 Py_XDECREF(newitem);
160 PyErr_SetString(PyExc_IndexError,
161 "tuple assignment index out of range");
162 return -1;
163 }
164 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchaka1ed017a2015-12-27 15:51:32 +0200165 Py_SETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167}
168
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000169void
170_PyTuple_MaybeUntrack(PyObject *op)
171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyTupleObject *t;
173 Py_ssize_t i, n;
174
175 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
176 return;
177 t = (PyTupleObject *) op;
178 n = Py_SIZE(t);
179 for (i = 0; i < n; i++) {
180 PyObject *elt = PyTuple_GET_ITEM(t, i);
181 /* Tuple with NULL elements aren't
182 fully constructed, don't untrack
183 them yet. */
184 if (!elt ||
185 _PyObject_GC_MAY_BE_TRACKED(elt))
186 return;
187 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000188#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 count_tracked--;
190 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000191#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000193}
194
Raymond Hettingercb2da432003-10-12 18:24:34 +0000195PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000196PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 Py_ssize_t i;
199 PyObject *o;
200 PyObject *result;
201 PyObject **items;
202 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 va_start(vargs, n);
205 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200206 if (result == NULL) {
207 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200209 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 items = ((PyTupleObject *)result)->ob_item;
211 for (i = 0; i < n; i++) {
212 o = va_arg(vargs, PyObject *);
213 Py_INCREF(o);
214 items[i] = o;
215 }
216 va_end(vargs);
217 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000218}
219
220
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221/* Methods */
222
223static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200224tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200226 Py_ssize_t i;
227 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 PyObject_GC_UnTrack(op);
229 Py_TRASHCAN_SAFE_BEGIN(op)
230 if (len > 0) {
231 i = len;
232 while (--i >= 0)
233 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000234#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if (len < PyTuple_MAXSAVESIZE &&
236 numfree[len] < PyTuple_MAXFREELIST &&
237 Py_TYPE(op) == &PyTuple_Type)
238 {
239 op->ob_item[0] = (PyObject *) free_list[len];
240 numfree[len]++;
241 free_list[len] = op;
242 goto done; /* return */
243 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 }
246 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000247done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249}
250
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000252tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100255 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 n = Py_SIZE(v);
258 if (n == 0)
259 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* While not mutable, it is still possible to end up with a cycle in a
262 tuple through an object that stores itself within a tuple (and thus
263 infinitely asks for the repr of itself). This should only be
264 possible within a type. */
265 i = Py_ReprEnter((PyObject *)v);
266 if (i != 0) {
267 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
268 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000269
Victor Stinner88a9cd92013-11-19 12:59:46 +0100270 _PyUnicodeWriter_Init(&writer);
271 writer.overallocate = 1;
272 if (Py_SIZE(v) > 1) {
273 /* "(" + "1" + ", 2" * (len - 1) + ")" */
274 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
275 }
276 else {
277 /* "(1,)" */
278 writer.min_length = 4;
279 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200280
Victor Stinner88a9cd92013-11-19 12:59:46 +0100281 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200282 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 /* Do repr() on each element. */
285 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100286 PyObject *s;
287
288 if (i > 0) {
289 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
290 goto error;
291 }
292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200294 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 s = PyObject_Repr(v->ob_item[i]);
296 Py_LeaveRecursiveCall();
Victor Stinner88a9cd92013-11-19 12:59:46 +0100297 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200298 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100299
300 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
301 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200302 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100303 }
304 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100306
307 writer.overallocate = 0;
308 if (n > 1) {
309 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
310 goto error;
311 }
312 else {
313 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
314 goto error;
315 }
Tim Petersa7259592001-06-16 05:11:17 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100318 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200319
320error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100321 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200322 Py_ReprLeave((PyObject *)v);
323 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326/* The addend 82520, was selected from the range(0, 1000000) for
327 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000328 upto length eight:
329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000331 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100332
333 Tests have shown that it's not worth to cache the hash value, see
334 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000335*/
336
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000337static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000338tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000339{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200340 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
341 Py_hash_t y;
342 Py_ssize_t len = Py_SIZE(v);
343 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800344 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800345 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 p = v->ob_item;
347 while (--len >= 0) {
348 y = PyObject_Hash(*p++);
349 if (y == -1)
350 return -1;
351 x = (x ^ y) * mult;
352 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800353 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800355 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100356 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 x = -2;
358 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000359}
360
Martin v. Löwis18e16552006-02-15 17:27:45 +0000361static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000362tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365}
366
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000367static int
Fred Drakeba096332000-07-09 07:04:36 +0000368tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 Py_ssize_t i;
371 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
374 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
375 Py_EQ);
376 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000377}
378
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200380tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (i < 0 || i >= Py_SIZE(a)) {
383 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
384 return NULL;
385 }
386 Py_INCREF(a->ob_item[i]);
387 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388}
389
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200391tupleslice(PyTupleObject *a, Py_ssize_t ilow,
392 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200394 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200396 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_ssize_t len;
398 if (ilow < 0)
399 ilow = 0;
400 if (ihigh > Py_SIZE(a))
401 ihigh = Py_SIZE(a);
402 if (ihigh < ilow)
403 ihigh = ilow;
404 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
405 Py_INCREF(a);
406 return (PyObject *)a;
407 }
408 len = ihigh - ilow;
409 np = (PyTupleObject *)PyTuple_New(len);
410 if (np == NULL)
411 return NULL;
412 src = a->ob_item + ilow;
413 dest = np->ob_item;
414 for (i = 0; i < len; i++) {
415 PyObject *v = src[i];
416 Py_INCREF(v);
417 dest[i] = v;
418 }
419 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420}
421
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000423PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (op == NULL || !PyTuple_Check(op)) {
426 PyErr_BadInternalCall();
427 return NULL;
428 }
429 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000430}
431
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200433tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200435 Py_ssize_t size;
436 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 PyObject **src, **dest;
438 PyTupleObject *np;
439 if (!PyTuple_Check(bb)) {
440 PyErr_Format(PyExc_TypeError,
441 "can only concatenate tuple (not \"%.200s\") to tuple",
442 Py_TYPE(bb)->tp_name);
443 return NULL;
444 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 size = Py_SIZE(a) + Py_SIZE(b);
447 if (size < 0)
448 return PyErr_NoMemory();
449 np = (PyTupleObject *) PyTuple_New(size);
450 if (np == NULL) {
451 return NULL;
452 }
453 src = a->ob_item;
454 dest = np->ob_item;
455 for (i = 0; i < Py_SIZE(a); i++) {
456 PyObject *v = src[i];
457 Py_INCREF(v);
458 dest[i] = v;
459 }
460 src = b->ob_item;
461 dest = np->ob_item + Py_SIZE(a);
462 for (i = 0; i < Py_SIZE(b); i++) {
463 PyObject *v = src[i];
464 Py_INCREF(v);
465 dest[i] = v;
466 }
467 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468#undef b
469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 Py_ssize_t i, j;
475 Py_ssize_t size;
476 PyTupleObject *np;
477 PyObject **p, **items;
478 if (n < 0)
479 n = 0;
480 if (Py_SIZE(a) == 0 || n == 1) {
481 if (PyTuple_CheckExact(a)) {
482 /* Since tuples are immutable, we can return a shared
483 copy in this case */
484 Py_INCREF(a);
485 return (PyObject *)a;
486 }
487 if (Py_SIZE(a) == 0)
488 return PyTuple_New(0);
489 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100490 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100492 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 np = (PyTupleObject *) PyTuple_New(size);
494 if (np == NULL)
495 return NULL;
496 p = np->ob_item;
497 items = a->ob_item;
498 for (i = 0; i < n; i++) {
499 for (j = 0; j < Py_SIZE(a); j++) {
500 *p = items[j];
501 Py_INCREF(*p);
502 p++;
503 }
504 }
505 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000506}
507
Raymond Hettinger65baa342008-02-07 00:41:02 +0000508static PyObject *
509tupleindex(PyTupleObject *self, PyObject *args)
510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200512 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000513
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200514 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
515 _PyEval_SliceIndex, &start,
516 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 return NULL;
518 if (start < 0) {
519 start += Py_SIZE(self);
520 if (start < 0)
521 start = 0;
522 }
523 if (stop < 0) {
524 stop += Py_SIZE(self);
525 if (stop < 0)
526 stop = 0;
527 }
528 for (i = start; i < stop && i < Py_SIZE(self); i++) {
529 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
530 if (cmp > 0)
531 return PyLong_FromSsize_t(i);
532 else if (cmp < 0)
533 return NULL;
534 }
535 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
536 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000537}
538
539static PyObject *
540tuplecount(PyTupleObject *self, PyObject *v)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_ssize_t count = 0;
543 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 for (i = 0; i < Py_SIZE(self); i++) {
546 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
547 if (cmp > 0)
548 count++;
549 else if (cmp < 0)
550 return NULL;
551 }
552 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000553}
554
Jeremy Hylton8caad492000-06-23 14:18:11 +0000555static int
556tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 for (i = Py_SIZE(o); --i >= 0; )
561 Py_VISIT(o->ob_item[i]);
562 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000563}
564
Guido van Rossumf77bc622001-01-18 00:00:53 +0000565static PyObject *
566tuplerichcompare(PyObject *v, PyObject *w, int op)
567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 PyTupleObject *vt, *wt;
569 Py_ssize_t i;
570 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000571
Brian Curtindfc80e32011-08-10 20:28:54 -0500572 if (!PyTuple_Check(v) || !PyTuple_Check(w))
573 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 vt = (PyTupleObject *)v;
576 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 vlen = Py_SIZE(vt);
579 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 /* Note: the corresponding code for lists has an "early out" test
582 * here when op is EQ or NE and the lengths differ. That pays there,
583 * but Tim was unable to find any real code where EQ/NE tuple
584 * compares don't have the same length, so testing for it here would
585 * have cost without benefit.
586 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 /* Search for the first index where items are different.
589 * Note that because tuples are immutable, it's safe to reuse
590 * vlen and wlen across the comparison calls.
591 */
592 for (i = 0; i < vlen && i < wlen; i++) {
593 int k = PyObject_RichCompareBool(vt->ob_item[i],
594 wt->ob_item[i], Py_EQ);
595 if (k < 0)
596 return NULL;
597 if (!k)
598 break;
599 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if (i >= vlen || i >= wlen) {
602 /* No more items to compare -- compare sizes */
603 int cmp;
604 PyObject *res;
605 switch (op) {
606 case Py_LT: cmp = vlen < wlen; break;
607 case Py_LE: cmp = vlen <= wlen; break;
608 case Py_EQ: cmp = vlen == wlen; break;
609 case Py_NE: cmp = vlen != wlen; break;
610 case Py_GT: cmp = vlen > wlen; break;
611 case Py_GE: cmp = vlen >= wlen; break;
612 default: return NULL; /* cannot happen */
613 }
614 if (cmp)
615 res = Py_True;
616 else
617 res = Py_False;
618 Py_INCREF(res);
619 return res;
620 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* We have an item that differs -- shortcuts for EQ/NE */
623 if (op == Py_EQ) {
624 Py_INCREF(Py_False);
625 return Py_False;
626 }
627 if (op == Py_NE) {
628 Py_INCREF(Py_True);
629 return Py_True;
630 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* Compare the final item again using the proper operator */
633 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000634}
635
Jeremy Hylton938ace62002-07-17 16:30:39 +0000636static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000637tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
638
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639static PyObject *
640tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyObject *arg = NULL;
643 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 if (type != &PyTuple_Type)
646 return tuple_subtype_new(type, args, kwds);
647 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
648 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (arg == NULL)
651 return PyTuple_New(0);
652 else
653 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000654}
655
Guido van Rossumae960af2001-08-30 03:11:59 +0000656static PyObject *
657tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 PyObject *tmp, *newobj, *item;
660 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 assert(PyType_IsSubtype(type, &PyTuple_Type));
663 tmp = tuple_new(&PyTuple_Type, args, kwds);
664 if (tmp == NULL)
665 return NULL;
666 assert(PyTuple_Check(tmp));
667 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
668 if (newobj == NULL)
669 return NULL;
670 for (i = 0; i < n; i++) {
671 item = PyTuple_GET_ITEM(tmp, i);
672 Py_INCREF(item);
673 PyTuple_SET_ITEM(newobj, i, item);
674 }
675 Py_DECREF(tmp);
676 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000677}
678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000679PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000680"tuple() -> empty tuple\n\
681tuple(iterable) -> tuple initialized from iterable's items\n\
682\n\
683If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000684
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 (lenfunc)tuplelength, /* sq_length */
687 (binaryfunc)tupleconcat, /* sq_concat */
688 (ssizeargfunc)tuplerepeat, /* sq_repeat */
689 (ssizeargfunc)tupleitem, /* sq_item */
690 0, /* sq_slice */
691 0, /* sq_ass_item */
692 0, /* sq_ass_slice */
693 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694};
695
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000696static PyObject*
697tuplesubscript(PyTupleObject* self, PyObject* item)
698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (PyIndex_Check(item)) {
700 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
701 if (i == -1 && PyErr_Occurred())
702 return NULL;
703 if (i < 0)
704 i += PyTuple_GET_SIZE(self);
705 return tupleitem(self, i);
706 }
707 else if (PySlice_Check(item)) {
708 Py_ssize_t start, stop, step, slicelength, cur, i;
709 PyObject* result;
710 PyObject* it;
711 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000712
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000713 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyTuple_GET_SIZE(self),
715 &start, &stop, &step, &slicelength) < 0) {
716 return NULL;
717 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 if (slicelength <= 0) {
720 return PyTuple_New(0);
721 }
722 else if (start == 0 && step == 1 &&
723 slicelength == PyTuple_GET_SIZE(self) &&
724 PyTuple_CheckExact(self)) {
725 Py_INCREF(self);
726 return (PyObject *)self;
727 }
728 else {
729 result = PyTuple_New(slicelength);
730 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 src = self->ob_item;
733 dest = ((PyTupleObject *)result)->ob_item;
734 for (cur = start, i = 0; i < slicelength;
735 cur += step, i++) {
736 it = src[cur];
737 Py_INCREF(it);
738 dest[i] = it;
739 }
740
741 return result;
742 }
743 }
744 else {
745 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400746 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 Py_TYPE(item)->tp_name);
748 return NULL;
749 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000750}
751
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000752static PyObject *
753tuple_getnewargs(PyTupleObject *v)
754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
756
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000757}
758
Raymond Hettinger65baa342008-02-07 00:41:02 +0000759PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000760"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
761"Raises ValueError if the value is not present."
762);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000763PyDoc_STRVAR(count_doc,
764"T.count(value) -> integer -- return number of occurrences of value");
765
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000766static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
769 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
770 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000771};
772
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000773static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 (lenfunc)tuplelength,
775 (binaryfunc)tuplesubscript,
776 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000777};
778
Raymond Hettinger48923c52002-08-09 01:30:17 +0000779static PyObject *tuple_iter(PyObject *seq);
780
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyVarObject_HEAD_INIT(&PyType_Type, 0)
783 "tuple",
784 sizeof(PyTupleObject) - sizeof(PyObject *),
785 sizeof(PyObject *),
786 (destructor)tupledealloc, /* tp_dealloc */
787 0, /* tp_print */
788 0, /* tp_getattr */
789 0, /* tp_setattr */
790 0, /* tp_reserved */
791 (reprfunc)tuplerepr, /* tp_repr */
792 0, /* tp_as_number */
793 &tuple_as_sequence, /* tp_as_sequence */
794 &tuple_as_mapping, /* tp_as_mapping */
795 (hashfunc)tuplehash, /* tp_hash */
796 0, /* tp_call */
797 0, /* tp_str */
798 PyObject_GenericGetAttr, /* tp_getattro */
799 0, /* tp_setattro */
800 0, /* tp_as_buffer */
801 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
802 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
803 tuple_doc, /* tp_doc */
804 (traverseproc)tupletraverse, /* tp_traverse */
805 0, /* tp_clear */
806 tuplerichcompare, /* tp_richcompare */
807 0, /* tp_weaklistoffset */
808 tuple_iter, /* tp_iter */
809 0, /* tp_iternext */
810 tuple_methods, /* tp_methods */
811 0, /* tp_members */
812 0, /* tp_getset */
813 0, /* tp_base */
814 0, /* tp_dict */
815 0, /* tp_descr_get */
816 0, /* tp_descr_set */
817 0, /* tp_dictoffset */
818 0, /* tp_init */
819 0, /* tp_alloc */
820 tuple_new, /* tp_new */
821 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000823
824/* The following function breaks the notion that tuples are immutable:
825 it changes the size of a tuple. We get away with this only if there
826 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000827 as creating a new tuple object and destroying the old one, only more
828 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000829 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000830
831int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000832_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000833{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200834 PyTupleObject *v;
835 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 Py_ssize_t i;
837 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 v = (PyTupleObject *) *pv;
840 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
841 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
842 *pv = 0;
843 Py_XDECREF(v);
844 PyErr_BadInternalCall();
845 return -1;
846 }
847 oldsize = Py_SIZE(v);
848 if (oldsize == newsize)
849 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 if (oldsize == 0) {
852 /* Empty tuples are often shared, so we should never
853 resize them in-place even if we do own the only
854 (current) reference */
855 Py_DECREF(v);
856 *pv = PyTuple_New(newsize);
857 return *pv == NULL ? -1 : 0;
858 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* XXX UNREF/NEWREF interface should be more symmetrical */
861 _Py_DEC_REFTOTAL;
862 if (_PyObject_GC_IS_TRACKED(v))
863 _PyObject_GC_UNTRACK(v);
864 _Py_ForgetReference((PyObject *) v);
865 /* DECREF items deleted by shrinkage */
866 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200867 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
869 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
870 if (sv == NULL) {
871 *pv = NULL;
872 PyObject_GC_Del(v);
873 return -1;
874 }
875 _Py_NewReference((PyObject *) sv);
876 /* Zero out items added by growing */
877 if (newsize > oldsize)
878 memset(&sv->ob_item[oldsize], 0,
879 sizeof(*sv->ob_item) * (newsize - oldsize));
880 *pv = (PyObject *) sv;
881 _PyObject_GC_TRACK(sv);
882 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000883}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000884
Christian Heimesa156e092008-02-16 07:38:31 +0000885int
886PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000889#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 int i;
891 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
892 PyTupleObject *p, *q;
893 p = free_list[i];
894 freelist_size += numfree[i];
895 free_list[i] = NULL;
896 numfree[i] = 0;
897 while (p) {
898 q = p;
899 p = (PyTupleObject *)(p->ob_item[0]);
900 PyObject_GC_Del(q);
901 }
902 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000903#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000905}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906
Christian Heimesa156e092008-02-16 07:38:31 +0000907void
908PyTuple_Fini(void)
909{
910#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* empty tuples are used all over the place and applications may
912 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200913 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000916#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000917#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000919#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000920}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000921
922/*********************** Tuple Iterator **************************/
923
924typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200926 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000928} tupleiterobject;
929
Raymond Hettinger48923c52002-08-09 01:30:17 +0000930static void
931tupleiter_dealloc(tupleiterobject *it)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 _PyObject_GC_UNTRACK(it);
934 Py_XDECREF(it->it_seq);
935 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000936}
937
938static int
939tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 Py_VISIT(it->it_seq);
942 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000943}
944
Raymond Hettinger48923c52002-08-09 01:30:17 +0000945static PyObject *
946tupleiter_next(tupleiterobject *it)
947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyTupleObject *seq;
949 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 assert(it != NULL);
952 seq = it->it_seq;
953 if (seq == NULL)
954 return NULL;
955 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 if (it->it_index < PyTuple_GET_SIZE(seq)) {
958 item = PyTuple_GET_ITEM(seq, it->it_index);
959 ++it->it_index;
960 Py_INCREF(item);
961 return item;
962 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 Py_DECREF(seq);
965 it->it_seq = NULL;
966 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000967}
968
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000969static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000970tupleiter_len(tupleiterobject *it)
971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 Py_ssize_t len = 0;
973 if (it->it_seq)
974 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
975 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000976}
977
Armin Rigof5b3e362006-02-11 21:32:43 +0000978PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000979
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000980static PyObject *
981tupleiter_reduce(tupleiterobject *it)
982{
983 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +0200984 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000985 it->it_seq, it->it_index);
986 else
Antoine Pitroua7013882012-04-05 00:04:20 +0200987 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000988}
989
990static PyObject *
991tupleiter_setstate(tupleiterobject *it, PyObject *state)
992{
Victor Stinner7660b882013-06-24 23:59:24 +0200993 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000994 if (index == -1 && PyErr_Occurred())
995 return NULL;
996 if (it->it_seq != NULL) {
997 if (index < 0)
998 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +0000999 else if (index > PyTuple_GET_SIZE(it->it_seq))
1000 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001001 it->it_index = index;
1002 }
1003 Py_RETURN_NONE;
1004}
1005
1006PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1007PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1008
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001009static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001011 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1012 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001014};
1015
Raymond Hettinger48923c52002-08-09 01:30:17 +00001016PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1018 "tuple_iterator", /* tp_name */
1019 sizeof(tupleiterobject), /* tp_basicsize */
1020 0, /* tp_itemsize */
1021 /* methods */
1022 (destructor)tupleiter_dealloc, /* tp_dealloc */
1023 0, /* tp_print */
1024 0, /* tp_getattr */
1025 0, /* tp_setattr */
1026 0, /* tp_reserved */
1027 0, /* tp_repr */
1028 0, /* tp_as_number */
1029 0, /* tp_as_sequence */
1030 0, /* tp_as_mapping */
1031 0, /* tp_hash */
1032 0, /* tp_call */
1033 0, /* tp_str */
1034 PyObject_GenericGetAttr, /* tp_getattro */
1035 0, /* tp_setattro */
1036 0, /* tp_as_buffer */
1037 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1038 0, /* tp_doc */
1039 (traverseproc)tupleiter_traverse, /* tp_traverse */
1040 0, /* tp_clear */
1041 0, /* tp_richcompare */
1042 0, /* tp_weaklistoffset */
1043 PyObject_SelfIter, /* tp_iter */
1044 (iternextfunc)tupleiter_next, /* tp_iternext */
1045 tupleiter_methods, /* tp_methods */
1046 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001047};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001048
1049static PyObject *
1050tuple_iter(PyObject *seq)
1051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (!PyTuple_Check(seq)) {
1055 PyErr_BadInternalCall();
1056 return NULL;
1057 }
1058 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1059 if (it == NULL)
1060 return NULL;
1061 it->it_index = 0;
1062 Py_INCREF(seq);
1063 it->it_seq = (PyTupleObject *)seq;
1064 _PyObject_GC_TRACK(it);
1065 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066}