blob: c0ff499e72c7ddaacb23c818aaa628feefe056a7 [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{
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030039 PyObject *xoptions, *value;
40 _Py_IDENTIFIER(showalloccount);
41
42 xoptions = PySys_GetXOptions();
43 if (xoptions == NULL)
44 return;
45 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
46 if (value != Py_True)
47 return;
48
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
50 count_tracked + count_untracked);
51 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
52 "d\n", count_tracked);
53 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
54 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +000055}
56#endif
57
David Malcolm49526f42012-06-22 14:55:41 -040058/* Print summary info about the state of the optimized allocator */
59void
60_PyTuple_DebugMallocStats(FILE *out)
61{
62#if PyTuple_MAXSAVESIZE > 0
63 int i;
64 char buf[128];
65 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
66 PyOS_snprintf(buf, sizeof(buf),
67 "free %d-sized PyTupleObject", i);
68 _PyDebugAllocatorStats(out,
69 buf,
70 numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
71 }
72#endif
73}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000074
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020076PyTuple_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020078 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 Py_ssize_t i;
80 if (size < 0) {
81 PyErr_BadInternalCall();
82 return NULL;
83 }
Christian Heimes2202f872008-02-06 14:31:34 +000084#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (size == 0 && free_list[0]) {
86 op = free_list[0];
87 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000088#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 tuple_zero_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return (PyObject *) op;
92 }
93 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
94 free_list[size] = (PyTupleObject *) op->ob_item[0];
95 numfree[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000096#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 fast_tuple_allocs++;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +0000100#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 Py_SIZE(op) = size;
102 Py_TYPE(op) = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +0000103#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 _Py_NewReference((PyObject *)op);
105 }
106 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 /* Check for overflow */
Victor Stinner049e5092014-08-17 22:20:00 +0200110 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100111 sizeof(PyObject *)) / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 return PyErr_NoMemory();
113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
115 if (op == NULL)
116 return NULL;
117 }
118 for (i=0; i < size; i++)
119 op->ob_item[i] = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +0000120#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (size == 0) {
122 free_list[0] = op;
123 ++numfree[0];
124 Py_INCREF(op); /* extra INCREF so that this is never freed */
125 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000126#endif
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000127#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 count_tracked++;
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000129#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 _PyObject_GC_TRACK(op);
131 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200135PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 if (!PyTuple_Check(op)) {
138 PyErr_BadInternalCall();
139 return -1;
140 }
141 else
142 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143}
144
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200146PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (!PyTuple_Check(op)) {
149 PyErr_BadInternalCall();
150 return NULL;
151 }
152 if (i < 0 || i >= Py_SIZE(op)) {
153 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
154 return NULL;
155 }
156 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157}
158
159int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200160PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200162 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
164 Py_XDECREF(newitem);
165 PyErr_BadInternalCall();
166 return -1;
167 }
168 if (i < 0 || i >= Py_SIZE(op)) {
169 Py_XDECREF(newitem);
170 PyErr_SetString(PyExc_IndexError,
171 "tuple assignment index out of range");
172 return -1;
173 }
174 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300175 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
178
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000179void
180_PyTuple_MaybeUntrack(PyObject *op)
181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 PyTupleObject *t;
183 Py_ssize_t i, n;
184
185 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
186 return;
187 t = (PyTupleObject *) op;
188 n = Py_SIZE(t);
189 for (i = 0; i < n; i++) {
190 PyObject *elt = PyTuple_GET_ITEM(t, i);
191 /* Tuple with NULL elements aren't
192 fully constructed, don't untrack
193 them yet. */
194 if (!elt ||
195 _PyObject_GC_MAY_BE_TRACKED(elt))
196 return;
197 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000198#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 count_tracked--;
200 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000201#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000203}
204
Raymond Hettingercb2da432003-10-12 18:24:34 +0000205PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 Py_ssize_t i;
209 PyObject *o;
210 PyObject *result;
211 PyObject **items;
212 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 va_start(vargs, n);
215 result = PyTuple_New(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200216 if (result == NULL) {
217 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 items = ((PyTupleObject *)result)->ob_item;
221 for (i = 0; i < n; i++) {
222 o = va_arg(vargs, PyObject *);
223 Py_INCREF(o);
224 items[i] = o;
225 }
226 va_end(vargs);
227 return result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000228}
229
230
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231/* Methods */
232
233static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200234tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200236 Py_ssize_t i;
237 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 PyObject_GC_UnTrack(op);
239 Py_TRASHCAN_SAFE_BEGIN(op)
240 if (len > 0) {
241 i = len;
242 while (--i >= 0)
243 Py_XDECREF(op->ob_item[i]);
Christian Heimes2202f872008-02-06 14:31:34 +0000244#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (len < PyTuple_MAXSAVESIZE &&
246 numfree[len] < PyTuple_MAXFREELIST &&
247 Py_TYPE(op) == &PyTuple_Type)
248 {
249 op->ob_item[0] = (PyObject *) free_list[len];
250 numfree[len]++;
251 free_list[len] = op;
252 goto done; /* return */
253 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 }
256 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000257done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259}
260
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000262tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100265 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 n = Py_SIZE(v);
268 if (n == 0)
269 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* While not mutable, it is still possible to end up with a cycle in a
272 tuple through an object that stores itself within a tuple (and thus
273 infinitely asks for the repr of itself). This should only be
274 possible within a type. */
275 i = Py_ReprEnter((PyObject *)v);
276 if (i != 0) {
277 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
278 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000279
Victor Stinner88a9cd92013-11-19 12:59:46 +0100280 _PyUnicodeWriter_Init(&writer);
281 writer.overallocate = 1;
282 if (Py_SIZE(v) > 1) {
283 /* "(" + "1" + ", 2" * (len - 1) + ")" */
284 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
285 }
286 else {
287 /* "(1,)" */
288 writer.min_length = 4;
289 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200290
Victor Stinner88a9cd92013-11-19 12:59:46 +0100291 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200292 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* Do repr() on each element. */
295 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100296 PyObject *s;
297
298 if (i > 0) {
299 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
300 goto error;
301 }
302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200304 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 s = PyObject_Repr(v->ob_item[i]);
306 Py_LeaveRecursiveCall();
Victor Stinner88a9cd92013-11-19 12:59:46 +0100307 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200308 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100309
310 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
311 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200312 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100313 }
314 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100316
317 writer.overallocate = 0;
318 if (n > 1) {
319 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
320 goto error;
321 }
322 else {
323 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
324 goto error;
325 }
Tim Petersa7259592001-06-16 05:11:17 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100328 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200329
330error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100331 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200332 Py_ReprLeave((PyObject *)v);
333 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334}
335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336/* The addend 82520, was selected from the range(0, 1000000) for
337 generating the greatest number of prime multipliers for tuples
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000338 upto length eight:
339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000341 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
Christian Heimes34bdeb52013-01-07 21:24:18 +0100342
343 Tests have shown that it's not worth to cache the hash value, see
344 issue #9685.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000345*/
346
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000347static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000348tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000349{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200350 Py_uhash_t x; /* Unsigned for defined overflow behavior. */
351 Py_hash_t y;
352 Py_ssize_t len = Py_SIZE(v);
353 PyObject **p;
Gregory P. Smithf5b62a92012-01-14 15:45:13 -0800354 Py_uhash_t mult = _PyHASH_MULTIPLIER;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800355 x = 0x345678UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 p = v->ob_item;
357 while (--len >= 0) {
358 y = PyObject_Hash(*p++);
359 if (y == -1)
360 return -1;
361 x = (x ^ y) * mult;
362 /* the cast might truncate len; that doesn't change hash stability */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800363 mult += (Py_hash_t)(82520UL + len + len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 }
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800365 x += 97531UL;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100366 if (x == (Py_uhash_t)-1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 x = -2;
368 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000369}
370
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000372tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375}
376
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000377static int
Fred Drakeba096332000-07-09 07:04:36 +0000378tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Py_ssize_t i;
381 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
384 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
385 Py_EQ);
386 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000387}
388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200390tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (i < 0 || i >= Py_SIZE(a)) {
393 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
394 return NULL;
395 }
396 Py_INCREF(a->ob_item[i]);
397 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200401tupleslice(PyTupleObject *a, Py_ssize_t ilow,
402 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200404 PyTupleObject *np;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyObject **src, **dest;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200406 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 Py_ssize_t len;
408 if (ilow < 0)
409 ilow = 0;
410 if (ihigh > Py_SIZE(a))
411 ihigh = Py_SIZE(a);
412 if (ihigh < ilow)
413 ihigh = ilow;
414 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
415 Py_INCREF(a);
416 return (PyObject *)a;
417 }
418 len = ihigh - ilow;
419 np = (PyTupleObject *)PyTuple_New(len);
420 if (np == NULL)
421 return NULL;
422 src = a->ob_item + ilow;
423 dest = np->ob_item;
424 for (i = 0; i < len; i++) {
425 PyObject *v = src[i];
426 Py_INCREF(v);
427 dest[i] = v;
428 }
429 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430}
431
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000433PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (op == NULL || !PyTuple_Check(op)) {
436 PyErr_BadInternalCall();
437 return NULL;
438 }
439 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000440}
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200443tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200445 Py_ssize_t size;
446 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 PyObject **src, **dest;
448 PyTupleObject *np;
449 if (!PyTuple_Check(bb)) {
450 PyErr_Format(PyExc_TypeError,
451 "can only concatenate tuple (not \"%.200s\") to tuple",
452 Py_TYPE(bb)->tp_name);
453 return NULL;
454 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455#define b ((PyTupleObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000456 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000458 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 np = (PyTupleObject *) PyTuple_New(size);
460 if (np == NULL) {
461 return NULL;
462 }
463 src = a->ob_item;
464 dest = np->ob_item;
465 for (i = 0; i < Py_SIZE(a); i++) {
466 PyObject *v = src[i];
467 Py_INCREF(v);
468 dest[i] = v;
469 }
470 src = b->ob_item;
471 dest = np->ob_item + Py_SIZE(a);
472 for (i = 0; i < Py_SIZE(b); i++) {
473 PyObject *v = src[i];
474 Py_INCREF(v);
475 dest[i] = v;
476 }
477 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478#undef b
479}
480
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 Py_ssize_t i, j;
485 Py_ssize_t size;
486 PyTupleObject *np;
487 PyObject **p, **items;
488 if (n < 0)
489 n = 0;
490 if (Py_SIZE(a) == 0 || n == 1) {
491 if (PyTuple_CheckExact(a)) {
492 /* Since tuples are immutable, we can return a shared
493 copy in this case */
494 Py_INCREF(a);
495 return (PyObject *)a;
496 }
497 if (Py_SIZE(a) == 0)
498 return PyTuple_New(0);
499 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100500 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100502 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 np = (PyTupleObject *) PyTuple_New(size);
504 if (np == NULL)
505 return NULL;
506 p = np->ob_item;
507 items = a->ob_item;
508 for (i = 0; i < n; i++) {
509 for (j = 0; j < Py_SIZE(a); j++) {
510 *p = items[j];
511 Py_INCREF(*p);
512 p++;
513 }
514 }
515 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000516}
517
Raymond Hettinger65baa342008-02-07 00:41:02 +0000518static PyObject *
519tupleindex(PyTupleObject *self, PyObject *args)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200522 PyObject *v;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000523
Petri Lehtinenebfaabd2011-11-06 21:02:39 +0200524 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
525 _PyEval_SliceIndex, &start,
526 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return NULL;
528 if (start < 0) {
529 start += Py_SIZE(self);
530 if (start < 0)
531 start = 0;
532 }
533 if (stop < 0) {
534 stop += Py_SIZE(self);
535 if (stop < 0)
536 stop = 0;
537 }
538 for (i = start; i < stop && i < Py_SIZE(self); i++) {
539 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
540 if (cmp > 0)
541 return PyLong_FromSsize_t(i);
542 else if (cmp < 0)
543 return NULL;
544 }
545 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
546 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000547}
548
549static PyObject *
550tuplecount(PyTupleObject *self, PyObject *v)
551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_ssize_t count = 0;
553 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 for (i = 0; i < Py_SIZE(self); i++) {
556 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
557 if (cmp > 0)
558 count++;
559 else if (cmp < 0)
560 return NULL;
561 }
562 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000563}
564
Jeremy Hylton8caad492000-06-23 14:18:11 +0000565static int
566tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 for (i = Py_SIZE(o); --i >= 0; )
571 Py_VISIT(o->ob_item[i]);
572 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000573}
574
Guido van Rossumf77bc622001-01-18 00:00:53 +0000575static PyObject *
576tuplerichcompare(PyObject *v, PyObject *w, int op)
577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyTupleObject *vt, *wt;
579 Py_ssize_t i;
580 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000581
Brian Curtindfc80e32011-08-10 20:28:54 -0500582 if (!PyTuple_Check(v) || !PyTuple_Check(w))
583 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 vt = (PyTupleObject *)v;
586 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 vlen = Py_SIZE(vt);
589 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Note: the corresponding code for lists has an "early out" test
592 * here when op is EQ or NE and the lengths differ. That pays there,
593 * but Tim was unable to find any real code where EQ/NE tuple
594 * compares don't have the same length, so testing for it here would
595 * have cost without benefit.
596 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* Search for the first index where items are different.
599 * Note that because tuples are immutable, it's safe to reuse
600 * vlen and wlen across the comparison calls.
601 */
602 for (i = 0; i < vlen && i < wlen; i++) {
603 int k = PyObject_RichCompareBool(vt->ob_item[i],
604 wt->ob_item[i], Py_EQ);
605 if (k < 0)
606 return NULL;
607 if (!k)
608 break;
609 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (i >= vlen || i >= wlen) {
612 /* No more items to compare -- compare sizes */
613 int cmp;
614 PyObject *res;
615 switch (op) {
616 case Py_LT: cmp = vlen < wlen; break;
617 case Py_LE: cmp = vlen <= wlen; break;
618 case Py_EQ: cmp = vlen == wlen; break;
619 case Py_NE: cmp = vlen != wlen; break;
620 case Py_GT: cmp = vlen > wlen; break;
621 case Py_GE: cmp = vlen >= wlen; break;
622 default: return NULL; /* cannot happen */
623 }
624 if (cmp)
625 res = Py_True;
626 else
627 res = Py_False;
628 Py_INCREF(res);
629 return res;
630 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* We have an item that differs -- shortcuts for EQ/NE */
633 if (op == Py_EQ) {
634 Py_INCREF(Py_False);
635 return Py_False;
636 }
637 if (op == Py_NE) {
638 Py_INCREF(Py_True);
639 return Py_True;
640 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* Compare the final item again using the proper operator */
643 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000644}
645
Jeremy Hylton938ace62002-07-17 16:30:39 +0000646static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000647tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
648
Tim Peters6d6c1a32001-08-02 04:15:00 +0000649static PyObject *
650tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyObject *arg = NULL;
653 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (type != &PyTuple_Type)
656 return tuple_subtype_new(type, args, kwds);
657 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
658 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (arg == NULL)
661 return PyTuple_New(0);
662 else
663 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000664}
665
Guido van Rossumae960af2001-08-30 03:11:59 +0000666static PyObject *
667tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyObject *tmp, *newobj, *item;
670 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 assert(PyType_IsSubtype(type, &PyTuple_Type));
673 tmp = tuple_new(&PyTuple_Type, args, kwds);
674 if (tmp == NULL)
675 return NULL;
676 assert(PyTuple_Check(tmp));
677 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
678 if (newobj == NULL)
679 return NULL;
680 for (i = 0; i < n; i++) {
681 item = PyTuple_GET_ITEM(tmp, i);
682 Py_INCREF(item);
683 PyTuple_SET_ITEM(newobj, i, item);
684 }
685 Py_DECREF(tmp);
686 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000687}
688
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000689PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000690"tuple() -> empty tuple\n\
691tuple(iterable) -> tuple initialized from iterable's items\n\
692\n\
693If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 (lenfunc)tuplelength, /* sq_length */
697 (binaryfunc)tupleconcat, /* sq_concat */
698 (ssizeargfunc)tuplerepeat, /* sq_repeat */
699 (ssizeargfunc)tupleitem, /* sq_item */
700 0, /* sq_slice */
701 0, /* sq_ass_item */
702 0, /* sq_ass_slice */
703 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000704};
705
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000706static PyObject*
707tuplesubscript(PyTupleObject* self, PyObject* item)
708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (PyIndex_Check(item)) {
710 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
711 if (i == -1 && PyErr_Occurred())
712 return NULL;
713 if (i < 0)
714 i += PyTuple_GET_SIZE(self);
715 return tupleitem(self, i);
716 }
717 else if (PySlice_Check(item)) {
718 Py_ssize_t start, stop, step, slicelength, cur, i;
719 PyObject* result;
720 PyObject* it;
721 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000722
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000723 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 PyTuple_GET_SIZE(self),
725 &start, &stop, &step, &slicelength) < 0) {
726 return NULL;
727 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 if (slicelength <= 0) {
730 return PyTuple_New(0);
731 }
732 else if (start == 0 && step == 1 &&
733 slicelength == PyTuple_GET_SIZE(self) &&
734 PyTuple_CheckExact(self)) {
735 Py_INCREF(self);
736 return (PyObject *)self;
737 }
738 else {
739 result = PyTuple_New(slicelength);
740 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 src = self->ob_item;
743 dest = ((PyTupleObject *)result)->ob_item;
744 for (cur = start, i = 0; i < slicelength;
745 cur += step, i++) {
746 it = src[cur];
747 Py_INCREF(it);
748 dest[i] = it;
749 }
750
751 return result;
752 }
753 }
754 else {
755 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400756 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 Py_TYPE(item)->tp_name);
758 return NULL;
759 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000760}
761
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000762static PyObject *
763tuple_getnewargs(PyTupleObject *v)
764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
766
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000767}
768
Raymond Hettinger65baa342008-02-07 00:41:02 +0000769PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000770"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
771"Raises ValueError if the value is not present."
772);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000773PyDoc_STRVAR(count_doc,
774"T.count(value) -> integer -- return number of occurrences of value");
775
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000776static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
779 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
780 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000781};
782
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000783static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 (lenfunc)tuplelength,
785 (binaryfunc)tuplesubscript,
786 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000787};
788
Raymond Hettinger48923c52002-08-09 01:30:17 +0000789static PyObject *tuple_iter(PyObject *seq);
790
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000791PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyVarObject_HEAD_INIT(&PyType_Type, 0)
793 "tuple",
794 sizeof(PyTupleObject) - sizeof(PyObject *),
795 sizeof(PyObject *),
796 (destructor)tupledealloc, /* tp_dealloc */
797 0, /* tp_print */
798 0, /* tp_getattr */
799 0, /* tp_setattr */
800 0, /* tp_reserved */
801 (reprfunc)tuplerepr, /* tp_repr */
802 0, /* tp_as_number */
803 &tuple_as_sequence, /* tp_as_sequence */
804 &tuple_as_mapping, /* tp_as_mapping */
805 (hashfunc)tuplehash, /* tp_hash */
806 0, /* tp_call */
807 0, /* tp_str */
808 PyObject_GenericGetAttr, /* tp_getattro */
809 0, /* tp_setattro */
810 0, /* tp_as_buffer */
811 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
812 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
813 tuple_doc, /* tp_doc */
814 (traverseproc)tupletraverse, /* tp_traverse */
815 0, /* tp_clear */
816 tuplerichcompare, /* tp_richcompare */
817 0, /* tp_weaklistoffset */
818 tuple_iter, /* tp_iter */
819 0, /* tp_iternext */
820 tuple_methods, /* tp_methods */
821 0, /* tp_members */
822 0, /* tp_getset */
823 0, /* tp_base */
824 0, /* tp_dict */
825 0, /* tp_descr_get */
826 0, /* tp_descr_set */
827 0, /* tp_dictoffset */
828 0, /* tp_init */
829 0, /* tp_alloc */
830 tuple_new, /* tp_new */
831 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000833
834/* The following function breaks the notion that tuples are immutable:
835 it changes the size of a tuple. We get away with this only if there
836 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000837 as creating a new tuple object and destroying the old one, only more
838 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000839 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000840
841int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000843{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200844 PyTupleObject *v;
845 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 Py_ssize_t i;
847 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 v = (PyTupleObject *) *pv;
850 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
851 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
852 *pv = 0;
853 Py_XDECREF(v);
854 PyErr_BadInternalCall();
855 return -1;
856 }
857 oldsize = Py_SIZE(v);
858 if (oldsize == newsize)
859 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (oldsize == 0) {
862 /* Empty tuples are often shared, so we should never
863 resize them in-place even if we do own the only
864 (current) reference */
865 Py_DECREF(v);
866 *pv = PyTuple_New(newsize);
867 return *pv == NULL ? -1 : 0;
868 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 /* XXX UNREF/NEWREF interface should be more symmetrical */
871 _Py_DEC_REFTOTAL;
872 if (_PyObject_GC_IS_TRACKED(v))
873 _PyObject_GC_UNTRACK(v);
874 _Py_ForgetReference((PyObject *) v);
875 /* DECREF items deleted by shrinkage */
876 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200877 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
879 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
880 if (sv == NULL) {
881 *pv = NULL;
882 PyObject_GC_Del(v);
883 return -1;
884 }
885 _Py_NewReference((PyObject *) sv);
886 /* Zero out items added by growing */
887 if (newsize > oldsize)
888 memset(&sv->ob_item[oldsize], 0,
889 sizeof(*sv->ob_item) * (newsize - oldsize));
890 *pv = (PyObject *) sv;
891 _PyObject_GC_TRACK(sv);
892 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000893}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000894
Christian Heimesa156e092008-02-16 07:38:31 +0000895int
896PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000899#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 int i;
901 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
902 PyTupleObject *p, *q;
903 p = free_list[i];
904 freelist_size += numfree[i];
905 free_list[i] = NULL;
906 numfree[i] = 0;
907 while (p) {
908 q = p;
909 p = (PyTupleObject *)(p->ob_item[0]);
910 PyObject_GC_Del(q);
911 }
912 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000913#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000915}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916
Christian Heimesa156e092008-02-16 07:38:31 +0000917void
918PyTuple_Fini(void)
919{
920#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* empty tuples are used all over the place and applications may
922 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200923 Py_CLEAR(free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 (void)PyTuple_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000926#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000927#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 show_track();
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000929#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000930}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000931
932/*********************** Tuple Iterator **************************/
933
934typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200936 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000938} tupleiterobject;
939
Raymond Hettinger48923c52002-08-09 01:30:17 +0000940static void
941tupleiter_dealloc(tupleiterobject *it)
942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 _PyObject_GC_UNTRACK(it);
944 Py_XDECREF(it->it_seq);
945 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +0000946}
947
948static int
949tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 Py_VISIT(it->it_seq);
952 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000953}
954
Raymond Hettinger48923c52002-08-09 01:30:17 +0000955static PyObject *
956tupleiter_next(tupleiterobject *it)
957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyTupleObject *seq;
959 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 assert(it != NULL);
962 seq = it->it_seq;
963 if (seq == NULL)
964 return NULL;
965 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 if (it->it_index < PyTuple_GET_SIZE(seq)) {
968 item = PyTuple_GET_ITEM(seq, it->it_index);
969 ++it->it_index;
970 Py_INCREF(item);
971 return item;
972 }
Raymond Hettinger48923c52002-08-09 01:30:17 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300975 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000977}
978
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000979static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000980tupleiter_len(tupleiterobject *it)
981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 Py_ssize_t len = 0;
983 if (it->it_seq)
984 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
985 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000986}
987
Armin Rigof5b3e362006-02-11 21:32:43 +0000988PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000989
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000990static PyObject *
991tupleiter_reduce(tupleiterobject *it)
992{
993 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +0200994 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000995 it->it_seq, it->it_index);
996 else
Antoine Pitroua7013882012-04-05 00:04:20 +0200997 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000998}
999
1000static PyObject *
1001tupleiter_setstate(tupleiterobject *it, PyObject *state)
1002{
Victor Stinner7660b882013-06-24 23:59:24 +02001003 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001004 if (index == -1 && PyErr_Occurred())
1005 return NULL;
1006 if (it->it_seq != NULL) {
1007 if (index < 0)
1008 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001009 else if (index > PyTuple_GET_SIZE(it->it_seq))
1010 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001011 it->it_index = index;
1012 }
1013 Py_RETURN_NONE;
1014}
1015
1016PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1017PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1018
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001019static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001021 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1022 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001024};
1025
Raymond Hettinger48923c52002-08-09 01:30:17 +00001026PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1028 "tuple_iterator", /* tp_name */
1029 sizeof(tupleiterobject), /* tp_basicsize */
1030 0, /* tp_itemsize */
1031 /* methods */
1032 (destructor)tupleiter_dealloc, /* tp_dealloc */
1033 0, /* tp_print */
1034 0, /* tp_getattr */
1035 0, /* tp_setattr */
1036 0, /* tp_reserved */
1037 0, /* tp_repr */
1038 0, /* tp_as_number */
1039 0, /* tp_as_sequence */
1040 0, /* tp_as_mapping */
1041 0, /* tp_hash */
1042 0, /* tp_call */
1043 0, /* tp_str */
1044 PyObject_GenericGetAttr, /* tp_getattro */
1045 0, /* tp_setattro */
1046 0, /* tp_as_buffer */
1047 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1048 0, /* tp_doc */
1049 (traverseproc)tupleiter_traverse, /* tp_traverse */
1050 0, /* tp_clear */
1051 0, /* tp_richcompare */
1052 0, /* tp_weaklistoffset */
1053 PyObject_SelfIter, /* tp_iter */
1054 (iternextfunc)tupleiter_next, /* tp_iternext */
1055 tupleiter_methods, /* tp_methods */
1056 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001057};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001058
1059static PyObject *
1060tuple_iter(PyObject *seq)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (!PyTuple_Check(seq)) {
1065 PyErr_BadInternalCall();
1066 return NULL;
1067 }
1068 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1069 if (it == NULL)
1070 return NULL;
1071 it->it_index = 0;
1072 Py_INCREF(seq);
1073 it->it_seq = (PyTupleObject *)seq;
1074 _PyObject_GC_TRACK(it);
1075 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001076}