blob: 3249cccdb79b541c28a3834b92da209c0761a1d2 [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"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum5ce78f82000-04-21 21:15:05 +00006/* Speed optimization to avoid frequent malloc/free of small tuples */
Christian Heimes5b970ad2008-02-06 13:33:44 +00007#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00008#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +00009#endif
Brett Cannonfee3acb2010-05-05 20:18:23 +000010#ifndef PyTuple_MAXFREELIST
Christian Heimes5b970ad2008-02-06 13:33:44 +000011#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000012#endif
13
Christian Heimes5b970ad2008-02-06 13:33:44 +000014#if PyTuple_MAXSAVESIZE > 0
15/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000016 tuple () of which at most one instance will be allocated.
17*/
Christian Heimes5b970ad2008-02-06 13:33:44 +000018static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
19static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000020#endif
21#ifdef COUNT_ALLOCS
Martin v. Löwisb90304a2009-01-07 18:40:40 +000022Py_ssize_t fast_tuple_allocs;
23Py_ssize_t tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000024#endif
25
Antoine Pitrouf8387af2009-03-23 18:41:45 +000026/* Debug statistic to count GC tracking of tuples.
27 Please note that tuples are only untracked when considered by the GC, and
28 many of them will be dead before. Therefore, a tracking rate close to 100%
29 does not necessarily prove that the heuristic is inefficient.
30*/
31#ifdef SHOW_TRACK_COUNT
32static Py_ssize_t count_untracked = 0;
33static Py_ssize_t count_tracked = 0;
34
35static void
36show_track(void)
37{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000038 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
39 count_tracked + count_untracked);
40 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
41 "d\n", count_tracked);
42 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
43 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrouf8387af2009-03-23 18:41:45 +000044}
45#endif
46
47
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000049PyTuple_New(register Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000051 register PyTupleObject *op;
52 Py_ssize_t i;
53 if (size < 0) {
54 PyErr_BadInternalCall();
55 return NULL;
56 }
Christian Heimes5b970ad2008-02-06 13:33:44 +000057#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +000058 if (size == 0 && free_list[0]) {
59 op = free_list[0];
60 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000061#ifdef COUNT_ALLOCS
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000063#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000064 return (PyObject *) op;
65 }
66 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
67 free_list[size] = (PyTupleObject *) op->ob_item[0];
68 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069#ifdef COUNT_ALLOCS
Antoine Pitrouc83ea132010-05-09 14:46:46 +000070 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000071#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000073#ifdef Py_TRACE_REFS
Antoine Pitrouc83ea132010-05-09 14:46:46 +000074 Py_SIZE(op) = size;
75 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000076#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000077 _Py_NewReference((PyObject *)op);
78 }
79 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 {
82 Py_ssize_t nbytes = size * sizeof(PyObject *);
83 /* Check for overflow */
84 if (nbytes / sizeof(PyObject *) != (size_t)size ||
85 (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
86 {
87 return PyErr_NoMemory();
88 }
Neal Norwitze7d8be82008-07-31 17:17:14 +000089
Antoine Pitrouc83ea132010-05-09 14:46:46 +000090 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
91 if (op == NULL)
92 return NULL;
93 }
94 for (i=0; i < size; i++)
95 op->ob_item[i] = NULL;
Christian Heimes5b970ad2008-02-06 13:33:44 +000096#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +000097 if (size == 0) {
98 free_list[0] = op;
99 ++numfree[0];
100 Py_INCREF(op); /* extra INCREF so that this is never freed */
101 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000102#endif
Antoine Pitrou1fba6242009-03-23 19:17:00 +0000103#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000104 count_tracked++;
Antoine Pitrou1fba6242009-03-23 19:17:00 +0000105#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000106 _PyObject_GC_TRACK(op);
107 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108}
109
Martin v. Löwis18e16552006-02-15 17:27:45 +0000110Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000111PyTuple_Size(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000113 if (!PyTuple_Check(op)) {
114 PyErr_BadInternalCall();
115 return -1;
116 }
117 else
118 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119}
120
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000122PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000124 if (!PyTuple_Check(op)) {
125 PyErr_BadInternalCall();
126 return NULL;
127 }
128 if (i < 0 || i >= Py_SIZE(op)) {
129 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
130 return NULL;
131 }
132 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
135int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000138 register PyObject *olditem;
139 register PyObject **p;
140 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
141 Py_XDECREF(newitem);
142 PyErr_BadInternalCall();
143 return -1;
144 }
145 if (i < 0 || i >= Py_SIZE(op)) {
146 Py_XDECREF(newitem);
147 PyErr_SetString(PyExc_IndexError,
148 "tuple assignment index out of range");
149 return -1;
150 }
151 p = ((PyTupleObject *)op) -> ob_item + i;
152 olditem = *p;
153 *p = newitem;
154 Py_XDECREF(olditem);
155 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156}
157
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000158void
159_PyTuple_MaybeUntrack(PyObject *op)
160{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 PyTupleObject *t;
162 Py_ssize_t i, n;
163
164 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
165 return;
166 t = (PyTupleObject *) op;
167 n = Py_SIZE(t);
168 for (i = 0; i < n; i++) {
169 PyObject *elt = PyTuple_GET_ITEM(t, i);
170 /* Tuple with NULL elements aren't
171 fully constructed, don't untrack
172 them yet. */
173 if (!elt ||
174 _PyObject_GC_MAY_BE_TRACKED(elt))
175 return;
176 }
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000177#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000178 count_tracked--;
179 count_untracked++;
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000180#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000181 _PyObject_GC_UNTRACK(op);
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000182}
183
Raymond Hettingercb2da432003-10-12 18:24:34 +0000184PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000185PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000186{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 Py_ssize_t i;
188 PyObject *o;
189 PyObject *result;
190 PyObject **items;
191 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000192
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 va_start(vargs, n);
194 result = PyTuple_New(n);
195 if (result == NULL)
196 return NULL;
197 items = ((PyTupleObject *)result)->ob_item;
198 for (i = 0; i < n; i++) {
199 o = va_arg(vargs, PyObject *);
200 Py_INCREF(o);
201 items[i] = o;
202 }
203 va_end(vargs);
204 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000205}
206
207
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208/* Methods */
209
210static void
Fred Drakeba096332000-07-09 07:04:36 +0000211tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000213 register Py_ssize_t i;
214 register Py_ssize_t len = Py_SIZE(op);
215 PyObject_GC_UnTrack(op);
216 Py_TRASHCAN_SAFE_BEGIN(op)
217 if (len > 0) {
218 i = len;
219 while (--i >= 0)
220 Py_XDECREF(op->ob_item[i]);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000221#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000222 if (len < PyTuple_MAXSAVESIZE &&
223 numfree[len] < PyTuple_MAXFREELIST &&
224 Py_TYPE(op) == &PyTuple_Type)
225 {
226 op->ob_item[0] = (PyObject *) free_list[len];
227 numfree[len]++;
228 free_list[len] = op;
229 goto done; /* return */
230 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000231#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000232 }
233 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000234done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000235 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236}
237
Guido van Rossum49e85141991-06-07 22:59:30 +0000238static int
Fred Drakeba096332000-07-09 07:04:36 +0000239tupleprint(PyTupleObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 Py_ssize_t i;
242 Py_BEGIN_ALLOW_THREADS
243 fprintf(fp, "(");
244 Py_END_ALLOW_THREADS
245 for (i = 0; i < Py_SIZE(op); i++) {
246 if (i > 0) {
247 Py_BEGIN_ALLOW_THREADS
248 fprintf(fp, ", ");
249 Py_END_ALLOW_THREADS
250 }
251 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
252 return -1;
253 }
254 i = Py_SIZE(op);
255 Py_BEGIN_ALLOW_THREADS
256 if (i == 1)
257 fprintf(fp, ",");
258 fprintf(fp, ")");
259 Py_END_ALLOW_THREADS
260 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261}
262
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000264tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 Py_ssize_t i, n;
267 PyObject *s, *temp;
268 PyObject *pieces, *result = NULL;
Tim Petersa7259592001-06-16 05:11:17 +0000269
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000270 n = Py_SIZE(v);
271 if (n == 0)
272 return PyString_FromString("()");
Brett Cannon31ba8482007-09-30 20:37:19 +0000273
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000274 /* While not mutable, it is still possible to end up with a cycle in a
275 tuple through an object that stores itself within a tuple (and thus
276 infinitely asks for the repr of itself). This should only be
277 possible within a type. */
278 i = Py_ReprEnter((PyObject *)v);
279 if (i != 0) {
280 return i > 0 ? PyString_FromString("(...)") : NULL;
281 }
Brett Cannon0b14f242007-09-30 19:45:10 +0000282
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000283 pieces = PyTuple_New(n);
284 if (pieces == NULL)
285 return NULL;
Tim Petersa7259592001-06-16 05:11:17 +0000286
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 /* Do repr() on each element. */
288 for (i = 0; i < n; ++i) {
289 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
290 goto Done;
291 s = PyObject_Repr(v->ob_item[i]);
292 Py_LeaveRecursiveCall();
293 if (s == NULL)
294 goto Done;
295 PyTuple_SET_ITEM(pieces, i, s);
296 }
Tim Petersa7259592001-06-16 05:11:17 +0000297
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 /* Add "()" decorations to the first and last items. */
299 assert(n > 0);
300 s = PyString_FromString("(");
301 if (s == NULL)
302 goto Done;
303 temp = PyTuple_GET_ITEM(pieces, 0);
304 PyString_ConcatAndDel(&s, temp);
305 PyTuple_SET_ITEM(pieces, 0, s);
306 if (s == NULL)
307 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +0000308
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 s = PyString_FromString(n == 1 ? ",)" : ")");
310 if (s == NULL)
311 goto Done;
312 temp = PyTuple_GET_ITEM(pieces, n-1);
313 PyString_ConcatAndDel(&temp, s);
314 PyTuple_SET_ITEM(pieces, n-1, temp);
315 if (temp == NULL)
316 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +0000317
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000318 /* Paste them all together with ", " between. */
319 s = PyString_FromString(", ");
320 if (s == NULL)
321 goto Done;
322 result = _PyString_Join(s, pieces);
323 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000324
325Done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000326 Py_DECREF(pieces);
327 Py_ReprLeave((PyObject *)v);
328 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329}
330
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331/* The addend 82520, was selected from the range(0, 1000000) for
332 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000333 upto length eight:
334
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000336 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
337*/
338
Guido van Rossum9bfef441993-03-29 10:43:31 +0000339static long
Fred Drakeba096332000-07-09 07:04:36 +0000340tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000341{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000342 register long x, y;
343 register Py_ssize_t len = Py_SIZE(v);
344 register PyObject **p;
345 long mult = 1000003L;
346 x = 0x345678L;
347 p = v->ob_item;
348 while (--len >= 0) {
349 y = PyObject_Hash(*p++);
350 if (y == -1)
351 return -1;
352 x = (x ^ y) * mult;
353 /* the cast might truncate len; that doesn't change hash stability */
354 mult += (long)(82520L + len + len);
355 }
356 x += 97531L;
357 if (x == -1)
358 x = -2;
359 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000360}
361
Martin v. Löwis18e16552006-02-15 17:27:45 +0000362static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000363tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366}
367
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000368static int
Fred Drakeba096332000-07-09 07:04:36 +0000369tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000370{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000371 Py_ssize_t i;
372 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000373
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000374 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
375 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
376 Py_EQ);
377 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000378}
379
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000381tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000383 if (i < 0 || i >= Py_SIZE(a)) {
384 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
385 return NULL;
386 }
387 Py_INCREF(a->ob_item[i]);
388 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389}
390
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391static PyObject *
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000392tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
393 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000395 register PyTupleObject *np;
396 PyObject **src, **dest;
397 register Py_ssize_t i;
398 Py_ssize_t len;
399 if (ilow < 0)
400 ilow = 0;
401 if (ihigh > Py_SIZE(a))
402 ihigh = Py_SIZE(a);
403 if (ihigh < ilow)
404 ihigh = ilow;
405 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
406 Py_INCREF(a);
407 return (PyObject *)a;
408 }
409 len = ihigh - ilow;
410 np = (PyTupleObject *)PyTuple_New(len);
411 if (np == NULL)
412 return NULL;
413 src = a->ob_item + ilow;
414 dest = np->ob_item;
415 for (i = 0; i < len; i++) {
416 PyObject *v = src[i];
417 Py_INCREF(v);
418 dest[i] = v;
419 }
420 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421}
422
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000424PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000425{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000426 if (op == NULL || !PyTuple_Check(op)) {
427 PyErr_BadInternalCall();
428 return NULL;
429 }
430 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000431}
432
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000434tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000436 register Py_ssize_t size;
437 register Py_ssize_t i;
438 PyObject **src, **dest;
439 PyTupleObject *np;
440 if (!PyTuple_Check(bb)) {
441 PyErr_Format(PyExc_TypeError,
442 "can only concatenate tuple (not \"%.200s\") to tuple",
443 Py_TYPE(bb)->tp_name);
444 return NULL;
445 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446#define b ((PyTupleObject *)bb)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000447 size = Py_SIZE(a) + Py_SIZE(b);
448 if (size < 0)
449 return PyErr_NoMemory();
450 np = (PyTupleObject *) PyTuple_New(size);
451 if (np == NULL) {
452 return NULL;
453 }
454 src = a->ob_item;
455 dest = np->ob_item;
456 for (i = 0; i < Py_SIZE(a); i++) {
457 PyObject *v = src[i];
458 Py_INCREF(v);
459 dest[i] = v;
460 }
461 src = b->ob_item;
462 dest = np->ob_item + Py_SIZE(a);
463 for (i = 0; i < Py_SIZE(b); i++) {
464 PyObject *v = src[i];
465 Py_INCREF(v);
466 dest[i] = v;
467 }
468 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469#undef b
470}
471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000474{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000475 Py_ssize_t i, j;
476 Py_ssize_t size;
477 PyTupleObject *np;
478 PyObject **p, **items;
479 if (n < 0)
480 n = 0;
481 if (Py_SIZE(a) == 0 || n == 1) {
482 if (PyTuple_CheckExact(a)) {
483 /* Since tuples are immutable, we can return a shared
484 copy in this case */
485 Py_INCREF(a);
486 return (PyObject *)a;
487 }
488 if (Py_SIZE(a) == 0)
489 return PyTuple_New(0);
490 }
491 size = Py_SIZE(a) * n;
492 if (size/Py_SIZE(a) != n)
493 return PyErr_NoMemory();
494 np = (PyTupleObject *) PyTuple_New(size);
495 if (np == NULL)
496 return NULL;
497 p = np->ob_item;
498 items = a->ob_item;
499 for (i = 0; i < n; i++) {
500 for (j = 0; j < Py_SIZE(a); j++) {
501 *p = items[j];
502 Py_INCREF(*p);
503 p++;
504 }
505 }
506 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000507}
508
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000509static PyObject *
510tupleindex(PyTupleObject *self, PyObject *args)
511{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000512 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinen3b9d92a2011-11-06 20:58:50 +0200513 PyObject *v;
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000514
Petri Lehtinen3b9d92a2011-11-06 20:58:50 +0200515 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
516 _PyEval_SliceIndex, &start,
517 _PyEval_SliceIndex, &stop))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000518 return NULL;
519 if (start < 0) {
520 start += Py_SIZE(self);
521 if (start < 0)
522 start = 0;
523 }
524 if (stop < 0) {
525 stop += Py_SIZE(self);
526 if (stop < 0)
527 stop = 0;
528 }
529 for (i = start; i < stop && i < Py_SIZE(self); i++) {
530 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
531 if (cmp > 0)
532 return PyInt_FromSsize_t(i);
533 else if (cmp < 0)
534 return NULL;
535 }
536 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
537 return NULL;
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000538}
539
540static PyObject *
541tuplecount(PyTupleObject *self, PyObject *v)
542{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000543 Py_ssize_t count = 0;
544 Py_ssize_t i;
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000545
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 for (i = 0; i < Py_SIZE(self); i++) {
547 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
548 if (cmp > 0)
549 count++;
550 else if (cmp < 0)
551 return NULL;
552 }
553 return PyInt_FromSsize_t(count);
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000554}
555
Jeremy Hylton8caad492000-06-23 14:18:11 +0000556static int
557tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
558{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000559 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000560
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000561 for (i = Py_SIZE(o); --i >= 0; )
562 Py_VISIT(o->ob_item[i]);
563 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000564}
565
Guido van Rossumf77bc622001-01-18 00:00:53 +0000566static PyObject *
567tuplerichcompare(PyObject *v, PyObject *w, int op)
568{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 PyTupleObject *vt, *wt;
570 Py_ssize_t i;
571 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000572
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000573 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
574 Py_INCREF(Py_NotImplemented);
575 return Py_NotImplemented;
576 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000577
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000578 vt = (PyTupleObject *)v;
579 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000580
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000581 vlen = Py_SIZE(vt);
582 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000583
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000645 PyObject *arg = NULL;
646 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000662 PyObject *tmp, *newobj, *item;
663 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000664
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Melottifb501122010-02-28 23:59:00 +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 Pitrouc83ea132010-05-09 14:46:46 +0000689 (lenfunc)tuplelength, /* sq_length */
690 (binaryfunc)tupleconcat, /* sq_concat */
691 (ssizeargfunc)tuplerepeat, /* sq_repeat */
692 (ssizeargfunc)tupleitem, /* sq_item */
693 (ssizessizeargfunc)tupleslice, /* 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 Pitrouc83ea132010-05-09 14:46:46 +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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000716 if (PySlice_GetIndicesEx((PySliceObject*)item,
717 PyTuple_GET_SIZE(self),
718 &start, &stop, &step, &slicelength) < 0) {
719 return NULL;
720 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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,
749 "tuple indices must be integers, not %.200s",
750 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 Pitrouc83ea132010-05-09 14:46:46 +0000758 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
759
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000760}
761
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000762static PyObject *
763tuple_sizeof(PyTupleObject *self)
764{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000765 Py_ssize_t res;
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000766
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000767 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
768 return PyInt_FromSsize_t(res);
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000769}
770
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000771PyDoc_STRVAR(index_doc,
Andrew M. Kuchlingb15d6fb2008-10-04 01:03:42 +0000772"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
773"Raises ValueError if the value is not present."
774);
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000775PyDoc_STRVAR(count_doc,
776"T.count(value) -> integer -- return number of occurrences of value");
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000777PyDoc_STRVAR(sizeof_doc,
778"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000779
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000780static PyMethodDef tuple_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000797 PyVarObject_HEAD_INIT(&PyType_Type, 0)
798 "tuple",
799 sizeof(PyTupleObject) - sizeof(PyObject *),
800 sizeof(PyObject *),
801 (destructor)tupledealloc, /* tp_dealloc */
802 (printfunc)tupleprint, /* tp_print */
803 0, /* tp_getattr */
804 0, /* tp_setattr */
805 0, /* tp_compare */
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 Pitrouc83ea132010-05-09 14:46:46 +0000849 register PyTupleObject *v;
850 register PyTupleObject *sv;
851 Py_ssize_t i;
852 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000853
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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++) {
882 Py_XDECREF(v->ob_item[i]);
883 v->ob_item[i] = NULL;
884 }
885 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
886 if (sv == NULL) {
887 *pv = NULL;
888 PyObject_GC_Del(v);
889 return -1;
890 }
891 _Py_NewReference((PyObject *) sv);
892 /* Zero out items added by growing */
893 if (newsize > oldsize)
894 memset(&sv->ob_item[oldsize], 0,
895 sizeof(*sv->ob_item) * (newsize - oldsize));
896 *pv = (PyObject *) sv;
897 _PyObject_GC_TRACK(sv);
898 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000899}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000900
Christian Heimes3b718a72008-02-14 12:47:33 +0000901int
902PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000903{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000904 int freelist_size = 0;
Christian Heimes5b970ad2008-02-06 13:33:44 +0000905#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000906 int i;
907 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
908 PyTupleObject *p, *q;
909 p = free_list[i];
910 freelist_size += numfree[i];
911 free_list[i] = NULL;
912 numfree[i] = 0;
913 while (p) {
914 q = p;
915 p = (PyTupleObject *)(p->ob_item[0]);
916 PyObject_GC_Del(q);
917 }
918 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000919#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000920 return freelist_size;
Christian Heimes3b718a72008-02-14 12:47:33 +0000921}
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922
Christian Heimes3b718a72008-02-14 12:47:33 +0000923void
924PyTuple_Fini(void)
925{
926#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 /* empty tuples are used all over the place and applications may
928 * rely on the fact that an empty tuple is a singleton. */
929 Py_XDECREF(free_list[0]);
930 free_list[0] = NULL;
Christian Heimes3b718a72008-02-14 12:47:33 +0000931
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 (void)PyTuple_ClearFreeList();
Christian Heimes3b718a72008-02-14 12:47:33 +0000933#endif
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000934#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935 show_track();
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000936#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000937}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000938
939/*********************** Tuple Iterator **************************/
940
941typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000942 PyObject_HEAD
943 long it_index;
944 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000945} tupleiterobject;
946
Raymond Hettinger48923c52002-08-09 01:30:17 +0000947static void
948tupleiter_dealloc(tupleiterobject *it)
949{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000950 _PyObject_GC_UNTRACK(it);
951 Py_XDECREF(it->it_seq);
952 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000953}
954
955static int
956tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
957{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000958 Py_VISIT(it->it_seq);
959 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960}
961
Raymond Hettinger48923c52002-08-09 01:30:17 +0000962static PyObject *
963tupleiter_next(tupleiterobject *it)
964{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000965 PyTupleObject *seq;
966 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000967
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000968 assert(it != NULL);
969 seq = it->it_seq;
970 if (seq == NULL)
971 return NULL;
972 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000974 if (it->it_index < PyTuple_GET_SIZE(seq)) {
975 item = PyTuple_GET_ITEM(seq, it->it_index);
976 ++it->it_index;
977 Py_INCREF(item);
978 return item;
979 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000980
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000981 Py_DECREF(seq);
982 it->it_seq = NULL;
983 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000984}
985
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000986static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000987tupleiter_len(tupleiterobject *it)
988{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000989 Py_ssize_t len = 0;
990 if (it->it_seq)
991 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
992 return PyInt_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000993}
994
Armin Rigof5b3e362006-02-11 21:32:43 +0000995PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000996
997static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000998 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
999 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001000};
1001
Raymond Hettinger48923c52002-08-09 01:30:17 +00001002PyTypeObject PyTupleIter_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001003 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1004 "tupleiterator", /* tp_name */
1005 sizeof(tupleiterobject), /* tp_basicsize */
1006 0, /* tp_itemsize */
1007 /* methods */
1008 (destructor)tupleiter_dealloc, /* tp_dealloc */
1009 0, /* tp_print */
1010 0, /* tp_getattr */
1011 0, /* tp_setattr */
1012 0, /* tp_compare */
1013 0, /* tp_repr */
1014 0, /* tp_as_number */
1015 0, /* tp_as_sequence */
1016 0, /* tp_as_mapping */
1017 0, /* tp_hash */
1018 0, /* tp_call */
1019 0, /* tp_str */
1020 PyObject_GenericGetAttr, /* tp_getattro */
1021 0, /* tp_setattro */
1022 0, /* tp_as_buffer */
1023 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1024 0, /* tp_doc */
1025 (traverseproc)tupleiter_traverse, /* tp_traverse */
1026 0, /* tp_clear */
1027 0, /* tp_richcompare */
1028 0, /* tp_weaklistoffset */
1029 PyObject_SelfIter, /* tp_iter */
1030 (iternextfunc)tupleiter_next, /* tp_iternext */
1031 tupleiter_methods, /* tp_methods */
1032 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001033};
Martin v. Löwis72d20672006-04-11 09:04:12 +00001034
1035static PyObject *
1036tuple_iter(PyObject *seq)
1037{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001038 tupleiterobject *it;
Martin v. Löwis72d20672006-04-11 09:04:12 +00001039
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001040 if (!PyTuple_Check(seq)) {
1041 PyErr_BadInternalCall();
1042 return NULL;
1043 }
1044 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1045 if (it == NULL)
1046 return NULL;
1047 it->it_index = 0;
1048 Py_INCREF(seq);
1049 it->it_seq = (PyTupleObject *)seq;
1050 _PyObject_GC_TRACK(it);
1051 return (PyObject *)it;
Martin v. Löwis72d20672006-04-11 09:04:12 +00001052}