blob: d58839e0917411a133d6c8c9a182c96be239eb99 [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 Heimes2202f872008-02-06 14:31:34 +00007#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +00009#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010#ifndef PyTuple_MAXFREELIST
Christian Heimes2202f872008-02-06 14:31:34 +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 Heimes2202f872008-02-06 14:31:34 +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 Heimes2202f872008-02-06 14:31:34 +000018static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
19static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000020#endif
21#ifdef COUNT_ALLOCS
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000022Py_ssize_t fast_tuple_allocs;
23Py_ssize_t tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000024#endif
25
Antoine Pitrou3a652b12009-03-23 18:52:06 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrou3a652b12009-03-23 18:52:06 +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 Pitrouf95a1b32010-05-09 15:52:27 +000051 register PyTupleObject *op;
52 Py_ssize_t i;
53 if (size < 0) {
54 PyErr_BadInternalCall();
55 return NULL;
56 }
Christian Heimes2202f872008-02-06 14:31:34 +000057#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000062 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000063#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000070 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000071#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000073#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 Py_SIZE(op) = size;
75 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 _Py_NewReference((PyObject *)op);
78 }
79 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 }
Brett Cannonb94767f2011-02-22 20:15:44 +000089 /* nbytes += sizeof(PyTupleObject) - sizeof(PyObject *); */
Neal Norwitz3ce5d922008-08-24 07:08:55 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
92 if (op == NULL)
93 return NULL;
94 }
95 for (i=0; i < size; i++)
96 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +000097#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (size == 0) {
99 free_list[0] = op;
100 ++numfree[0];
101 Py_INCREF(op); /* extra INCREF so that this is never freed */
102 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000103#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000104#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 _PyObject_GC_TRACK(op);
108 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109}
110
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000112PyTuple_Size(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (!PyTuple_Check(op)) {
115 PyErr_BadInternalCall();
116 return -1;
117 }
118 else
119 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120}
121
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000123PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (!PyTuple_Check(op)) {
126 PyErr_BadInternalCall();
127 return NULL;
128 }
129 if (i < 0 || i >= Py_SIZE(op)) {
130 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
131 return NULL;
132 }
133 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
136int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000137PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 register PyObject *olditem;
140 register PyObject **p;
141 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
142 Py_XDECREF(newitem);
143 PyErr_BadInternalCall();
144 return -1;
145 }
146 if (i < 0 || i >= Py_SIZE(op)) {
147 Py_XDECREF(newitem);
148 PyErr_SetString(PyExc_IndexError,
149 "tuple assignment index out of range");
150 return -1;
151 }
152 p = ((PyTupleObject *)op) -> ob_item + i;
153 olditem = *p;
154 *p = newitem;
155 Py_XDECREF(olditem);
156 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157}
158
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000159void
160_PyTuple_MaybeUntrack(PyObject *op)
161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 PyTupleObject *t;
163 Py_ssize_t i, n;
164
165 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
166 return;
167 t = (PyTupleObject *) op;
168 n = Py_SIZE(t);
169 for (i = 0; i < n; i++) {
170 PyObject *elt = PyTuple_GET_ITEM(t, i);
171 /* Tuple with NULL elements aren't
172 fully constructed, don't untrack
173 them yet. */
174 if (!elt ||
175 _PyObject_GC_MAY_BE_TRACKED(elt))
176 return;
177 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000178#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 count_tracked--;
180 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000181#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000183}
184
Raymond Hettingercb2da432003-10-12 18:24:34 +0000185PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000186PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 Py_ssize_t i;
189 PyObject *o;
190 PyObject *result;
191 PyObject **items;
192 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 va_start(vargs, n);
195 result = PyTuple_New(n);
196 if (result == NULL)
197 return NULL;
198 items = ((PyTupleObject *)result)->ob_item;
199 for (i = 0; i < n; i++) {
200 o = va_arg(vargs, PyObject *);
201 Py_INCREF(o);
202 items[i] = o;
203 }
204 va_end(vargs);
205 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000206}
207
208
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209/* Methods */
210
211static void
Fred Drakeba096332000-07-09 07:04:36 +0000212tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 register Py_ssize_t i;
215 register Py_ssize_t len = Py_SIZE(op);
216 PyObject_GC_UnTrack(op);
217 Py_TRASHCAN_SAFE_BEGIN(op)
218 if (len > 0) {
219 i = len;
220 while (--i >= 0)
221 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000222#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (len < PyTuple_MAXSAVESIZE &&
224 numfree[len] < PyTuple_MAXFREELIST &&
225 Py_TYPE(op) == &PyTuple_Type)
226 {
227 op->ob_item[0] = (PyObject *) free_list[len];
228 numfree[len]++;
229 free_list[len] = op;
230 goto done; /* return */
231 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000232#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 }
234 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000235done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237}
238
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000240tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 Py_ssize_t i, n;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200243 PyObject *s = NULL;
244 _PyAccu acc;
245 static PyObject *sep = NULL;
Tim Petersa7259592001-06-16 05:11:17 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 n = Py_SIZE(v);
248 if (n == 0)
249 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000250
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200251 if (sep == NULL) {
252 sep = PyUnicode_FromString(", ");
253 if (sep == NULL)
254 return NULL;
255 }
256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 /* While not mutable, it is still possible to end up with a cycle in a
258 tuple through an object that stores itself within a tuple (and thus
259 infinitely asks for the repr of itself). This should only be
260 possible within a type. */
261 i = Py_ReprEnter((PyObject *)v);
262 if (i != 0) {
263 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
264 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000265
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200266 if (_PyAccu_Init(&acc))
267 goto error;
268
269 s = PyUnicode_FromString("(");
270 if (s == NULL || _PyAccu_Accumulate(&acc, s))
271 goto error;
272 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* Do repr() on each element. */
275 for (i = 0; i < n; ++i) {
276 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200277 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 s = PyObject_Repr(v->ob_item[i]);
279 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200280 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
281 goto error;
282 if (s == NULL || _PyAccu_Accumulate(&acc, s))
283 goto error;
284 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200286 if (n > 1)
287 s = PyUnicode_FromString(")");
288 else
289 s = PyUnicode_FromString(",)");
290 if (s == NULL || _PyAccu_Accumulate(&acc, s))
291 goto error;
292 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200295 return _PyAccu_Finish(&acc);
296
297error:
298 _PyAccu_Destroy(&acc);
299 Py_XDECREF(s);
300 Py_ReprLeave((PyObject *)v);
301 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302}
303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304/* The addend 82520, was selected from the range(0, 1000000) for
305 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000306 upto length eight:
307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000309 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
310*/
311
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000312static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000313tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000314{
Mark Dickinson57e683e2011-09-24 18:18:40 +0100315 register Py_uhash_t x;
316 register Py_hash_t y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 register Py_ssize_t len = Py_SIZE(v);
318 register PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800319 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100320 x = 0x345678;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 p = v->ob_item;
322 while (--len >= 0) {
323 y = PyObject_Hash(*p++);
324 if (y == -1)
325 return -1;
326 x = (x ^ y) * mult;
327 /* the cast might truncate len; that doesn't change hash stability */
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000328 mult += (Py_hash_t)(82520L + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 }
330 x += 97531L;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100331 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 x = -2;
333 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000334}
335
Martin v. Löwis18e16552006-02-15 17:27:45 +0000336static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000337tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340}
341
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000342static int
Fred Drakeba096332000-07-09 07:04:36 +0000343tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_ssize_t i;
346 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
349 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
350 Py_EQ);
351 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000352}
353
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000355tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 if (i < 0 || i >= Py_SIZE(a)) {
358 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
359 return NULL;
360 }
361 Py_INCREF(a->ob_item[i]);
362 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363}
364
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
367 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 register PyTupleObject *np;
370 PyObject **src, **dest;
371 register Py_ssize_t i;
372 Py_ssize_t len;
373 if (ilow < 0)
374 ilow = 0;
375 if (ihigh > Py_SIZE(a))
376 ihigh = Py_SIZE(a);
377 if (ihigh < ilow)
378 ihigh = ilow;
379 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
380 Py_INCREF(a);
381 return (PyObject *)a;
382 }
383 len = ihigh - ilow;
384 np = (PyTupleObject *)PyTuple_New(len);
385 if (np == NULL)
386 return NULL;
387 src = a->ob_item + ilow;
388 dest = np->ob_item;
389 for (i = 0; i < len; i++) {
390 PyObject *v = src[i];
391 Py_INCREF(v);
392 dest[i] = v;
393 }
394 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395}
396
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000398PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (op == NULL || !PyTuple_Check(op)) {
401 PyErr_BadInternalCall();
402 return NULL;
403 }
404 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000405}
406
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000408tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 register Py_ssize_t size;
411 register Py_ssize_t i;
412 PyObject **src, **dest;
413 PyTupleObject *np;
414 if (!PyTuple_Check(bb)) {
415 PyErr_Format(PyExc_TypeError,
416 "can only concatenate tuple (not \"%.200s\") to tuple",
417 Py_TYPE(bb)->tp_name);
418 return NULL;
419 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 size = Py_SIZE(a) + Py_SIZE(b);
422 if (size < 0)
423 return PyErr_NoMemory();
424 np = (PyTupleObject *) PyTuple_New(size);
425 if (np == NULL) {
426 return NULL;
427 }
428 src = a->ob_item;
429 dest = np->ob_item;
430 for (i = 0; i < Py_SIZE(a); i++) {
431 PyObject *v = src[i];
432 Py_INCREF(v);
433 dest[i] = v;
434 }
435 src = b->ob_item;
436 dest = np->ob_item + Py_SIZE(a);
437 for (i = 0; i < Py_SIZE(b); i++) {
438 PyObject *v = src[i];
439 Py_INCREF(v);
440 dest[i] = v;
441 }
442 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443#undef b
444}
445
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000447tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 Py_ssize_t i, j;
450 Py_ssize_t size;
451 PyTupleObject *np;
452 PyObject **p, **items;
453 if (n < 0)
454 n = 0;
455 if (Py_SIZE(a) == 0 || n == 1) {
456 if (PyTuple_CheckExact(a)) {
457 /* Since tuples are immutable, we can return a shared
458 copy in this case */
459 Py_INCREF(a);
460 return (PyObject *)a;
461 }
462 if (Py_SIZE(a) == 0)
463 return PyTuple_New(0);
464 }
465 size = Py_SIZE(a) * n;
466 if (size/Py_SIZE(a) != n)
467 return PyErr_NoMemory();
468 np = (PyTupleObject *) PyTuple_New(size);
469 if (np == NULL)
470 return NULL;
471 p = np->ob_item;
472 items = a->ob_item;
473 for (i = 0; i < n; i++) {
474 for (j = 0; j < Py_SIZE(a); j++) {
475 *p = items[j];
476 Py_INCREF(*p);
477 p++;
478 }
479 }
480 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000481}
482
Raymond Hettinger65baa342008-02-07 00:41:02 +0000483static PyObject *
484tupleindex(PyTupleObject *self, PyObject *args)
485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200487 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000488
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200489 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
490 _PyEval_SliceIndex, &start,
491 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return NULL;
493 if (start < 0) {
494 start += Py_SIZE(self);
495 if (start < 0)
496 start = 0;
497 }
498 if (stop < 0) {
499 stop += Py_SIZE(self);
500 if (stop < 0)
501 stop = 0;
502 }
503 for (i = start; i < stop && i < Py_SIZE(self); i++) {
504 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
505 if (cmp > 0)
506 return PyLong_FromSsize_t(i);
507 else if (cmp < 0)
508 return NULL;
509 }
510 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
511 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000512}
513
514static PyObject *
515tuplecount(PyTupleObject *self, PyObject *v)
516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 Py_ssize_t count = 0;
518 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 for (i = 0; i < Py_SIZE(self); i++) {
521 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
522 if (cmp > 0)
523 count++;
524 else if (cmp < 0)
525 return NULL;
526 }
527 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000528}
529
Jeremy Hylton8caad492000-06-23 14:18:11 +0000530static int
531tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 for (i = Py_SIZE(o); --i >= 0; )
536 Py_VISIT(o->ob_item[i]);
537 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000538}
539
Guido van Rossumf77bc622001-01-18 00:00:53 +0000540static PyObject *
541tuplerichcompare(PyObject *v, PyObject *w, int op)
542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 PyTupleObject *vt, *wt;
544 Py_ssize_t i;
545 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000546
Brian Curtindfc80e32011-08-10 20:28:54 -0500547 if (!PyTuple_Check(v) || !PyTuple_Check(w))
548 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 vt = (PyTupleObject *)v;
551 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 vlen = Py_SIZE(vt);
554 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* Note: the corresponding code for lists has an "early out" test
557 * here when op is EQ or NE and the lengths differ. That pays there,
558 * but Tim was unable to find any real code where EQ/NE tuple
559 * compares don't have the same length, so testing for it here would
560 * have cost without benefit.
561 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* Search for the first index where items are different.
564 * Note that because tuples are immutable, it's safe to reuse
565 * vlen and wlen across the comparison calls.
566 */
567 for (i = 0; i < vlen && i < wlen; i++) {
568 int k = PyObject_RichCompareBool(vt->ob_item[i],
569 wt->ob_item[i], Py_EQ);
570 if (k < 0)
571 return NULL;
572 if (!k)
573 break;
574 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (i >= vlen || i >= wlen) {
577 /* No more items to compare -- compare sizes */
578 int cmp;
579 PyObject *res;
580 switch (op) {
581 case Py_LT: cmp = vlen < wlen; break;
582 case Py_LE: cmp = vlen <= wlen; break;
583 case Py_EQ: cmp = vlen == wlen; break;
584 case Py_NE: cmp = vlen != wlen; break;
585 case Py_GT: cmp = vlen > wlen; break;
586 case Py_GE: cmp = vlen >= wlen; break;
587 default: return NULL; /* cannot happen */
588 }
589 if (cmp)
590 res = Py_True;
591 else
592 res = Py_False;
593 Py_INCREF(res);
594 return res;
595 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 /* We have an item that differs -- shortcuts for EQ/NE */
598 if (op == Py_EQ) {
599 Py_INCREF(Py_False);
600 return Py_False;
601 }
602 if (op == Py_NE) {
603 Py_INCREF(Py_True);
604 return Py_True;
605 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 /* Compare the final item again using the proper operator */
608 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000609}
610
Jeremy Hylton938ace62002-07-17 16:30:39 +0000611static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000612tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
613
Tim Peters6d6c1a32001-08-02 04:15:00 +0000614static PyObject *
615tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 PyObject *arg = NULL;
618 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 if (type != &PyTuple_Type)
621 return tuple_subtype_new(type, args, kwds);
622 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
623 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 if (arg == NULL)
626 return PyTuple_New(0);
627 else
628 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629}
630
Guido van Rossumae960af2001-08-30 03:11:59 +0000631static PyObject *
632tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PyObject *tmp, *newobj, *item;
635 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 assert(PyType_IsSubtype(type, &PyTuple_Type));
638 tmp = tuple_new(&PyTuple_Type, args, kwds);
639 if (tmp == NULL)
640 return NULL;
641 assert(PyTuple_Check(tmp));
642 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
643 if (newobj == NULL)
644 return NULL;
645 for (i = 0; i < n; i++) {
646 item = PyTuple_GET_ITEM(tmp, i);
647 Py_INCREF(item);
648 PyTuple_SET_ITEM(newobj, i, item);
649 }
650 Py_DECREF(tmp);
651 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000652}
653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000654PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000655"tuple() -> empty tuple\n\
656tuple(iterable) -> tuple initialized from iterable's items\n\
657\n\
658If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 (lenfunc)tuplelength, /* sq_length */
662 (binaryfunc)tupleconcat, /* sq_concat */
663 (ssizeargfunc)tuplerepeat, /* sq_repeat */
664 (ssizeargfunc)tupleitem, /* sq_item */
665 0, /* sq_slice */
666 0, /* sq_ass_item */
667 0, /* sq_ass_slice */
668 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000669};
670
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000671static PyObject*
672tuplesubscript(PyTupleObject* self, PyObject* item)
673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (PyIndex_Check(item)) {
675 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
676 if (i == -1 && PyErr_Occurred())
677 return NULL;
678 if (i < 0)
679 i += PyTuple_GET_SIZE(self);
680 return tupleitem(self, i);
681 }
682 else if (PySlice_Check(item)) {
683 Py_ssize_t start, stop, step, slicelength, cur, i;
684 PyObject* result;
685 PyObject* it;
686 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000687
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000688 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 PyTuple_GET_SIZE(self),
690 &start, &stop, &step, &slicelength) < 0) {
691 return NULL;
692 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 if (slicelength <= 0) {
695 return PyTuple_New(0);
696 }
697 else if (start == 0 && step == 1 &&
698 slicelength == PyTuple_GET_SIZE(self) &&
699 PyTuple_CheckExact(self)) {
700 Py_INCREF(self);
701 return (PyObject *)self;
702 }
703 else {
704 result = PyTuple_New(slicelength);
705 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 src = self->ob_item;
708 dest = ((PyTupleObject *)result)->ob_item;
709 for (cur = start, i = 0; i < slicelength;
710 cur += step, i++) {
711 it = src[cur];
712 Py_INCREF(it);
713 dest[i] = it;
714 }
715
716 return result;
717 }
718 }
719 else {
720 PyErr_Format(PyExc_TypeError,
721 "tuple indices must be integers, not %.200s",
722 Py_TYPE(item)->tp_name);
723 return NULL;
724 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000725}
726
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000727static PyObject *
728tuple_getnewargs(PyTupleObject *v)
729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
731
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000732}
733
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000734static PyObject *
735tuple_sizeof(PyTupleObject *self)
736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
740 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000741}
742
Raymond Hettinger65baa342008-02-07 00:41:02 +0000743PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000744"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
745"Raises ValueError if the value is not present."
746);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000747PyDoc_STRVAR(count_doc,
748"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000749PyDoc_STRVAR(sizeof_doc,
750"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000751
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000752static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
754 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
755 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
756 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
757 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000758};
759
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000760static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 (lenfunc)tuplelength,
762 (binaryfunc)tuplesubscript,
763 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000764};
765
Raymond Hettinger48923c52002-08-09 01:30:17 +0000766static PyObject *tuple_iter(PyObject *seq);
767
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000768PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyVarObject_HEAD_INIT(&PyType_Type, 0)
770 "tuple",
771 sizeof(PyTupleObject) - sizeof(PyObject *),
772 sizeof(PyObject *),
773 (destructor)tupledealloc, /* tp_dealloc */
774 0, /* tp_print */
775 0, /* tp_getattr */
776 0, /* tp_setattr */
777 0, /* tp_reserved */
778 (reprfunc)tuplerepr, /* tp_repr */
779 0, /* tp_as_number */
780 &tuple_as_sequence, /* tp_as_sequence */
781 &tuple_as_mapping, /* tp_as_mapping */
782 (hashfunc)tuplehash, /* tp_hash */
783 0, /* tp_call */
784 0, /* tp_str */
785 PyObject_GenericGetAttr, /* tp_getattro */
786 0, /* tp_setattro */
787 0, /* tp_as_buffer */
788 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
789 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
790 tuple_doc, /* tp_doc */
791 (traverseproc)tupletraverse, /* tp_traverse */
792 0, /* tp_clear */
793 tuplerichcompare, /* tp_richcompare */
794 0, /* tp_weaklistoffset */
795 tuple_iter, /* tp_iter */
796 0, /* tp_iternext */
797 tuple_methods, /* tp_methods */
798 0, /* tp_members */
799 0, /* tp_getset */
800 0, /* tp_base */
801 0, /* tp_dict */
802 0, /* tp_descr_get */
803 0, /* tp_descr_set */
804 0, /* tp_dictoffset */
805 0, /* tp_init */
806 0, /* tp_alloc */
807 tuple_new, /* tp_new */
808 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000810
811/* The following function breaks the notion that tuples are immutable:
812 it changes the size of a tuple. We get away with this only if there
813 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000814 as creating a new tuple object and destroying the old one, only more
815 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000816 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000817
818int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000819_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 register PyTupleObject *v;
822 register PyTupleObject *sv;
823 Py_ssize_t i;
824 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 v = (PyTupleObject *) *pv;
827 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
828 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
829 *pv = 0;
830 Py_XDECREF(v);
831 PyErr_BadInternalCall();
832 return -1;
833 }
834 oldsize = Py_SIZE(v);
835 if (oldsize == newsize)
836 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (oldsize == 0) {
839 /* Empty tuples are often shared, so we should never
840 resize them in-place even if we do own the only
841 (current) reference */
842 Py_DECREF(v);
843 *pv = PyTuple_New(newsize);
844 return *pv == NULL ? -1 : 0;
845 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 /* XXX UNREF/NEWREF interface should be more symmetrical */
848 _Py_DEC_REFTOTAL;
849 if (_PyObject_GC_IS_TRACKED(v))
850 _PyObject_GC_UNTRACK(v);
851 _Py_ForgetReference((PyObject *) v);
852 /* DECREF items deleted by shrinkage */
853 for (i = newsize; i < oldsize; i++) {
854 Py_XDECREF(v->ob_item[i]);
855 v->ob_item[i] = NULL;
856 }
857 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
858 if (sv == NULL) {
859 *pv = NULL;
860 PyObject_GC_Del(v);
861 return -1;
862 }
863 _Py_NewReference((PyObject *) sv);
864 /* Zero out items added by growing */
865 if (newsize > oldsize)
866 memset(&sv->ob_item[oldsize], 0,
867 sizeof(*sv->ob_item) * (newsize - oldsize));
868 *pv = (PyObject *) sv;
869 _PyObject_GC_TRACK(sv);
870 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000871}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000872
Christian Heimesa156e092008-02-16 07:38:31 +0000873int
874PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000877#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 int i;
879 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
880 PyTupleObject *p, *q;
881 p = free_list[i];
882 freelist_size += numfree[i];
883 free_list[i] = NULL;
884 numfree[i] = 0;
885 while (p) {
886 q = p;
887 p = (PyTupleObject *)(p->ob_item[0]);
888 PyObject_GC_Del(q);
889 }
890 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000891#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000893}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894
Christian Heimesa156e092008-02-16 07:38:31 +0000895void
896PyTuple_Fini(void)
897{
898#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 /* empty tuples are used all over the place and applications may
900 * rely on the fact that an empty tuple is a singleton. */
901 Py_XDECREF(free_list[0]);
902 free_list[0] = NULL;
Christian Heimesa156e092008-02-16 07:38:31 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000905#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000906#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000908#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000909}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000910
911/*********************** Tuple Iterator **************************/
912
913typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 PyObject_HEAD
915 long it_index;
916 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000917} tupleiterobject;
918
Raymond Hettinger48923c52002-08-09 01:30:17 +0000919static void
920tupleiter_dealloc(tupleiterobject *it)
921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 _PyObject_GC_UNTRACK(it);
923 Py_XDECREF(it->it_seq);
924 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000925}
926
927static int
928tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 Py_VISIT(it->it_seq);
931 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000932}
933
Raymond Hettinger48923c52002-08-09 01:30:17 +0000934static PyObject *
935tupleiter_next(tupleiterobject *it)
936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyTupleObject *seq;
938 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 assert(it != NULL);
941 seq = it->it_seq;
942 if (seq == NULL)
943 return NULL;
944 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 if (it->it_index < PyTuple_GET_SIZE(seq)) {
947 item = PyTuple_GET_ITEM(seq, it->it_index);
948 ++it->it_index;
949 Py_INCREF(item);
950 return item;
951 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 Py_DECREF(seq);
954 it->it_seq = NULL;
955 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000956}
957
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000958static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000959tupleiter_len(tupleiterobject *it)
960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 Py_ssize_t len = 0;
962 if (it->it_seq)
963 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
964 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000965}
966
Armin Rigof5b3e362006-02-11 21:32:43 +0000967PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000968
969static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
971 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +0000972};
973
Raymond Hettinger48923c52002-08-09 01:30:17 +0000974PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyVarObject_HEAD_INIT(&PyType_Type, 0)
976 "tuple_iterator", /* tp_name */
977 sizeof(tupleiterobject), /* tp_basicsize */
978 0, /* tp_itemsize */
979 /* methods */
980 (destructor)tupleiter_dealloc, /* tp_dealloc */
981 0, /* tp_print */
982 0, /* tp_getattr */
983 0, /* tp_setattr */
984 0, /* tp_reserved */
985 0, /* tp_repr */
986 0, /* tp_as_number */
987 0, /* tp_as_sequence */
988 0, /* tp_as_mapping */
989 0, /* tp_hash */
990 0, /* tp_call */
991 0, /* tp_str */
992 PyObject_GenericGetAttr, /* tp_getattro */
993 0, /* tp_setattro */
994 0, /* tp_as_buffer */
995 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
996 0, /* tp_doc */
997 (traverseproc)tupleiter_traverse, /* tp_traverse */
998 0, /* tp_clear */
999 0, /* tp_richcompare */
1000 0, /* tp_weaklistoffset */
1001 PyObject_SelfIter, /* tp_iter */
1002 (iternextfunc)tupleiter_next, /* tp_iternext */
1003 tupleiter_methods, /* tp_methods */
1004 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001005};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006
1007static PyObject *
1008tuple_iter(PyObject *seq)
1009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (!PyTuple_Check(seq)) {
1013 PyErr_BadInternalCall();
1014 return NULL;
1015 }
1016 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1017 if (it == NULL)
1018 return NULL;
1019 it->it_index = 0;
1020 Py_INCREF(seq);
1021 it->it_seq = (PyTupleObject *)seq;
1022 _PyObject_GC_TRACK(it);
1023 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024}