blob: f81559504fcb49dbce9deeec9a20afc5a75901db [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 {
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
Fred Drakeba096332000-07-09 07:04:36 +0000125PyTuple_Size(register 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 *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136PyTuple_GetItem(register PyObject *op, register 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
Martin v. Löwis18e16552006-02-15 17:27:45 +0000150PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 register PyObject *olditem;
153 register PyObject **p;
154 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
Fred Drakeba096332000-07-09 07:04:36 +0000227tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 register Py_ssize_t i;
230 register Py_ssize_t len = Py_SIZE(op);
231 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
325*/
326
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000327static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000328tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000329{
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800330 register Py_uhash_t x; /* Unsigned for defined overflow behavior. */
Mark Dickinson57e683e2011-09-24 18:18:40 +0100331 register Py_hash_t y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 register Py_ssize_t len = Py_SIZE(v);
333 register PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800334 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800335 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 p = v->ob_item;
337 while (--len >= 0) {
338 y = PyObject_Hash(*p++);
339 if (y == -1)
340 return -1;
341 x = (x ^ y) * mult;
342 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800343 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800345 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100346 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 x = -2;
348 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000349}
350
Martin v. Löwis18e16552006-02-15 17:27:45 +0000351static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000352tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355}
356
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000357static int
Fred Drakeba096332000-07-09 07:04:36 +0000358tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 Py_ssize_t i;
361 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
364 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
365 Py_EQ);
366 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000367}
368
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000370tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (i < 0 || i >= Py_SIZE(a)) {
373 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
374 return NULL;
375 }
376 Py_INCREF(a->ob_item[i]);
377 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378}
379
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
382 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 register PyTupleObject *np;
385 PyObject **src, **dest;
386 register Py_ssize_t i;
387 Py_ssize_t len;
388 if (ilow < 0)
389 ilow = 0;
390 if (ihigh > Py_SIZE(a))
391 ihigh = Py_SIZE(a);
392 if (ihigh < ilow)
393 ihigh = ilow;
394 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
395 Py_INCREF(a);
396 return (PyObject *)a;
397 }
398 len = ihigh - ilow;
399 np = (PyTupleObject *)PyTuple_New(len);
400 if (np == NULL)
401 return NULL;
402 src = a->ob_item + ilow;
403 dest = np->ob_item;
404 for (i = 0; i < len; i++) {
405 PyObject *v = src[i];
406 Py_INCREF(v);
407 dest[i] = v;
408 }
409 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410}
411
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000413PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (op == NULL || !PyTuple_Check(op)) {
416 PyErr_BadInternalCall();
417 return NULL;
418 }
419 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000420}
421
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000423tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 register Py_ssize_t size;
426 register Py_ssize_t i;
427 PyObject **src, **dest;
428 PyTupleObject *np;
429 if (!PyTuple_Check(bb)) {
430 PyErr_Format(PyExc_TypeError,
431 "can only concatenate tuple (not \"%.200s\") to tuple",
432 Py_TYPE(bb)->tp_name);
433 return NULL;
434 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 size = Py_SIZE(a) + Py_SIZE(b);
437 if (size < 0)
438 return PyErr_NoMemory();
439 np = (PyTupleObject *) PyTuple_New(size);
440 if (np == NULL) {
441 return NULL;
442 }
443 src = a->ob_item;
444 dest = np->ob_item;
445 for (i = 0; i < Py_SIZE(a); i++) {
446 PyObject *v = src[i];
447 Py_INCREF(v);
448 dest[i] = v;
449 }
450 src = b->ob_item;
451 dest = np->ob_item + Py_SIZE(a);
452 for (i = 0; i < Py_SIZE(b); i++) {
453 PyObject *v = src[i];
454 Py_INCREF(v);
455 dest[i] = v;
456 }
457 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458#undef b
459}
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 Py_ssize_t i, j;
465 Py_ssize_t size;
466 PyTupleObject *np;
467 PyObject **p, **items;
468 if (n < 0)
469 n = 0;
470 if (Py_SIZE(a) == 0 || n == 1) {
471 if (PyTuple_CheckExact(a)) {
472 /* Since tuples are immutable, we can return a shared
473 copy in this case */
474 Py_INCREF(a);
475 return (PyObject *)a;
476 }
477 if (Py_SIZE(a) == 0)
478 return PyTuple_New(0);
479 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100480 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100482 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 np = (PyTupleObject *) PyTuple_New(size);
484 if (np == NULL)
485 return NULL;
486 p = np->ob_item;
487 items = a->ob_item;
488 for (i = 0; i < n; i++) {
489 for (j = 0; j < Py_SIZE(a); j++) {
490 *p = items[j];
491 Py_INCREF(*p);
492 p++;
493 }
494 }
495 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000496}
497
Raymond Hettinger65baa342008-02-07 00:41:02 +0000498static PyObject *
499tupleindex(PyTupleObject *self, PyObject *args)
500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200502 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000503
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200504 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
505 _PyEval_SliceIndex, &start,
506 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 return NULL;
508 if (start < 0) {
509 start += Py_SIZE(self);
510 if (start < 0)
511 start = 0;
512 }
513 if (stop < 0) {
514 stop += Py_SIZE(self);
515 if (stop < 0)
516 stop = 0;
517 }
518 for (i = start; i < stop && i < Py_SIZE(self); i++) {
519 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
520 if (cmp > 0)
521 return PyLong_FromSsize_t(i);
522 else if (cmp < 0)
523 return NULL;
524 }
525 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
526 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000527}
528
529static PyObject *
530tuplecount(PyTupleObject *self, PyObject *v)
531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 Py_ssize_t count = 0;
533 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 for (i = 0; i < Py_SIZE(self); i++) {
536 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
537 if (cmp > 0)
538 count++;
539 else if (cmp < 0)
540 return NULL;
541 }
542 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000543}
544
Jeremy Hylton8caad492000-06-23 14:18:11 +0000545static int
546tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 for (i = Py_SIZE(o); --i >= 0; )
551 Py_VISIT(o->ob_item[i]);
552 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000553}
554
Guido van Rossumf77bc622001-01-18 00:00:53 +0000555static PyObject *
556tuplerichcompare(PyObject *v, PyObject *w, int op)
557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 PyTupleObject *vt, *wt;
559 Py_ssize_t i;
560 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000561
Brian Curtindfc80e32011-08-10 20:28:54 -0500562 if (!PyTuple_Check(v) || !PyTuple_Check(w))
563 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 vt = (PyTupleObject *)v;
566 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 vlen = Py_SIZE(vt);
569 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 /* Note: the corresponding code for lists has an "early out" test
572 * here when op is EQ or NE and the lengths differ. That pays there,
573 * but Tim was unable to find any real code where EQ/NE tuple
574 * compares don't have the same length, so testing for it here would
575 * have cost without benefit.
576 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 /* Search for the first index where items are different.
579 * Note that because tuples are immutable, it's safe to reuse
580 * vlen and wlen across the comparison calls.
581 */
582 for (i = 0; i < vlen && i < wlen; i++) {
583 int k = PyObject_RichCompareBool(vt->ob_item[i],
584 wt->ob_item[i], Py_EQ);
585 if (k < 0)
586 return NULL;
587 if (!k)
588 break;
589 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (i >= vlen || i >= wlen) {
592 /* No more items to compare -- compare sizes */
593 int cmp;
594 PyObject *res;
595 switch (op) {
596 case Py_LT: cmp = vlen < wlen; break;
597 case Py_LE: cmp = vlen <= wlen; break;
598 case Py_EQ: cmp = vlen == wlen; break;
599 case Py_NE: cmp = vlen != wlen; break;
600 case Py_GT: cmp = vlen > wlen; break;
601 case Py_GE: cmp = vlen >= wlen; break;
602 default: return NULL; /* cannot happen */
603 }
604 if (cmp)
605 res = Py_True;
606 else
607 res = Py_False;
608 Py_INCREF(res);
609 return res;
610 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 /* We have an item that differs -- shortcuts for EQ/NE */
613 if (op == Py_EQ) {
614 Py_INCREF(Py_False);
615 return Py_False;
616 }
617 if (op == Py_NE) {
618 Py_INCREF(Py_True);
619 return Py_True;
620 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* Compare the final item again using the proper operator */
623 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000624}
625
Jeremy Hylton938ace62002-07-17 16:30:39 +0000626static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000627tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
628
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629static PyObject *
630tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PyObject *arg = NULL;
633 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (type != &PyTuple_Type)
636 return tuple_subtype_new(type, args, kwds);
637 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
638 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (arg == NULL)
641 return PyTuple_New(0);
642 else
643 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644}
645
Guido van Rossumae960af2001-08-30 03:11:59 +0000646static PyObject *
647tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyObject *tmp, *newobj, *item;
650 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 assert(PyType_IsSubtype(type, &PyTuple_Type));
653 tmp = tuple_new(&PyTuple_Type, args, kwds);
654 if (tmp == NULL)
655 return NULL;
656 assert(PyTuple_Check(tmp));
657 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
658 if (newobj == NULL)
659 return NULL;
660 for (i = 0; i < n; i++) {
661 item = PyTuple_GET_ITEM(tmp, i);
662 Py_INCREF(item);
663 PyTuple_SET_ITEM(newobj, i, item);
664 }
665 Py_DECREF(tmp);
666 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000667}
668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000670"tuple() -> empty tuple\n\
671tuple(iterable) -> tuple initialized from iterable's items\n\
672\n\
673If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 (lenfunc)tuplelength, /* sq_length */
677 (binaryfunc)tupleconcat, /* sq_concat */
678 (ssizeargfunc)tuplerepeat, /* sq_repeat */
679 (ssizeargfunc)tupleitem, /* sq_item */
680 0, /* sq_slice */
681 0, /* sq_ass_item */
682 0, /* sq_ass_slice */
683 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000684};
685
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000686static PyObject*
687tuplesubscript(PyTupleObject* self, PyObject* item)
688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 if (PyIndex_Check(item)) {
690 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
691 if (i == -1 && PyErr_Occurred())
692 return NULL;
693 if (i < 0)
694 i += PyTuple_GET_SIZE(self);
695 return tupleitem(self, i);
696 }
697 else if (PySlice_Check(item)) {
698 Py_ssize_t start, stop, step, slicelength, cur, i;
699 PyObject* result;
700 PyObject* it;
701 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000702
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000703 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyTuple_GET_SIZE(self),
705 &start, &stop, &step, &slicelength) < 0) {
706 return NULL;
707 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (slicelength <= 0) {
710 return PyTuple_New(0);
711 }
712 else if (start == 0 && step == 1 &&
713 slicelength == PyTuple_GET_SIZE(self) &&
714 PyTuple_CheckExact(self)) {
715 Py_INCREF(self);
716 return (PyObject *)self;
717 }
718 else {
719 result = PyTuple_New(slicelength);
720 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 src = self->ob_item;
723 dest = ((PyTupleObject *)result)->ob_item;
724 for (cur = start, i = 0; i < slicelength;
725 cur += step, i++) {
726 it = src[cur];
727 Py_INCREF(it);
728 dest[i] = it;
729 }
730
731 return result;
732 }
733 }
734 else {
735 PyErr_Format(PyExc_TypeError,
736 "tuple indices must be integers, not %.200s",
737 Py_TYPE(item)->tp_name);
738 return NULL;
739 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000740}
741
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000742static PyObject *
743tuple_getnewargs(PyTupleObject *v)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
746
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000747}
748
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000749static PyObject *
750tuple_sizeof(PyTupleObject *self)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
755 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000756}
757
Raymond Hettinger65baa342008-02-07 00:41:02 +0000758PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000759"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
760"Raises ValueError if the value is not present."
761);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000762PyDoc_STRVAR(count_doc,
763"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000764PyDoc_STRVAR(sizeof_doc,
765"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000766
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000767static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
769 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
770 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
771 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
772 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000773};
774
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000775static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 (lenfunc)tuplelength,
777 (binaryfunc)tuplesubscript,
778 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000779};
780
Raymond Hettinger48923c52002-08-09 01:30:17 +0000781static PyObject *tuple_iter(PyObject *seq);
782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 PyVarObject_HEAD_INIT(&PyType_Type, 0)
785 "tuple",
786 sizeof(PyTupleObject) - sizeof(PyObject *),
787 sizeof(PyObject *),
788 (destructor)tupledealloc, /* tp_dealloc */
789 0, /* tp_print */
790 0, /* tp_getattr */
791 0, /* tp_setattr */
792 0, /* tp_reserved */
793 (reprfunc)tuplerepr, /* tp_repr */
794 0, /* tp_as_number */
795 &tuple_as_sequence, /* tp_as_sequence */
796 &tuple_as_mapping, /* tp_as_mapping */
797 (hashfunc)tuplehash, /* tp_hash */
798 0, /* tp_call */
799 0, /* tp_str */
800 PyObject_GenericGetAttr, /* tp_getattro */
801 0, /* tp_setattro */
802 0, /* tp_as_buffer */
803 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
804 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
805 tuple_doc, /* tp_doc */
806 (traverseproc)tupletraverse, /* tp_traverse */
807 0, /* tp_clear */
808 tuplerichcompare, /* tp_richcompare */
809 0, /* tp_weaklistoffset */
810 tuple_iter, /* tp_iter */
811 0, /* tp_iternext */
812 tuple_methods, /* tp_methods */
813 0, /* tp_members */
814 0, /* tp_getset */
815 0, /* tp_base */
816 0, /* tp_dict */
817 0, /* tp_descr_get */
818 0, /* tp_descr_set */
819 0, /* tp_dictoffset */
820 0, /* tp_init */
821 0, /* tp_alloc */
822 tuple_new, /* tp_new */
823 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000824};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825
826/* The following function breaks the notion that tuples are immutable:
827 it changes the size of a tuple. We get away with this only if there
828 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000829 as creating a new tuple object and destroying the old one, only more
830 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000831 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000832
833int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000834_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 register PyTupleObject *v;
837 register PyTupleObject *sv;
838 Py_ssize_t i;
839 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 v = (PyTupleObject *) *pv;
842 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
843 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
844 *pv = 0;
845 Py_XDECREF(v);
846 PyErr_BadInternalCall();
847 return -1;
848 }
849 oldsize = Py_SIZE(v);
850 if (oldsize == newsize)
851 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 if (oldsize == 0) {
854 /* Empty tuples are often shared, so we should never
855 resize them in-place even if we do own the only
856 (current) reference */
857 Py_DECREF(v);
858 *pv = PyTuple_New(newsize);
859 return *pv == NULL ? -1 : 0;
860 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 /* XXX UNREF/NEWREF interface should be more symmetrical */
863 _Py_DEC_REFTOTAL;
864 if (_PyObject_GC_IS_TRACKED(v))
865 _PyObject_GC_UNTRACK(v);
866 _Py_ForgetReference((PyObject *) v);
867 /* DECREF items deleted by shrinkage */
868 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200869 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 }
871 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
872 if (sv == NULL) {
873 *pv = NULL;
874 PyObject_GC_Del(v);
875 return -1;
876 }
877 _Py_NewReference((PyObject *) sv);
878 /* Zero out items added by growing */
879 if (newsize > oldsize)
880 memset(&sv->ob_item[oldsize], 0,
881 sizeof(*sv->ob_item) * (newsize - oldsize));
882 *pv = (PyObject *) sv;
883 _PyObject_GC_TRACK(sv);
884 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000885}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000886
Christian Heimesa156e092008-02-16 07:38:31 +0000887int
888PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000891#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 int i;
893 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
894 PyTupleObject *p, *q;
895 p = free_list[i];
896 freelist_size += numfree[i];
897 free_list[i] = NULL;
898 numfree[i] = 0;
899 while (p) {
900 q = p;
901 p = (PyTupleObject *)(p->ob_item[0]);
902 PyObject_GC_Del(q);
903 }
904 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000905#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000907}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908
Christian Heimesa156e092008-02-16 07:38:31 +0000909void
910PyTuple_Fini(void)
911{
912#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 /* empty tuples are used all over the place and applications may
914 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200915 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000918#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000919#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000921#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000922}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000923
924/*********************** Tuple Iterator **************************/
925
926typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 PyObject_HEAD
928 long it_index;
929 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000930} tupleiterobject;
931
Raymond Hettinger48923c52002-08-09 01:30:17 +0000932static void
933tupleiter_dealloc(tupleiterobject *it)
934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 _PyObject_GC_UNTRACK(it);
936 Py_XDECREF(it->it_seq);
937 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000938}
939
940static int
941tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 Py_VISIT(it->it_seq);
944 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000945}
946
Raymond Hettinger48923c52002-08-09 01:30:17 +0000947static PyObject *
948tupleiter_next(tupleiterobject *it)
949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 PyTupleObject *seq;
951 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 assert(it != NULL);
954 seq = it->it_seq;
955 if (seq == NULL)
956 return NULL;
957 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (it->it_index < PyTuple_GET_SIZE(seq)) {
960 item = PyTuple_GET_ITEM(seq, it->it_index);
961 ++it->it_index;
962 Py_INCREF(item);
963 return item;
964 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_DECREF(seq);
967 it->it_seq = NULL;
968 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000969}
970
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000971static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000972tupleiter_len(tupleiterobject *it)
973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_ssize_t len = 0;
975 if (it->it_seq)
976 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
977 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000978}
979
Armin Rigof5b3e362006-02-11 21:32:43 +0000980PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000981
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000982static PyObject *
983tupleiter_reduce(tupleiterobject *it)
984{
985 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +0200986 return Py_BuildValue("N(O)l", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000987 it->it_seq, it->it_index);
988 else
Antoine Pitroua7013882012-04-05 00:04:20 +0200989 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000990}
991
992static PyObject *
993tupleiter_setstate(tupleiterobject *it, PyObject *state)
994{
995 long index = PyLong_AsLong(state);
996 if (index == -1 && PyErr_Occurred())
997 return NULL;
998 if (it->it_seq != NULL) {
999 if (index < 0)
1000 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001001 else if (index > PyTuple_GET_SIZE(it->it_seq))
1002 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001003 it->it_index = index;
1004 }
1005 Py_RETURN_NONE;
1006}
1007
1008PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1009PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1010
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001011static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001013 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1014 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001016};
1017
Raymond Hettinger48923c52002-08-09 01:30:17 +00001018PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1020 "tuple_iterator", /* tp_name */
1021 sizeof(tupleiterobject), /* tp_basicsize */
1022 0, /* tp_itemsize */
1023 /* methods */
1024 (destructor)tupleiter_dealloc, /* tp_dealloc */
1025 0, /* tp_print */
1026 0, /* tp_getattr */
1027 0, /* tp_setattr */
1028 0, /* tp_reserved */
1029 0, /* tp_repr */
1030 0, /* tp_as_number */
1031 0, /* tp_as_sequence */
1032 0, /* tp_as_mapping */
1033 0, /* tp_hash */
1034 0, /* tp_call */
1035 0, /* tp_str */
1036 PyObject_GenericGetAttr, /* tp_getattro */
1037 0, /* tp_setattro */
1038 0, /* tp_as_buffer */
1039 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1040 0, /* tp_doc */
1041 (traverseproc)tupleiter_traverse, /* tp_traverse */
1042 0, /* tp_clear */
1043 0, /* tp_richcompare */
1044 0, /* tp_weaklistoffset */
1045 PyObject_SelfIter, /* tp_iter */
1046 (iternextfunc)tupleiter_next, /* tp_iternext */
1047 tupleiter_methods, /* tp_methods */
1048 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001049};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001050
1051static PyObject *
1052tuple_iter(PyObject *seq)
1053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (!PyTuple_Check(seq)) {
1057 PyErr_BadInternalCall();
1058 return NULL;
1059 }
1060 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1061 if (it == NULL)
1062 return NULL;
1063 it->it_index = 0;
1064 Py_INCREF(seq);
1065 it->it_seq = (PyTupleObject *)seq;
1066 _PyObject_GC_TRACK(it);
1067 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068}