blob: 1f2ab552aeb9632086033bfa4348a22cb5bed13e [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 Lehtinen819d8d42011-11-05 23:18:06 +0200513 PyObject *v, *start_obj = NULL, *stop_obj = NULL;
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000514
Petri Lehtinen819d8d42011-11-05 23:18:06 +0200515 if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 return NULL;
Petri Lehtinen819d8d42011-11-05 23:18:06 +0200517
518 if (start_obj != Py_None)
519 if (!_PyEval_SliceIndex(start_obj, &start))
520 return NULL;
521
522 if (stop_obj != Py_None)
523 if (!_PyEval_SliceIndex(stop_obj, &stop))
524 return NULL;
525
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 if (start < 0) {
527 start += Py_SIZE(self);
528 if (start < 0)
529 start = 0;
530 }
531 if (stop < 0) {
532 stop += Py_SIZE(self);
533 if (stop < 0)
534 stop = 0;
535 }
536 for (i = start; i < stop && i < Py_SIZE(self); i++) {
537 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
538 if (cmp > 0)
539 return PyInt_FromSsize_t(i);
540 else if (cmp < 0)
541 return NULL;
542 }
543 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
544 return NULL;
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000545}
546
547static PyObject *
548tuplecount(PyTupleObject *self, PyObject *v)
549{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000550 Py_ssize_t count = 0;
551 Py_ssize_t i;
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000552
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 for (i = 0; i < Py_SIZE(self); i++) {
554 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
555 if (cmp > 0)
556 count++;
557 else if (cmp < 0)
558 return NULL;
559 }
560 return PyInt_FromSsize_t(count);
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000561}
562
Jeremy Hylton8caad492000-06-23 14:18:11 +0000563static int
564tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
565{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000567
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000568 for (i = Py_SIZE(o); --i >= 0; )
569 Py_VISIT(o->ob_item[i]);
570 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000571}
572
Guido van Rossumf77bc622001-01-18 00:00:53 +0000573static PyObject *
574tuplerichcompare(PyObject *v, PyObject *w, int op)
575{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000576 PyTupleObject *vt, *wt;
577 Py_ssize_t i;
578 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000579
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000580 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
581 Py_INCREF(Py_NotImplemented);
582 return Py_NotImplemented;
583 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000584
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 vt = (PyTupleObject *)v;
586 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000587
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000588 vlen = Py_SIZE(vt);
589 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000590
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000591 /* Note: the corresponding code for lists has an "early out" test
592 * here when op is EQ or NE and the lengths differ. That pays there,
593 * but Tim was unable to find any real code where EQ/NE tuple
594 * compares don't have the same length, so testing for it here would
595 * have cost without benefit.
596 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000597
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 /* Search for the first index where items are different.
599 * Note that because tuples are immutable, it's safe to reuse
600 * vlen and wlen across the comparison calls.
601 */
602 for (i = 0; i < vlen && i < wlen; i++) {
603 int k = PyObject_RichCompareBool(vt->ob_item[i],
604 wt->ob_item[i], Py_EQ);
605 if (k < 0)
606 return NULL;
607 if (!k)
608 break;
609 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000610
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000611 if (i >= vlen || i >= wlen) {
612 /* No more items to compare -- compare sizes */
613 int cmp;
614 PyObject *res;
615 switch (op) {
616 case Py_LT: cmp = vlen < wlen; break;
617 case Py_LE: cmp = vlen <= wlen; break;
618 case Py_EQ: cmp = vlen == wlen; break;
619 case Py_NE: cmp = vlen != wlen; break;
620 case Py_GT: cmp = vlen > wlen; break;
621 case Py_GE: cmp = vlen >= wlen; break;
622 default: return NULL; /* cannot happen */
623 }
624 if (cmp)
625 res = Py_True;
626 else
627 res = Py_False;
628 Py_INCREF(res);
629 return res;
630 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000631
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000632 /* We have an item that differs -- shortcuts for EQ/NE */
633 if (op == Py_EQ) {
634 Py_INCREF(Py_False);
635 return Py_False;
636 }
637 if (op == Py_NE) {
638 Py_INCREF(Py_True);
639 return Py_True;
640 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000641
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000642 /* Compare the final item again using the proper operator */
643 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000644}
645
Jeremy Hylton938ace62002-07-17 16:30:39 +0000646static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000647tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
648
Tim Peters6d6c1a32001-08-02 04:15:00 +0000649static PyObject *
650tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
651{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 PyObject *arg = NULL;
653 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000654
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000655 if (type != &PyTuple_Type)
656 return tuple_subtype_new(type, args, kwds);
657 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
658 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000660 if (arg == NULL)
661 return PyTuple_New(0);
662 else
663 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000664}
665
Guido van Rossumae960af2001-08-30 03:11:59 +0000666static PyObject *
667tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
668{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000669 PyObject *tmp, *newobj, *item;
670 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000671
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000672 assert(PyType_IsSubtype(type, &PyTuple_Type));
673 tmp = tuple_new(&PyTuple_Type, args, kwds);
674 if (tmp == NULL)
675 return NULL;
676 assert(PyTuple_Check(tmp));
677 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
678 if (newobj == NULL)
679 return NULL;
680 for (i = 0; i < n; i++) {
681 item = PyTuple_GET_ITEM(tmp, i);
682 Py_INCREF(item);
683 PyTuple_SET_ITEM(newobj, i, item);
684 }
685 Py_DECREF(tmp);
686 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000687}
688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000689PyDoc_STRVAR(tuple_doc,
Ezio Melottifb501122010-02-28 23:59:00 +0000690"tuple() -> empty tuple\n\
691tuple(iterable) -> tuple initialized from iterable's items\n\
692\n\
693If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000696 (lenfunc)tuplelength, /* sq_length */
697 (binaryfunc)tupleconcat, /* sq_concat */
698 (ssizeargfunc)tuplerepeat, /* sq_repeat */
699 (ssizeargfunc)tupleitem, /* sq_item */
700 (ssizessizeargfunc)tupleslice, /* sq_slice */
701 0, /* sq_ass_item */
702 0, /* sq_ass_slice */
703 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000704};
705
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000706static PyObject*
707tuplesubscript(PyTupleObject* self, PyObject* item)
708{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 if (PyIndex_Check(item)) {
710 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
711 if (i == -1 && PyErr_Occurred())
712 return NULL;
713 if (i < 0)
714 i += PyTuple_GET_SIZE(self);
715 return tupleitem(self, i);
716 }
717 else if (PySlice_Check(item)) {
718 Py_ssize_t start, stop, step, slicelength, cur, i;
719 PyObject* result;
720 PyObject* it;
721 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000722
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000723 if (PySlice_GetIndicesEx((PySliceObject*)item,
724 PyTuple_GET_SIZE(self),
725 &start, &stop, &step, &slicelength) < 0) {
726 return NULL;
727 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000728
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 if (slicelength <= 0) {
730 return PyTuple_New(0);
731 }
732 else if (start == 0 && step == 1 &&
733 slicelength == PyTuple_GET_SIZE(self) &&
734 PyTuple_CheckExact(self)) {
735 Py_INCREF(self);
736 return (PyObject *)self;
737 }
738 else {
739 result = PyTuple_New(slicelength);
740 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000741
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000742 src = self->ob_item;
743 dest = ((PyTupleObject *)result)->ob_item;
744 for (cur = start, i = 0; i < slicelength;
745 cur += step, i++) {
746 it = src[cur];
747 Py_INCREF(it);
748 dest[i] = it;
749 }
750
751 return result;
752 }
753 }
754 else {
755 PyErr_Format(PyExc_TypeError,
756 "tuple indices must be integers, not %.200s",
757 Py_TYPE(item)->tp_name);
758 return NULL;
759 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000760}
761
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000762static PyObject *
763tuple_getnewargs(PyTupleObject *v)
764{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000765 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
766
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000767}
768
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000769static PyObject *
770tuple_sizeof(PyTupleObject *self)
771{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 Py_ssize_t res;
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000773
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000774 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
775 return PyInt_FromSsize_t(res);
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000776}
777
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000778PyDoc_STRVAR(index_doc,
Andrew M. Kuchlingb15d6fb2008-10-04 01:03:42 +0000779"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
780"Raises ValueError if the value is not present."
781);
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000782PyDoc_STRVAR(count_doc,
783"T.count(value) -> integer -- return number of occurrences of value");
Robert Schuppenies73e9ffc2008-06-13 13:29:37 +0000784PyDoc_STRVAR(sizeof_doc,
785"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000786
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000787static PyMethodDef tuple_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000788 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
789 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
790 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
791 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
792 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000793};
794
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000795static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 (lenfunc)tuplelength,
797 (binaryfunc)tuplesubscript,
798 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000799};
800
Raymond Hettinger48923c52002-08-09 01:30:17 +0000801static PyObject *tuple_iter(PyObject *seq);
802
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803PyTypeObject PyTuple_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 PyVarObject_HEAD_INIT(&PyType_Type, 0)
805 "tuple",
806 sizeof(PyTupleObject) - sizeof(PyObject *),
807 sizeof(PyObject *),
808 (destructor)tupledealloc, /* tp_dealloc */
809 (printfunc)tupleprint, /* tp_print */
810 0, /* tp_getattr */
811 0, /* tp_setattr */
812 0, /* tp_compare */
813 (reprfunc)tuplerepr, /* tp_repr */
814 0, /* tp_as_number */
815 &tuple_as_sequence, /* tp_as_sequence */
816 &tuple_as_mapping, /* tp_as_mapping */
817 (hashfunc)tuplehash, /* tp_hash */
818 0, /* tp_call */
819 0, /* tp_str */
820 PyObject_GenericGetAttr, /* tp_getattro */
821 0, /* tp_setattro */
822 0, /* tp_as_buffer */
823 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
824 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
825 tuple_doc, /* tp_doc */
826 (traverseproc)tupletraverse, /* tp_traverse */
827 0, /* tp_clear */
828 tuplerichcompare, /* tp_richcompare */
829 0, /* tp_weaklistoffset */
830 tuple_iter, /* tp_iter */
831 0, /* tp_iternext */
832 tuple_methods, /* tp_methods */
833 0, /* tp_members */
834 0, /* tp_getset */
835 0, /* tp_base */
836 0, /* tp_dict */
837 0, /* tp_descr_get */
838 0, /* tp_descr_set */
839 0, /* tp_dictoffset */
840 0, /* tp_init */
841 0, /* tp_alloc */
842 tuple_new, /* tp_new */
843 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000845
846/* The following function breaks the notion that tuples are immutable:
847 it changes the size of a tuple. We get away with this only if there
848 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000849 as creating a new tuple object and destroying the old one, only more
850 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000851 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000852
853int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000854_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000855{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856 register PyTupleObject *v;
857 register PyTupleObject *sv;
858 Py_ssize_t i;
859 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000860
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000861 v = (PyTupleObject *) *pv;
862 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
863 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
864 *pv = 0;
865 Py_XDECREF(v);
866 PyErr_BadInternalCall();
867 return -1;
868 }
869 oldsize = Py_SIZE(v);
870 if (oldsize == newsize)
871 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000872
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873 if (oldsize == 0) {
874 /* Empty tuples are often shared, so we should never
875 resize them in-place even if we do own the only
876 (current) reference */
877 Py_DECREF(v);
878 *pv = PyTuple_New(newsize);
879 return *pv == NULL ? -1 : 0;
880 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000881
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000882 /* XXX UNREF/NEWREF interface should be more symmetrical */
883 _Py_DEC_REFTOTAL;
884 if (_PyObject_GC_IS_TRACKED(v))
885 _PyObject_GC_UNTRACK(v);
886 _Py_ForgetReference((PyObject *) v);
887 /* DECREF items deleted by shrinkage */
888 for (i = newsize; i < oldsize; i++) {
889 Py_XDECREF(v->ob_item[i]);
890 v->ob_item[i] = NULL;
891 }
892 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
893 if (sv == NULL) {
894 *pv = NULL;
895 PyObject_GC_Del(v);
896 return -1;
897 }
898 _Py_NewReference((PyObject *) sv);
899 /* Zero out items added by growing */
900 if (newsize > oldsize)
901 memset(&sv->ob_item[oldsize], 0,
902 sizeof(*sv->ob_item) * (newsize - oldsize));
903 *pv = (PyObject *) sv;
904 _PyObject_GC_TRACK(sv);
905 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000906}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000907
Christian Heimes3b718a72008-02-14 12:47:33 +0000908int
909PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000910{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000911 int freelist_size = 0;
Christian Heimes5b970ad2008-02-06 13:33:44 +0000912#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 int i;
914 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
915 PyTupleObject *p, *q;
916 p = free_list[i];
917 freelist_size += numfree[i];
918 free_list[i] = NULL;
919 numfree[i] = 0;
920 while (p) {
921 q = p;
922 p = (PyTupleObject *)(p->ob_item[0]);
923 PyObject_GC_Del(q);
924 }
925 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000926#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 return freelist_size;
Christian Heimes3b718a72008-02-14 12:47:33 +0000928}
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000929
Christian Heimes3b718a72008-02-14 12:47:33 +0000930void
931PyTuple_Fini(void)
932{
933#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 /* empty tuples are used all over the place and applications may
935 * rely on the fact that an empty tuple is a singleton. */
936 Py_XDECREF(free_list[0]);
937 free_list[0] = NULL;
Christian Heimes3b718a72008-02-14 12:47:33 +0000938
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 (void)PyTuple_ClearFreeList();
Christian Heimes3b718a72008-02-14 12:47:33 +0000940#endif
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000941#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000942 show_track();
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000943#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000944}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000945
946/*********************** Tuple Iterator **************************/
947
948typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000949 PyObject_HEAD
950 long it_index;
951 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000952} tupleiterobject;
953
Raymond Hettinger48923c52002-08-09 01:30:17 +0000954static void
955tupleiter_dealloc(tupleiterobject *it)
956{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000957 _PyObject_GC_UNTRACK(it);
958 Py_XDECREF(it->it_seq);
959 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960}
961
962static int
963tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
964{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000965 Py_VISIT(it->it_seq);
966 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000967}
968
Raymond Hettinger48923c52002-08-09 01:30:17 +0000969static PyObject *
970tupleiter_next(tupleiterobject *it)
971{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000972 PyTupleObject *seq;
973 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000974
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000975 assert(it != NULL);
976 seq = it->it_seq;
977 if (seq == NULL)
978 return NULL;
979 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000980
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000981 if (it->it_index < PyTuple_GET_SIZE(seq)) {
982 item = PyTuple_GET_ITEM(seq, it->it_index);
983 ++it->it_index;
984 Py_INCREF(item);
985 return item;
986 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000987
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000988 Py_DECREF(seq);
989 it->it_seq = NULL;
990 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000991}
992
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000993static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000994tupleiter_len(tupleiterobject *it)
995{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000996 Py_ssize_t len = 0;
997 if (it->it_seq)
998 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
999 return PyInt_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001000}
1001
Armin Rigof5b3e362006-02-11 21:32:43 +00001002PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001003
1004static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001005 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
1006 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001007};
1008
Raymond Hettinger48923c52002-08-09 01:30:17 +00001009PyTypeObject PyTupleIter_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001010 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1011 "tupleiterator", /* tp_name */
1012 sizeof(tupleiterobject), /* tp_basicsize */
1013 0, /* tp_itemsize */
1014 /* methods */
1015 (destructor)tupleiter_dealloc, /* tp_dealloc */
1016 0, /* tp_print */
1017 0, /* tp_getattr */
1018 0, /* tp_setattr */
1019 0, /* tp_compare */
1020 0, /* tp_repr */
1021 0, /* tp_as_number */
1022 0, /* tp_as_sequence */
1023 0, /* tp_as_mapping */
1024 0, /* tp_hash */
1025 0, /* tp_call */
1026 0, /* tp_str */
1027 PyObject_GenericGetAttr, /* tp_getattro */
1028 0, /* tp_setattro */
1029 0, /* tp_as_buffer */
1030 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1031 0, /* tp_doc */
1032 (traverseproc)tupleiter_traverse, /* tp_traverse */
1033 0, /* tp_clear */
1034 0, /* tp_richcompare */
1035 0, /* tp_weaklistoffset */
1036 PyObject_SelfIter, /* tp_iter */
1037 (iternextfunc)tupleiter_next, /* tp_iternext */
1038 tupleiter_methods, /* tp_methods */
1039 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001040};
Martin v. Löwis72d20672006-04-11 09:04:12 +00001041
1042static PyObject *
1043tuple_iter(PyObject *seq)
1044{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001045 tupleiterobject *it;
Martin v. Löwis72d20672006-04-11 09:04:12 +00001046
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001047 if (!PyTuple_Check(seq)) {
1048 PyErr_BadInternalCall();
1049 return NULL;
1050 }
1051 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1052 if (it == NULL)
1053 return NULL;
1054 it->it_index = 0;
1055 Py_INCREF(seq);
1056 it->it_seq = (PyTupleObject *)seq;
1057 _PyObject_GC_TRACK(it);
1058 return (PyObject *)it;
Martin v. Löwis72d20672006-04-11 09:04:12 +00001059}