blob: e45462b9cdc3fbada9bdffa1e9fbbb30e1b66bc6 [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 *olditem;
153 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
155 Py_XDECREF(newitem);
156 PyErr_BadInternalCall();
157 return -1;
158 }
159 if (i < 0 || i >= Py_SIZE(op)) {
160 Py_XDECREF(newitem);
161 PyErr_SetString(PyExc_IndexError,
162 "tuple assignment index out of range");
163 return -1;
164 }
165 p = ((PyTupleObject *)op) -> ob_item + i;
166 olditem = *p;
167 *p = newitem;
168 Py_XDECREF(olditem);
169 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000172void
173_PyTuple_MaybeUntrack(PyObject *op)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 PyTupleObject *t;
176 Py_ssize_t i, n;
177
178 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
179 return;
180 t = (PyTupleObject *) op;
181 n = Py_SIZE(t);
182 for (i = 0; i < n; i++) {
183 PyObject *elt = PyTuple_GET_ITEM(t, i);
184 /* Tuple with NULL elements aren't
185 fully constructed, don't untrack
186 them yet. */
187 if (!elt ||
188 _PyObject_GC_MAY_BE_TRACKED(elt))
189 return;
190 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000191#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 count_tracked--;
193 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000194#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000196}
197
Raymond Hettingercb2da432003-10-12 18:24:34 +0000198PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 Py_ssize_t i;
202 PyObject *o;
203 PyObject *result;
204 PyObject **items;
205 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 va_start(vargs, n);
208 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200209 if (result == NULL) {
210 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200212 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 items = ((PyTupleObject *)result)->ob_item;
214 for (i = 0; i < n; i++) {
215 o = va_arg(vargs, PyObject *);
216 Py_INCREF(o);
217 items[i] = o;
218 }
219 va_end(vargs);
220 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000221}
222
223
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224/* Methods */
225
226static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200227tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200229 Py_ssize_t i;
230 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 PyObject_GC_UnTrack(op);
232 Py_TRASHCAN_SAFE_BEGIN(op)
233 if (len > 0) {
234 i = len;
235 while (--i >= 0)
236 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000237#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (len < PyTuple_MAXSAVESIZE &&
239 numfree[len] < PyTuple_MAXFREELIST &&
240 Py_TYPE(op) == &PyTuple_Type)
241 {
242 op->ob_item[0] = (PyObject *) free_list[len];
243 numfree[len]++;
244 free_list[len] = op;
245 goto done; /* return */
246 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 }
249 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000250done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000255tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100258 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 n = Py_SIZE(v);
261 if (n == 0)
262 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* While not mutable, it is still possible to end up with a cycle in a
265 tuple through an object that stores itself within a tuple (and thus
266 infinitely asks for the repr of itself). This should only be
267 possible within a type. */
268 i = Py_ReprEnter((PyObject *)v);
269 if (i != 0) {
270 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
271 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000272
Victor Stinner88a9cd92013-11-19 12:59:46 +0100273 _PyUnicodeWriter_Init(&writer);
274 writer.overallocate = 1;
275 if (Py_SIZE(v) > 1) {
276 /* "(" + "1" + ", 2" * (len - 1) + ")" */
277 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
278 }
279 else {
280 /* "(1,)" */
281 writer.min_length = 4;
282 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200283
Victor Stinner88a9cd92013-11-19 12:59:46 +0100284 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200285 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Do repr() on each element. */
288 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100289 PyObject *s;
290
291 if (i > 0) {
292 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
293 goto error;
294 }
295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200297 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 s = PyObject_Repr(v->ob_item[i]);
299 Py_LeaveRecursiveCall();
Victor Stinner88a9cd92013-11-19 12:59:46 +0100300 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200301 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100302
303 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
304 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200305 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100306 }
307 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100309
310 writer.overallocate = 0;
311 if (n > 1) {
312 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
313 goto error;
314 }
315 else {
316 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
317 goto error;
318 }
Tim Petersa7259592001-06-16 05:11:17 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100321 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200322
323error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100324 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200325 Py_ReprLeave((PyObject *)v);
326 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327}
328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329/* The addend 82520, was selected from the range(0, 1000000) for
330 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000331 upto length eight:
332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000334 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100335
336 Tests have shown that it's not worth to cache the hash value, see
337 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000338*/
339
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000340static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000341tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000342{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200343 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
344 Py_hash_t y;
345 Py_ssize_t len = Py_SIZE(v);
346 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800347 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800348 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 p = v->ob_item;
350 while (--len >= 0) {
351 y = PyObject_Hash(*p++);
352 if (y == -1)
353 return -1;
354 x = (x ^ y) * mult;
355 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800356 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800358 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100359 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 x = -2;
361 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000362}
363
Martin v. Löwis18e16552006-02-15 17:27:45 +0000364static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000365tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368}
369
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000370static int
Fred Drakeba096332000-07-09 07:04:36 +0000371tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 Py_ssize_t i;
374 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
377 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
378 Py_EQ);
379 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000380}
381
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200383tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (i < 0 || i >= Py_SIZE(a)) {
386 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
387 return NULL;
388 }
389 Py_INCREF(a->ob_item[i]);
390 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391}
392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200394tupleslice(PyTupleObject *a, Py_ssize_t ilow,
395 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200397 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200399 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 Py_ssize_t len;
401 if (ilow < 0)
402 ilow = 0;
403 if (ihigh > Py_SIZE(a))
404 ihigh = Py_SIZE(a);
405 if (ihigh < ilow)
406 ihigh = ilow;
407 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
408 Py_INCREF(a);
409 return (PyObject *)a;
410 }
411 len = ihigh - ilow;
412 np = (PyTupleObject *)PyTuple_New(len);
413 if (np == NULL)
414 return NULL;
415 src = a->ob_item + ilow;
416 dest = np->ob_item;
417 for (i = 0; i < len; i++) {
418 PyObject *v = src[i];
419 Py_INCREF(v);
420 dest[i] = v;
421 }
422 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423}
424
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000426PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (op == NULL || !PyTuple_Check(op)) {
429 PyErr_BadInternalCall();
430 return NULL;
431 }
432 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000433}
434
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200436tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200438 Py_ssize_t size;
439 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 PyObject **src, **dest;
441 PyTupleObject *np;
442 if (!PyTuple_Check(bb)) {
443 PyErr_Format(PyExc_TypeError,
444 "can only concatenate tuple (not \"%.200s\") to tuple",
445 Py_TYPE(bb)->tp_name);
446 return NULL;
447 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 size = Py_SIZE(a) + Py_SIZE(b);
450 if (size < 0)
451 return PyErr_NoMemory();
452 np = (PyTupleObject *) PyTuple_New(size);
453 if (np == NULL) {
454 return NULL;
455 }
456 src = a->ob_item;
457 dest = np->ob_item;
458 for (i = 0; i < Py_SIZE(a); i++) {
459 PyObject *v = src[i];
460 Py_INCREF(v);
461 dest[i] = v;
462 }
463 src = b->ob_item;
464 dest = np->ob_item + Py_SIZE(a);
465 for (i = 0; i < Py_SIZE(b); i++) {
466 PyObject *v = src[i];
467 Py_INCREF(v);
468 dest[i] = v;
469 }
470 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471#undef b
472}
473
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 Py_ssize_t i, j;
478 Py_ssize_t size;
479 PyTupleObject *np;
480 PyObject **p, **items;
481 if (n < 0)
482 n = 0;
483 if (Py_SIZE(a) == 0 || n == 1) {
484 if (PyTuple_CheckExact(a)) {
485 /* Since tuples are immutable, we can return a shared
486 copy in this case */
487 Py_INCREF(a);
488 return (PyObject *)a;
489 }
490 if (Py_SIZE(a) == 0)
491 return PyTuple_New(0);
492 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100493 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100495 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 np = (PyTupleObject *) PyTuple_New(size);
497 if (np == NULL)
498 return NULL;
499 p = np->ob_item;
500 items = a->ob_item;
501 for (i = 0; i < n; i++) {
502 for (j = 0; j < Py_SIZE(a); j++) {
503 *p = items[j];
504 Py_INCREF(*p);
505 p++;
506 }
507 }
508 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000509}
510
Raymond Hettinger65baa342008-02-07 00:41:02 +0000511static PyObject *
512tupleindex(PyTupleObject *self, PyObject *args)
513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200515 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000516
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200517 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
518 _PyEval_SliceIndex, &start,
519 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return NULL;
521 if (start < 0) {
522 start += Py_SIZE(self);
523 if (start < 0)
524 start = 0;
525 }
526 if (stop < 0) {
527 stop += Py_SIZE(self);
528 if (stop < 0)
529 stop = 0;
530 }
531 for (i = start; i < stop && i < Py_SIZE(self); i++) {
532 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
533 if (cmp > 0)
534 return PyLong_FromSsize_t(i);
535 else if (cmp < 0)
536 return NULL;
537 }
538 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
539 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000540}
541
542static PyObject *
543tuplecount(PyTupleObject *self, PyObject *v)
544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_ssize_t count = 0;
546 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 for (i = 0; i < Py_SIZE(self); i++) {
549 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
550 if (cmp > 0)
551 count++;
552 else if (cmp < 0)
553 return NULL;
554 }
555 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000556}
557
Jeremy Hylton8caad492000-06-23 14:18:11 +0000558static int
559tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 for (i = Py_SIZE(o); --i >= 0; )
564 Py_VISIT(o->ob_item[i]);
565 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000566}
567
Guido van Rossumf77bc622001-01-18 00:00:53 +0000568static PyObject *
569tuplerichcompare(PyObject *v, PyObject *w, int op)
570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 PyTupleObject *vt, *wt;
572 Py_ssize_t i;
573 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000574
Brian Curtindfc80e32011-08-10 20:28:54 -0500575 if (!PyTuple_Check(v) || !PyTuple_Check(w))
576 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 vt = (PyTupleObject *)v;
579 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 vlen = Py_SIZE(vt);
582 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 /* Note: the corresponding code for lists has an "early out" test
585 * here when op is EQ or NE and the lengths differ. That pays there,
586 * but Tim was unable to find any real code where EQ/NE tuple
587 * compares don't have the same length, so testing for it here would
588 * have cost without benefit.
589 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Search for the first index where items are different.
592 * Note that because tuples are immutable, it's safe to reuse
593 * vlen and wlen across the comparison calls.
594 */
595 for (i = 0; i < vlen && i < wlen; i++) {
596 int k = PyObject_RichCompareBool(vt->ob_item[i],
597 wt->ob_item[i], Py_EQ);
598 if (k < 0)
599 return NULL;
600 if (!k)
601 break;
602 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 if (i >= vlen || i >= wlen) {
605 /* No more items to compare -- compare sizes */
606 int cmp;
607 PyObject *res;
608 switch (op) {
609 case Py_LT: cmp = vlen < wlen; break;
610 case Py_LE: cmp = vlen <= wlen; break;
611 case Py_EQ: cmp = vlen == wlen; break;
612 case Py_NE: cmp = vlen != wlen; break;
613 case Py_GT: cmp = vlen > wlen; break;
614 case Py_GE: cmp = vlen >= wlen; break;
615 default: return NULL; /* cannot happen */
616 }
617 if (cmp)
618 res = Py_True;
619 else
620 res = Py_False;
621 Py_INCREF(res);
622 return res;
623 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 /* We have an item that differs -- shortcuts for EQ/NE */
626 if (op == Py_EQ) {
627 Py_INCREF(Py_False);
628 return Py_False;
629 }
630 if (op == Py_NE) {
631 Py_INCREF(Py_True);
632 return Py_True;
633 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 /* Compare the final item again using the proper operator */
636 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000637}
638
Jeremy Hylton938ace62002-07-17 16:30:39 +0000639static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000640tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
641
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642static PyObject *
643tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 PyObject *arg = NULL;
646 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 if (type != &PyTuple_Type)
649 return tuple_subtype_new(type, args, kwds);
650 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
651 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (arg == NULL)
654 return PyTuple_New(0);
655 else
656 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000657}
658
Guido van Rossumae960af2001-08-30 03:11:59 +0000659static PyObject *
660tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyObject *tmp, *newobj, *item;
663 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 assert(PyType_IsSubtype(type, &PyTuple_Type));
666 tmp = tuple_new(&PyTuple_Type, args, kwds);
667 if (tmp == NULL)
668 return NULL;
669 assert(PyTuple_Check(tmp));
670 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
671 if (newobj == NULL)
672 return NULL;
673 for (i = 0; i < n; i++) {
674 item = PyTuple_GET_ITEM(tmp, i);
675 Py_INCREF(item);
676 PyTuple_SET_ITEM(newobj, i, item);
677 }
678 Py_DECREF(tmp);
679 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000680}
681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000683"tuple() -> empty tuple\n\
684tuple(iterable) -> tuple initialized from iterable's items\n\
685\n\
686If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000687
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 (lenfunc)tuplelength, /* sq_length */
690 (binaryfunc)tupleconcat, /* sq_concat */
691 (ssizeargfunc)tuplerepeat, /* sq_repeat */
692 (ssizeargfunc)tupleitem, /* sq_item */
693 0, /* sq_slice */
694 0, /* sq_ass_item */
695 0, /* sq_ass_slice */
696 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697};
698
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000699static PyObject*
700tuplesubscript(PyTupleObject* self, PyObject* item)
701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if (PyIndex_Check(item)) {
703 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
704 if (i == -1 && PyErr_Occurred())
705 return NULL;
706 if (i < 0)
707 i += PyTuple_GET_SIZE(self);
708 return tupleitem(self, i);
709 }
710 else if (PySlice_Check(item)) {
711 Py_ssize_t start, stop, step, slicelength, cur, i;
712 PyObject* result;
713 PyObject* it;
714 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000715
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000716 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 PyTuple_GET_SIZE(self),
718 &start, &stop, &step, &slicelength) < 0) {
719 return NULL;
720 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 if (slicelength <= 0) {
723 return PyTuple_New(0);
724 }
725 else if (start == 0 && step == 1 &&
726 slicelength == PyTuple_GET_SIZE(self) &&
727 PyTuple_CheckExact(self)) {
728 Py_INCREF(self);
729 return (PyObject *)self;
730 }
731 else {
732 result = PyTuple_New(slicelength);
733 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 src = self->ob_item;
736 dest = ((PyTupleObject *)result)->ob_item;
737 for (cur = start, i = 0; i < slicelength;
738 cur += step, i++) {
739 it = src[cur];
740 Py_INCREF(it);
741 dest[i] = it;
742 }
743
744 return result;
745 }
746 }
747 else {
748 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400749 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 Py_TYPE(item)->tp_name);
751 return NULL;
752 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000753}
754
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000755static PyObject *
756tuple_getnewargs(PyTupleObject *v)
757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
759
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000760}
761
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000762static PyObject *
763tuple_sizeof(PyTupleObject *self)
764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
768 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000769}
770
Raymond Hettinger65baa342008-02-07 00:41:02 +0000771PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000772"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
773"Raises ValueError if the value is not present."
774);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000775PyDoc_STRVAR(count_doc,
776"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000777PyDoc_STRVAR(sizeof_doc,
778"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000779
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},
782 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
783 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
784 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
785 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000786};
787
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000788static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 (lenfunc)tuplelength,
790 (binaryfunc)tuplesubscript,
791 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000792};
793
Raymond Hettinger48923c52002-08-09 01:30:17 +0000794static PyObject *tuple_iter(PyObject *seq);
795
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 PyVarObject_HEAD_INIT(&PyType_Type, 0)
798 "tuple",
799 sizeof(PyTupleObject) - sizeof(PyObject *),
800 sizeof(PyObject *),
801 (destructor)tupledealloc, /* tp_dealloc */
802 0, /* tp_print */
803 0, /* tp_getattr */
804 0, /* tp_setattr */
805 0, /* tp_reserved */
806 (reprfunc)tuplerepr, /* tp_repr */
807 0, /* tp_as_number */
808 &tuple_as_sequence, /* tp_as_sequence */
809 &tuple_as_mapping, /* tp_as_mapping */
810 (hashfunc)tuplehash, /* tp_hash */
811 0, /* tp_call */
812 0, /* tp_str */
813 PyObject_GenericGetAttr, /* tp_getattro */
814 0, /* tp_setattro */
815 0, /* tp_as_buffer */
816 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
817 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
818 tuple_doc, /* tp_doc */
819 (traverseproc)tupletraverse, /* tp_traverse */
820 0, /* tp_clear */
821 tuplerichcompare, /* tp_richcompare */
822 0, /* tp_weaklistoffset */
823 tuple_iter, /* tp_iter */
824 0, /* tp_iternext */
825 tuple_methods, /* tp_methods */
826 0, /* tp_members */
827 0, /* tp_getset */
828 0, /* tp_base */
829 0, /* tp_dict */
830 0, /* tp_descr_get */
831 0, /* tp_descr_set */
832 0, /* tp_dictoffset */
833 0, /* tp_init */
834 0, /* tp_alloc */
835 tuple_new, /* tp_new */
836 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000838
839/* The following function breaks the notion that tuples are immutable:
840 it changes the size of a tuple. We get away with this only if there
841 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000842 as creating a new tuple object and destroying the old one, only more
843 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000844 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000845
846int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000847_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200849 PyTupleObject *v;
850 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 Py_ssize_t i;
852 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 v = (PyTupleObject *) *pv;
855 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
856 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
857 *pv = 0;
858 Py_XDECREF(v);
859 PyErr_BadInternalCall();
860 return -1;
861 }
862 oldsize = Py_SIZE(v);
863 if (oldsize == newsize)
864 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (oldsize == 0) {
867 /* Empty tuples are often shared, so we should never
868 resize them in-place even if we do own the only
869 (current) reference */
870 Py_DECREF(v);
871 *pv = PyTuple_New(newsize);
872 return *pv == NULL ? -1 : 0;
873 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 /* XXX UNREF/NEWREF interface should be more symmetrical */
876 _Py_DEC_REFTOTAL;
877 if (_PyObject_GC_IS_TRACKED(v))
878 _PyObject_GC_UNTRACK(v);
879 _Py_ForgetReference((PyObject *) v);
880 /* DECREF items deleted by shrinkage */
881 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200882 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 }
884 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
885 if (sv == NULL) {
886 *pv = NULL;
887 PyObject_GC_Del(v);
888 return -1;
889 }
890 _Py_NewReference((PyObject *) sv);
891 /* Zero out items added by growing */
892 if (newsize > oldsize)
893 memset(&sv->ob_item[oldsize], 0,
894 sizeof(*sv->ob_item) * (newsize - oldsize));
895 *pv = (PyObject *) sv;
896 _PyObject_GC_TRACK(sv);
897 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000898}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000899
Christian Heimesa156e092008-02-16 07:38:31 +0000900int
901PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000904#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 int i;
906 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
907 PyTupleObject *p, *q;
908 p = free_list[i];
909 freelist_size += numfree[i];
910 free_list[i] = NULL;
911 numfree[i] = 0;
912 while (p) {
913 q = p;
914 p = (PyTupleObject *)(p->ob_item[0]);
915 PyObject_GC_Del(q);
916 }
917 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000918#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000920}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921
Christian Heimesa156e092008-02-16 07:38:31 +0000922void
923PyTuple_Fini(void)
924{
925#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 /* empty tuples are used all over the place and applications may
927 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200928 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000931#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000932#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000934#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000935}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000936
937/*********************** Tuple Iterator **************************/
938
939typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200941 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000943} tupleiterobject;
944
Raymond Hettinger48923c52002-08-09 01:30:17 +0000945static void
946tupleiter_dealloc(tupleiterobject *it)
947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 _PyObject_GC_UNTRACK(it);
949 Py_XDECREF(it->it_seq);
950 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000951}
952
953static int
954tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 Py_VISIT(it->it_seq);
957 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000958}
959
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960static PyObject *
961tupleiter_next(tupleiterobject *it)
962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PyTupleObject *seq;
964 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 assert(it != NULL);
967 seq = it->it_seq;
968 if (seq == NULL)
969 return NULL;
970 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (it->it_index < PyTuple_GET_SIZE(seq)) {
973 item = PyTuple_GET_ITEM(seq, it->it_index);
974 ++it->it_index;
975 Py_INCREF(item);
976 return item;
977 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 Py_DECREF(seq);
980 it->it_seq = NULL;
981 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000982}
983
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000984static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000985tupleiter_len(tupleiterobject *it)
986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 Py_ssize_t len = 0;
988 if (it->it_seq)
989 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
990 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000991}
992
Armin Rigof5b3e362006-02-11 21:32:43 +0000993PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000994
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000995static PyObject *
996tupleiter_reduce(tupleiterobject *it)
997{
998 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +0200999 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001000 it->it_seq, it->it_index);
1001 else
Antoine Pitroua7013882012-04-05 00:04:20 +02001002 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001003}
1004
1005static PyObject *
1006tupleiter_setstate(tupleiterobject *it, PyObject *state)
1007{
Victor Stinner7660b882013-06-24 23:59:24 +02001008 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001009 if (index == -1 && PyErr_Occurred())
1010 return NULL;
1011 if (it->it_seq != NULL) {
1012 if (index < 0)
1013 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001014 else if (index > PyTuple_GET_SIZE(it->it_seq))
1015 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001016 it->it_index = index;
1017 }
1018 Py_RETURN_NONE;
1019}
1020
1021PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1022PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1023
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001024static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001026 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1027 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001029};
1030
Raymond Hettinger48923c52002-08-09 01:30:17 +00001031PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1033 "tuple_iterator", /* tp_name */
1034 sizeof(tupleiterobject), /* tp_basicsize */
1035 0, /* tp_itemsize */
1036 /* methods */
1037 (destructor)tupleiter_dealloc, /* tp_dealloc */
1038 0, /* tp_print */
1039 0, /* tp_getattr */
1040 0, /* tp_setattr */
1041 0, /* tp_reserved */
1042 0, /* tp_repr */
1043 0, /* tp_as_number */
1044 0, /* tp_as_sequence */
1045 0, /* tp_as_mapping */
1046 0, /* tp_hash */
1047 0, /* tp_call */
1048 0, /* tp_str */
1049 PyObject_GenericGetAttr, /* tp_getattro */
1050 0, /* tp_setattro */
1051 0, /* tp_as_buffer */
1052 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1053 0, /* tp_doc */
1054 (traverseproc)tupleiter_traverse, /* tp_traverse */
1055 0, /* tp_clear */
1056 0, /* tp_richcompare */
1057 0, /* tp_weaklistoffset */
1058 PyObject_SelfIter, /* tp_iter */
1059 (iternextfunc)tupleiter_next, /* tp_iternext */
1060 tupleiter_methods, /* tp_methods */
1061 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001062};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001063
1064static PyObject *
1065tuple_iter(PyObject *seq)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (!PyTuple_Check(seq)) {
1070 PyErr_BadInternalCall();
1071 return NULL;
1072 }
1073 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1074 if (it == NULL)
1075 return NULL;
1076 it->it_index = 0;
1077 Py_INCREF(seq);
1078 it->it_seq = (PyTupleObject *)seq;
1079 _PyObject_GC_TRACK(it);
1080 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001081}