blob: a33d8c06ee0c2cee3c444588debbe765ea5c6df8 [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 *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020066PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020068 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 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 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 /* Check for overflow */
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100100 if (size > (PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
101 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 return PyErr_NoMemory();
103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
105 if (op == NULL)
106 return NULL;
107 }
108 for (i=0; i < size; i++)
109 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000110#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 if (size == 0) {
112 free_list[0] = op;
113 ++numfree[0];
114 Py_INCREF(op); /* extra INCREF so that this is never freed */
115 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000116#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000117#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000119#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 _PyObject_GC_TRACK(op);
121 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122}
123
Martin v. Löwis18e16552006-02-15 17:27:45 +0000124Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200125PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 if (!PyTuple_Check(op)) {
128 PyErr_BadInternalCall();
129 return -1;
130 }
131 else
132 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200136PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (!PyTuple_Check(op)) {
139 PyErr_BadInternalCall();
140 return NULL;
141 }
142 if (i < 0 || i >= Py_SIZE(op)) {
143 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
144 return NULL;
145 }
146 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147}
148
149int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200150PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200152 PyObject *olditem;
153 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
155 Py_XDECREF(newitem);
156 PyErr_BadInternalCall();
157 return -1;
158 }
159 if (i < 0 || i >= Py_SIZE(op)) {
160 Py_XDECREF(newitem);
161 PyErr_SetString(PyExc_IndexError,
162 "tuple assignment index out of range");
163 return -1;
164 }
165 p = ((PyTupleObject *)op) -> ob_item + i;
166 olditem = *p;
167 *p = newitem;
168 Py_XDECREF(olditem);
169 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000172void
173_PyTuple_MaybeUntrack(PyObject *op)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 PyTupleObject *t;
176 Py_ssize_t i, n;
177
178 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
179 return;
180 t = (PyTupleObject *) op;
181 n = Py_SIZE(t);
182 for (i = 0; i < n; i++) {
183 PyObject *elt = PyTuple_GET_ITEM(t, i);
184 /* Tuple with NULL elements aren't
185 fully constructed, don't untrack
186 them yet. */
187 if (!elt ||
188 _PyObject_GC_MAY_BE_TRACKED(elt))
189 return;
190 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000191#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 count_tracked--;
193 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000194#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000196}
197
Raymond Hettingercb2da432003-10-12 18:24:34 +0000198PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 Py_ssize_t i;
202 PyObject *o;
203 PyObject *result;
204 PyObject **items;
205 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 va_start(vargs, n);
208 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200209 if (result == NULL) {
210 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200212 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 items = ((PyTupleObject *)result)->ob_item;
214 for (i = 0; i < n; i++) {
215 o = va_arg(vargs, PyObject *);
216 Py_INCREF(o);
217 items[i] = o;
218 }
219 va_end(vargs);
220 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000221}
222
223
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224/* Methods */
225
226static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200227tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200229 Py_ssize_t i;
230 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 PyObject_GC_UnTrack(op);
232 Py_TRASHCAN_SAFE_BEGIN(op)
233 if (len > 0) {
234 i = len;
235 while (--i >= 0)
236 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000237#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (len < PyTuple_MAXSAVESIZE &&
239 numfree[len] < PyTuple_MAXFREELIST &&
240 Py_TYPE(op) == &PyTuple_Type)
241 {
242 op->ob_item[0] = (PyObject *) free_list[len];
243 numfree[len]++;
244 free_list[len] = op;
245 goto done; /* return */
246 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 }
249 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000250done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000255tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_ssize_t i, n;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200258 PyObject *s = NULL;
259 _PyAccu acc;
260 static PyObject *sep = NULL;
Tim Petersa7259592001-06-16 05:11:17 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 n = Py_SIZE(v);
263 if (n == 0)
264 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000265
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200266 if (sep == NULL) {
267 sep = PyUnicode_FromString(", ");
268 if (sep == NULL)
269 return NULL;
270 }
271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* While not mutable, it is still possible to end up with a cycle in a
273 tuple through an object that stores itself within a tuple (and thus
274 infinitely asks for the repr of itself). This should only be
275 possible within a type. */
276 i = Py_ReprEnter((PyObject *)v);
277 if (i != 0) {
278 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
279 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000280
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200281 if (_PyAccu_Init(&acc))
282 goto error;
283
284 s = PyUnicode_FromString("(");
285 if (s == NULL || _PyAccu_Accumulate(&acc, s))
286 goto error;
287 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 /* Do repr() on each element. */
290 for (i = 0; i < n; ++i) {
291 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 s = PyObject_Repr(v->ob_item[i]);
294 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200295 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
296 goto error;
297 if (s == NULL || _PyAccu_Accumulate(&acc, s))
298 goto error;
299 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200301 if (n > 1)
302 s = PyUnicode_FromString(")");
303 else
304 s = PyUnicode_FromString(",)");
305 if (s == NULL || _PyAccu_Accumulate(&acc, s))
306 goto error;
307 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200310 return _PyAccu_Finish(&acc);
311
312error:
313 _PyAccu_Destroy(&acc);
314 Py_XDECREF(s);
315 Py_ReprLeave((PyObject *)v);
316 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317}
318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319/* The addend 82520, was selected from the range(0, 1000000) for
320 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000321 upto length eight:
322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000324 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100325
326 Tests have shown that it's not worth to cache the hash value, see
327 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000328*/
329
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000330static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000331tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000332{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200333 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
334 Py_hash_t y;
335 Py_ssize_t len = Py_SIZE(v);
336 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800337 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800338 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 p = v->ob_item;
340 while (--len >= 0) {
341 y = PyObject_Hash(*p++);
342 if (y == -1)
343 return -1;
344 x = (x ^ y) * mult;
345 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800346 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800348 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100349 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 x = -2;
351 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000352}
353
Martin v. Löwis18e16552006-02-15 17:27:45 +0000354static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000355tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358}
359
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000360static int
Fred Drakeba096332000-07-09 07:04:36 +0000361tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 Py_ssize_t i;
364 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
367 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
368 Py_EQ);
369 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000370}
371
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200373tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 if (i < 0 || i >= Py_SIZE(a)) {
376 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
377 return NULL;
378 }
379 Py_INCREF(a->ob_item[i]);
380 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200384tupleslice(PyTupleObject *a, Py_ssize_t ilow,
385 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200387 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200389 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 Py_ssize_t len;
391 if (ilow < 0)
392 ilow = 0;
393 if (ihigh > Py_SIZE(a))
394 ihigh = Py_SIZE(a);
395 if (ihigh < ilow)
396 ihigh = ilow;
397 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
398 Py_INCREF(a);
399 return (PyObject *)a;
400 }
401 len = ihigh - ilow;
402 np = (PyTupleObject *)PyTuple_New(len);
403 if (np == NULL)
404 return NULL;
405 src = a->ob_item + ilow;
406 dest = np->ob_item;
407 for (i = 0; i < len; i++) {
408 PyObject *v = src[i];
409 Py_INCREF(v);
410 dest[i] = v;
411 }
412 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413}
414
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000415PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000416PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (op == NULL || !PyTuple_Check(op)) {
419 PyErr_BadInternalCall();
420 return NULL;
421 }
422 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000423}
424
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200426tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200428 Py_ssize_t size;
429 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyObject **src, **dest;
431 PyTupleObject *np;
432 if (!PyTuple_Check(bb)) {
433 PyErr_Format(PyExc_TypeError,
434 "can only concatenate tuple (not \"%.200s\") to tuple",
435 Py_TYPE(bb)->tp_name);
436 return NULL;
437 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 size = Py_SIZE(a) + Py_SIZE(b);
440 if (size < 0)
441 return PyErr_NoMemory();
442 np = (PyTupleObject *) PyTuple_New(size);
443 if (np == NULL) {
444 return NULL;
445 }
446 src = a->ob_item;
447 dest = np->ob_item;
448 for (i = 0; i < Py_SIZE(a); i++) {
449 PyObject *v = src[i];
450 Py_INCREF(v);
451 dest[i] = v;
452 }
453 src = b->ob_item;
454 dest = np->ob_item + Py_SIZE(a);
455 for (i = 0; i < Py_SIZE(b); i++) {
456 PyObject *v = src[i];
457 Py_INCREF(v);
458 dest[i] = v;
459 }
460 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461#undef b
462}
463
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 Py_ssize_t i, j;
468 Py_ssize_t size;
469 PyTupleObject *np;
470 PyObject **p, **items;
471 if (n < 0)
472 n = 0;
473 if (Py_SIZE(a) == 0 || n == 1) {
474 if (PyTuple_CheckExact(a)) {
475 /* Since tuples are immutable, we can return a shared
476 copy in this case */
477 Py_INCREF(a);
478 return (PyObject *)a;
479 }
480 if (Py_SIZE(a) == 0)
481 return PyTuple_New(0);
482 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100483 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100485 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 np = (PyTupleObject *) PyTuple_New(size);
487 if (np == NULL)
488 return NULL;
489 p = np->ob_item;
490 items = a->ob_item;
491 for (i = 0; i < n; i++) {
492 for (j = 0; j < Py_SIZE(a); j++) {
493 *p = items[j];
494 Py_INCREF(*p);
495 p++;
496 }
497 }
498 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000499}
500
Raymond Hettinger65baa342008-02-07 00:41:02 +0000501static PyObject *
502tupleindex(PyTupleObject *self, PyObject *args)
503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200505 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000506
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200507 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
508 _PyEval_SliceIndex, &start,
509 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return NULL;
511 if (start < 0) {
512 start += Py_SIZE(self);
513 if (start < 0)
514 start = 0;
515 }
516 if (stop < 0) {
517 stop += Py_SIZE(self);
518 if (stop < 0)
519 stop = 0;
520 }
521 for (i = start; i < stop && i < Py_SIZE(self); i++) {
522 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
523 if (cmp > 0)
524 return PyLong_FromSsize_t(i);
525 else if (cmp < 0)
526 return NULL;
527 }
528 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
529 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000530}
531
532static PyObject *
533tuplecount(PyTupleObject *self, PyObject *v)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 Py_ssize_t count = 0;
536 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 for (i = 0; i < Py_SIZE(self); i++) {
539 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
540 if (cmp > 0)
541 count++;
542 else if (cmp < 0)
543 return NULL;
544 }
545 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000546}
547
Jeremy Hylton8caad492000-06-23 14:18:11 +0000548static int
549tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 for (i = Py_SIZE(o); --i >= 0; )
554 Py_VISIT(o->ob_item[i]);
555 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000556}
557
Guido van Rossumf77bc622001-01-18 00:00:53 +0000558static PyObject *
559tuplerichcompare(PyObject *v, PyObject *w, int op)
560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 PyTupleObject *vt, *wt;
562 Py_ssize_t i;
563 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000564
Brian Curtindfc80e32011-08-10 20:28:54 -0500565 if (!PyTuple_Check(v) || !PyTuple_Check(w))
566 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 vt = (PyTupleObject *)v;
569 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 vlen = Py_SIZE(vt);
572 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 /* Note: the corresponding code for lists has an "early out" test
575 * here when op is EQ or NE and the lengths differ. That pays there,
576 * but Tim was unable to find any real code where EQ/NE tuple
577 * compares don't have the same length, so testing for it here would
578 * have cost without benefit.
579 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 /* Search for the first index where items are different.
582 * Note that because tuples are immutable, it's safe to reuse
583 * vlen and wlen across the comparison calls.
584 */
585 for (i = 0; i < vlen && i < wlen; i++) {
586 int k = PyObject_RichCompareBool(vt->ob_item[i],
587 wt->ob_item[i], Py_EQ);
588 if (k < 0)
589 return NULL;
590 if (!k)
591 break;
592 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 if (i >= vlen || i >= wlen) {
595 /* No more items to compare -- compare sizes */
596 int cmp;
597 PyObject *res;
598 switch (op) {
599 case Py_LT: cmp = vlen < wlen; break;
600 case Py_LE: cmp = vlen <= wlen; break;
601 case Py_EQ: cmp = vlen == wlen; break;
602 case Py_NE: cmp = vlen != wlen; break;
603 case Py_GT: cmp = vlen > wlen; break;
604 case Py_GE: cmp = vlen >= wlen; break;
605 default: return NULL; /* cannot happen */
606 }
607 if (cmp)
608 res = Py_True;
609 else
610 res = Py_False;
611 Py_INCREF(res);
612 return res;
613 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* We have an item that differs -- shortcuts for EQ/NE */
616 if (op == Py_EQ) {
617 Py_INCREF(Py_False);
618 return Py_False;
619 }
620 if (op == Py_NE) {
621 Py_INCREF(Py_True);
622 return Py_True;
623 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 /* Compare the final item again using the proper operator */
626 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000627}
628
Jeremy Hylton938ace62002-07-17 16:30:39 +0000629static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000630tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
631
Tim Peters6d6c1a32001-08-02 04:15:00 +0000632static PyObject *
633tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyObject *arg = NULL;
636 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 if (type != &PyTuple_Type)
639 return tuple_subtype_new(type, args, kwds);
640 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
641 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if (arg == NULL)
644 return PyTuple_New(0);
645 else
646 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647}
648
Guido van Rossumae960af2001-08-30 03:11:59 +0000649static PyObject *
650tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyObject *tmp, *newobj, *item;
653 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 assert(PyType_IsSubtype(type, &PyTuple_Type));
656 tmp = tuple_new(&PyTuple_Type, args, kwds);
657 if (tmp == NULL)
658 return NULL;
659 assert(PyTuple_Check(tmp));
660 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
661 if (newobj == NULL)
662 return NULL;
663 for (i = 0; i < n; i++) {
664 item = PyTuple_GET_ITEM(tmp, i);
665 Py_INCREF(item);
666 PyTuple_SET_ITEM(newobj, i, item);
667 }
668 Py_DECREF(tmp);
669 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000670}
671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000672PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000673"tuple() -> empty tuple\n\
674tuple(iterable) -> tuple initialized from iterable's items\n\
675\n\
676If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 (lenfunc)tuplelength, /* sq_length */
680 (binaryfunc)tupleconcat, /* sq_concat */
681 (ssizeargfunc)tuplerepeat, /* sq_repeat */
682 (ssizeargfunc)tupleitem, /* sq_item */
683 0, /* sq_slice */
684 0, /* sq_ass_item */
685 0, /* sq_ass_slice */
686 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000687};
688
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000689static PyObject*
690tuplesubscript(PyTupleObject* self, PyObject* item)
691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (PyIndex_Check(item)) {
693 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
694 if (i == -1 && PyErr_Occurred())
695 return NULL;
696 if (i < 0)
697 i += PyTuple_GET_SIZE(self);
698 return tupleitem(self, i);
699 }
700 else if (PySlice_Check(item)) {
701 Py_ssize_t start, stop, step, slicelength, cur, i;
702 PyObject* result;
703 PyObject* it;
704 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000705
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000706 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyTuple_GET_SIZE(self),
708 &start, &stop, &step, &slicelength) < 0) {
709 return NULL;
710 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 if (slicelength <= 0) {
713 return PyTuple_New(0);
714 }
715 else if (start == 0 && step == 1 &&
716 slicelength == PyTuple_GET_SIZE(self) &&
717 PyTuple_CheckExact(self)) {
718 Py_INCREF(self);
719 return (PyObject *)self;
720 }
721 else {
722 result = PyTuple_New(slicelength);
723 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 src = self->ob_item;
726 dest = ((PyTupleObject *)result)->ob_item;
727 for (cur = start, i = 0; i < slicelength;
728 cur += step, i++) {
729 it = src[cur];
730 Py_INCREF(it);
731 dest[i] = it;
732 }
733
734 return result;
735 }
736 }
737 else {
738 PyErr_Format(PyExc_TypeError,
739 "tuple indices must be integers, not %.200s",
740 Py_TYPE(item)->tp_name);
741 return NULL;
742 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000743}
744
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000745static PyObject *
746tuple_getnewargs(PyTupleObject *v)
747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
749
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000750}
751
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000752static PyObject *
753tuple_sizeof(PyTupleObject *self)
754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
758 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000759}
760
Raymond Hettinger65baa342008-02-07 00:41:02 +0000761PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000762"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
763"Raises ValueError if the value is not present."
764);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000765PyDoc_STRVAR(count_doc,
766"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000767PyDoc_STRVAR(sizeof_doc,
768"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000769
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000770static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
772 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
773 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
774 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
775 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000776};
777
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000778static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 (lenfunc)tuplelength,
780 (binaryfunc)tuplesubscript,
781 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000782};
783
Raymond Hettinger48923c52002-08-09 01:30:17 +0000784static PyObject *tuple_iter(PyObject *seq);
785
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyVarObject_HEAD_INIT(&PyType_Type, 0)
788 "tuple",
789 sizeof(PyTupleObject) - sizeof(PyObject *),
790 sizeof(PyObject *),
791 (destructor)tupledealloc, /* tp_dealloc */
792 0, /* tp_print */
793 0, /* tp_getattr */
794 0, /* tp_setattr */
795 0, /* tp_reserved */
796 (reprfunc)tuplerepr, /* tp_repr */
797 0, /* tp_as_number */
798 &tuple_as_sequence, /* tp_as_sequence */
799 &tuple_as_mapping, /* tp_as_mapping */
800 (hashfunc)tuplehash, /* tp_hash */
801 0, /* tp_call */
802 0, /* tp_str */
803 PyObject_GenericGetAttr, /* tp_getattro */
804 0, /* tp_setattro */
805 0, /* tp_as_buffer */
806 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
807 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
808 tuple_doc, /* tp_doc */
809 (traverseproc)tupletraverse, /* tp_traverse */
810 0, /* tp_clear */
811 tuplerichcompare, /* tp_richcompare */
812 0, /* tp_weaklistoffset */
813 tuple_iter, /* tp_iter */
814 0, /* tp_iternext */
815 tuple_methods, /* tp_methods */
816 0, /* tp_members */
817 0, /* tp_getset */
818 0, /* tp_base */
819 0, /* tp_dict */
820 0, /* tp_descr_get */
821 0, /* tp_descr_set */
822 0, /* tp_dictoffset */
823 0, /* tp_init */
824 0, /* tp_alloc */
825 tuple_new, /* tp_new */
826 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000827};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000828
829/* The following function breaks the notion that tuples are immutable:
830 it changes the size of a tuple. We get away with this only if there
831 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000832 as creating a new tuple object and destroying the old one, only more
833 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000834 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000835
836int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000837_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000838{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200839 PyTupleObject *v;
840 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 Py_ssize_t i;
842 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 v = (PyTupleObject *) *pv;
845 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
846 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
847 *pv = 0;
848 Py_XDECREF(v);
849 PyErr_BadInternalCall();
850 return -1;
851 }
852 oldsize = Py_SIZE(v);
853 if (oldsize == newsize)
854 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (oldsize == 0) {
857 /* Empty tuples are often shared, so we should never
858 resize them in-place even if we do own the only
859 (current) reference */
860 Py_DECREF(v);
861 *pv = PyTuple_New(newsize);
862 return *pv == NULL ? -1 : 0;
863 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 /* XXX UNREF/NEWREF interface should be more symmetrical */
866 _Py_DEC_REFTOTAL;
867 if (_PyObject_GC_IS_TRACKED(v))
868 _PyObject_GC_UNTRACK(v);
869 _Py_ForgetReference((PyObject *) v);
870 /* DECREF items deleted by shrinkage */
871 for (i = newsize; i < oldsize; i++) {
872 Py_XDECREF(v->ob_item[i]);
873 v->ob_item[i] = NULL;
874 }
875 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
876 if (sv == NULL) {
877 *pv = NULL;
878 PyObject_GC_Del(v);
879 return -1;
880 }
881 _Py_NewReference((PyObject *) sv);
882 /* Zero out items added by growing */
883 if (newsize > oldsize)
884 memset(&sv->ob_item[oldsize], 0,
885 sizeof(*sv->ob_item) * (newsize - oldsize));
886 *pv = (PyObject *) sv;
887 _PyObject_GC_TRACK(sv);
888 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000889}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000890
Christian Heimesa156e092008-02-16 07:38:31 +0000891int
892PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000895#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 int i;
897 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
898 PyTupleObject *p, *q;
899 p = free_list[i];
900 freelist_size += numfree[i];
901 free_list[i] = NULL;
902 numfree[i] = 0;
903 while (p) {
904 q = p;
905 p = (PyTupleObject *)(p->ob_item[0]);
906 PyObject_GC_Del(q);
907 }
908 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000909#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000911}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912
Christian Heimesa156e092008-02-16 07:38:31 +0000913void
914PyTuple_Fini(void)
915{
916#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 /* empty tuples are used all over the place and applications may
918 * rely on the fact that an empty tuple is a singleton. */
919 Py_XDECREF(free_list[0]);
920 free_list[0] = NULL;
Christian Heimesa156e092008-02-16 07:38:31 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000923#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000924#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000926#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000927}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000928
929/*********************** Tuple Iterator **************************/
930
931typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200933 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000935} tupleiterobject;
936
Raymond Hettinger48923c52002-08-09 01:30:17 +0000937static void
938tupleiter_dealloc(tupleiterobject *it)
939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 _PyObject_GC_UNTRACK(it);
941 Py_XDECREF(it->it_seq);
942 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000943}
944
945static int
946tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 Py_VISIT(it->it_seq);
949 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000950}
951
Raymond Hettinger48923c52002-08-09 01:30:17 +0000952static PyObject *
953tupleiter_next(tupleiterobject *it)
954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyTupleObject *seq;
956 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 assert(it != NULL);
959 seq = it->it_seq;
960 if (seq == NULL)
961 return NULL;
962 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 if (it->it_index < PyTuple_GET_SIZE(seq)) {
965 item = PyTuple_GET_ITEM(seq, it->it_index);
966 ++it->it_index;
967 Py_INCREF(item);
968 return item;
969 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 Py_DECREF(seq);
972 it->it_seq = NULL;
973 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000974}
975
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000976static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000977tupleiter_len(tupleiterobject *it)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 Py_ssize_t len = 0;
980 if (it->it_seq)
981 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
982 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000983}
984
Armin Rigof5b3e362006-02-11 21:32:43 +0000985PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000986
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000987static PyObject *
988tupleiter_reduce(tupleiterobject *it)
989{
990 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +0200991 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000992 it->it_seq, it->it_index);
993 else
Antoine Pitroua7013882012-04-05 00:04:20 +0200994 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000995}
996
997static PyObject *
998tupleiter_setstate(tupleiterobject *it, PyObject *state)
999{
Victor Stinner7660b882013-06-24 23:59:24 +02001000 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001001 if (index == -1 && PyErr_Occurred())
1002 return NULL;
1003 if (it->it_seq != NULL) {
1004 if (index < 0)
1005 index = 0;
1006 else if (it->it_seq != NULL && index > PyTuple_GET_SIZE(it->it_seq))
1007 index = PyTuple_GET_SIZE(it->it_seq);
1008 it->it_index = index;
1009 }
1010 Py_RETURN_NONE;
1011}
1012
1013PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1014PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1015
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001016static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001018 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1019 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001021};
1022
Raymond Hettinger48923c52002-08-09 01:30:17 +00001023PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1025 "tuple_iterator", /* tp_name */
1026 sizeof(tupleiterobject), /* tp_basicsize */
1027 0, /* tp_itemsize */
1028 /* methods */
1029 (destructor)tupleiter_dealloc, /* tp_dealloc */
1030 0, /* tp_print */
1031 0, /* tp_getattr */
1032 0, /* tp_setattr */
1033 0, /* tp_reserved */
1034 0, /* tp_repr */
1035 0, /* tp_as_number */
1036 0, /* tp_as_sequence */
1037 0, /* tp_as_mapping */
1038 0, /* tp_hash */
1039 0, /* tp_call */
1040 0, /* tp_str */
1041 PyObject_GenericGetAttr, /* tp_getattro */
1042 0, /* tp_setattro */
1043 0, /* tp_as_buffer */
1044 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1045 0, /* tp_doc */
1046 (traverseproc)tupleiter_traverse, /* tp_traverse */
1047 0, /* tp_clear */
1048 0, /* tp_richcompare */
1049 0, /* tp_weaklistoffset */
1050 PyObject_SelfIter, /* tp_iter */
1051 (iternextfunc)tupleiter_next, /* tp_iternext */
1052 tupleiter_methods, /* tp_methods */
1053 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001054};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055
1056static PyObject *
1057tuple_iter(PyObject *seq)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 if (!PyTuple_Check(seq)) {
1062 PyErr_BadInternalCall();
1063 return NULL;
1064 }
1065 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1066 if (it == NULL)
1067 return NULL;
1068 it->it_index = 0;
1069 Py_INCREF(seq);
1070 it->it_seq = (PyTupleObject *)seq;
1071 _PyObject_GC_TRACK(it);
1072 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001073}