blob: b76125a1c18c0c13804358371db61edd444649ea [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);
Christian Heimesd5a88042012-09-10 02:54:51 +0200213 if (result == NULL) {
214 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200216 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 items = ((PyTupleObject *)result)->ob_item;
218 for (i = 0; i < n; i++) {
219 o = va_arg(vargs, PyObject *);
220 Py_INCREF(o);
221 items[i] = o;
222 }
223 va_end(vargs);
224 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000225}
226
227
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228/* Methods */
229
230static void
Fred Drakeba096332000-07-09 07:04:36 +0000231tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 register Py_ssize_t i;
234 register Py_ssize_t len = Py_SIZE(op);
235 PyObject_GC_UnTrack(op);
236 Py_TRASHCAN_SAFE_BEGIN(op)
237 if (len > 0) {
238 i = len;
239 while (--i >= 0)
240 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000241#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (len < PyTuple_MAXSAVESIZE &&
243 numfree[len] < PyTuple_MAXFREELIST &&
244 Py_TYPE(op) == &PyTuple_Type)
245 {
246 op->ob_item[0] = (PyObject *) free_list[len];
247 numfree[len]++;
248 free_list[len] = op;
249 goto done; /* return */
250 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 }
253 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000254done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256}
257
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000259tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_ssize_t i, n;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200262 PyObject *s = NULL;
263 _PyAccu acc;
264 static PyObject *sep = NULL;
Tim Petersa7259592001-06-16 05:11:17 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 n = Py_SIZE(v);
267 if (n == 0)
268 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000269
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200270 if (sep == NULL) {
271 sep = PyUnicode_FromString(", ");
272 if (sep == NULL)
273 return NULL;
274 }
275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 /* While not mutable, it is still possible to end up with a cycle in a
277 tuple through an object that stores itself within a tuple (and thus
278 infinitely asks for the repr of itself). This should only be
279 possible within a type. */
280 i = Py_ReprEnter((PyObject *)v);
281 if (i != 0) {
282 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
283 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000284
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200285 if (_PyAccu_Init(&acc))
286 goto error;
287
288 s = PyUnicode_FromString("(");
289 if (s == NULL || _PyAccu_Accumulate(&acc, s))
290 goto error;
291 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 /* Do repr() on each element. */
294 for (i = 0; i < n; ++i) {
295 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200296 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 s = PyObject_Repr(v->ob_item[i]);
298 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200299 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
300 goto error;
301 if (s == NULL || _PyAccu_Accumulate(&acc, s))
302 goto error;
303 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200305 if (n > 1)
306 s = PyUnicode_FromString(")");
307 else
308 s = PyUnicode_FromString(",)");
309 if (s == NULL || _PyAccu_Accumulate(&acc, s))
310 goto error;
311 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200314 return _PyAccu_Finish(&acc);
315
316error:
317 _PyAccu_Destroy(&acc);
318 Py_XDECREF(s);
319 Py_ReprLeave((PyObject *)v);
320 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321}
322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323/* The addend 82520, was selected from the range(0, 1000000) for
324 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000325 upto length eight:
326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000328 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
329*/
330
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000331static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000332tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000333{
Mark Dickinson57e683e2011-09-24 18:18:40 +0100334 register Py_uhash_t x;
335 register Py_hash_t y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 register Py_ssize_t len = Py_SIZE(v);
337 register PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800338 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100339 x = 0x345678;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 p = v->ob_item;
341 while (--len >= 0) {
342 y = PyObject_Hash(*p++);
343 if (y == -1)
344 return -1;
345 x = (x ^ y) * mult;
346 /* the cast might truncate len; that doesn't change hash stability */
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000347 mult += (Py_hash_t)(82520L + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 }
349 x += 97531L;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100350 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 x = -2;
352 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000353}
354
Martin v. Löwis18e16552006-02-15 17:27:45 +0000355static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000356tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359}
360
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000361static int
Fred Drakeba096332000-07-09 07:04:36 +0000362tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 Py_ssize_t i;
365 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
368 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
369 Py_EQ);
370 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000371}
372
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000374tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (i < 0 || i >= Py_SIZE(a)) {
377 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
378 return NULL;
379 }
380 Py_INCREF(a->ob_item[i]);
381 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382}
383
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
386 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 register PyTupleObject *np;
389 PyObject **src, **dest;
390 register Py_ssize_t i;
391 Py_ssize_t len;
392 if (ilow < 0)
393 ilow = 0;
394 if (ihigh > Py_SIZE(a))
395 ihigh = Py_SIZE(a);
396 if (ihigh < ilow)
397 ihigh = ilow;
398 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
399 Py_INCREF(a);
400 return (PyObject *)a;
401 }
402 len = ihigh - ilow;
403 np = (PyTupleObject *)PyTuple_New(len);
404 if (np == NULL)
405 return NULL;
406 src = a->ob_item + ilow;
407 dest = np->ob_item;
408 for (i = 0; i < len; i++) {
409 PyObject *v = src[i];
410 Py_INCREF(v);
411 dest[i] = v;
412 }
413 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414}
415
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000417PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (op == NULL || !PyTuple_Check(op)) {
420 PyErr_BadInternalCall();
421 return NULL;
422 }
423 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000424}
425
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000427tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 register Py_ssize_t size;
430 register Py_ssize_t i;
431 PyObject **src, **dest;
432 PyTupleObject *np;
433 if (!PyTuple_Check(bb)) {
434 PyErr_Format(PyExc_TypeError,
435 "can only concatenate tuple (not \"%.200s\") to tuple",
436 Py_TYPE(bb)->tp_name);
437 return NULL;
438 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439#define b ((PyTupleObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 size = Py_SIZE(a) + Py_SIZE(b);
441 if (size < 0)
442 return PyErr_NoMemory();
443 np = (PyTupleObject *) PyTuple_New(size);
444 if (np == NULL) {
445 return NULL;
446 }
447 src = a->ob_item;
448 dest = np->ob_item;
449 for (i = 0; i < Py_SIZE(a); i++) {
450 PyObject *v = src[i];
451 Py_INCREF(v);
452 dest[i] = v;
453 }
454 src = b->ob_item;
455 dest = np->ob_item + Py_SIZE(a);
456 for (i = 0; i < Py_SIZE(b); i++) {
457 PyObject *v = src[i];
458 Py_INCREF(v);
459 dest[i] = v;
460 }
461 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462#undef b
463}
464
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000466tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 Py_ssize_t i, j;
469 Py_ssize_t size;
470 PyTupleObject *np;
471 PyObject **p, **items;
472 if (n < 0)
473 n = 0;
474 if (Py_SIZE(a) == 0 || n == 1) {
475 if (PyTuple_CheckExact(a)) {
476 /* Since tuples are immutable, we can return a shared
477 copy in this case */
478 Py_INCREF(a);
479 return (PyObject *)a;
480 }
481 if (Py_SIZE(a) == 0)
482 return PyTuple_New(0);
483 }
484 size = Py_SIZE(a) * n;
485 if (size/Py_SIZE(a) != n)
486 return PyErr_NoMemory();
487 np = (PyTupleObject *) PyTuple_New(size);
488 if (np == NULL)
489 return NULL;
490 p = np->ob_item;
491 items = a->ob_item;
492 for (i = 0; i < n; i++) {
493 for (j = 0; j < Py_SIZE(a); j++) {
494 *p = items[j];
495 Py_INCREF(*p);
496 p++;
497 }
498 }
499 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000500}
501
Raymond Hettinger65baa342008-02-07 00:41:02 +0000502static PyObject *
503tupleindex(PyTupleObject *self, PyObject *args)
504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200506 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000507
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200508 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
509 _PyEval_SliceIndex, &start,
510 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return NULL;
512 if (start < 0) {
513 start += Py_SIZE(self);
514 if (start < 0)
515 start = 0;
516 }
517 if (stop < 0) {
518 stop += Py_SIZE(self);
519 if (stop < 0)
520 stop = 0;
521 }
522 for (i = start; i < stop && i < Py_SIZE(self); i++) {
523 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
524 if (cmp > 0)
525 return PyLong_FromSsize_t(i);
526 else if (cmp < 0)
527 return NULL;
528 }
529 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
530 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000531}
532
533static PyObject *
534tuplecount(PyTupleObject *self, PyObject *v)
535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_ssize_t count = 0;
537 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 for (i = 0; i < Py_SIZE(self); i++) {
540 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
541 if (cmp > 0)
542 count++;
543 else if (cmp < 0)
544 return NULL;
545 }
546 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000547}
548
Jeremy Hylton8caad492000-06-23 14:18:11 +0000549static int
550tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 for (i = Py_SIZE(o); --i >= 0; )
555 Py_VISIT(o->ob_item[i]);
556 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000557}
558
Guido van Rossumf77bc622001-01-18 00:00:53 +0000559static PyObject *
560tuplerichcompare(PyObject *v, PyObject *w, int op)
561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyTupleObject *vt, *wt;
563 Py_ssize_t i;
564 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000565
Brian Curtindfc80e32011-08-10 20:28:54 -0500566 if (!PyTuple_Check(v) || !PyTuple_Check(w))
567 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 vt = (PyTupleObject *)v;
570 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 vlen = Py_SIZE(vt);
573 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* Note: the corresponding code for lists has an "early out" test
576 * here when op is EQ or NE and the lengths differ. That pays there,
577 * but Tim was unable to find any real code where EQ/NE tuple
578 * compares don't have the same length, so testing for it here would
579 * have cost without benefit.
580 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* Search for the first index where items are different.
583 * Note that because tuples are immutable, it's safe to reuse
584 * vlen and wlen across the comparison calls.
585 */
586 for (i = 0; i < vlen && i < wlen; i++) {
587 int k = PyObject_RichCompareBool(vt->ob_item[i],
588 wt->ob_item[i], Py_EQ);
589 if (k < 0)
590 return NULL;
591 if (!k)
592 break;
593 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 if (i >= vlen || i >= wlen) {
596 /* No more items to compare -- compare sizes */
597 int cmp;
598 PyObject *res;
599 switch (op) {
600 case Py_LT: cmp = vlen < wlen; break;
601 case Py_LE: cmp = vlen <= wlen; break;
602 case Py_EQ: cmp = vlen == wlen; break;
603 case Py_NE: cmp = vlen != wlen; break;
604 case Py_GT: cmp = vlen > wlen; break;
605 case Py_GE: cmp = vlen >= wlen; break;
606 default: return NULL; /* cannot happen */
607 }
608 if (cmp)
609 res = Py_True;
610 else
611 res = Py_False;
612 Py_INCREF(res);
613 return res;
614 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* We have an item that differs -- shortcuts for EQ/NE */
617 if (op == Py_EQ) {
618 Py_INCREF(Py_False);
619 return Py_False;
620 }
621 if (op == Py_NE) {
622 Py_INCREF(Py_True);
623 return Py_True;
624 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 /* Compare the final item again using the proper operator */
627 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000628}
629
Jeremy Hylton938ace62002-07-17 16:30:39 +0000630static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000631tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
632
Tim Peters6d6c1a32001-08-02 04:15:00 +0000633static PyObject *
634tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PyObject *arg = NULL;
637 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 if (type != &PyTuple_Type)
640 return tuple_subtype_new(type, args, kwds);
641 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
642 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 if (arg == NULL)
645 return PyTuple_New(0);
646 else
647 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000648}
649
Guido van Rossumae960af2001-08-30 03:11:59 +0000650static PyObject *
651tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 PyObject *tmp, *newobj, *item;
654 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 assert(PyType_IsSubtype(type, &PyTuple_Type));
657 tmp = tuple_new(&PyTuple_Type, args, kwds);
658 if (tmp == NULL)
659 return NULL;
660 assert(PyTuple_Check(tmp));
661 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
662 if (newobj == NULL)
663 return NULL;
664 for (i = 0; i < n; i++) {
665 item = PyTuple_GET_ITEM(tmp, i);
666 Py_INCREF(item);
667 PyTuple_SET_ITEM(newobj, i, item);
668 }
669 Py_DECREF(tmp);
670 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000671}
672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000673PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000674"tuple() -> empty tuple\n\
675tuple(iterable) -> tuple initialized from iterable's items\n\
676\n\
677If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000678
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 (lenfunc)tuplelength, /* sq_length */
681 (binaryfunc)tupleconcat, /* sq_concat */
682 (ssizeargfunc)tuplerepeat, /* sq_repeat */
683 (ssizeargfunc)tupleitem, /* sq_item */
684 0, /* sq_slice */
685 0, /* sq_ass_item */
686 0, /* sq_ass_slice */
687 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688};
689
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000690static PyObject*
691tuplesubscript(PyTupleObject* self, PyObject* item)
692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 if (PyIndex_Check(item)) {
694 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
695 if (i == -1 && PyErr_Occurred())
696 return NULL;
697 if (i < 0)
698 i += PyTuple_GET_SIZE(self);
699 return tupleitem(self, i);
700 }
701 else if (PySlice_Check(item)) {
702 Py_ssize_t start, stop, step, slicelength, cur, i;
703 PyObject* result;
704 PyObject* it;
705 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000706
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000707 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyTuple_GET_SIZE(self),
709 &start, &stop, &step, &slicelength) < 0) {
710 return NULL;
711 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (slicelength <= 0) {
714 return PyTuple_New(0);
715 }
716 else if (start == 0 && step == 1 &&
717 slicelength == PyTuple_GET_SIZE(self) &&
718 PyTuple_CheckExact(self)) {
719 Py_INCREF(self);
720 return (PyObject *)self;
721 }
722 else {
723 result = PyTuple_New(slicelength);
724 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 src = self->ob_item;
727 dest = ((PyTupleObject *)result)->ob_item;
728 for (cur = start, i = 0; i < slicelength;
729 cur += step, i++) {
730 it = src[cur];
731 Py_INCREF(it);
732 dest[i] = it;
733 }
734
735 return result;
736 }
737 }
738 else {
739 PyErr_Format(PyExc_TypeError,
740 "tuple indices must be integers, not %.200s",
741 Py_TYPE(item)->tp_name);
742 return NULL;
743 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000744}
745
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000746static PyObject *
747tuple_getnewargs(PyTupleObject *v)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
750
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000751}
752
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000753static PyObject *
754tuple_sizeof(PyTupleObject *self)
755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_ssize_t res;
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
759 return PyLong_FromSsize_t(res);
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000760}
761
Raymond Hettinger65baa342008-02-07 00:41:02 +0000762PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000763"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
764"Raises ValueError if the value is not present."
765);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000766PyDoc_STRVAR(count_doc,
767"T.count(value) -> integer -- return number of occurrences of value");
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000768PyDoc_STRVAR(sizeof_doc,
769"T.__sizeof__() -- size of T in memory, in bytes");
Raymond Hettinger65baa342008-02-07 00:41:02 +0000770
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000771static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
773 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
774 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
775 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
776 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000777};
778
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000779static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 (lenfunc)tuplelength,
781 (binaryfunc)tuplesubscript,
782 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000783};
784
Raymond Hettinger48923c52002-08-09 01:30:17 +0000785static PyObject *tuple_iter(PyObject *seq);
786
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyVarObject_HEAD_INIT(&PyType_Type, 0)
789 "tuple",
790 sizeof(PyTupleObject) - sizeof(PyObject *),
791 sizeof(PyObject *),
792 (destructor)tupledealloc, /* tp_dealloc */
793 0, /* tp_print */
794 0, /* tp_getattr */
795 0, /* tp_setattr */
796 0, /* tp_reserved */
797 (reprfunc)tuplerepr, /* tp_repr */
798 0, /* tp_as_number */
799 &tuple_as_sequence, /* tp_as_sequence */
800 &tuple_as_mapping, /* tp_as_mapping */
801 (hashfunc)tuplehash, /* tp_hash */
802 0, /* tp_call */
803 0, /* tp_str */
804 PyObject_GenericGetAttr, /* tp_getattro */
805 0, /* tp_setattro */
806 0, /* tp_as_buffer */
807 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
808 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
809 tuple_doc, /* tp_doc */
810 (traverseproc)tupletraverse, /* tp_traverse */
811 0, /* tp_clear */
812 tuplerichcompare, /* tp_richcompare */
813 0, /* tp_weaklistoffset */
814 tuple_iter, /* tp_iter */
815 0, /* tp_iternext */
816 tuple_methods, /* tp_methods */
817 0, /* tp_members */
818 0, /* tp_getset */
819 0, /* tp_base */
820 0, /* tp_dict */
821 0, /* tp_descr_get */
822 0, /* tp_descr_set */
823 0, /* tp_dictoffset */
824 0, /* tp_init */
825 0, /* tp_alloc */
826 tuple_new, /* tp_new */
827 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000828};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000829
830/* The following function breaks the notion that tuples are immutable:
831 it changes the size of a tuple. We get away with this only if there
832 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000833 as creating a new tuple object and destroying the old one, only more
834 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000835 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000836
837int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000838_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 register PyTupleObject *v;
841 register PyTupleObject *sv;
842 Py_ssize_t i;
843 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 v = (PyTupleObject *) *pv;
846 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
847 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
848 *pv = 0;
849 Py_XDECREF(v);
850 PyErr_BadInternalCall();
851 return -1;
852 }
853 oldsize = Py_SIZE(v);
854 if (oldsize == newsize)
855 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (oldsize == 0) {
858 /* Empty tuples are often shared, so we should never
859 resize them in-place even if we do own the only
860 (current) reference */
861 Py_DECREF(v);
862 *pv = PyTuple_New(newsize);
863 return *pv == NULL ? -1 : 0;
864 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 /* XXX UNREF/NEWREF interface should be more symmetrical */
867 _Py_DEC_REFTOTAL;
868 if (_PyObject_GC_IS_TRACKED(v))
869 _PyObject_GC_UNTRACK(v);
870 _Py_ForgetReference((PyObject *) v);
871 /* DECREF items deleted by shrinkage */
872 for (i = newsize; i < oldsize; i++) {
873 Py_XDECREF(v->ob_item[i]);
874 v->ob_item[i] = NULL;
875 }
876 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
877 if (sv == NULL) {
878 *pv = NULL;
879 PyObject_GC_Del(v);
880 return -1;
881 }
882 _Py_NewReference((PyObject *) sv);
883 /* Zero out items added by growing */
884 if (newsize > oldsize)
885 memset(&sv->ob_item[oldsize], 0,
886 sizeof(*sv->ob_item) * (newsize - oldsize));
887 *pv = (PyObject *) sv;
888 _PyObject_GC_TRACK(sv);
889 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000890}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000891
Christian Heimesa156e092008-02-16 07:38:31 +0000892int
893PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000896#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 int i;
898 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
899 PyTupleObject *p, *q;
900 p = free_list[i];
901 freelist_size += numfree[i];
902 free_list[i] = NULL;
903 numfree[i] = 0;
904 while (p) {
905 q = p;
906 p = (PyTupleObject *)(p->ob_item[0]);
907 PyObject_GC_Del(q);
908 }
909 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000910#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000912}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913
Christian Heimesa156e092008-02-16 07:38:31 +0000914void
915PyTuple_Fini(void)
916{
917#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* empty tuples are used all over the place and applications may
919 * rely on the fact that an empty tuple is a singleton. */
920 Py_XDECREF(free_list[0]);
921 free_list[0] = NULL;
Christian Heimesa156e092008-02-16 07:38:31 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000924#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000925#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000927#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000928}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000929
930/*********************** Tuple Iterator **************************/
931
932typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 PyObject_HEAD
934 long it_index;
935 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000936} tupleiterobject;
937
Raymond Hettinger48923c52002-08-09 01:30:17 +0000938static void
939tupleiter_dealloc(tupleiterobject *it)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 _PyObject_GC_UNTRACK(it);
942 Py_XDECREF(it->it_seq);
943 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000944}
945
946static int
947tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 Py_VISIT(it->it_seq);
950 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000951}
952
Raymond Hettinger48923c52002-08-09 01:30:17 +0000953static PyObject *
954tupleiter_next(tupleiterobject *it)
955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyTupleObject *seq;
957 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 assert(it != NULL);
960 seq = it->it_seq;
961 if (seq == NULL)
962 return NULL;
963 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (it->it_index < PyTuple_GET_SIZE(seq)) {
966 item = PyTuple_GET_ITEM(seq, it->it_index);
967 ++it->it_index;
968 Py_INCREF(item);
969 return item;
970 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 Py_DECREF(seq);
973 it->it_seq = NULL;
974 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000975}
976
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000977static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000978tupleiter_len(tupleiterobject *it)
979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 Py_ssize_t len = 0;
981 if (it->it_seq)
982 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
983 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000984}
985
Armin Rigof5b3e362006-02-11 21:32:43 +0000986PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000987
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000988static PyObject *
989tupleiter_reduce(tupleiterobject *it)
990{
991 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +0200992 return Py_BuildValue("N(O)l", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000993 it->it_seq, it->it_index);
994 else
Antoine Pitroua7013882012-04-05 00:04:20 +0200995 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000996}
997
998static PyObject *
999tupleiter_setstate(tupleiterobject *it, PyObject *state)
1000{
1001 long index = PyLong_AsLong(state);
1002 if (index == -1 && PyErr_Occurred())
1003 return NULL;
1004 if (it->it_seq != NULL) {
1005 if (index < 0)
1006 index = 0;
1007 else if (it->it_seq != NULL && index > PyTuple_GET_SIZE(it->it_seq))
1008 index = PyTuple_GET_SIZE(it->it_seq);
1009 it->it_index = index;
1010 }
1011 Py_RETURN_NONE;
1012}
1013
1014PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1015PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1016
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001017static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001019 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1020 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001022};
1023
Raymond Hettinger48923c52002-08-09 01:30:17 +00001024PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1026 "tuple_iterator", /* tp_name */
1027 sizeof(tupleiterobject), /* tp_basicsize */
1028 0, /* tp_itemsize */
1029 /* methods */
1030 (destructor)tupleiter_dealloc, /* tp_dealloc */
1031 0, /* tp_print */
1032 0, /* tp_getattr */
1033 0, /* tp_setattr */
1034 0, /* tp_reserved */
1035 0, /* tp_repr */
1036 0, /* tp_as_number */
1037 0, /* tp_as_sequence */
1038 0, /* tp_as_mapping */
1039 0, /* tp_hash */
1040 0, /* tp_call */
1041 0, /* tp_str */
1042 PyObject_GenericGetAttr, /* tp_getattro */
1043 0, /* tp_setattro */
1044 0, /* tp_as_buffer */
1045 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1046 0, /* tp_doc */
1047 (traverseproc)tupleiter_traverse, /* tp_traverse */
1048 0, /* tp_clear */
1049 0, /* tp_richcompare */
1050 0, /* tp_weaklistoffset */
1051 PyObject_SelfIter, /* tp_iter */
1052 (iternextfunc)tupleiter_next, /* tp_iternext */
1053 tupleiter_methods, /* tp_methods */
1054 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001055};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001056
1057static PyObject *
1058tuple_iter(PyObject *seq)
1059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 if (!PyTuple_Check(seq)) {
1063 PyErr_BadInternalCall();
1064 return NULL;
1065 }
1066 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1067 if (it == NULL)
1068 return NULL;
1069 it->it_index = 0;
1070 Py_INCREF(seq);
1071 it->it_seq = (PyTupleObject *)seq;
1072 _PyObject_GC_TRACK(it);
1073 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001074}