blob: 6f893d9c682ba50f167e5b825dbc01c6f4d089d7 [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 }
89 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{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000315 register Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 register Py_ssize_t len = Py_SIZE(v);
317 register PyObject **p;
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000318 Py_hash_t mult = 1000003L;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 x = 0x345678L;
320 p = v->ob_item;
321 while (--len >= 0) {
322 y = PyObject_Hash(*p++);
323 if (y == -1)
324 return -1;
325 x = (x ^ y) * mult;
326 /* the cast might truncate len; that doesn't change hash stability */
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000327 mult += (Py_hash_t)(82520L + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 }
329 x += 97531L;
330 if (x == -1)
331 x = -2;
332 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000333}
334
Martin v. Löwis18e16552006-02-15 17:27:45 +0000335static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000336tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339}
340
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000341static int
Fred Drakeba096332000-07-09 07:04:36 +0000342tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_ssize_t i;
345 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
348 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
349 Py_EQ);
350 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000351}
352
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000354tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (i < 0 || i >= Py_SIZE(a)) {
357 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
358 return NULL;
359 }
360 Py_INCREF(a->ob_item[i]);
361 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362}
363
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
366 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 register PyTupleObject *np;
369 PyObject **src, **dest;
370 register Py_ssize_t i;
371 Py_ssize_t len;
372 if (ilow < 0)
373 ilow = 0;
374 if (ihigh > Py_SIZE(a))
375 ihigh = Py_SIZE(a);
376 if (ihigh < ilow)
377 ihigh = ilow;
378 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
379 Py_INCREF(a);
380 return (PyObject *)a;
381 }
382 len = ihigh - ilow;
383 np = (PyTupleObject *)PyTuple_New(len);
384 if (np == NULL)
385 return NULL;
386 src = a->ob_item + ilow;
387 dest = np->ob_item;
388 for (i = 0; i < len; i++) {
389 PyObject *v = src[i];
390 Py_INCREF(v);
391 dest[i] = v;
392 }
393 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394}
395
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000397PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (op == NULL || !PyTuple_Check(op)) {
400 PyErr_BadInternalCall();
401 return NULL;
402 }
403 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000404}
405
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000407tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 register Py_ssize_t size;
410 register Py_ssize_t i;
411 PyObject **src, **dest;
412 PyTupleObject *np;
413 if (!PyTuple_Check(bb)) {
414 PyErr_Format(PyExc_TypeError,
415 "can only concatenate tuple (not \"%.200s\") to tuple",
416 Py_TYPE(bb)->tp_name);
417 return NULL;
418 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 size = Py_SIZE(a) + Py_SIZE(b);
421 if (size < 0)
422 return PyErr_NoMemory();
423 np = (PyTupleObject *) PyTuple_New(size);
424 if (np == NULL) {
425 return NULL;
426 }
427 src = a->ob_item;
428 dest = np->ob_item;
429 for (i = 0; i < Py_SIZE(a); i++) {
430 PyObject *v = src[i];
431 Py_INCREF(v);
432 dest[i] = v;
433 }
434 src = b->ob_item;
435 dest = np->ob_item + Py_SIZE(a);
436 for (i = 0; i < Py_SIZE(b); i++) {
437 PyObject *v = src[i];
438 Py_INCREF(v);
439 dest[i] = v;
440 }
441 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442#undef b
443}
444
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000446tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 Py_ssize_t i, j;
449 Py_ssize_t size;
450 PyTupleObject *np;
451 PyObject **p, **items;
452 if (n < 0)
453 n = 0;
454 if (Py_SIZE(a) == 0 || n == 1) {
455 if (PyTuple_CheckExact(a)) {
456 /* Since tuples are immutable, we can return a shared
457 copy in this case */
458 Py_INCREF(a);
459 return (PyObject *)a;
460 }
461 if (Py_SIZE(a) == 0)
462 return PyTuple_New(0);
463 }
464 size = Py_SIZE(a) * n;
465 if (size/Py_SIZE(a) != n)
466 return PyErr_NoMemory();
467 np = (PyTupleObject *) PyTuple_New(size);
468 if (np == NULL)
469 return NULL;
470 p = np->ob_item;
471 items = a->ob_item;
472 for (i = 0; i < n; i++) {
473 for (j = 0; j < Py_SIZE(a); j++) {
474 *p = items[j];
475 Py_INCREF(*p);
476 p++;
477 }
478 }
479 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000480}
481
Raymond Hettinger65baa342008-02-07 00:41:02 +0000482static PyObject *
483tupleindex(PyTupleObject *self, PyObject *args)
484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenc2f0a462011-11-05 23:20:57 +0200486 PyObject *v, *start_obj = NULL, *stop_obj = NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000487
Petri Lehtinenc2f0a462011-11-05 23:20:57 +0200488 if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 return NULL;
Petri Lehtinenc2f0a462011-11-05 23:20:57 +0200490
491 if (start_obj != Py_None)
492 if (!_PyEval_SliceIndex(start_obj, &start))
493 return NULL;
494
495 if (stop_obj != Py_None)
496 if (!_PyEval_SliceIndex(stop_obj, &stop))
497 return NULL;
498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (start < 0) {
500 start += Py_SIZE(self);
501 if (start < 0)
502 start = 0;
503 }
504 if (stop < 0) {
505 stop += Py_SIZE(self);
506 if (stop < 0)
507 stop = 0;
508 }
509 for (i = start; i < stop && i < Py_SIZE(self); i++) {
510 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
511 if (cmp > 0)
512 return PyLong_FromSsize_t(i);
513 else if (cmp < 0)
514 return NULL;
515 }
516 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
517 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000518}
519
520static PyObject *
521tuplecount(PyTupleObject *self, PyObject *v)
522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_ssize_t count = 0;
524 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 for (i = 0; i < Py_SIZE(self); i++) {
527 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
528 if (cmp > 0)
529 count++;
530 else if (cmp < 0)
531 return NULL;
532 }
533 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000534}
535
Jeremy Hylton8caad492000-06-23 14:18:11 +0000536static int
537tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 for (i = Py_SIZE(o); --i >= 0; )
542 Py_VISIT(o->ob_item[i]);
543 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000544}
545
Guido van Rossumf77bc622001-01-18 00:00:53 +0000546static PyObject *
547tuplerichcompare(PyObject *v, PyObject *w, int op)
548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyTupleObject *vt, *wt;
550 Py_ssize_t i;
551 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
554 Py_INCREF(Py_NotImplemented);
555 return Py_NotImplemented;
556 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 vt = (PyTupleObject *)v;
559 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 vlen = Py_SIZE(vt);
562 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 /* Note: the corresponding code for lists has an "early out" test
565 * here when op is EQ or NE and the lengths differ. That pays there,
566 * but Tim was unable to find any real code where EQ/NE tuple
567 * compares don't have the same length, so testing for it here would
568 * have cost without benefit.
569 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 /* Search for the first index where items are different.
572 * Note that because tuples are immutable, it's safe to reuse
573 * vlen and wlen across the comparison calls.
574 */
575 for (i = 0; i < vlen && i < wlen; i++) {
576 int k = PyObject_RichCompareBool(vt->ob_item[i],
577 wt->ob_item[i], Py_EQ);
578 if (k < 0)
579 return NULL;
580 if (!k)
581 break;
582 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (i >= vlen || i >= wlen) {
585 /* No more items to compare -- compare sizes */
586 int cmp;
587 PyObject *res;
588 switch (op) {
589 case Py_LT: cmp = vlen < wlen; break;
590 case Py_LE: cmp = vlen <= wlen; break;
591 case Py_EQ: cmp = vlen == wlen; break;
592 case Py_NE: cmp = vlen != wlen; break;
593 case Py_GT: cmp = vlen > wlen; break;
594 case Py_GE: cmp = vlen >= wlen; break;
595 default: return NULL; /* cannot happen */
596 }
597 if (cmp)
598 res = Py_True;
599 else
600 res = Py_False;
601 Py_INCREF(res);
602 return res;
603 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 /* We have an item that differs -- shortcuts for EQ/NE */
606 if (op == Py_EQ) {
607 Py_INCREF(Py_False);
608 return Py_False;
609 }
610 if (op == Py_NE) {
611 Py_INCREF(Py_True);
612 return Py_True;
613 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* Compare the final item again using the proper operator */
616 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000617}
618
Jeremy Hylton938ace62002-07-17 16:30:39 +0000619static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000620tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
621
Tim Peters6d6c1a32001-08-02 04:15:00 +0000622static PyObject *
623tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 PyObject *arg = NULL;
626 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 if (type != &PyTuple_Type)
629 return tuple_subtype_new(type, args, kwds);
630 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
631 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 if (arg == NULL)
634 return PyTuple_New(0);
635 else
636 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637}
638
Guido van Rossumae960af2001-08-30 03:11:59 +0000639static PyObject *
640tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyObject *tmp, *newobj, *item;
643 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 assert(PyType_IsSubtype(type, &PyTuple_Type));
646 tmp = tuple_new(&PyTuple_Type, args, kwds);
647 if (tmp == NULL)
648 return NULL;
649 assert(PyTuple_Check(tmp));
650 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
651 if (newobj == NULL)
652 return NULL;
653 for (i = 0; i < n; i++) {
654 item = PyTuple_GET_ITEM(tmp, i);
655 Py_INCREF(item);
656 PyTuple_SET_ITEM(newobj, i, item);
657 }
658 Py_DECREF(tmp);
659 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000660}
661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000662PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000663"tuple() -> empty tuple\n\
664tuple(iterable) -> tuple initialized from iterable's items\n\
665\n\
666If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000667
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 (lenfunc)tuplelength, /* sq_length */
670 (binaryfunc)tupleconcat, /* sq_concat */
671 (ssizeargfunc)tuplerepeat, /* sq_repeat */
672 (ssizeargfunc)tupleitem, /* sq_item */
673 0, /* sq_slice */
674 0, /* sq_ass_item */
675 0, /* sq_ass_slice */
676 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677};
678
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000679static PyObject*
680tuplesubscript(PyTupleObject* self, PyObject* item)
681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (PyIndex_Check(item)) {
683 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
684 if (i == -1 && PyErr_Occurred())
685 return NULL;
686 if (i < 0)
687 i += PyTuple_GET_SIZE(self);
688 return tupleitem(self, i);
689 }
690 else if (PySlice_Check(item)) {
691 Py_ssize_t start, stop, step, slicelength, cur, i;
692 PyObject* result;
693 PyObject* it;
694 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000695
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000696 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 PyTuple_GET_SIZE(self),
698 &start, &stop, &step, &slicelength) < 0) {
699 return NULL;
700 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if (slicelength <= 0) {
703 return PyTuple_New(0);
704 }
705 else if (start == 0 && step == 1 &&
706 slicelength == PyTuple_GET_SIZE(self) &&
707 PyTuple_CheckExact(self)) {
708 Py_INCREF(self);
709 return (PyObject *)self;
710 }
711 else {
712 result = PyTuple_New(slicelength);
713 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 src = self->ob_item;
716 dest = ((PyTupleObject *)result)->ob_item;
717 for (cur = start, i = 0; i < slicelength;
718 cur += step, i++) {
719 it = src[cur];
720 Py_INCREF(it);
721 dest[i] = it;
722 }
723
724 return result;
725 }
726 }
727 else {
728 PyErr_Format(PyExc_TypeError,
729 "tuple indices must be integers, not %.200s",
730 Py_TYPE(item)->tp_name);
731 return NULL;
732 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000733}
734
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000735static PyObject *
736tuple_getnewargs(PyTupleObject *v)
737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
739
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000740}
741
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000742static PyObject *
743tuple_sizeof(PyTupleObject *self)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
748 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000749}
750
Raymond Hettinger65baa342008-02-07 00:41:02 +0000751PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000752"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
753"Raises ValueError if the value is not present."
754);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000755PyDoc_STRVAR(count_doc,
756"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000757PyDoc_STRVAR(sizeof_doc,
758"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000759
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000760static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
762 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
763 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
764 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
765 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000766};
767
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000768static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 (lenfunc)tuplelength,
770 (binaryfunc)tuplesubscript,
771 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000772};
773
Raymond Hettinger48923c52002-08-09 01:30:17 +0000774static PyObject *tuple_iter(PyObject *seq);
775
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000776PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 PyVarObject_HEAD_INIT(&PyType_Type, 0)
778 "tuple",
779 sizeof(PyTupleObject) - sizeof(PyObject *),
780 sizeof(PyObject *),
781 (destructor)tupledealloc, /* tp_dealloc */
782 0, /* tp_print */
783 0, /* tp_getattr */
784 0, /* tp_setattr */
785 0, /* tp_reserved */
786 (reprfunc)tuplerepr, /* tp_repr */
787 0, /* tp_as_number */
788 &tuple_as_sequence, /* tp_as_sequence */
789 &tuple_as_mapping, /* tp_as_mapping */
790 (hashfunc)tuplehash, /* tp_hash */
791 0, /* tp_call */
792 0, /* tp_str */
793 PyObject_GenericGetAttr, /* tp_getattro */
794 0, /* tp_setattro */
795 0, /* tp_as_buffer */
796 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
797 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
798 tuple_doc, /* tp_doc */
799 (traverseproc)tupletraverse, /* tp_traverse */
800 0, /* tp_clear */
801 tuplerichcompare, /* tp_richcompare */
802 0, /* tp_weaklistoffset */
803 tuple_iter, /* tp_iter */
804 0, /* tp_iternext */
805 tuple_methods, /* tp_methods */
806 0, /* tp_members */
807 0, /* tp_getset */
808 0, /* tp_base */
809 0, /* tp_dict */
810 0, /* tp_descr_get */
811 0, /* tp_descr_set */
812 0, /* tp_dictoffset */
813 0, /* tp_init */
814 0, /* tp_alloc */
815 tuple_new, /* tp_new */
816 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000818
819/* The following function breaks the notion that tuples are immutable:
820 it changes the size of a tuple. We get away with this only if there
821 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000822 as creating a new tuple object and destroying the old one, only more
823 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000824 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825
826int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000827_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 register PyTupleObject *v;
830 register PyTupleObject *sv;
831 Py_ssize_t i;
832 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 v = (PyTupleObject *) *pv;
835 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
836 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
837 *pv = 0;
838 Py_XDECREF(v);
839 PyErr_BadInternalCall();
840 return -1;
841 }
842 oldsize = Py_SIZE(v);
843 if (oldsize == newsize)
844 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 if (oldsize == 0) {
847 /* Empty tuples are often shared, so we should never
848 resize them in-place even if we do own the only
849 (current) reference */
850 Py_DECREF(v);
851 *pv = PyTuple_New(newsize);
852 return *pv == NULL ? -1 : 0;
853 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 /* XXX UNREF/NEWREF interface should be more symmetrical */
856 _Py_DEC_REFTOTAL;
857 if (_PyObject_GC_IS_TRACKED(v))
858 _PyObject_GC_UNTRACK(v);
859 _Py_ForgetReference((PyObject *) v);
860 /* DECREF items deleted by shrinkage */
861 for (i = newsize; i < oldsize; i++) {
862 Py_XDECREF(v->ob_item[i]);
863 v->ob_item[i] = NULL;
864 }
865 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
866 if (sv == NULL) {
867 *pv = NULL;
868 PyObject_GC_Del(v);
869 return -1;
870 }
871 _Py_NewReference((PyObject *) sv);
872 /* Zero out items added by growing */
873 if (newsize > oldsize)
874 memset(&sv->ob_item[oldsize], 0,
875 sizeof(*sv->ob_item) * (newsize - oldsize));
876 *pv = (PyObject *) sv;
877 _PyObject_GC_TRACK(sv);
878 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000879}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000880
Christian Heimesa156e092008-02-16 07:38:31 +0000881int
882PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000885#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 int i;
887 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
888 PyTupleObject *p, *q;
889 p = free_list[i];
890 freelist_size += numfree[i];
891 free_list[i] = NULL;
892 numfree[i] = 0;
893 while (p) {
894 q = p;
895 p = (PyTupleObject *)(p->ob_item[0]);
896 PyObject_GC_Del(q);
897 }
898 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000899#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000901}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902
Christian Heimesa156e092008-02-16 07:38:31 +0000903void
904PyTuple_Fini(void)
905{
906#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 /* empty tuples are used all over the place and applications may
908 * rely on the fact that an empty tuple is a singleton. */
909 Py_XDECREF(free_list[0]);
910 free_list[0] = NULL;
Christian Heimesa156e092008-02-16 07:38:31 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000913#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000914#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000916#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000917}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000918
919/*********************** Tuple Iterator **************************/
920
921typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 PyObject_HEAD
923 long it_index;
924 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000925} tupleiterobject;
926
Raymond Hettinger48923c52002-08-09 01:30:17 +0000927static void
928tupleiter_dealloc(tupleiterobject *it)
929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 _PyObject_GC_UNTRACK(it);
931 Py_XDECREF(it->it_seq);
932 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000933}
934
935static int
936tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Py_VISIT(it->it_seq);
939 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000940}
941
Raymond Hettinger48923c52002-08-09 01:30:17 +0000942static PyObject *
943tupleiter_next(tupleiterobject *it)
944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 PyTupleObject *seq;
946 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 assert(it != NULL);
949 seq = it->it_seq;
950 if (seq == NULL)
951 return NULL;
952 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (it->it_index < PyTuple_GET_SIZE(seq)) {
955 item = PyTuple_GET_ITEM(seq, it->it_index);
956 ++it->it_index;
957 Py_INCREF(item);
958 return item;
959 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 Py_DECREF(seq);
962 it->it_seq = NULL;
963 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000964}
965
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000966static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000967tupleiter_len(tupleiterobject *it)
968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 Py_ssize_t len = 0;
970 if (it->it_seq)
971 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
972 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000973}
974
Armin Rigof5b3e362006-02-11 21:32:43 +0000975PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000976
977static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
979 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +0000980};
981
Raymond Hettinger48923c52002-08-09 01:30:17 +0000982PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 PyVarObject_HEAD_INIT(&PyType_Type, 0)
984 "tuple_iterator", /* tp_name */
985 sizeof(tupleiterobject), /* tp_basicsize */
986 0, /* tp_itemsize */
987 /* methods */
988 (destructor)tupleiter_dealloc, /* tp_dealloc */
989 0, /* tp_print */
990 0, /* tp_getattr */
991 0, /* tp_setattr */
992 0, /* tp_reserved */
993 0, /* tp_repr */
994 0, /* tp_as_number */
995 0, /* tp_as_sequence */
996 0, /* tp_as_mapping */
997 0, /* tp_hash */
998 0, /* tp_call */
999 0, /* tp_str */
1000 PyObject_GenericGetAttr, /* tp_getattro */
1001 0, /* tp_setattro */
1002 0, /* tp_as_buffer */
1003 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1004 0, /* tp_doc */
1005 (traverseproc)tupleiter_traverse, /* tp_traverse */
1006 0, /* tp_clear */
1007 0, /* tp_richcompare */
1008 0, /* tp_weaklistoffset */
1009 PyObject_SelfIter, /* tp_iter */
1010 (iternextfunc)tupleiter_next, /* tp_iternext */
1011 tupleiter_methods, /* tp_methods */
1012 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001013};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014
1015static PyObject *
1016tuple_iter(PyObject *seq)
1017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 if (!PyTuple_Check(seq)) {
1021 PyErr_BadInternalCall();
1022 return NULL;
1023 }
1024 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1025 if (it == NULL)
1026 return NULL;
1027 it->it_index = 0;
1028 Py_INCREF(seq);
1029 it->it_seq = (PyTupleObject *)seq;
1030 _PyObject_GC_TRACK(it);
1031 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001032}