blob: 013db69414fb6dedc9c4d35b2be1bb68347c601b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Tuple object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01005#include "accu.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum5ce78f82000-04-21 21:15:05 +00007/* Speed optimization to avoid frequent malloc/free of small tuples */
Christian Heimes2202f872008-02-06 14:31:34 +00008#ifndef PyTuple_MAXSAVESIZE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +000010#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011#ifndef PyTuple_MAXFREELIST
Christian Heimes2202f872008-02-06 14:31:34 +000012#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000013#endif
14
Christian Heimes2202f872008-02-06 14:31:34 +000015#if PyTuple_MAXSAVESIZE > 0
16/* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000017 tuple () of which at most one instance will be allocated.
18*/
Christian Heimes2202f872008-02-06 14:31:34 +000019static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
20static int numfree[PyTuple_MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000021#endif
22#ifdef COUNT_ALLOCS
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000023Py_ssize_t fast_tuple_allocs;
24Py_ssize_t tuple_zero_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025#endif
26
Antoine Pitrou3a652b12009-03-23 18:52:06 +000027/* Debug statistic to count GC tracking of tuples.
28 Please note that tuples are only untracked when considered by the GC, and
29 many of them will be dead before. Therefore, a tracking rate close to 100%
30 does not necessarily prove that the heuristic is inefficient.
31*/
32#ifdef SHOW_TRACK_COUNT
33static Py_ssize_t count_untracked = 0;
34static Py_ssize_t count_tracked = 0;
35
36static void
37show_track(void)
38{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
40 count_tracked + count_untracked);
41 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
42 "d\n", count_tracked);
43 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
44 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000045}
46#endif
47
David Malcolm49526f42012-06-22 14:55:41 -040048/* Print summary info about the state of the optimized allocator */
49void
50_PyTuple_DebugMallocStats(FILE *out)
51{
52#if PyTuple_MAXSAVESIZE > 0
53 int i;
54 char buf[128];
55 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
56 PyOS_snprintf(buf, sizeof(buf),
57 "free %d-sized PyTupleObject", i);
58 _PyDebugAllocatorStats(out,
59 buf,
60 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
61 }
62#endif
63}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000064
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000066PyTuple_New(register Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 register PyTupleObject *op;
69 Py_ssize_t i;
70 if (size < 0) {
71 PyErr_BadInternalCall();
72 return NULL;
73 }
Christian Heimes2202f872008-02-06 14:31:34 +000074#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (size == 0 && free_list[0]) {
76 op = free_list[0];
77 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000078#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 return (PyObject *) op;
82 }
83 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
84 free_list[size] = (PyTupleObject *) op->ob_item[0];
85 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000086#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000088#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000090#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 Py_SIZE(op) = size;
92 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 _Py_NewReference((PyObject *)op);
95 }
96 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000097#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 {
99 Py_ssize_t nbytes = size * sizeof(PyObject *);
100 /* Check for overflow */
101 if (nbytes / sizeof(PyObject *) != (size_t)size ||
102 (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
103 {
104 return PyErr_NoMemory();
105 }
Brett Cannonb94767f2011-02-22 20:15:44 +0000106 /* nbytes += sizeof(PyTupleObject) - sizeof(PyObject *); */
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
109 if (op == NULL)
110 return NULL;
111 }
112 for (i=0; i < size; i++)
113 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000114#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (size == 0) {
116 free_list[0] = op;
117 ++numfree[0];
118 Py_INCREF(op); /* extra INCREF so that this is never freed */
119 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000120#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000121#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000123#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 _PyObject_GC_TRACK(op);
125 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126}
127
Martin v. Löwis18e16552006-02-15 17:27:45 +0000128Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000129PyTuple_Size(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 if (!PyTuple_Check(op)) {
132 PyErr_BadInternalCall();
133 return -1;
134 }
135 else
136 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137}
138
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000140PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (!PyTuple_Check(op)) {
143 PyErr_BadInternalCall();
144 return NULL;
145 }
146 if (i < 0 || i >= Py_SIZE(op)) {
147 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
148 return NULL;
149 }
150 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151}
152
153int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 register PyObject *olditem;
157 register PyObject **p;
158 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
159 Py_XDECREF(newitem);
160 PyErr_BadInternalCall();
161 return -1;
162 }
163 if (i < 0 || i >= Py_SIZE(op)) {
164 Py_XDECREF(newitem);
165 PyErr_SetString(PyExc_IndexError,
166 "tuple assignment index out of range");
167 return -1;
168 }
169 p = ((PyTupleObject *)op) -> ob_item + i;
170 olditem = *p;
171 *p = newitem;
172 Py_XDECREF(olditem);
173 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174}
175
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000176void
177_PyTuple_MaybeUntrack(PyObject *op)
178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 PyTupleObject *t;
180 Py_ssize_t i, n;
181
182 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
183 return;
184 t = (PyTupleObject *) op;
185 n = Py_SIZE(t);
186 for (i = 0; i < n; i++) {
187 PyObject *elt = PyTuple_GET_ITEM(t, i);
188 /* Tuple with NULL elements aren't
189 fully constructed, don't untrack
190 them yet. */
191 if (!elt ||
192 _PyObject_GC_MAY_BE_TRACKED(elt))
193 return;
194 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000195#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 count_tracked--;
197 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000198#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000200}
201
Raymond Hettingercb2da432003-10-12 18:24:34 +0000202PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000203PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 Py_ssize_t i;
206 PyObject *o;
207 PyObject *result;
208 PyObject **items;
209 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 va_start(vargs, n);
212 result = PyTuple_New(n);
213 if (result == NULL)
214 return NULL;
215 items = ((PyTupleObject *)result)->ob_item;
216 for (i = 0; i < n; i++) {
217 o = va_arg(vargs, PyObject *);
218 Py_INCREF(o);
219 items[i] = o;
220 }
221 va_end(vargs);
222 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000223}
224
225
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226/* Methods */
227
228static void
Fred Drakeba096332000-07-09 07:04:36 +0000229tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 register Py_ssize_t i;
232 register Py_ssize_t len = Py_SIZE(op);
233 PyObject_GC_UnTrack(op);
234 Py_TRASHCAN_SAFE_BEGIN(op)
235 if (len > 0) {
236 i = len;
237 while (--i >= 0)
238 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000239#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (len < PyTuple_MAXSAVESIZE &&
241 numfree[len] < PyTuple_MAXFREELIST &&
242 Py_TYPE(op) == &PyTuple_Type)
243 {
244 op->ob_item[0] = (PyObject *) free_list[len];
245 numfree[len]++;
246 free_list[len] = op;
247 goto done; /* return */
248 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000249#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 }
251 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000252done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254}
255
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000257tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 Py_ssize_t i, n;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200260 PyObject *s = NULL;
261 _PyAccu acc;
262 static PyObject *sep = NULL;
Tim Petersa7259592001-06-16 05:11:17 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 n = Py_SIZE(v);
265 if (n == 0)
266 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000267
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200268 if (sep == NULL) {
269 sep = PyUnicode_FromString(", ");
270 if (sep == NULL)
271 return NULL;
272 }
273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 ? PyUnicode_FromString("(...)") : NULL;
281 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000282
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200283 if (_PyAccu_Init(&acc))
284 goto error;
285
286 s = PyUnicode_FromString("(");
287 if (s == NULL || _PyAccu_Accumulate(&acc, s))
288 goto error;
289 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* Do repr() on each element. */
292 for (i = 0; i < n; ++i) {
293 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200294 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 s = PyObject_Repr(v->ob_item[i]);
296 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200297 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
298 goto error;
299 if (s == NULL || _PyAccu_Accumulate(&acc, s))
300 goto error;
301 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200303 if (n > 1)
304 s = PyUnicode_FromString(")");
305 else
306 s = PyUnicode_FromString(",)");
307 if (s == NULL || _PyAccu_Accumulate(&acc, s))
308 goto error;
309 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200312 return _PyAccu_Finish(&acc);
313
314error:
315 _PyAccu_Destroy(&acc);
316 Py_XDECREF(s);
317 Py_ReprLeave((PyObject *)v);
318 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319}
320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321/* The addend 82520, was selected from the range(0, 1000000) for
322 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000323 upto length eight:
324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000326 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
327*/
328
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000329static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000330tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000331{
Mark Dickinson57e683e2011-09-24 18:18:40 +0100332 register Py_uhash_t x;
333 register Py_hash_t y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 register Py_ssize_t len = Py_SIZE(v);
335 register PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800336 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100337 x = 0x345678;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 p = v->ob_item;
339 while (--len >= 0) {
340 y = PyObject_Hash(*p++);
341 if (y == -1)
342 return -1;
343 x = (x ^ y) * mult;
344 /* the cast might truncate len; that doesn't change hash stability */
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000345 mult += (Py_hash_t)(82520L + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 }
347 x += 97531L;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100348 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 x = -2;
350 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000351}
352
Martin v. Löwis18e16552006-02-15 17:27:45 +0000353static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000354tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357}
358
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000359static int
Fred Drakeba096332000-07-09 07:04:36 +0000360tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Py_ssize_t i;
363 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
366 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
367 Py_EQ);
368 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000369}
370
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 if (i < 0 || i >= Py_SIZE(a)) {
375 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
376 return NULL;
377 }
378 Py_INCREF(a->ob_item[i]);
379 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380}
381
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
384 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 register PyTupleObject *np;
387 PyObject **src, **dest;
388 register Py_ssize_t i;
389 Py_ssize_t len;
390 if (ilow < 0)
391 ilow = 0;
392 if (ihigh > Py_SIZE(a))
393 ihigh = Py_SIZE(a);
394 if (ihigh < ilow)
395 ihigh = ilow;
396 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
397 Py_INCREF(a);
398 return (PyObject *)a;
399 }
400 len = ihigh - ilow;
401 np = (PyTupleObject *)PyTuple_New(len);
402 if (np == NULL)
403 return NULL;
404 src = a->ob_item + ilow;
405 dest = np->ob_item;
406 for (i = 0; i < len; i++) {
407 PyObject *v = src[i];
408 Py_INCREF(v);
409 dest[i] = v;
410 }
411 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412}
413
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000415PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (op == NULL || !PyTuple_Check(op)) {
418 PyErr_BadInternalCall();
419 return NULL;
420 }
421 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000422}
423
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000425tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 register Py_ssize_t size;
428 register Py_ssize_t i;
429 PyObject **src, **dest;
430 PyTupleObject *np;
431 if (!PyTuple_Check(bb)) {
432 PyErr_Format(PyExc_TypeError,
433 "can only concatenate tuple (not \"%.200s\") to tuple",
434 Py_TYPE(bb)->tp_name);
435 return NULL;
436 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 size = Py_SIZE(a) + Py_SIZE(b);
439 if (size < 0)
440 return PyErr_NoMemory();
441 np = (PyTupleObject *) PyTuple_New(size);
442 if (np == NULL) {
443 return NULL;
444 }
445 src = a->ob_item;
446 dest = np->ob_item;
447 for (i = 0; i < Py_SIZE(a); i++) {
448 PyObject *v = src[i];
449 Py_INCREF(v);
450 dest[i] = v;
451 }
452 src = b->ob_item;
453 dest = np->ob_item + Py_SIZE(a);
454 for (i = 0; i < Py_SIZE(b); i++) {
455 PyObject *v = src[i];
456 Py_INCREF(v);
457 dest[i] = v;
458 }
459 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460#undef b
461}
462
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000464tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 Py_ssize_t i, j;
467 Py_ssize_t size;
468 PyTupleObject *np;
469 PyObject **p, **items;
470 if (n < 0)
471 n = 0;
472 if (Py_SIZE(a) == 0 || n == 1) {
473 if (PyTuple_CheckExact(a)) {
474 /* Since tuples are immutable, we can return a shared
475 copy in this case */
476 Py_INCREF(a);
477 return (PyObject *)a;
478 }
479 if (Py_SIZE(a) == 0)
480 return PyTuple_New(0);
481 }
482 size = Py_SIZE(a) * n;
483 if (size/Py_SIZE(a) != n)
484 return PyErr_NoMemory();
485 np = (PyTupleObject *) PyTuple_New(size);
486 if (np == NULL)
487 return NULL;
488 p = np->ob_item;
489 items = a->ob_item;
490 for (i = 0; i < n; i++) {
491 for (j = 0; j < Py_SIZE(a); j++) {
492 *p = items[j];
493 Py_INCREF(*p);
494 p++;
495 }
496 }
497 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000498}
499
Raymond Hettinger65baa342008-02-07 00:41:02 +0000500static PyObject *
501tupleindex(PyTupleObject *self, PyObject *args)
502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200504 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000505
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200506 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
507 _PyEval_SliceIndex, &start,
508 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return NULL;
510 if (start < 0) {
511 start += Py_SIZE(self);
512 if (start < 0)
513 start = 0;
514 }
515 if (stop < 0) {
516 stop += Py_SIZE(self);
517 if (stop < 0)
518 stop = 0;
519 }
520 for (i = start; i < stop && i < Py_SIZE(self); i++) {
521 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
522 if (cmp > 0)
523 return PyLong_FromSsize_t(i);
524 else if (cmp < 0)
525 return NULL;
526 }
527 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
528 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000529}
530
531static PyObject *
532tuplecount(PyTupleObject *self, PyObject *v)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 Py_ssize_t count = 0;
535 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 for (i = 0; i < Py_SIZE(self); i++) {
538 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
539 if (cmp > 0)
540 count++;
541 else if (cmp < 0)
542 return NULL;
543 }
544 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000545}
546
Jeremy Hylton8caad492000-06-23 14:18:11 +0000547static int
548tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 for (i = Py_SIZE(o); --i >= 0; )
553 Py_VISIT(o->ob_item[i]);
554 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000555}
556
Guido van Rossumf77bc622001-01-18 00:00:53 +0000557static PyObject *
558tuplerichcompare(PyObject *v, PyObject *w, int op)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 PyTupleObject *vt, *wt;
561 Py_ssize_t i;
562 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000563
Brian Curtindfc80e32011-08-10 20:28:54 -0500564 if (!PyTuple_Check(v) || !PyTuple_Check(w))
565 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 vt = (PyTupleObject *)v;
568 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 vlen = Py_SIZE(vt);
571 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 /* Note: the corresponding code for lists has an "early out" test
574 * here when op is EQ or NE and the lengths differ. That pays there,
575 * but Tim was unable to find any real code where EQ/NE tuple
576 * compares don't have the same length, so testing for it here would
577 * have cost without benefit.
578 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 /* Search for the first index where items are different.
581 * Note that because tuples are immutable, it's safe to reuse
582 * vlen and wlen across the comparison calls.
583 */
584 for (i = 0; i < vlen && i < wlen; i++) {
585 int k = PyObject_RichCompareBool(vt->ob_item[i],
586 wt->ob_item[i], Py_EQ);
587 if (k < 0)
588 return NULL;
589 if (!k)
590 break;
591 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (i >= vlen || i >= wlen) {
594 /* No more items to compare -- compare sizes */
595 int cmp;
596 PyObject *res;
597 switch (op) {
598 case Py_LT: cmp = vlen < wlen; break;
599 case Py_LE: cmp = vlen <= wlen; break;
600 case Py_EQ: cmp = vlen == wlen; break;
601 case Py_NE: cmp = vlen != wlen; break;
602 case Py_GT: cmp = vlen > wlen; break;
603 case Py_GE: cmp = vlen >= wlen; break;
604 default: return NULL; /* cannot happen */
605 }
606 if (cmp)
607 res = Py_True;
608 else
609 res = Py_False;
610 Py_INCREF(res);
611 return res;
612 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 /* We have an item that differs -- shortcuts for EQ/NE */
615 if (op == Py_EQ) {
616 Py_INCREF(Py_False);
617 return Py_False;
618 }
619 if (op == Py_NE) {
620 Py_INCREF(Py_True);
621 return Py_True;
622 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 /* Compare the final item again using the proper operator */
625 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000626}
627
Jeremy Hylton938ace62002-07-17 16:30:39 +0000628static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000629tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
630
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631static PyObject *
632tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PyObject *arg = NULL;
635 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (type != &PyTuple_Type)
638 return tuple_subtype_new(type, args, kwds);
639 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
640 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 if (arg == NULL)
643 return PyTuple_New(0);
644 else
645 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000646}
647
Guido van Rossumae960af2001-08-30 03:11:59 +0000648static PyObject *
649tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PyObject *tmp, *newobj, *item;
652 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 assert(PyType_IsSubtype(type, &PyTuple_Type));
655 tmp = tuple_new(&PyTuple_Type, args, kwds);
656 if (tmp == NULL)
657 return NULL;
658 assert(PyTuple_Check(tmp));
659 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
660 if (newobj == NULL)
661 return NULL;
662 for (i = 0; i < n; i++) {
663 item = PyTuple_GET_ITEM(tmp, i);
664 Py_INCREF(item);
665 PyTuple_SET_ITEM(newobj, i, item);
666 }
667 Py_DECREF(tmp);
668 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000669}
670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000672"tuple() -> empty tuple\n\
673tuple(iterable) -> tuple initialized from iterable's items\n\
674\n\
675If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 (lenfunc)tuplelength, /* sq_length */
679 (binaryfunc)tupleconcat, /* sq_concat */
680 (ssizeargfunc)tuplerepeat, /* sq_repeat */
681 (ssizeargfunc)tupleitem, /* sq_item */
682 0, /* sq_slice */
683 0, /* sq_ass_item */
684 0, /* sq_ass_slice */
685 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686};
687
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000688static PyObject*
689tuplesubscript(PyTupleObject* self, PyObject* item)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if (PyIndex_Check(item)) {
692 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
693 if (i == -1 && PyErr_Occurred())
694 return NULL;
695 if (i < 0)
696 i += PyTuple_GET_SIZE(self);
697 return tupleitem(self, i);
698 }
699 else if (PySlice_Check(item)) {
700 Py_ssize_t start, stop, step, slicelength, cur, i;
701 PyObject* result;
702 PyObject* it;
703 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000704
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000705 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyTuple_GET_SIZE(self),
707 &start, &stop, &step, &slicelength) < 0) {
708 return NULL;
709 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 if (slicelength <= 0) {
712 return PyTuple_New(0);
713 }
714 else if (start == 0 && step == 1 &&
715 slicelength == PyTuple_GET_SIZE(self) &&
716 PyTuple_CheckExact(self)) {
717 Py_INCREF(self);
718 return (PyObject *)self;
719 }
720 else {
721 result = PyTuple_New(slicelength);
722 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 src = self->ob_item;
725 dest = ((PyTupleObject *)result)->ob_item;
726 for (cur = start, i = 0; i < slicelength;
727 cur += step, i++) {
728 it = src[cur];
729 Py_INCREF(it);
730 dest[i] = it;
731 }
732
733 return result;
734 }
735 }
736 else {
737 PyErr_Format(PyExc_TypeError,
738 "tuple indices must be integers, not %.200s",
739 Py_TYPE(item)->tp_name);
740 return NULL;
741 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000742}
743
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000744static PyObject *
745tuple_getnewargs(PyTupleObject *v)
746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
748
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000749}
750
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000751static PyObject *
752tuple_sizeof(PyTupleObject *self)
753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
757 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000758}
759
Raymond Hettinger65baa342008-02-07 00:41:02 +0000760PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000761"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
762"Raises ValueError if the value is not present."
763);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000764PyDoc_STRVAR(count_doc,
765"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000766PyDoc_STRVAR(sizeof_doc,
767"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000768
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000769static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
771 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
772 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
773 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
774 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000775};
776
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000777static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 (lenfunc)tuplelength,
779 (binaryfunc)tuplesubscript,
780 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000781};
782
Raymond Hettinger48923c52002-08-09 01:30:17 +0000783static PyObject *tuple_iter(PyObject *seq);
784
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PyVarObject_HEAD_INIT(&PyType_Type, 0)
787 "tuple",
788 sizeof(PyTupleObject) - sizeof(PyObject *),
789 sizeof(PyObject *),
790 (destructor)tupledealloc, /* tp_dealloc */
791 0, /* tp_print */
792 0, /* tp_getattr */
793 0, /* tp_setattr */
794 0, /* tp_reserved */
795 (reprfunc)tuplerepr, /* tp_repr */
796 0, /* tp_as_number */
797 &tuple_as_sequence, /* tp_as_sequence */
798 &tuple_as_mapping, /* tp_as_mapping */
799 (hashfunc)tuplehash, /* tp_hash */
800 0, /* tp_call */
801 0, /* tp_str */
802 PyObject_GenericGetAttr, /* tp_getattro */
803 0, /* tp_setattro */
804 0, /* tp_as_buffer */
805 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
806 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
807 tuple_doc, /* tp_doc */
808 (traverseproc)tupletraverse, /* tp_traverse */
809 0, /* tp_clear */
810 tuplerichcompare, /* tp_richcompare */
811 0, /* tp_weaklistoffset */
812 tuple_iter, /* tp_iter */
813 0, /* tp_iternext */
814 tuple_methods, /* tp_methods */
815 0, /* tp_members */
816 0, /* tp_getset */
817 0, /* tp_base */
818 0, /* tp_dict */
819 0, /* tp_descr_get */
820 0, /* tp_descr_set */
821 0, /* tp_dictoffset */
822 0, /* tp_init */
823 0, /* tp_alloc */
824 tuple_new, /* tp_new */
825 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000826};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000827
828/* The following function breaks the notion that tuples are immutable:
829 it changes the size of a tuple. We get away with this only if there
830 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000831 as creating a new tuple object and destroying the old one, only more
832 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000833 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000834
835int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000836_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 register PyTupleObject *v;
839 register PyTupleObject *sv;
840 Py_ssize_t i;
841 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 v = (PyTupleObject *) *pv;
844 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
845 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
846 *pv = 0;
847 Py_XDECREF(v);
848 PyErr_BadInternalCall();
849 return -1;
850 }
851 oldsize = Py_SIZE(v);
852 if (oldsize == newsize)
853 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 if (oldsize == 0) {
856 /* Empty tuples are often shared, so we should never
857 resize them in-place even if we do own the only
858 (current) reference */
859 Py_DECREF(v);
860 *pv = PyTuple_New(newsize);
861 return *pv == NULL ? -1 : 0;
862 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 /* XXX UNREF/NEWREF interface should be more symmetrical */
865 _Py_DEC_REFTOTAL;
866 if (_PyObject_GC_IS_TRACKED(v))
867 _PyObject_GC_UNTRACK(v);
868 _Py_ForgetReference((PyObject *) v);
869 /* DECREF items deleted by shrinkage */
870 for (i = newsize; i < oldsize; i++) {
871 Py_XDECREF(v->ob_item[i]);
872 v->ob_item[i] = NULL;
873 }
874 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
875 if (sv == NULL) {
876 *pv = NULL;
877 PyObject_GC_Del(v);
878 return -1;
879 }
880 _Py_NewReference((PyObject *) sv);
881 /* Zero out items added by growing */
882 if (newsize > oldsize)
883 memset(&sv->ob_item[oldsize], 0,
884 sizeof(*sv->ob_item) * (newsize - oldsize));
885 *pv = (PyObject *) sv;
886 _PyObject_GC_TRACK(sv);
887 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000888}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000889
Christian Heimesa156e092008-02-16 07:38:31 +0000890int
891PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000894#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 int i;
896 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
897 PyTupleObject *p, *q;
898 p = free_list[i];
899 freelist_size += numfree[i];
900 free_list[i] = NULL;
901 numfree[i] = 0;
902 while (p) {
903 q = p;
904 p = (PyTupleObject *)(p->ob_item[0]);
905 PyObject_GC_Del(q);
906 }
907 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000908#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000910}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911
Christian Heimesa156e092008-02-16 07:38:31 +0000912void
913PyTuple_Fini(void)
914{
915#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 /* empty tuples are used all over the place and applications may
917 * rely on the fact that an empty tuple is a singleton. */
918 Py_XDECREF(free_list[0]);
919 free_list[0] = NULL;
Christian Heimesa156e092008-02-16 07:38:31 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000922#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000923#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000925#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000926}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000927
928/*********************** Tuple Iterator **************************/
929
930typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 PyObject_HEAD
932 long it_index;
933 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000934} tupleiterobject;
935
Raymond Hettinger48923c52002-08-09 01:30:17 +0000936static void
937tupleiter_dealloc(tupleiterobject *it)
938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 _PyObject_GC_UNTRACK(it);
940 Py_XDECREF(it->it_seq);
941 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000942}
943
944static int
945tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 Py_VISIT(it->it_seq);
948 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000949}
950
Raymond Hettinger48923c52002-08-09 01:30:17 +0000951static PyObject *
952tupleiter_next(tupleiterobject *it)
953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 PyTupleObject *seq;
955 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 assert(it != NULL);
958 seq = it->it_seq;
959 if (seq == NULL)
960 return NULL;
961 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 if (it->it_index < PyTuple_GET_SIZE(seq)) {
964 item = PyTuple_GET_ITEM(seq, it->it_index);
965 ++it->it_index;
966 Py_INCREF(item);
967 return item;
968 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 Py_DECREF(seq);
971 it->it_seq = NULL;
972 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973}
974
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000975static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000976tupleiter_len(tupleiterobject *it)
977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 Py_ssize_t len = 0;
979 if (it->it_seq)
980 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
981 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000982}
983
Armin Rigof5b3e362006-02-11 21:32:43 +0000984PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000985
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000986static PyObject *
987tupleiter_reduce(tupleiterobject *it)
988{
989 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +0200990 return Py_BuildValue("N(O)l", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000991 it->it_seq, it->it_index);
992 else
Antoine Pitroua7013882012-04-05 00:04:20 +0200993 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000994}
995
996static PyObject *
997tupleiter_setstate(tupleiterobject *it, PyObject *state)
998{
999 long index = PyLong_AsLong(state);
1000 if (index == -1 && PyErr_Occurred())
1001 return NULL;
1002 if (it->it_seq != NULL) {
1003 if (index < 0)
1004 index = 0;
1005 else if (it->it_seq != NULL && index > PyTuple_GET_SIZE(it->it_seq))
1006 index = PyTuple_GET_SIZE(it->it_seq);
1007 it->it_index = index;
1008 }
1009 Py_RETURN_NONE;
1010}
1011
1012PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1013PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1014
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001015static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001017 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1018 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001020};
1021
Raymond Hettinger48923c52002-08-09 01:30:17 +00001022PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1024 "tuple_iterator", /* tp_name */
1025 sizeof(tupleiterobject), /* tp_basicsize */
1026 0, /* tp_itemsize */
1027 /* methods */
1028 (destructor)tupleiter_dealloc, /* tp_dealloc */
1029 0, /* tp_print */
1030 0, /* tp_getattr */
1031 0, /* tp_setattr */
1032 0, /* tp_reserved */
1033 0, /* tp_repr */
1034 0, /* tp_as_number */
1035 0, /* tp_as_sequence */
1036 0, /* tp_as_mapping */
1037 0, /* tp_hash */
1038 0, /* tp_call */
1039 0, /* tp_str */
1040 PyObject_GenericGetAttr, /* tp_getattro */
1041 0, /* tp_setattro */
1042 0, /* tp_as_buffer */
1043 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1044 0, /* tp_doc */
1045 (traverseproc)tupleiter_traverse, /* tp_traverse */
1046 0, /* tp_clear */
1047 0, /* tp_richcompare */
1048 0, /* tp_weaklistoffset */
1049 PyObject_SelfIter, /* tp_iter */
1050 (iternextfunc)tupleiter_next, /* tp_iternext */
1051 tupleiter_methods, /* tp_methods */
1052 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001053};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001054
1055static PyObject *
1056tuple_iter(PyObject *seq)
1057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 if (!PyTuple_Check(seq)) {
1061 PyErr_BadInternalCall();
1062 return NULL;
1063 }
1064 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1065 if (it == NULL)
1066 return NULL;
1067 it->it_index = 0;
1068 Py_INCREF(seq);
1069 it->it_seq = (PyTupleObject *)seq;
1070 _PyObject_GC_TRACK(it);
1071 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001072}