blob: b3454600c9ba087f34fbf51113a2d567a222ac7a [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
48
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000050PyTuple_New(register Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 register PyTupleObject *op;
53 Py_ssize_t i;
54 if (size < 0) {
55 PyErr_BadInternalCall();
56 return NULL;
57 }
Christian Heimes2202f872008-02-06 14:31:34 +000058#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 if (size == 0 && free_list[0]) {
60 op = free_list[0];
61 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000062#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000064#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return (PyObject *) op;
66 }
67 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
68 free_list[size] = (PyTupleObject *) op->ob_item[0];
69 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000070#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000072#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000074#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 Py_SIZE(op) = size;
76 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000077#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 _Py_NewReference((PyObject *)op);
79 }
80 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000081#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 {
83 Py_ssize_t nbytes = size * sizeof(PyObject *);
84 /* Check for overflow */
85 if (nbytes / sizeof(PyObject *) != (size_t)size ||
86 (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
87 {
88 return PyErr_NoMemory();
89 }
90 nbytes += sizeof(PyTupleObject) - sizeof(PyObject *);
Neal Norwitz3ce5d922008-08-24 07:08:55 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
93 if (op == NULL)
94 return NULL;
95 }
96 for (i=0; i < size; i++)
97 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +000098#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (size == 0) {
100 free_list[0] = op;
101 ++numfree[0];
102 Py_INCREF(op); /* extra INCREF so that this is never freed */
103 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000104#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000105#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 _PyObject_GC_TRACK(op);
109 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110}
111
Martin v. Löwis18e16552006-02-15 17:27:45 +0000112Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000113PyTuple_Size(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (!PyTuple_Check(op)) {
116 PyErr_BadInternalCall();
117 return -1;
118 }
119 else
120 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000123PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000124PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (!PyTuple_Check(op)) {
127 PyErr_BadInternalCall();
128 return NULL;
129 }
130 if (i < 0 || i >= Py_SIZE(op)) {
131 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
132 return NULL;
133 }
134 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
137int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 register PyObject *olditem;
141 register PyObject **p;
142 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
143 Py_XDECREF(newitem);
144 PyErr_BadInternalCall();
145 return -1;
146 }
147 if (i < 0 || i >= Py_SIZE(op)) {
148 Py_XDECREF(newitem);
149 PyErr_SetString(PyExc_IndexError,
150 "tuple assignment index out of range");
151 return -1;
152 }
153 p = ((PyTupleObject *)op) -> ob_item + i;
154 olditem = *p;
155 *p = newitem;
156 Py_XDECREF(olditem);
157 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}
159
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000160void
161_PyTuple_MaybeUntrack(PyObject *op)
162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 PyTupleObject *t;
164 Py_ssize_t i, n;
165
166 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
167 return;
168 t = (PyTupleObject *) op;
169 n = Py_SIZE(t);
170 for (i = 0; i < n; i++) {
171 PyObject *elt = PyTuple_GET_ITEM(t, i);
172 /* Tuple with NULL elements aren't
173 fully constructed, don't untrack
174 them yet. */
175 if (!elt ||
176 _PyObject_GC_MAY_BE_TRACKED(elt))
177 return;
178 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000179#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 count_tracked--;
181 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000182#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000184}
185
Raymond Hettingercb2da432003-10-12 18:24:34 +0000186PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 Py_ssize_t i;
190 PyObject *o;
191 PyObject *result;
192 PyObject **items;
193 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 va_start(vargs, n);
196 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200197 if (result == NULL) {
198 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 items = ((PyTupleObject *)result)->ob_item;
202 for (i = 0; i < n; i++) {
203 o = va_arg(vargs, PyObject *);
204 Py_INCREF(o);
205 items[i] = o;
206 }
207 va_end(vargs);
208 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000209}
210
211
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212/* Methods */
213
214static void
Fred Drakeba096332000-07-09 07:04:36 +0000215tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 register Py_ssize_t i;
218 register Py_ssize_t len = Py_SIZE(op);
219 PyObject_GC_UnTrack(op);
220 Py_TRASHCAN_SAFE_BEGIN(op)
221 if (len > 0) {
222 i = len;
223 while (--i >= 0)
224 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000225#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (len < PyTuple_MAXSAVESIZE &&
227 numfree[len] < PyTuple_MAXFREELIST &&
228 Py_TYPE(op) == &PyTuple_Type)
229 {
230 op->ob_item[0] = (PyObject *) free_list[len];
231 numfree[len]++;
232 free_list[len] = op;
233 goto done; /* return */
234 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 }
237 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000238done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240}
241
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000243tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 Py_ssize_t i, n;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200246 PyObject *s = NULL;
247 _PyAccu acc;
248 static PyObject *sep = NULL;
Tim Petersa7259592001-06-16 05:11:17 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 n = Py_SIZE(v);
251 if (n == 0)
252 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000253
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200254 if (sep == NULL) {
255 sep = PyUnicode_FromString(", ");
256 if (sep == NULL)
257 return NULL;
258 }
259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* While not mutable, it is still possible to end up with a cycle in a
261 tuple through an object that stores itself within a tuple (and thus
262 infinitely asks for the repr of itself). This should only be
263 possible within a type. */
264 i = Py_ReprEnter((PyObject *)v);
265 if (i != 0) {
266 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
267 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000268
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200269 if (_PyAccu_Init(&acc))
270 goto error;
271
272 s = PyUnicode_FromString("(");
273 if (s == NULL || _PyAccu_Accumulate(&acc, s))
274 goto error;
275 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 /* Do repr() on each element. */
278 for (i = 0; i < n; ++i) {
279 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 s = PyObject_Repr(v->ob_item[i]);
282 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200283 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
284 goto error;
285 if (s == NULL || _PyAccu_Accumulate(&acc, s))
286 goto error;
287 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200289 if (n > 1)
290 s = PyUnicode_FromString(")");
291 else
292 s = PyUnicode_FromString(",)");
293 if (s == NULL || _PyAccu_Accumulate(&acc, s))
294 goto error;
295 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200298 return _PyAccu_Finish(&acc);
299
300error:
301 _PyAccu_Destroy(&acc);
302 Py_XDECREF(s);
303 Py_ReprLeave((PyObject *)v);
304 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305}
306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307/* The addend 82520, was selected from the range(0, 1000000) for
308 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000309 upto length eight:
310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000312 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
313*/
314
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000315static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000316tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000317{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000318 register Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 register Py_ssize_t len = Py_SIZE(v);
320 register PyObject **p;
Gregory P. Smith63e6c322012-01-14 15:31:34 -0800321 Py_hash_t mult = _PyHASH_MULTIPLIER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 x = 0x345678L;
323 p = v->ob_item;
324 while (--len >= 0) {
325 y = PyObject_Hash(*p++);
326 if (y == -1)
327 return -1;
328 x = (x ^ y) * mult;
329 /* the cast might truncate len; that doesn't change hash stability */
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000330 mult += (Py_hash_t)(82520L + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 }
332 x += 97531L;
333 if (x == -1)
334 x = -2;
335 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000336}
337
Martin v. Löwis18e16552006-02-15 17:27:45 +0000338static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000339tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342}
343
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000344static int
Fred Drakeba096332000-07-09 07:04:36 +0000345tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_ssize_t i;
348 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
351 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
352 Py_EQ);
353 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000354}
355
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000357tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (i < 0 || i >= Py_SIZE(a)) {
360 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
361 return NULL;
362 }
363 Py_INCREF(a->ob_item[i]);
364 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365}
366
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
369 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 register PyTupleObject *np;
372 PyObject **src, **dest;
373 register Py_ssize_t i;
374 Py_ssize_t len;
375 if (ilow < 0)
376 ilow = 0;
377 if (ihigh > Py_SIZE(a))
378 ihigh = Py_SIZE(a);
379 if (ihigh < ilow)
380 ihigh = ilow;
381 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
382 Py_INCREF(a);
383 return (PyObject *)a;
384 }
385 len = ihigh - ilow;
386 np = (PyTupleObject *)PyTuple_New(len);
387 if (np == NULL)
388 return NULL;
389 src = a->ob_item + ilow;
390 dest = np->ob_item;
391 for (i = 0; i < len; i++) {
392 PyObject *v = src[i];
393 Py_INCREF(v);
394 dest[i] = v;
395 }
396 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397}
398
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000400PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (op == NULL || !PyTuple_Check(op)) {
403 PyErr_BadInternalCall();
404 return NULL;
405 }
406 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000407}
408
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000410tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 register Py_ssize_t size;
413 register Py_ssize_t i;
414 PyObject **src, **dest;
415 PyTupleObject *np;
416 if (!PyTuple_Check(bb)) {
417 PyErr_Format(PyExc_TypeError,
418 "can only concatenate tuple (not \"%.200s\") to tuple",
419 Py_TYPE(bb)->tp_name);
420 return NULL;
421 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 size = Py_SIZE(a) + Py_SIZE(b);
424 if (size < 0)
425 return PyErr_NoMemory();
426 np = (PyTupleObject *) PyTuple_New(size);
427 if (np == NULL) {
428 return NULL;
429 }
430 src = a->ob_item;
431 dest = np->ob_item;
432 for (i = 0; i < Py_SIZE(a); i++) {
433 PyObject *v = src[i];
434 Py_INCREF(v);
435 dest[i] = v;
436 }
437 src = b->ob_item;
438 dest = np->ob_item + Py_SIZE(a);
439 for (i = 0; i < Py_SIZE(b); i++) {
440 PyObject *v = src[i];
441 Py_INCREF(v);
442 dest[i] = v;
443 }
444 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445#undef b
446}
447
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000449tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_ssize_t i, j;
452 Py_ssize_t size;
453 PyTupleObject *np;
454 PyObject **p, **items;
455 if (n < 0)
456 n = 0;
457 if (Py_SIZE(a) == 0 || n == 1) {
458 if (PyTuple_CheckExact(a)) {
459 /* Since tuples are immutable, we can return a shared
460 copy in this case */
461 Py_INCREF(a);
462 return (PyObject *)a;
463 }
464 if (Py_SIZE(a) == 0)
465 return PyTuple_New(0);
466 }
467 size = Py_SIZE(a) * n;
468 if (size/Py_SIZE(a) != n)
469 return PyErr_NoMemory();
470 np = (PyTupleObject *) PyTuple_New(size);
471 if (np == NULL)
472 return NULL;
473 p = np->ob_item;
474 items = a->ob_item;
475 for (i = 0; i < n; i++) {
476 for (j = 0; j < Py_SIZE(a); j++) {
477 *p = items[j];
478 Py_INCREF(*p);
479 p++;
480 }
481 }
482 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000483}
484
Raymond Hettinger65baa342008-02-07 00:41:02 +0000485static PyObject *
486tupleindex(PyTupleObject *self, PyObject *args)
487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200489 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000490
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200491 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
492 _PyEval_SliceIndex, &start,
493 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return NULL;
495 if (start < 0) {
496 start += Py_SIZE(self);
497 if (start < 0)
498 start = 0;
499 }
500 if (stop < 0) {
501 stop += Py_SIZE(self);
502 if (stop < 0)
503 stop = 0;
504 }
505 for (i = start; i < stop && i < Py_SIZE(self); i++) {
506 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
507 if (cmp > 0)
508 return PyLong_FromSsize_t(i);
509 else if (cmp < 0)
510 return NULL;
511 }
512 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
513 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000514}
515
516static PyObject *
517tuplecount(PyTupleObject *self, PyObject *v)
518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 Py_ssize_t count = 0;
520 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 for (i = 0; i < Py_SIZE(self); i++) {
523 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
524 if (cmp > 0)
525 count++;
526 else if (cmp < 0)
527 return NULL;
528 }
529 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000530}
531
Jeremy Hylton8caad492000-06-23 14:18:11 +0000532static int
533tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 for (i = Py_SIZE(o); --i >= 0; )
538 Py_VISIT(o->ob_item[i]);
539 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000540}
541
Guido van Rossumf77bc622001-01-18 00:00:53 +0000542static PyObject *
543tuplerichcompare(PyObject *v, PyObject *w, int op)
544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyTupleObject *vt, *wt;
546 Py_ssize_t i;
547 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
550 Py_INCREF(Py_NotImplemented);
551 return Py_NotImplemented;
552 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 vt = (PyTupleObject *)v;
555 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 vlen = Py_SIZE(vt);
558 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Note: the corresponding code for lists has an "early out" test
561 * here when op is EQ or NE and the lengths differ. That pays there,
562 * but Tim was unable to find any real code where EQ/NE tuple
563 * compares don't have the same length, so testing for it here would
564 * have cost without benefit.
565 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 /* Search for the first index where items are different.
568 * Note that because tuples are immutable, it's safe to reuse
569 * vlen and wlen across the comparison calls.
570 */
571 for (i = 0; i < vlen && i < wlen; i++) {
572 int k = PyObject_RichCompareBool(vt->ob_item[i],
573 wt->ob_item[i], Py_EQ);
574 if (k < 0)
575 return NULL;
576 if (!k)
577 break;
578 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (i >= vlen || i >= wlen) {
581 /* No more items to compare -- compare sizes */
582 int cmp;
583 PyObject *res;
584 switch (op) {
585 case Py_LT: cmp = vlen < wlen; break;
586 case Py_LE: cmp = vlen <= wlen; break;
587 case Py_EQ: cmp = vlen == wlen; break;
588 case Py_NE: cmp = vlen != wlen; break;
589 case Py_GT: cmp = vlen > wlen; break;
590 case Py_GE: cmp = vlen >= wlen; break;
591 default: return NULL; /* cannot happen */
592 }
593 if (cmp)
594 res = Py_True;
595 else
596 res = Py_False;
597 Py_INCREF(res);
598 return res;
599 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 /* We have an item that differs -- shortcuts for EQ/NE */
602 if (op == Py_EQ) {
603 Py_INCREF(Py_False);
604 return Py_False;
605 }
606 if (op == Py_NE) {
607 Py_INCREF(Py_True);
608 return Py_True;
609 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 /* Compare the final item again using the proper operator */
612 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000613}
614
Jeremy Hylton938ace62002-07-17 16:30:39 +0000615static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000616tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
617
Tim Peters6d6c1a32001-08-02 04:15:00 +0000618static PyObject *
619tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 PyObject *arg = NULL;
622 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (type != &PyTuple_Type)
625 return tuple_subtype_new(type, args, kwds);
626 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
627 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 if (arg == NULL)
630 return PyTuple_New(0);
631 else
632 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000633}
634
Guido van Rossumae960af2001-08-30 03:11:59 +0000635static PyObject *
636tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PyObject *tmp, *newobj, *item;
639 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 assert(PyType_IsSubtype(type, &PyTuple_Type));
642 tmp = tuple_new(&PyTuple_Type, args, kwds);
643 if (tmp == NULL)
644 return NULL;
645 assert(PyTuple_Check(tmp));
646 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
647 if (newobj == NULL)
648 return NULL;
649 for (i = 0; i < n; i++) {
650 item = PyTuple_GET_ITEM(tmp, i);
651 Py_INCREF(item);
652 PyTuple_SET_ITEM(newobj, i, item);
653 }
654 Py_DECREF(tmp);
655 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000656}
657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000658PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000659"tuple() -> empty tuple\n\
660tuple(iterable) -> tuple initialized from iterable's items\n\
661\n\
662If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000663
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 (lenfunc)tuplelength, /* sq_length */
666 (binaryfunc)tupleconcat, /* sq_concat */
667 (ssizeargfunc)tuplerepeat, /* sq_repeat */
668 (ssizeargfunc)tupleitem, /* sq_item */
669 0, /* sq_slice */
670 0, /* sq_ass_item */
671 0, /* sq_ass_slice */
672 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673};
674
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000675static PyObject*
676tuplesubscript(PyTupleObject* self, PyObject* item)
677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (PyIndex_Check(item)) {
679 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
680 if (i == -1 && PyErr_Occurred())
681 return NULL;
682 if (i < 0)
683 i += PyTuple_GET_SIZE(self);
684 return tupleitem(self, i);
685 }
686 else if (PySlice_Check(item)) {
687 Py_ssize_t start, stop, step, slicelength, cur, i;
688 PyObject* result;
689 PyObject* it;
690 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000691
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000692 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyTuple_GET_SIZE(self),
694 &start, &stop, &step, &slicelength) < 0) {
695 return NULL;
696 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (slicelength <= 0) {
699 return PyTuple_New(0);
700 }
701 else if (start == 0 && step == 1 &&
702 slicelength == PyTuple_GET_SIZE(self) &&
703 PyTuple_CheckExact(self)) {
704 Py_INCREF(self);
705 return (PyObject *)self;
706 }
707 else {
708 result = PyTuple_New(slicelength);
709 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 src = self->ob_item;
712 dest = ((PyTupleObject *)result)->ob_item;
713 for (cur = start, i = 0; i < slicelength;
714 cur += step, i++) {
715 it = src[cur];
716 Py_INCREF(it);
717 dest[i] = it;
718 }
719
720 return result;
721 }
722 }
723 else {
724 PyErr_Format(PyExc_TypeError,
725 "tuple indices must be integers, not %.200s",
726 Py_TYPE(item)->tp_name);
727 return NULL;
728 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000729}
730
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000731static PyObject *
732tuple_getnewargs(PyTupleObject *v)
733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
735
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000736}
737
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000738static PyObject *
739tuple_sizeof(PyTupleObject *self)
740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
744 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000745}
746
Raymond Hettinger65baa342008-02-07 00:41:02 +0000747PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000748"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
749"Raises ValueError if the value is not present."
750);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000751PyDoc_STRVAR(count_doc,
752"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000753PyDoc_STRVAR(sizeof_doc,
754"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000755
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000756static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
758 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
759 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
760 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
761 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000762};
763
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000764static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 (lenfunc)tuplelength,
766 (binaryfunc)tuplesubscript,
767 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000768};
769
Raymond Hettinger48923c52002-08-09 01:30:17 +0000770static PyObject *tuple_iter(PyObject *seq);
771
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 PyVarObject_HEAD_INIT(&PyType_Type, 0)
774 "tuple",
775 sizeof(PyTupleObject) - sizeof(PyObject *),
776 sizeof(PyObject *),
777 (destructor)tupledealloc, /* tp_dealloc */
778 0, /* tp_print */
779 0, /* tp_getattr */
780 0, /* tp_setattr */
781 0, /* tp_reserved */
782 (reprfunc)tuplerepr, /* tp_repr */
783 0, /* tp_as_number */
784 &tuple_as_sequence, /* tp_as_sequence */
785 &tuple_as_mapping, /* tp_as_mapping */
786 (hashfunc)tuplehash, /* tp_hash */
787 0, /* tp_call */
788 0, /* tp_str */
789 PyObject_GenericGetAttr, /* tp_getattro */
790 0, /* tp_setattro */
791 0, /* tp_as_buffer */
792 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
793 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
794 tuple_doc, /* tp_doc */
795 (traverseproc)tupletraverse, /* tp_traverse */
796 0, /* tp_clear */
797 tuplerichcompare, /* tp_richcompare */
798 0, /* tp_weaklistoffset */
799 tuple_iter, /* tp_iter */
800 0, /* tp_iternext */
801 tuple_methods, /* tp_methods */
802 0, /* tp_members */
803 0, /* tp_getset */
804 0, /* tp_base */
805 0, /* tp_dict */
806 0, /* tp_descr_get */
807 0, /* tp_descr_set */
808 0, /* tp_dictoffset */
809 0, /* tp_init */
810 0, /* tp_alloc */
811 tuple_new, /* tp_new */
812 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000813};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000814
815/* The following function breaks the notion that tuples are immutable:
816 it changes the size of a tuple. We get away with this only if there
817 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000818 as creating a new tuple object and destroying the old one, only more
819 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000820 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000821
822int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000823_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 register PyTupleObject *v;
826 register PyTupleObject *sv;
827 Py_ssize_t i;
828 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 v = (PyTupleObject *) *pv;
831 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
832 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
833 *pv = 0;
834 Py_XDECREF(v);
835 PyErr_BadInternalCall();
836 return -1;
837 }
838 oldsize = Py_SIZE(v);
839 if (oldsize == newsize)
840 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 if (oldsize == 0) {
843 /* Empty tuples are often shared, so we should never
844 resize them in-place even if we do own the only
845 (current) reference */
846 Py_DECREF(v);
847 *pv = PyTuple_New(newsize);
848 return *pv == NULL ? -1 : 0;
849 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 /* XXX UNREF/NEWREF interface should be more symmetrical */
852 _Py_DEC_REFTOTAL;
853 if (_PyObject_GC_IS_TRACKED(v))
854 _PyObject_GC_UNTRACK(v);
855 _Py_ForgetReference((PyObject *) v);
856 /* DECREF items deleted by shrinkage */
857 for (i = newsize; i < oldsize; i++) {
858 Py_XDECREF(v->ob_item[i]);
859 v->ob_item[i] = NULL;
860 }
861 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
862 if (sv == NULL) {
863 *pv = NULL;
864 PyObject_GC_Del(v);
865 return -1;
866 }
867 _Py_NewReference((PyObject *) sv);
868 /* Zero out items added by growing */
869 if (newsize > oldsize)
870 memset(&sv->ob_item[oldsize], 0,
871 sizeof(*sv->ob_item) * (newsize - oldsize));
872 *pv = (PyObject *) sv;
873 _PyObject_GC_TRACK(sv);
874 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000875}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000876
Christian Heimesa156e092008-02-16 07:38:31 +0000877int
878PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000881#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 int i;
883 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
884 PyTupleObject *p, *q;
885 p = free_list[i];
886 freelist_size += numfree[i];
887 free_list[i] = NULL;
888 numfree[i] = 0;
889 while (p) {
890 q = p;
891 p = (PyTupleObject *)(p->ob_item[0]);
892 PyObject_GC_Del(q);
893 }
894 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000895#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000897}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898
Christian Heimesa156e092008-02-16 07:38:31 +0000899void
900PyTuple_Fini(void)
901{
902#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 /* empty tuples are used all over the place and applications may
904 * rely on the fact that an empty tuple is a singleton. */
905 Py_XDECREF(free_list[0]);
906 free_list[0] = NULL;
Christian Heimesa156e092008-02-16 07:38:31 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000909#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000910#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000912#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000913}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000914
915/*********************** Tuple Iterator **************************/
916
917typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PyObject_HEAD
919 long it_index;
920 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000921} tupleiterobject;
922
Raymond Hettinger48923c52002-08-09 01:30:17 +0000923static void
924tupleiter_dealloc(tupleiterobject *it)
925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 _PyObject_GC_UNTRACK(it);
927 Py_XDECREF(it->it_seq);
928 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000929}
930
931static int
932tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 Py_VISIT(it->it_seq);
935 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000936}
937
Raymond Hettinger48923c52002-08-09 01:30:17 +0000938static PyObject *
939tupleiter_next(tupleiterobject *it)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyTupleObject *seq;
942 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 assert(it != NULL);
945 seq = it->it_seq;
946 if (seq == NULL)
947 return NULL;
948 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (it->it_index < PyTuple_GET_SIZE(seq)) {
951 item = PyTuple_GET_ITEM(seq, it->it_index);
952 ++it->it_index;
953 Py_INCREF(item);
954 return item;
955 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 Py_DECREF(seq);
958 it->it_seq = NULL;
959 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960}
961
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000962static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000963tupleiter_len(tupleiterobject *it)
964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 Py_ssize_t len = 0;
966 if (it->it_seq)
967 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
968 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000969}
970
Armin Rigof5b3e362006-02-11 21:32:43 +0000971PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000972
973static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
975 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +0000976};
977
Raymond Hettinger48923c52002-08-09 01:30:17 +0000978PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyVarObject_HEAD_INIT(&PyType_Type, 0)
980 "tuple_iterator", /* tp_name */
981 sizeof(tupleiterobject), /* tp_basicsize */
982 0, /* tp_itemsize */
983 /* methods */
984 (destructor)tupleiter_dealloc, /* tp_dealloc */
985 0, /* tp_print */
986 0, /* tp_getattr */
987 0, /* tp_setattr */
988 0, /* tp_reserved */
989 0, /* tp_repr */
990 0, /* tp_as_number */
991 0, /* tp_as_sequence */
992 0, /* tp_as_mapping */
993 0, /* tp_hash */
994 0, /* tp_call */
995 0, /* tp_str */
996 PyObject_GenericGetAttr, /* tp_getattro */
997 0, /* tp_setattro */
998 0, /* tp_as_buffer */
999 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1000 0, /* tp_doc */
1001 (traverseproc)tupleiter_traverse, /* tp_traverse */
1002 0, /* tp_clear */
1003 0, /* tp_richcompare */
1004 0, /* tp_weaklistoffset */
1005 PyObject_SelfIter, /* tp_iter */
1006 (iternextfunc)tupleiter_next, /* tp_iternext */
1007 tupleiter_methods, /* tp_methods */
1008 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001009};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001010
1011static PyObject *
1012tuple_iter(PyObject *seq)
1013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 if (!PyTuple_Check(seq)) {
1017 PyErr_BadInternalCall();
1018 return NULL;
1019 }
1020 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1021 if (it == NULL)
1022 return NULL;
1023 it->it_index = 0;
1024 Py_INCREF(seq);
1025 it->it_seq = (PyTupleObject *)seq;
1026 _PyObject_GC_TRACK(it);
1027 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028}