blob: 5bcadeb9f645986f628991284e30324d5a4cf0e5 [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) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200634 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 }
636 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200637 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /* Compare the final item again using the proper operator */
641 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000642}
643
Jeremy Hylton938ace62002-07-17 16:30:39 +0000644static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000645tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
646
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647static PyObject *
648tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyObject *arg = NULL;
651 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (type != &PyTuple_Type)
654 return tuple_subtype_new(type, args, kwds);
655 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
656 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (arg == NULL)
659 return PyTuple_New(0);
660 else
661 return PySequence_Tuple(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662}
663
Guido van Rossumae960af2001-08-30 03:11:59 +0000664static PyObject *
665tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 PyObject *tmp, *newobj, *item;
668 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 assert(PyType_IsSubtype(type, &PyTuple_Type));
671 tmp = tuple_new(&PyTuple_Type, args, kwds);
672 if (tmp == NULL)
673 return NULL;
674 assert(PyTuple_Check(tmp));
675 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
676 if (newobj == NULL)
677 return NULL;
678 for (i = 0; i < n; i++) {
679 item = PyTuple_GET_ITEM(tmp, i);
680 Py_INCREF(item);
681 PyTuple_SET_ITEM(newobj, i, item);
682 }
683 Py_DECREF(tmp);
684 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000685}
686
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000687PyDoc_STRVAR(tuple_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +0000688"tuple() -> empty tuple\n\
689tuple(iterable) -> tuple initialized from iterable's items\n\
690\n\
691If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 (lenfunc)tuplelength, /* sq_length */
695 (binaryfunc)tupleconcat, /* sq_concat */
696 (ssizeargfunc)tuplerepeat, /* sq_repeat */
697 (ssizeargfunc)tupleitem, /* sq_item */
698 0, /* sq_slice */
699 0, /* sq_ass_item */
700 0, /* sq_ass_slice */
701 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702};
703
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000704static PyObject*
705tuplesubscript(PyTupleObject* self, PyObject* item)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (PyIndex_Check(item)) {
708 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
709 if (i == -1 && PyErr_Occurred())
710 return NULL;
711 if (i < 0)
712 i += PyTuple_GET_SIZE(self);
713 return tupleitem(self, i);
714 }
715 else if (PySlice_Check(item)) {
716 Py_ssize_t start, stop, step, slicelength, cur, i;
717 PyObject* result;
718 PyObject* it;
719 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000720
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000721 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 PyTuple_GET_SIZE(self),
723 &start, &stop, &step, &slicelength) < 0) {
724 return NULL;
725 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (slicelength <= 0) {
728 return PyTuple_New(0);
729 }
730 else if (start == 0 && step == 1 &&
731 slicelength == PyTuple_GET_SIZE(self) &&
732 PyTuple_CheckExact(self)) {
733 Py_INCREF(self);
734 return (PyObject *)self;
735 }
736 else {
737 result = PyTuple_New(slicelength);
738 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 src = self->ob_item;
741 dest = ((PyTupleObject *)result)->ob_item;
742 for (cur = start, i = 0; i < slicelength;
743 cur += step, i++) {
744 it = src[cur];
745 Py_INCREF(it);
746 dest[i] = it;
747 }
748
749 return result;
750 }
751 }
752 else {
753 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400754 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 Py_TYPE(item)->tp_name);
756 return NULL;
757 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000758}
759
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000760static PyObject *
761tuple_getnewargs(PyTupleObject *v)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
764
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000765}
766
Raymond Hettinger65baa342008-02-07 00:41:02 +0000767PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000768"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
769"Raises ValueError if the value is not present."
770);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000771PyDoc_STRVAR(count_doc,
772"T.count(value) -> integer -- return number of occurrences of value");
773
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000774static PyMethodDef tuple_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
777 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
778 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000779};
780
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000781static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 (lenfunc)tuplelength,
783 (binaryfunc)tuplesubscript,
784 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000785};
786
Raymond Hettinger48923c52002-08-09 01:30:17 +0000787static PyObject *tuple_iter(PyObject *seq);
788
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyVarObject_HEAD_INIT(&PyType_Type, 0)
791 "tuple",
792 sizeof(PyTupleObject) - sizeof(PyObject *),
793 sizeof(PyObject *),
794 (destructor)tupledealloc, /* tp_dealloc */
795 0, /* tp_print */
796 0, /* tp_getattr */
797 0, /* tp_setattr */
798 0, /* tp_reserved */
799 (reprfunc)tuplerepr, /* tp_repr */
800 0, /* tp_as_number */
801 &tuple_as_sequence, /* tp_as_sequence */
802 &tuple_as_mapping, /* tp_as_mapping */
803 (hashfunc)tuplehash, /* tp_hash */
804 0, /* tp_call */
805 0, /* tp_str */
806 PyObject_GenericGetAttr, /* tp_getattro */
807 0, /* tp_setattro */
808 0, /* tp_as_buffer */
809 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
810 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
811 tuple_doc, /* tp_doc */
812 (traverseproc)tupletraverse, /* tp_traverse */
813 0, /* tp_clear */
814 tuplerichcompare, /* tp_richcompare */
815 0, /* tp_weaklistoffset */
816 tuple_iter, /* tp_iter */
817 0, /* tp_iternext */
818 tuple_methods, /* tp_methods */
819 0, /* tp_members */
820 0, /* tp_getset */
821 0, /* tp_base */
822 0, /* tp_dict */
823 0, /* tp_descr_get */
824 0, /* tp_descr_set */
825 0, /* tp_dictoffset */
826 0, /* tp_init */
827 0, /* tp_alloc */
828 tuple_new, /* tp_new */
829 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000831
832/* The following function breaks the notion that tuples are immutable:
833 it changes the size of a tuple. We get away with this only if there
834 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000835 as creating a new tuple object and destroying the old one, only more
836 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000837 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000838
839int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000840_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200842 PyTupleObject *v;
843 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_ssize_t i;
845 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 v = (PyTupleObject *) *pv;
848 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
849 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
850 *pv = 0;
851 Py_XDECREF(v);
852 PyErr_BadInternalCall();
853 return -1;
854 }
855 oldsize = Py_SIZE(v);
856 if (oldsize == newsize)
857 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (oldsize == 0) {
860 /* Empty tuples are often shared, so we should never
861 resize them in-place even if we do own the only
862 (current) reference */
863 Py_DECREF(v);
864 *pv = PyTuple_New(newsize);
865 return *pv == NULL ? -1 : 0;
866 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 /* XXX UNREF/NEWREF interface should be more symmetrical */
869 _Py_DEC_REFTOTAL;
870 if (_PyObject_GC_IS_TRACKED(v))
871 _PyObject_GC_UNTRACK(v);
872 _Py_ForgetReference((PyObject *) v);
873 /* DECREF items deleted by shrinkage */
874 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200875 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 }
877 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
878 if (sv == NULL) {
879 *pv = NULL;
880 PyObject_GC_Del(v);
881 return -1;
882 }
883 _Py_NewReference((PyObject *) sv);
884 /* Zero out items added by growing */
885 if (newsize > oldsize)
886 memset(&sv->ob_item[oldsize], 0,
887 sizeof(*sv->ob_item) * (newsize - oldsize));
888 *pv = (PyObject *) sv;
889 _PyObject_GC_TRACK(sv);
890 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000892
Christian Heimesa156e092008-02-16 07:38:31 +0000893int
894PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 int freelist_size = 0;
Christian Heimes2202f872008-02-06 14:31:34 +0000897#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 int i;
899 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
900 PyTupleObject *p, *q;
901 p = free_list[i];
902 freelist_size += numfree[i];
903 free_list[i] = NULL;
904 numfree[i] = 0;
905 while (p) {
906 q = p;
907 p = (PyTupleObject *)(p->ob_item[0]);
908 PyObject_GC_Del(q);
909 }
910 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000911#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000913}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914
Christian Heimesa156e092008-02-16 07:38:31 +0000915void
916PyTuple_Fini(void)
917{
918#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 /* empty tuples are used all over the place and applications may
920 * rely on the fact that an empty tuple is a singleton. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200921 Py_CLEAR(free_list[0]);
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
Victor Stinnera2d56982013-06-05 00:11:34 +0200934 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 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 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300973 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 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)
Victor Stinner7660b882013-06-24 23:59:24 +0200992 return Py_BuildValue("N(O)n", _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{
Victor Stinner7660b882013-06-24 23:59:24 +02001001 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001002 if (index == -1 && PyErr_Occurred())
1003 return NULL;
1004 if (it->it_seq != NULL) {
1005 if (index < 0)
1006 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001007 else if (index > PyTuple_GET_SIZE(it->it_seq))
1008 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001009 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}