blob: 6b1ab740121e8fc553c6367af956f47f40524122 [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"
Victor Stinner0430dfa2020-06-24 15:21:54 +02005#include "pycore_abstract.h" // _PyIndex_Check()
6#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
7#include "pycore_initconfig.h" // _PyStatus_OK()
8#include "pycore_object.h" // _PyObject_GC_TRACK()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Serhiy Storchaka0b561592017-03-19 08:47:58 +020010/*[clinic input]
11class tuple "PyTupleObject *" "&PyTuple_Type"
12[clinic start generated code]*/
13/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f051ba3cfdf9a189]*/
14
15#include "clinic/tupleobject.c.h"
16
Victor Stinner522691c2020-06-23 16:40:40 +020017
Victor Stinner0430dfa2020-06-24 15:21:54 +020018#if PyTuple_MAXSAVESIZE > 0
Victor Stinner522691c2020-06-23 16:40:40 +020019static struct _Py_tuple_state *
20get_tuple_state(void)
21{
22 PyInterpreterState *interp = _PyInterpreterState_GET();
23 return &interp->tuple;
24}
Victor Stinner0430dfa2020-06-24 15:21:54 +020025#endif
Victor Stinner522691c2020-06-23 16:40:40 +020026
27
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050028static inline void
29tuple_gc_track(PyTupleObject *op)
30{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050031 _PyObject_GC_TRACK(op);
32}
33
Victor Stinner522691c2020-06-23 16:40:40 +020034
David Malcolm49526f42012-06-22 14:55:41 -040035/* Print summary info about the state of the optimized allocator */
36void
37_PyTuple_DebugMallocStats(FILE *out)
38{
39#if PyTuple_MAXSAVESIZE > 0
Victor Stinner522691c2020-06-23 16:40:40 +020040 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinner69ac6e52020-06-04 23:38:36 +020041 for (int i = 1; i < PyTuple_MAXSAVESIZE; i++) {
42 char buf[128];
David Malcolm49526f42012-06-22 14:55:41 -040043 PyOS_snprintf(buf, sizeof(buf),
44 "free %d-sized PyTupleObject", i);
Victor Stinner69ac6e52020-06-04 23:38:36 +020045 _PyDebugAllocatorStats(out, buf, state->numfree[i],
46 _PyObject_VAR_SIZE(&PyTuple_Type, i));
David Malcolm49526f42012-06-22 14:55:41 -040047 }
48#endif
49}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000050
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050051/* Allocate an uninitialized tuple object. Before making it public following
52 steps must be done:
53 - initialize its items
54 - call tuple_gc_track() on it
55 Because the empty tuple is always reused and it's already tracked by GC,
56 this function must not be called with size == 0 (unless from PyTuple_New()
57 which wraps this function).
58*/
59static PyTupleObject *
Victor Stinner0430dfa2020-06-24 15:21:54 +020060tuple_alloc(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020062 PyTupleObject *op;
Victor Stinner0430dfa2020-06-24 15:21:54 +020063#if PyTuple_MAXSAVESIZE > 0
64 // If Python is built with the empty tuple singleton,
65 // tuple_alloc(0) must not be called.
66 assert(size != 0);
67#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (size < 0) {
69 PyErr_BadInternalCall();
70 return NULL;
71 }
Victor Stinner0430dfa2020-06-24 15:21:54 +020072
Christian Heimes2202f872008-02-06 14:31:34 +000073#if PyTuple_MAXSAVESIZE > 0
Victor Stinner0430dfa2020-06-24 15:21:54 +020074 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinnerbcb19832020-06-08 02:14:47 +020075#ifdef Py_DEBUG
76 // tuple_alloc() must not be called after _PyTuple_Fini()
77 assert(state->numfree[0] != -1);
78#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +020079 if (size < PyTuple_MAXSAVESIZE && (op = state->free_list[size]) != NULL) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050080 assert(size != 0);
Victor Stinner69ac6e52020-06-04 23:38:36 +020081 state->free_list[size] = (PyTupleObject *) op->ob_item[0];
82 state->numfree[size]--;
Victor Stinner04fc4f22020-06-16 01:28:07 +020083 /* Inlined _PyObject_InitVar() without _PyType_HasFeature() test */
Guido van Rossum68055ce1998-12-11 14:56:38 +000084#ifdef Py_TRACE_REFS
Victor Stinnerfe2978b2020-05-27 14:55:10 +020085 Py_SET_SIZE(op, size);
Dong-hee Na7d847e22020-05-26 02:25:28 +090086 Py_SET_TYPE(op, &PyTuple_Type);
Guido van Rossum68055ce1998-12-11 14:56:38 +000087#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 _Py_NewReference((PyObject *)op);
89 }
90 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000091#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 /* Check for overflow */
Sergey Fedoseev755d4ef2019-09-10 01:40:58 +050094 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
95 sizeof(PyObject *))) / sizeof(PyObject *)) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050096 return (PyTupleObject *)PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
99 if (op == NULL)
100 return NULL;
101 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500102 return op;
103}
104
Victor Stinner0430dfa2020-06-24 15:21:54 +0200105static int
106tuple_create_empty_tuple_singleton(struct _Py_tuple_state *state)
107{
108#if PyTuple_MAXSAVESIZE > 0
109 assert(state->free_list[0] == NULL);
110
111 PyTupleObject *op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, 0);
112 if (op == NULL) {
113 return -1;
114 }
115 // The empty tuple singleton is not tracked by the GC.
116 // It does not contain any Python object.
117
118 state->free_list[0] = op;
119 state->numfree[0]++;
120
121 assert(state->numfree[0] == 1);
122#endif
123 return 0;
124}
125
126
127static PyObject *
128tuple_get_empty(void)
129{
130#if PyTuple_MAXSAVESIZE > 0
131 struct _Py_tuple_state *state = get_tuple_state();
132 PyTupleObject *op = state->free_list[0];
133 // tuple_get_empty() must not be called before _PyTuple_Init()
134 // or after _PyTuple_Fini()
135 assert(op != NULL);
136#ifdef Py_DEBUG
137 assert(state->numfree[0] != -1);
138#endif
139
140 Py_INCREF(op);
141 return (PyObject *) op;
142#else
143 return PyTuple_New(0);
144#endif
145}
146
147
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500148PyObject *
149PyTuple_New(Py_ssize_t size)
150{
151 PyTupleObject *op;
152#if PyTuple_MAXSAVESIZE > 0
Victor Stinner0430dfa2020-06-24 15:21:54 +0200153 if (size == 0) {
154 return tuple_get_empty();
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500155 }
156#endif
Victor Stinner0430dfa2020-06-24 15:21:54 +0200157 op = tuple_alloc(size);
Zackery Spytz60bd1f82019-09-04 07:58:05 -0600158 if (op == NULL) {
159 return NULL;
160 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500161 for (Py_ssize_t i = 0; i < size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 op->ob_item[i] = NULL;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500163 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500164 tuple_gc_track(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
167
Martin v. Löwis18e16552006-02-15 17:27:45 +0000168Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200169PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (!PyTuple_Check(op)) {
172 PyErr_BadInternalCall();
173 return -1;
174 }
175 else
176 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
178
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200180PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 if (!PyTuple_Check(op)) {
183 PyErr_BadInternalCall();
184 return NULL;
185 }
186 if (i < 0 || i >= Py_SIZE(op)) {
187 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
188 return NULL;
189 }
190 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191}
192
193int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200194PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200196 PyObject **p;
Victor Stinnera93c51e2020-02-07 00:38:59 +0100197 if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 Py_XDECREF(newitem);
199 PyErr_BadInternalCall();
200 return -1;
201 }
202 if (i < 0 || i >= Py_SIZE(op)) {
203 Py_XDECREF(newitem);
204 PyErr_SetString(PyExc_IndexError,
205 "tuple assignment index out of range");
206 return -1;
207 }
208 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300209 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211}
212
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000213void
214_PyTuple_MaybeUntrack(PyObject *op)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 PyTupleObject *t;
217 Py_ssize_t i, n;
218
219 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
220 return;
221 t = (PyTupleObject *) op;
222 n = Py_SIZE(t);
223 for (i = 0; i < n; i++) {
224 PyObject *elt = PyTuple_GET_ITEM(t, i);
225 /* Tuple with NULL elements aren't
226 fully constructed, don't untrack
227 them yet. */
228 if (!elt ||
229 _PyObject_GC_MAY_BE_TRACKED(elt))
230 return;
231 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000233}
234
Raymond Hettingercb2da432003-10-12 18:24:34 +0000235PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000236PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 Py_ssize_t i;
239 PyObject *o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PyObject **items;
241 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000242
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500243 if (n == 0) {
Victor Stinner0430dfa2020-06-24 15:21:54 +0200244 return tuple_get_empty();
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500245 }
246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 va_start(vargs, n);
Victor Stinner0430dfa2020-06-24 15:21:54 +0200248 PyTupleObject *result = tuple_alloc(n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200249 if (result == NULL) {
250 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200252 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500253 items = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 for (i = 0; i < n; i++) {
255 o = va_arg(vargs, PyObject *);
256 Py_INCREF(o);
257 items[i] = o;
258 }
259 va_end(vargs);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500260 tuple_gc_track(result);
261 return (PyObject *)result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000262}
263
264
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265/* Methods */
266
267static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200268tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200270 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200272 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (len > 0) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200274 Py_ssize_t i = len;
275 while (--i >= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 Py_XDECREF(op->ob_item[i]);
Victor Stinner69ac6e52020-06-04 23:38:36 +0200277 }
Christian Heimes2202f872008-02-06 14:31:34 +0000278#if PyTuple_MAXSAVESIZE > 0
Victor Stinner522691c2020-06-23 16:40:40 +0200279 struct _Py_tuple_state *state = get_tuple_state();
Victor Stinnerbcb19832020-06-08 02:14:47 +0200280#ifdef Py_DEBUG
281 // tupledealloc() must not be called after _PyTuple_Fini()
282 assert(state->numfree[0] != -1);
283#endif
Victor Stinner0430dfa2020-06-24 15:21:54 +0200284 if (len < PyTuple_MAXSAVESIZE
285 && state->numfree[len] < PyTuple_MAXFREELIST
286 && Py_IS_TYPE(op, &PyTuple_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200288 op->ob_item[0] = (PyObject *) state->free_list[len];
289 state->numfree[len]++;
290 state->free_list[len] = op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 goto done; /* return */
292 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000293#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 }
295 Py_TYPE(op)->tp_free((PyObject *)op);
Victor Stinner0430dfa2020-06-24 15:21:54 +0200296
Victor Stinnerb4b53862020-05-05 19:55:29 +0200297#if PyTuple_MAXSAVESIZE > 0
Guido van Rossumd724b232000-03-13 16:01:29 +0000298done:
Victor Stinnerb4b53862020-05-05 19:55:29 +0200299#endif
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200300 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301}
302
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000304tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100307 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 n = Py_SIZE(v);
310 if (n == 0)
311 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 /* While not mutable, it is still possible to end up with a cycle in a
314 tuple through an object that stores itself within a tuple (and thus
315 infinitely asks for the repr of itself). This should only be
316 possible within a type. */
317 i = Py_ReprEnter((PyObject *)v);
318 if (i != 0) {
319 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
320 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000321
Victor Stinner88a9cd92013-11-19 12:59:46 +0100322 _PyUnicodeWriter_Init(&writer);
323 writer.overallocate = 1;
324 if (Py_SIZE(v) > 1) {
325 /* "(" + "1" + ", 2" * (len - 1) + ")" */
326 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
327 }
328 else {
329 /* "(1,)" */
330 writer.min_length = 4;
331 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200332
Victor Stinner88a9cd92013-11-19 12:59:46 +0100333 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200334 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* Do repr() on each element. */
337 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100338 PyObject *s;
339
340 if (i > 0) {
341 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
342 goto error;
343 }
344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100346 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200347 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100348
349 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
350 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200351 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100352 }
353 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100355
356 writer.overallocate = 0;
357 if (n > 1) {
358 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
359 goto error;
360 }
361 else {
362 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
363 goto error;
364 }
Tim Petersa7259592001-06-16 05:11:17 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100367 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200368
369error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100370 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200371 Py_ReprLeave((PyObject *)v);
372 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373}
374
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000375
jdemeyeraeb1be52018-10-28 02:06:38 +0200376/* Hash for tuples. This is a slightly simplified version of the xxHash
377 non-cryptographic hash:
378 - we do not use any parallellism, there is only 1 accumulator.
379 - we drop the final mixing since this is just a permutation of the
380 output space: it does not help against collisions.
381 - at the end, we mangle the length with a single constant.
382 For the xxHash specification, see
383 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100384
jdemeyeraeb1be52018-10-28 02:06:38 +0200385 Below are the official constants from the xxHash specification. Optimizing
386 compilers should emit a single "rotate" instruction for the
387 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
388 platform, the macro could be changed to expand to a platform-specific rotate
389 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000390*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200391#if SIZEOF_PY_UHASH_T > 4
392#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
393#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
394#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
395#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
396#else
397#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
398#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
399#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
400#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
401#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000402
jdemeyeraeb1be52018-10-28 02:06:38 +0200403/* Tests have shown that it's not worth to cache the hash value, see
404 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000405static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000406tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000407{
jdemeyeraeb1be52018-10-28 02:06:38 +0200408 Py_ssize_t i, len = Py_SIZE(v);
409 PyObject **item = v->ob_item;
410
411 Py_uhash_t acc = _PyHASH_XXPRIME_5;
412 for (i = 0; i < len; i++) {
413 Py_uhash_t lane = PyObject_Hash(item[i]);
414 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200416 }
417 acc += lane * _PyHASH_XXPRIME_2;
418 acc = _PyHASH_XXROTATE(acc);
419 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200421
422 /* Add input length, mangled to keep the historical value of hash(()). */
423 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
424
425 if (acc == (Py_uhash_t)-1) {
426 return 1546275796;
427 }
428 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000429}
430
Martin v. Löwis18e16552006-02-15 17:27:45 +0000431static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000432tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435}
436
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000437static int
Fred Drakeba096332000-07-09 07:04:36 +0000438tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 Py_ssize_t i;
441 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300444 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000446}
447
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200449tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (i < 0 || i >= Py_SIZE(a)) {
452 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
453 return NULL;
454 }
455 Py_INCREF(a->ob_item[i]);
456 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457}
458
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500459PyObject *
460_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
461{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500462 if (n == 0) {
Victor Stinner0430dfa2020-06-24 15:21:54 +0200463 return tuple_get_empty();
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500464 }
465
Victor Stinner0430dfa2020-06-24 15:21:54 +0200466 PyTupleObject *tuple = tuple_alloc(n);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500467 if (tuple == NULL) {
468 return NULL;
469 }
470 PyObject **dst = tuple->ob_item;
471 for (Py_ssize_t i = 0; i < n; i++) {
472 PyObject *item = src[i];
473 Py_INCREF(item);
474 dst[i] = item;
475 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500476 tuple_gc_track(tuple);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500477 return (PyObject *)tuple;
478}
479
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200481tupleslice(PyTupleObject *a, Py_ssize_t ilow,
482 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (ilow < 0)
485 ilow = 0;
486 if (ihigh > Py_SIZE(a))
487 ihigh = Py_SIZE(a);
488 if (ihigh < ilow)
489 ihigh = ilow;
490 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
491 Py_INCREF(a);
492 return (PyObject *)a;
493 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500494 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495}
496
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000498PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (op == NULL || !PyTuple_Check(op)) {
501 PyErr_BadInternalCall();
502 return NULL;
503 }
504 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000505}
506
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000507static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200508tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200510 Py_ssize_t size;
511 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 PyObject **src, **dest;
513 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200514 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
515 Py_INCREF(bb);
516 return bb;
517 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (!PyTuple_Check(bb)) {
519 PyErr_Format(PyExc_TypeError,
520 "can only concatenate tuple (not \"%.200s\") to tuple",
521 Py_TYPE(bb)->tp_name);
522 return NULL;
523 }
Victor Stinner69ac6e52020-06-04 23:38:36 +0200524 PyTupleObject *b = (PyTupleObject *)bb;
525
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200526 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
527 Py_INCREF(a);
528 return (PyObject *)a;
529 }
Sergey Fedoseeve682b262020-05-25 19:54:40 +0500530 assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
Martin Panterb93d8632016-07-25 02:39:20 +0000531 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500532 if (size == 0) {
Victor Stinner0430dfa2020-06-24 15:21:54 +0200533 return tuple_get_empty();
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500534 }
535
Victor Stinner0430dfa2020-06-24 15:21:54 +0200536 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (np == NULL) {
538 return NULL;
539 }
540 src = a->ob_item;
541 dest = np->ob_item;
542 for (i = 0; i < Py_SIZE(a); i++) {
543 PyObject *v = src[i];
544 Py_INCREF(v);
545 dest[i] = v;
546 }
547 src = b->ob_item;
548 dest = np->ob_item + Py_SIZE(a);
549 for (i = 0; i < Py_SIZE(b); i++) {
550 PyObject *v = src[i];
551 Py_INCREF(v);
552 dest[i] = v;
553 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500554 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556}
557
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000559tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 Py_ssize_t i, j;
562 Py_ssize_t size;
563 PyTupleObject *np;
564 PyObject **p, **items;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (Py_SIZE(a) == 0 || n == 1) {
566 if (PyTuple_CheckExact(a)) {
567 /* Since tuples are immutable, we can return a shared
568 copy in this case */
569 Py_INCREF(a);
570 return (PyObject *)a;
571 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500572 }
573 if (Py_SIZE(a) == 0 || n <= 0) {
Victor Stinner0430dfa2020-06-24 15:21:54 +0200574 return tuple_get_empty();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100576 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100578 size = Py_SIZE(a) * n;
Victor Stinner0430dfa2020-06-24 15:21:54 +0200579 np = tuple_alloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (np == NULL)
581 return NULL;
582 p = np->ob_item;
583 items = a->ob_item;
584 for (i = 0; i < n; i++) {
585 for (j = 0; j < Py_SIZE(a); j++) {
586 *p = items[j];
587 Py_INCREF(*p);
588 p++;
589 }
590 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500591 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000593}
594
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200595/*[clinic input]
596tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000597
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200598 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300599 start: slice_index(accept={int}) = 0
600 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200601 /
602
603Return first index of value.
604
605Raises ValueError if the value is not present.
606[clinic start generated code]*/
607
608static PyObject *
609tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
610 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300611/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200612{
613 Py_ssize_t i;
614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (start < 0) {
616 start += Py_SIZE(self);
617 if (start < 0)
618 start = 0;
619 }
620 if (stop < 0) {
621 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200623 else if (stop > Py_SIZE(self)) {
624 stop = Py_SIZE(self);
625 }
626 for (i = start; i < stop; i++) {
627 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 if (cmp > 0)
629 return PyLong_FromSsize_t(i);
630 else if (cmp < 0)
631 return NULL;
632 }
633 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
634 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000635}
636
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200637/*[clinic input]
638tuple.count
639
640 value: object
641 /
642
643Return number of occurrences of value.
644[clinic start generated code]*/
645
Raymond Hettinger65baa342008-02-07 00:41:02 +0000646static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200647tuple_count(PyTupleObject *self, PyObject *value)
648/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 Py_ssize_t count = 0;
651 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200654 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (cmp > 0)
656 count++;
657 else if (cmp < 0)
658 return NULL;
659 }
660 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000661}
662
Jeremy Hylton8caad492000-06-23 14:18:11 +0000663static int
664tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 for (i = Py_SIZE(o); --i >= 0; )
669 Py_VISIT(o->ob_item[i]);
670 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000671}
672
Guido van Rossumf77bc622001-01-18 00:00:53 +0000673static PyObject *
674tuplerichcompare(PyObject *v, PyObject *w, int op)
675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 PyTupleObject *vt, *wt;
677 Py_ssize_t i;
678 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000679
Brian Curtindfc80e32011-08-10 20:28:54 -0500680 if (!PyTuple_Check(v) || !PyTuple_Check(w))
681 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 vt = (PyTupleObject *)v;
684 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 vlen = Py_SIZE(vt);
687 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 /* Note: the corresponding code for lists has an "early out" test
690 * here when op is EQ or NE and the lengths differ. That pays there,
691 * but Tim was unable to find any real code where EQ/NE tuple
692 * compares don't have the same length, so testing for it here would
693 * have cost without benefit.
694 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 /* Search for the first index where items are different.
697 * Note that because tuples are immutable, it's safe to reuse
698 * vlen and wlen across the comparison calls.
699 */
700 for (i = 0; i < vlen && i < wlen; i++) {
701 int k = PyObject_RichCompareBool(vt->ob_item[i],
702 wt->ob_item[i], Py_EQ);
703 if (k < 0)
704 return NULL;
705 if (!k)
706 break;
707 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (i >= vlen || i >= wlen) {
710 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100711 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* We have an item that differs -- shortcuts for EQ/NE */
715 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200716 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 }
718 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200719 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* Compare the final item again using the proper operator */
723 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000724}
725
Jeremy Hylton938ace62002-07-17 16:30:39 +0000726static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200727tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
728
729/*[clinic input]
730@classmethod
731tuple.__new__ as tuple_new
732 iterable: object(c_default="NULL") = ()
733 /
734
735Built-in immutable sequence.
736
737If no argument is given, the constructor returns an empty tuple.
738If iterable is specified the tuple is initialized from iterable's items.
739
740If the argument is a tuple, the return value is the same object.
741[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000742
Tim Peters6d6c1a32001-08-02 04:15:00 +0000743static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200744tuple_new_impl(PyTypeObject *type, PyObject *iterable)
745/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200748 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749
Victor Stinner0430dfa2020-06-24 15:21:54 +0200750 if (iterable == NULL) {
751 return tuple_get_empty();
752 }
753 else {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200754 return PySequence_Tuple(iterable);
Victor Stinner0430dfa2020-06-24 15:21:54 +0200755 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756}
757
Guido van Rossumae960af2001-08-30 03:11:59 +0000758static PyObject *
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900759tuple_vectorcall(PyObject *type, PyObject * const*args,
760 size_t nargsf, PyObject *kwnames)
761{
Dong-hee Na87ec86c2020-03-16 23:06:20 +0900762 if (!_PyArg_NoKwnames("tuple", kwnames)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900763 return NULL;
764 }
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900765
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900766 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900767 if (!_PyArg_CheckPositional("tuple", nargs, 0, 1)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900768 return NULL;
769 }
770
771 if (nargs) {
772 return tuple_new_impl((PyTypeObject *)type, args[0]);
773 }
Victor Stinner0430dfa2020-06-24 15:21:54 +0200774 else {
775 return tuple_get_empty();
776 }
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900777}
778
779static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200780tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyObject *tmp, *newobj, *item;
783 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200786 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 if (tmp == NULL)
788 return NULL;
789 assert(PyTuple_Check(tmp));
790 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
Hai Shic81609e2020-03-16 03:37:49 +0800791 if (newobj == NULL) {
792 Py_DECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 return NULL;
Hai Shic81609e2020-03-16 03:37:49 +0800794 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 for (i = 0; i < n; i++) {
796 item = PyTuple_GET_ITEM(tmp, i);
797 Py_INCREF(item);
798 PyTuple_SET_ITEM(newobj, i, item);
799 }
800 Py_DECREF(tmp);
801 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000802}
803
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 (lenfunc)tuplelength, /* sq_length */
806 (binaryfunc)tupleconcat, /* sq_concat */
807 (ssizeargfunc)tuplerepeat, /* sq_repeat */
808 (ssizeargfunc)tupleitem, /* sq_item */
809 0, /* sq_slice */
810 0, /* sq_ass_item */
811 0, /* sq_ass_slice */
812 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000813};
814
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000815static PyObject*
816tuplesubscript(PyTupleObject* self, PyObject* item)
817{
Victor Stinnera15e2602020-04-08 02:01:56 +0200818 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
820 if (i == -1 && PyErr_Occurred())
821 return NULL;
822 if (i < 0)
823 i += PyTuple_GET_SIZE(self);
824 return tupleitem(self, i);
825 }
826 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600827 Py_ssize_t start, stop, step, slicelength, i;
828 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyObject* it;
830 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000831
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300832 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 return NULL;
834 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300835 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
836 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (slicelength <= 0) {
Victor Stinner0430dfa2020-06-24 15:21:54 +0200839 return tuple_get_empty();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 }
841 else if (start == 0 && step == 1 &&
842 slicelength == PyTuple_GET_SIZE(self) &&
843 PyTuple_CheckExact(self)) {
844 Py_INCREF(self);
845 return (PyObject *)self;
846 }
847 else {
Victor Stinner0430dfa2020-06-24 15:21:54 +0200848 PyTupleObject* result = tuple_alloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 src = self->ob_item;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500852 dest = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 for (cur = start, i = 0; i < slicelength;
854 cur += step, i++) {
855 it = src[cur];
856 Py_INCREF(it);
857 dest[i] = it;
858 }
859
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500860 tuple_gc_track(result);
861 return (PyObject *)result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 }
863 }
864 else {
865 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400866 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 Py_TYPE(item)->tp_name);
868 return NULL;
869 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000870}
871
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200872/*[clinic input]
873tuple.__getnewargs__
874[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200876static PyObject *
877tuple___getnewargs___impl(PyTupleObject *self)
878/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
879{
880 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000881}
882
883static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200884 TUPLE___GETNEWARGS___METHODDEF
885 TUPLE_INDEX_METHODDEF
886 TUPLE_COUNT_METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -0700887 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000889};
890
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000891static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 (lenfunc)tuplelength,
893 (binaryfunc)tuplesubscript,
894 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000895};
896
Raymond Hettinger48923c52002-08-09 01:30:17 +0000897static PyObject *tuple_iter(PyObject *seq);
898
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyVarObject_HEAD_INIT(&PyType_Type, 0)
901 "tuple",
902 sizeof(PyTupleObject) - sizeof(PyObject *),
903 sizeof(PyObject *),
904 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200905 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 0, /* tp_getattr */
907 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200908 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 (reprfunc)tuplerepr, /* tp_repr */
910 0, /* tp_as_number */
911 &tuple_as_sequence, /* tp_as_sequence */
912 &tuple_as_mapping, /* tp_as_mapping */
913 (hashfunc)tuplehash, /* tp_hash */
914 0, /* tp_call */
915 0, /* tp_str */
916 PyObject_GenericGetAttr, /* tp_getattro */
917 0, /* tp_setattro */
918 0, /* tp_as_buffer */
919 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Brandt Bucher145bf262021-02-26 14:51:55 -0800920 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS |
Mark Shannon069e81a2021-04-30 09:50:28 +0100921 _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200922 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 (traverseproc)tupletraverse, /* tp_traverse */
924 0, /* tp_clear */
925 tuplerichcompare, /* tp_richcompare */
926 0, /* tp_weaklistoffset */
927 tuple_iter, /* tp_iter */
928 0, /* tp_iternext */
929 tuple_methods, /* tp_methods */
930 0, /* tp_members */
931 0, /* tp_getset */
932 0, /* tp_base */
933 0, /* tp_dict */
934 0, /* tp_descr_get */
935 0, /* tp_descr_set */
936 0, /* tp_dictoffset */
937 0, /* tp_init */
938 0, /* tp_alloc */
939 tuple_new, /* tp_new */
940 PyObject_GC_Del, /* tp_free */
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900941 .tp_vectorcall = tuple_vectorcall,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000942};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000943
944/* The following function breaks the notion that tuples are immutable:
945 it changes the size of a tuple. We get away with this only if there
946 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000947 as creating a new tuple object and destroying the old one, only more
948 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000949 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000950
951int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000952_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000953{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200954 PyTupleObject *v;
955 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 Py_ssize_t i;
957 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 v = (PyTupleObject *) *pv;
Andy Lester55728702020-03-06 16:53:17 -0600960 if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
962 *pv = 0;
963 Py_XDECREF(v);
964 PyErr_BadInternalCall();
965 return -1;
966 }
967 oldsize = Py_SIZE(v);
968 if (oldsize == newsize)
969 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 if (oldsize == 0) {
972 /* Empty tuples are often shared, so we should never
973 resize them in-place even if we do own the only
974 (current) reference */
975 Py_DECREF(v);
976 *pv = PyTuple_New(newsize);
977 return *pv == NULL ? -1 : 0;
978 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 /* XXX UNREF/NEWREF interface should be more symmetrical */
Victor Stinner49932fe2020-02-03 17:55:05 +0100981#ifdef Py_REF_DEBUG
982 _Py_RefTotal--;
983#endif
984 if (_PyObject_GC_IS_TRACKED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 _PyObject_GC_UNTRACK(v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100986 }
987#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 _Py_ForgetReference((PyObject *) v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100989#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 /* DECREF items deleted by shrinkage */
991 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200992 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 }
994 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
995 if (sv == NULL) {
996 *pv = NULL;
997 PyObject_GC_Del(v);
998 return -1;
999 }
1000 _Py_NewReference((PyObject *) sv);
1001 /* Zero out items added by growing */
1002 if (newsize > oldsize)
1003 memset(&sv->ob_item[oldsize], 0,
1004 sizeof(*sv->ob_item) * (newsize - oldsize));
1005 *pv = (PyObject *) sv;
1006 _PyObject_GC_TRACK(sv);
1007 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001008}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001009
Victor Stinnerae00a5a2020-04-29 02:29:20 +02001010void
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001011_PyTuple_ClearFreeList(PyInterpreterState *interp)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001012{
Christian Heimes2202f872008-02-06 14:31:34 +00001013#if PyTuple_MAXSAVESIZE > 0
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001014 struct _Py_tuple_state *state = &interp->tuple;
Victor Stinnerae00a5a2020-04-29 02:29:20 +02001015 for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) {
Victor Stinner69ac6e52020-06-04 23:38:36 +02001016 PyTupleObject *p = state->free_list[i];
1017 state->free_list[i] = NULL;
1018 state->numfree[i] = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 while (p) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +02001020 PyTupleObject *q = p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 p = (PyTupleObject *)(p->ob_item[0]);
1022 PyObject_GC_Del(q);
1023 }
1024 }
Victor Stinnerae00a5a2020-04-29 02:29:20 +02001025 // the empty tuple singleton is only cleared by _PyTuple_Fini()
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001026#endif
Christian Heimesa156e092008-02-16 07:38:31 +00001027}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028
Victor Stinner0430dfa2020-06-24 15:21:54 +02001029
1030PyStatus
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001031_PyTuple_Init(PyInterpreterState *interp)
Victor Stinner0430dfa2020-06-24 15:21:54 +02001032{
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001033 struct _Py_tuple_state *state = &interp->tuple;
Victor Stinner0430dfa2020-06-24 15:21:54 +02001034 if (tuple_create_empty_tuple_singleton(state) < 0) {
1035 return _PyStatus_NO_MEMORY();
1036 }
1037 return _PyStatus_OK();
1038}
1039
1040
Christian Heimesa156e092008-02-16 07:38:31 +00001041void
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001042_PyTuple_Fini(PyInterpreterState *interp)
Christian Heimesa156e092008-02-16 07:38:31 +00001043{
1044#if PyTuple_MAXSAVESIZE > 0
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001045 struct _Py_tuple_state *state = &interp->tuple;
Victor Stinner0430dfa2020-06-24 15:21:54 +02001046 // The empty tuple singleton must not be tracked by the GC
1047 assert(!_PyObject_GC_IS_TRACKED(state->free_list[0]));
Victor Stinner69ac6e52020-06-04 23:38:36 +02001048 Py_CLEAR(state->free_list[0]);
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001049 _PyTuple_ClearFreeList(interp);
Victor Stinnerbcb19832020-06-08 02:14:47 +02001050#ifdef Py_DEBUG
1051 state->numfree[0] = -1;
1052#endif
Christian Heimesa156e092008-02-16 07:38:31 +00001053#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001054}
Raymond Hettinger48923c52002-08-09 01:30:17 +00001055
1056/*********************** Tuple Iterator **************************/
1057
1058typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +02001060 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +00001062} tupleiterobject;
1063
Raymond Hettinger48923c52002-08-09 01:30:17 +00001064static void
1065tupleiter_dealloc(tupleiterobject *it)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 _PyObject_GC_UNTRACK(it);
1068 Py_XDECREF(it->it_seq);
1069 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +00001070}
1071
1072static int
1073tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
1074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 Py_VISIT(it->it_seq);
1076 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001077}
1078
Raymond Hettinger48923c52002-08-09 01:30:17 +00001079static PyObject *
1080tupleiter_next(tupleiterobject *it)
1081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 PyTupleObject *seq;
1083 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 assert(it != NULL);
1086 seq = it->it_seq;
1087 if (seq == NULL)
1088 return NULL;
1089 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1092 item = PyTuple_GET_ITEM(seq, it->it_index);
1093 ++it->it_index;
1094 Py_INCREF(item);
1095 return item;
1096 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001099 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001101}
1102
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001103static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301104tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 Py_ssize_t len = 0;
1107 if (it->it_seq)
1108 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1109 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001110}
1111
Armin Rigof5b3e362006-02-11 21:32:43 +00001112PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001113
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001114static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301115tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001116{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001117 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001118 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001119 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001120 it->it_seq, it->it_index);
1121 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001122 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001123}
1124
1125static PyObject *
1126tupleiter_setstate(tupleiterobject *it, PyObject *state)
1127{
Victor Stinner7660b882013-06-24 23:59:24 +02001128 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001129 if (index == -1 && PyErr_Occurred())
1130 return NULL;
1131 if (it->it_seq != NULL) {
1132 if (index < 0)
1133 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001134 else if (index > PyTuple_GET_SIZE(it->it_seq))
1135 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001136 it->it_index = index;
1137 }
1138 Py_RETURN_NONE;
1139}
1140
1141PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1142PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1143
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001144static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001146 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1147 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001149};
1150
Raymond Hettinger48923c52002-08-09 01:30:17 +00001151PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1153 "tuple_iterator", /* tp_name */
1154 sizeof(tupleiterobject), /* tp_basicsize */
1155 0, /* tp_itemsize */
1156 /* methods */
1157 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001158 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 0, /* tp_getattr */
1160 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001161 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 0, /* tp_repr */
1163 0, /* tp_as_number */
1164 0, /* tp_as_sequence */
1165 0, /* tp_as_mapping */
1166 0, /* tp_hash */
1167 0, /* tp_call */
1168 0, /* tp_str */
1169 PyObject_GenericGetAttr, /* tp_getattro */
1170 0, /* tp_setattro */
1171 0, /* tp_as_buffer */
1172 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1173 0, /* tp_doc */
1174 (traverseproc)tupleiter_traverse, /* tp_traverse */
1175 0, /* tp_clear */
1176 0, /* tp_richcompare */
1177 0, /* tp_weaklistoffset */
1178 PyObject_SelfIter, /* tp_iter */
1179 (iternextfunc)tupleiter_next, /* tp_iternext */
1180 tupleiter_methods, /* tp_methods */
1181 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001182};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183
1184static PyObject *
1185tuple_iter(PyObject *seq)
1186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (!PyTuple_Check(seq)) {
1190 PyErr_BadInternalCall();
1191 return NULL;
1192 }
1193 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1194 if (it == NULL)
1195 return NULL;
1196 it->it_index = 0;
1197 Py_INCREF(seq);
1198 it->it_seq = (PyTupleObject *)seq;
1199 _PyObject_GC_TRACK(it);
1200 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001201}