blob: 951cd1faf7e8fce4b79481e8ca7208768245f72f [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 Stinnera15e2602020-04-08 02:01:56 +02005#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinnere281f7d2018-11-01 02:30:36 +01006#include "pycore_accu.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02007#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
8#include "pycore_object.h"
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
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050017static inline void
18tuple_gc_track(PyTupleObject *op)
19{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050020 _PyObject_GC_TRACK(op);
21}
22
David Malcolm49526f42012-06-22 14:55:41 -040023/* Print summary info about the state of the optimized allocator */
24void
25_PyTuple_DebugMallocStats(FILE *out)
26{
27#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +020028 PyInterpreterState *interp = _PyInterpreterState_GET();
29 struct _Py_tuple_state *state = &interp->tuple;
30 for (int i = 1; i < PyTuple_MAXSAVESIZE; i++) {
31 char buf[128];
David Malcolm49526f42012-06-22 14:55:41 -040032 PyOS_snprintf(buf, sizeof(buf),
33 "free %d-sized PyTupleObject", i);
Victor Stinner69ac6e52020-06-04 23:38:36 +020034 _PyDebugAllocatorStats(out, buf, state->numfree[i],
35 _PyObject_VAR_SIZE(&PyTuple_Type, i));
David Malcolm49526f42012-06-22 14:55:41 -040036 }
37#endif
38}
Antoine Pitrou3a652b12009-03-23 18:52:06 +000039
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050040/* Allocate an uninitialized tuple object. Before making it public following
41 steps must be done:
42 - initialize its items
43 - call tuple_gc_track() on it
44 Because the empty tuple is always reused and it's already tracked by GC,
45 this function must not be called with size == 0 (unless from PyTuple_New()
46 which wraps this function).
47*/
48static PyTupleObject *
Victor Stinner69ac6e52020-06-04 23:38:36 +020049tuple_alloc(struct _Py_tuple_state *state, Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020051 PyTupleObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (size < 0) {
53 PyErr_BadInternalCall();
54 return NULL;
55 }
Christian Heimes2202f872008-02-06 14:31:34 +000056#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +020057 if (size < PyTuple_MAXSAVESIZE && (op = state->free_list[size]) != NULL) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050058 assert(size != 0);
Victor Stinner69ac6e52020-06-04 23:38:36 +020059 state->free_list[size] = (PyTupleObject *) op->ob_item[0];
60 state->numfree[size]--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000062#ifdef Py_TRACE_REFS
Victor Stinnerfe2978b2020-05-27 14:55:10 +020063 Py_SET_SIZE(op, size);
Dong-hee Na7d847e22020-05-26 02:25:28 +090064 Py_SET_TYPE(op, &PyTuple_Type);
Guido van Rossum68055ce1998-12-11 14:56:38 +000065#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 _Py_NewReference((PyObject *)op);
67 }
68 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 /* Check for overflow */
Sergey Fedoseev755d4ef2019-09-10 01:40:58 +050072 if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
73 sizeof(PyObject *))) / sizeof(PyObject *)) {
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050074 return (PyTupleObject *)PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
77 if (op == NULL)
78 return NULL;
79 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050080 return op;
81}
82
83PyObject *
84PyTuple_New(Py_ssize_t size)
85{
86 PyTupleObject *op;
87#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +020088 PyInterpreterState *interp = _PyInterpreterState_GET();
89 struct _Py_tuple_state *state = &interp->tuple;
90 if (size == 0 && state->free_list[0]) {
91 op = state->free_list[0];
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050092 Py_INCREF(op);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +050093 return (PyObject *) op;
94 }
95#endif
Victor Stinner69ac6e52020-06-04 23:38:36 +020096 op = tuple_alloc(state, size);
Zackery Spytz60bd1f82019-09-04 07:58:05 -060097 if (op == NULL) {
98 return NULL;
99 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500100 for (Py_ssize_t i = 0; i < size; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 op->ob_item[i] = NULL;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500102 }
Christian Heimes2202f872008-02-06 14:31:34 +0000103#if PyTuple_MAXSAVESIZE > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 if (size == 0) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200105 state->free_list[0] = op;
106 ++state->numfree[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 Py_INCREF(op); /* extra INCREF so that this is never freed */
108 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000109#endif
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500110 tuple_gc_track(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112}
113
Martin v. Löwis18e16552006-02-15 17:27:45 +0000114Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200115PyTuple_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (!PyTuple_Check(op)) {
118 PyErr_BadInternalCall();
119 return -1;
120 }
121 else
122 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123}
124
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200126PyTuple_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (!PyTuple_Check(op)) {
129 PyErr_BadInternalCall();
130 return NULL;
131 }
132 if (i < 0 || i >= Py_SIZE(op)) {
133 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
134 return NULL;
135 }
136 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137}
138
139int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200140PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142 PyObject **p;
Victor Stinnera93c51e2020-02-07 00:38:59 +0100143 if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 Py_XDECREF(newitem);
145 PyErr_BadInternalCall();
146 return -1;
147 }
148 if (i < 0 || i >= Py_SIZE(op)) {
149 Py_XDECREF(newitem);
150 PyErr_SetString(PyExc_IndexError,
151 "tuple assignment index out of range");
152 return -1;
153 }
154 p = ((PyTupleObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300155 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157}
158
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000159void
160_PyTuple_MaybeUntrack(PyObject *op)
161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 PyTupleObject *t;
163 Py_ssize_t i, n;
164
165 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
166 return;
167 t = (PyTupleObject *) op;
168 n = Py_SIZE(t);
169 for (i = 0; i < n; i++) {
170 PyObject *elt = PyTuple_GET_ITEM(t, i);
171 /* Tuple with NULL elements aren't
172 fully constructed, don't untrack
173 them yet. */
174 if (!elt ||
175 _PyObject_GC_MAY_BE_TRACKED(elt))
176 return;
177 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000179}
180
Raymond Hettingercb2da432003-10-12 18:24:34 +0000181PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000182PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 Py_ssize_t i;
185 PyObject *o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 PyObject **items;
187 va_list vargs;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000188
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500189 if (n == 0) {
190 return PyTuple_New(0);
191 }
192
Victor Stinner69ac6e52020-06-04 23:38:36 +0200193 PyInterpreterState *interp = _PyInterpreterState_GET();
194 struct _Py_tuple_state *state = &interp->tuple;
195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 va_start(vargs, n);
Victor Stinner69ac6e52020-06-04 23:38:36 +0200197 PyTupleObject *result = tuple_alloc(state, n);
Christian Heimesd5a88042012-09-10 02:54:51 +0200198 if (result == NULL) {
199 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 return NULL;
Christian Heimesd5a88042012-09-10 02:54:51 +0200201 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500202 items = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 for (i = 0; i < n; i++) {
204 o = va_arg(vargs, PyObject *);
205 Py_INCREF(o);
206 items[i] = o;
207 }
208 va_end(vargs);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500209 tuple_gc_track(result);
210 return (PyObject *)result;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000211}
212
213
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214/* Methods */
215
216static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200217tupledealloc(PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200219 Py_ssize_t len = Py_SIZE(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200221 Py_TRASHCAN_BEGIN(op, tupledealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (len > 0) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200223 Py_ssize_t i = len;
224 while (--i >= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 Py_XDECREF(op->ob_item[i]);
Victor Stinner69ac6e52020-06-04 23:38:36 +0200226 }
Christian Heimes2202f872008-02-06 14:31:34 +0000227#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200228 PyInterpreterState *interp = _PyInterpreterState_GET();
229 struct _Py_tuple_state *state = &interp->tuple;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (len < PyTuple_MAXSAVESIZE &&
Victor Stinner69ac6e52020-06-04 23:38:36 +0200231 state->numfree[len] < PyTuple_MAXFREELIST &&
Andy Lesterdffe4c02020-03-04 07:15:20 -0600232 Py_IS_TYPE(op, &PyTuple_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200234 op->ob_item[0] = (PyObject *) state->free_list[len];
235 state->numfree[len]++;
236 state->free_list[len] = op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 goto done; /* return */
238 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000239#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 }
241 Py_TYPE(op)->tp_free((PyObject *)op);
Victor Stinnerb4b53862020-05-05 19:55:29 +0200242#if PyTuple_MAXSAVESIZE > 0
Guido van Rossumd724b232000-03-13 16:01:29 +0000243done:
Victor Stinnerb4b53862020-05-05 19:55:29 +0200244#endif
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200245 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246}
247
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000249tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 Py_ssize_t i, n;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100252 _PyUnicodeWriter writer;
Tim Petersa7259592001-06-16 05:11:17 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 n = Py_SIZE(v);
255 if (n == 0)
256 return PyUnicode_FromString("()");
Tim Petersa7259592001-06-16 05:11:17 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* While not mutable, it is still possible to end up with a cycle in a
259 tuple through an object that stores itself within a tuple (and thus
260 infinitely asks for the repr of itself). This should only be
261 possible within a type. */
262 i = Py_ReprEnter((PyObject *)v);
263 if (i != 0) {
264 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
265 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000266
Victor Stinner88a9cd92013-11-19 12:59:46 +0100267 _PyUnicodeWriter_Init(&writer);
268 writer.overallocate = 1;
269 if (Py_SIZE(v) > 1) {
270 /* "(" + "1" + ", 2" * (len - 1) + ")" */
271 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
272 }
273 else {
274 /* "(1,)" */
275 writer.min_length = 4;
276 }
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200277
Victor Stinner88a9cd92013-11-19 12:59:46 +0100278 if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200279 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 /* Do repr() on each element. */
282 for (i = 0; i < n; ++i) {
Victor Stinner88a9cd92013-11-19 12:59:46 +0100283 PyObject *s;
284
285 if (i > 0) {
286 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
287 goto error;
288 }
289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100291 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200292 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100293
294 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
295 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200296 goto error;
Victor Stinner88a9cd92013-11-19 12:59:46 +0100297 }
298 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 }
Victor Stinner88a9cd92013-11-19 12:59:46 +0100300
301 writer.overallocate = 0;
302 if (n > 1) {
303 if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0)
304 goto error;
305 }
306 else {
307 if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0)
308 goto error;
309 }
Tim Petersa7259592001-06-16 05:11:17 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_ReprLeave((PyObject *)v);
Victor Stinner88a9cd92013-11-19 12:59:46 +0100312 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200313
314error:
Victor Stinner88a9cd92013-11-19 12:59:46 +0100315 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200316 Py_ReprLeave((PyObject *)v);
317 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318}
319
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000320
jdemeyeraeb1be52018-10-28 02:06:38 +0200321/* Hash for tuples. This is a slightly simplified version of the xxHash
322 non-cryptographic hash:
323 - we do not use any parallellism, there is only 1 accumulator.
324 - we drop the final mixing since this is just a permutation of the
325 output space: it does not help against collisions.
326 - at the end, we mangle the length with a single constant.
327 For the xxHash specification, see
328 https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md
Christian Heimes34bdeb52013-01-07 21:24:18 +0100329
jdemeyeraeb1be52018-10-28 02:06:38 +0200330 Below are the official constants from the xxHash specification. Optimizing
331 compilers should emit a single "rotate" instruction for the
332 _PyHASH_XXROTATE() expansion. If that doesn't happen for some important
333 platform, the macro could be changed to expand to a platform-specific rotate
334 spelling instead.
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000335*/
jdemeyeraeb1be52018-10-28 02:06:38 +0200336#if SIZEOF_PY_UHASH_T > 4
337#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
338#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
339#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
340#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
341#else
342#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
343#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
344#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
345#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
346#endif
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000347
jdemeyeraeb1be52018-10-28 02:06:38 +0200348/* Tests have shown that it's not worth to cache the hash value, see
349 https://bugs.python.org/issue9685 */
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000350static Py_hash_t
Fred Drakeba096332000-07-09 07:04:36 +0000351tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000352{
jdemeyeraeb1be52018-10-28 02:06:38 +0200353 Py_ssize_t i, len = Py_SIZE(v);
354 PyObject **item = v->ob_item;
355
356 Py_uhash_t acc = _PyHASH_XXPRIME_5;
357 for (i = 0; i < len; i++) {
358 Py_uhash_t lane = PyObject_Hash(item[i]);
359 if (lane == (Py_uhash_t)-1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 return -1;
jdemeyeraeb1be52018-10-28 02:06:38 +0200361 }
362 acc += lane * _PyHASH_XXPRIME_2;
363 acc = _PyHASH_XXROTATE(acc);
364 acc *= _PyHASH_XXPRIME_1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 }
jdemeyeraeb1be52018-10-28 02:06:38 +0200366
367 /* Add input length, mangled to keep the historical value of hash(()). */
368 acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL);
369
370 if (acc == (Py_uhash_t)-1) {
371 return 1546275796;
372 }
373 return acc;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000374}
375
Martin v. Löwis18e16552006-02-15 17:27:45 +0000376static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000377tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380}
381
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000382static int
Fred Drakeba096332000-07-09 07:04:36 +0000383tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 Py_ssize_t i;
386 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300389 cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000391}
392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200394tupleitem(PyTupleObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (i < 0 || i >= Py_SIZE(a)) {
397 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
398 return NULL;
399 }
400 Py_INCREF(a->ob_item[i]);
401 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402}
403
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500404PyObject *
405_PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
406{
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500407 if (n == 0) {
408 return PyTuple_New(0);
409 }
410
Victor Stinner69ac6e52020-06-04 23:38:36 +0200411 PyInterpreterState *interp = _PyInterpreterState_GET();
412 struct _Py_tuple_state *state = &interp->tuple;
413 PyTupleObject *tuple = tuple_alloc(state, n);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500414 if (tuple == NULL) {
415 return NULL;
416 }
417 PyObject **dst = tuple->ob_item;
418 for (Py_ssize_t i = 0; i < n; i++) {
419 PyObject *item = src[i];
420 Py_INCREF(item);
421 dst[i] = item;
422 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500423 tuple_gc_track(tuple);
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500424 return (PyObject *)tuple;
425}
426
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200428tupleslice(PyTupleObject *a, Py_ssize_t ilow,
429 Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (ilow < 0)
432 ilow = 0;
433 if (ihigh > Py_SIZE(a))
434 ihigh = Py_SIZE(a);
435 if (ihigh < ilow)
436 ihigh = ilow;
437 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
438 Py_INCREF(a);
439 return (PyObject *)a;
440 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +0500441 return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442}
443
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000445PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (op == NULL || !PyTuple_Check(op)) {
448 PyErr_BadInternalCall();
449 return NULL;
450 }
451 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000452}
453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200455tupleconcat(PyTupleObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200457 Py_ssize_t size;
458 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyObject **src, **dest;
460 PyTupleObject *np;
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200461 if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
462 Py_INCREF(bb);
463 return bb;
464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (!PyTuple_Check(bb)) {
466 PyErr_Format(PyExc_TypeError,
467 "can only concatenate tuple (not \"%.200s\") to tuple",
468 Py_TYPE(bb)->tp_name);
469 return NULL;
470 }
Victor Stinner69ac6e52020-06-04 23:38:36 +0200471 PyTupleObject *b = (PyTupleObject *)bb;
472
Serhiy Storchaka98e80c22017-03-06 23:39:35 +0200473 if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
474 Py_INCREF(a);
475 return (PyObject *)a;
476 }
Sergey Fedoseeve682b262020-05-25 19:54:40 +0500477 assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
Martin Panterb93d8632016-07-25 02:39:20 +0000478 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500479 if (size == 0) {
480 return PyTuple_New(0);
481 }
482
Victor Stinner69ac6e52020-06-04 23:38:36 +0200483 PyInterpreterState *interp = _PyInterpreterState_GET();
484 struct _Py_tuple_state *state = &interp->tuple;
485 np = tuple_alloc(state, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (np == NULL) {
487 return NULL;
488 }
489 src = a->ob_item;
490 dest = np->ob_item;
491 for (i = 0; i < Py_SIZE(a); i++) {
492 PyObject *v = src[i];
493 Py_INCREF(v);
494 dest[i] = v;
495 }
496 src = b->ob_item;
497 dest = np->ob_item + Py_SIZE(a);
498 for (i = 0; i < Py_SIZE(b); i++) {
499 PyObject *v = src[i];
500 Py_INCREF(v);
501 dest[i] = v;
502 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500503 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000505}
506
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000507static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000508tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 Py_ssize_t i, j;
511 Py_ssize_t size;
512 PyTupleObject *np;
513 PyObject **p, **items;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 if (Py_SIZE(a) == 0 || n == 1) {
515 if (PyTuple_CheckExact(a)) {
516 /* Since tuples are immutable, we can return a shared
517 copy in this case */
518 Py_INCREF(a);
519 return (PyObject *)a;
520 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500521 }
522 if (Py_SIZE(a) == 0 || n <= 0) {
523 return PyTuple_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100525 if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 return PyErr_NoMemory();
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100527 size = Py_SIZE(a) * n;
Victor Stinner69ac6e52020-06-04 23:38:36 +0200528 PyInterpreterState *interp = _PyInterpreterState_GET();
529 struct _Py_tuple_state *state = &interp->tuple;
530 np = tuple_alloc(state, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (np == NULL)
532 return NULL;
533 p = np->ob_item;
534 items = a->ob_item;
535 for (i = 0; i < n; i++) {
536 for (j = 0; j < Py_SIZE(a); j++) {
537 *p = items[j];
538 Py_INCREF(*p);
539 p++;
540 }
541 }
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500542 tuple_gc_track(np);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000544}
545
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200546/*[clinic input]
547tuple.index
Raymond Hettinger65baa342008-02-07 00:41:02 +0000548
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200549 value: object
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300550 start: slice_index(accept={int}) = 0
551 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200552 /
553
554Return first index of value.
555
556Raises ValueError if the value is not present.
557[clinic start generated code]*/
558
559static PyObject *
560tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
561 Py_ssize_t stop)
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300562/*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200563{
564 Py_ssize_t i;
565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (start < 0) {
567 start += Py_SIZE(self);
568 if (start < 0)
569 start = 0;
570 }
571 if (stop < 0) {
572 stop += Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 }
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200574 else if (stop > Py_SIZE(self)) {
575 stop = Py_SIZE(self);
576 }
577 for (i = start; i < stop; i++) {
578 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (cmp > 0)
580 return PyLong_FromSsize_t(i);
581 else if (cmp < 0)
582 return NULL;
583 }
584 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
585 return NULL;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000586}
587
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200588/*[clinic input]
589tuple.count
590
591 value: object
592 /
593
594Return number of occurrences of value.
595[clinic start generated code]*/
596
Raymond Hettinger65baa342008-02-07 00:41:02 +0000597static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200598tuple_count(PyTupleObject *self, PyObject *value)
599/*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/
Raymond Hettinger65baa342008-02-07 00:41:02 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 Py_ssize_t count = 0;
602 Py_ssize_t i;
Raymond Hettinger65baa342008-02-07 00:41:02 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200605 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (cmp > 0)
607 count++;
608 else if (cmp < 0)
609 return NULL;
610 }
611 return PyLong_FromSsize_t(count);
Raymond Hettinger65baa342008-02-07 00:41:02 +0000612}
613
Jeremy Hylton8caad492000-06-23 14:18:11 +0000614static int
615tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 for (i = Py_SIZE(o); --i >= 0; )
620 Py_VISIT(o->ob_item[i]);
621 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000622}
623
Guido van Rossumf77bc622001-01-18 00:00:53 +0000624static PyObject *
625tuplerichcompare(PyObject *v, PyObject *w, int op)
626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 PyTupleObject *vt, *wt;
628 Py_ssize_t i;
629 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000630
Brian Curtindfc80e32011-08-10 20:28:54 -0500631 if (!PyTuple_Check(v) || !PyTuple_Check(w))
632 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 vt = (PyTupleObject *)v;
635 wt = (PyTupleObject *)w;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 vlen = Py_SIZE(vt);
638 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /* Note: the corresponding code for lists has an "early out" test
641 * here when op is EQ or NE and the lengths differ. That pays there,
642 * but Tim was unable to find any real code where EQ/NE tuple
643 * compares don't have the same length, so testing for it here would
644 * have cost without benefit.
645 */
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 /* Search for the first index where items are different.
648 * Note that because tuples are immutable, it's safe to reuse
649 * vlen and wlen across the comparison calls.
650 */
651 for (i = 0; i < vlen && i < wlen; i++) {
652 int k = PyObject_RichCompareBool(vt->ob_item[i],
653 wt->ob_item[i], Py_EQ);
654 if (k < 0)
655 return NULL;
656 if (!k)
657 break;
658 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (i >= vlen || i >= wlen) {
661 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +0100662 Py_RETURN_RICHCOMPARE(vlen, wlen, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 /* We have an item that differs -- shortcuts for EQ/NE */
666 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200667 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 }
669 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200670 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 }
Guido van Rossumf77bc622001-01-18 00:00:53 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 /* Compare the final item again using the proper operator */
674 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000675}
676
Jeremy Hylton938ace62002-07-17 16:30:39 +0000677static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200678tuple_subtype_new(PyTypeObject *type, PyObject *iterable);
679
680/*[clinic input]
681@classmethod
682tuple.__new__ as tuple_new
683 iterable: object(c_default="NULL") = ()
684 /
685
686Built-in immutable sequence.
687
688If no argument is given, the constructor returns an empty tuple.
689If iterable is specified the tuple is initialized from iterable's items.
690
691If the argument is a tuple, the return value is the same object.
692[clinic start generated code]*/
Guido van Rossumae960af2001-08-30 03:11:59 +0000693
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200695tuple_new_impl(PyTypeObject *type, PyObject *iterable)
696/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (type != &PyTuple_Type)
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200699 return tuple_subtype_new(type, iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200701 if (iterable == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 return PyTuple_New(0);
703 else
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200704 return PySequence_Tuple(iterable);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000705}
706
Guido van Rossumae960af2001-08-30 03:11:59 +0000707static PyObject *
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900708tuple_vectorcall(PyObject *type, PyObject * const*args,
709 size_t nargsf, PyObject *kwnames)
710{
Dong-hee Na87ec86c2020-03-16 23:06:20 +0900711 if (!_PyArg_NoKwnames("tuple", kwnames)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900712 return NULL;
713 }
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900714
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900715 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Dong-hee Nac98f87f2020-03-16 23:04:14 +0900716 if (!_PyArg_CheckPositional("tuple", nargs, 0, 1)) {
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900717 return NULL;
718 }
719
720 if (nargs) {
721 return tuple_new_impl((PyTypeObject *)type, args[0]);
722 }
723 return PyTuple_New(0);
724}
725
726static PyObject *
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200727tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
Guido van Rossumae960af2001-08-30 03:11:59 +0000728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 PyObject *tmp, *newobj, *item;
730 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 assert(PyType_IsSubtype(type, &PyTuple_Type));
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200733 tmp = tuple_new_impl(&PyTuple_Type, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 if (tmp == NULL)
735 return NULL;
736 assert(PyTuple_Check(tmp));
737 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
Hai Shic81609e2020-03-16 03:37:49 +0800738 if (newobj == NULL) {
739 Py_DECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return NULL;
Hai Shic81609e2020-03-16 03:37:49 +0800741 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 for (i = 0; i < n; i++) {
743 item = PyTuple_GET_ITEM(tmp, i);
744 Py_INCREF(item);
745 PyTuple_SET_ITEM(newobj, i, item);
746 }
747 Py_DECREF(tmp);
748 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000749}
750
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751static PySequenceMethods tuple_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 (lenfunc)tuplelength, /* sq_length */
753 (binaryfunc)tupleconcat, /* sq_concat */
754 (ssizeargfunc)tuplerepeat, /* sq_repeat */
755 (ssizeargfunc)tupleitem, /* sq_item */
756 0, /* sq_slice */
757 0, /* sq_ass_item */
758 0, /* sq_ass_slice */
759 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000760};
761
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000762static PyObject*
763tuplesubscript(PyTupleObject* self, PyObject* item)
764{
Victor Stinnera15e2602020-04-08 02:01:56 +0200765 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
767 if (i == -1 && PyErr_Occurred())
768 return NULL;
769 if (i < 0)
770 i += PyTuple_GET_SIZE(self);
771 return tupleitem(self, i);
772 }
773 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600774 Py_ssize_t start, stop, step, slicelength, i;
775 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 PyObject* it;
777 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000778
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300779 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return NULL;
781 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300782 slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
783 &stop, step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (slicelength <= 0) {
786 return PyTuple_New(0);
787 }
788 else if (start == 0 && step == 1 &&
789 slicelength == PyTuple_GET_SIZE(self) &&
790 PyTuple_CheckExact(self)) {
791 Py_INCREF(self);
792 return (PyObject *)self;
793 }
794 else {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200795 PyInterpreterState *interp = _PyInterpreterState_GET();
796 struct _Py_tuple_state *state = &interp->tuple;
797 PyTupleObject* result = tuple_alloc(state, slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 src = self->ob_item;
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500801 dest = result->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 for (cur = start, i = 0; i < slicelength;
803 cur += step, i++) {
804 it = src[cur];
805 Py_INCREF(it);
806 dest[i] = it;
807 }
808
Sergey Fedoseev4fa10dd2019-08-14 19:10:33 +0500809 tuple_gc_track(result);
810 return (PyObject *)result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 }
812 }
813 else {
814 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400815 "tuple indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_TYPE(item)->tp_name);
817 return NULL;
818 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000819}
820
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200821/*[clinic input]
822tuple.__getnewargs__
823[clinic start generated code]*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200825static PyObject *
826tuple___getnewargs___impl(PyTupleObject *self)
827/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
828{
829 return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000830}
831
832static PyMethodDef tuple_methods[] = {
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200833 TUPLE___GETNEWARGS___METHODDEF
834 TUPLE_INDEX_METHODDEF
835 TUPLE_COUNT_METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -0700836 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000838};
839
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000840static PyMappingMethods tuple_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 (lenfunc)tuplelength,
842 (binaryfunc)tuplesubscript,
843 0
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000844};
845
Raymond Hettinger48923c52002-08-09 01:30:17 +0000846static PyObject *tuple_iter(PyObject *seq);
847
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000848PyTypeObject PyTuple_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 PyVarObject_HEAD_INIT(&PyType_Type, 0)
850 "tuple",
851 sizeof(PyTupleObject) - sizeof(PyObject *),
852 sizeof(PyObject *),
853 (destructor)tupledealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200854 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 0, /* tp_getattr */
856 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200857 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 (reprfunc)tuplerepr, /* tp_repr */
859 0, /* tp_as_number */
860 &tuple_as_sequence, /* tp_as_sequence */
861 &tuple_as_mapping, /* tp_as_mapping */
862 (hashfunc)tuplehash, /* tp_hash */
863 0, /* tp_call */
864 0, /* tp_str */
865 PyObject_GenericGetAttr, /* tp_getattro */
866 0, /* tp_setattro */
867 0, /* tp_as_buffer */
868 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
869 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Serhiy Storchaka0b561592017-03-19 08:47:58 +0200870 tuple_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 (traverseproc)tupletraverse, /* tp_traverse */
872 0, /* tp_clear */
873 tuplerichcompare, /* tp_richcompare */
874 0, /* tp_weaklistoffset */
875 tuple_iter, /* tp_iter */
876 0, /* tp_iternext */
877 tuple_methods, /* tp_methods */
878 0, /* tp_members */
879 0, /* tp_getset */
880 0, /* tp_base */
881 0, /* tp_dict */
882 0, /* tp_descr_get */
883 0, /* tp_descr_set */
884 0, /* tp_dictoffset */
885 0, /* tp_init */
886 0, /* tp_alloc */
887 tuple_new, /* tp_new */
888 PyObject_GC_Del, /* tp_free */
Dong-hee Na9ee88cd2020-03-13 22:57:00 +0900889 .tp_vectorcall = tuple_vectorcall,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000890};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891
892/* The following function breaks the notion that tuples are immutable:
893 it changes the size of a tuple. We get away with this only if there
894 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000895 as creating a new tuple object and destroying the old one, only more
896 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000897 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000898
899int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000900_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000901{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200902 PyTupleObject *v;
903 PyTupleObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Py_ssize_t i;
905 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 v = (PyTupleObject *) *pv;
Andy Lester55728702020-03-06 16:53:17 -0600908 if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
910 *pv = 0;
911 Py_XDECREF(v);
912 PyErr_BadInternalCall();
913 return -1;
914 }
915 oldsize = Py_SIZE(v);
916 if (oldsize == newsize)
917 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (oldsize == 0) {
920 /* Empty tuples are often shared, so we should never
921 resize them in-place even if we do own the only
922 (current) reference */
923 Py_DECREF(v);
924 *pv = PyTuple_New(newsize);
925 return *pv == NULL ? -1 : 0;
926 }
Thomas Wouters6a922372001-05-28 13:11:02 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* XXX UNREF/NEWREF interface should be more symmetrical */
Victor Stinner49932fe2020-02-03 17:55:05 +0100929#ifdef Py_REF_DEBUG
930 _Py_RefTotal--;
931#endif
932 if (_PyObject_GC_IS_TRACKED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 _PyObject_GC_UNTRACK(v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100934 }
935#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 _Py_ForgetReference((PyObject *) v);
Victor Stinner49932fe2020-02-03 17:55:05 +0100937#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 /* DECREF items deleted by shrinkage */
939 for (i = newsize; i < oldsize; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200940 Py_CLEAR(v->ob_item[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 }
942 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
943 if (sv == NULL) {
944 *pv = NULL;
945 PyObject_GC_Del(v);
946 return -1;
947 }
948 _Py_NewReference((PyObject *) sv);
949 /* Zero out items added by growing */
950 if (newsize > oldsize)
951 memset(&sv->ob_item[oldsize], 0,
952 sizeof(*sv->ob_item) * (newsize - oldsize));
953 *pv = (PyObject *) sv;
954 _PyObject_GC_TRACK(sv);
955 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000956}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000957
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200958void
Victor Stinner69ac6e52020-06-04 23:38:36 +0200959_PyTuple_ClearFreeList(PyThreadState *tstate)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000960{
Christian Heimes2202f872008-02-06 14:31:34 +0000961#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200962 struct _Py_tuple_state *state = &tstate->interp->tuple;
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200963 for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) {
Victor Stinner69ac6e52020-06-04 23:38:36 +0200964 PyTupleObject *p = state->free_list[i];
965 state->free_list[i] = NULL;
966 state->numfree[i] = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 while (p) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200968 PyTupleObject *q = p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 p = (PyTupleObject *)(p->ob_item[0]);
970 PyObject_GC_Del(q);
971 }
972 }
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200973 // the empty tuple singleton is only cleared by _PyTuple_Fini()
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000974#endif
Christian Heimesa156e092008-02-16 07:38:31 +0000975}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976
Christian Heimesa156e092008-02-16 07:38:31 +0000977void
Victor Stinner69ac6e52020-06-04 23:38:36 +0200978_PyTuple_Fini(PyThreadState *tstate)
Christian Heimesa156e092008-02-16 07:38:31 +0000979{
980#if PyTuple_MAXSAVESIZE > 0
Victor Stinner69ac6e52020-06-04 23:38:36 +0200981 struct _Py_tuple_state *state = &tstate->interp->tuple;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* empty tuples are used all over the place and applications may
983 * rely on the fact that an empty tuple is a singleton. */
Victor Stinner69ac6e52020-06-04 23:38:36 +0200984 Py_CLEAR(state->free_list[0]);
Christian Heimesa156e092008-02-16 07:38:31 +0000985
Victor Stinner69ac6e52020-06-04 23:38:36 +0200986 _PyTuple_ClearFreeList(tstate);
Christian Heimesa156e092008-02-16 07:38:31 +0000987#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000988}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000989
990/*********************** Tuple Iterator **************************/
991
992typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyObject_HEAD
Victor Stinnera2d56982013-06-05 00:11:34 +0200994 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000996} tupleiterobject;
997
Raymond Hettinger48923c52002-08-09 01:30:17 +0000998static void
999tupleiter_dealloc(tupleiterobject *it)
1000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 _PyObject_GC_UNTRACK(it);
1002 Py_XDECREF(it->it_seq);
1003 PyObject_GC_Del(it);
Raymond Hettinger48923c52002-08-09 01:30:17 +00001004}
1005
1006static int
1007tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
1008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_VISIT(it->it_seq);
1010 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001011}
1012
Raymond Hettinger48923c52002-08-09 01:30:17 +00001013static PyObject *
1014tupleiter_next(tupleiterobject *it)
1015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyTupleObject *seq;
1017 PyObject *item;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 assert(it != NULL);
1020 seq = it->it_seq;
1021 if (seq == NULL)
1022 return NULL;
1023 assert(PyTuple_Check(seq));
Raymond Hettinger48923c52002-08-09 01:30:17 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (it->it_index < PyTuple_GET_SIZE(seq)) {
1026 item = PyTuple_GET_ITEM(seq, it->it_index);
1027 ++it->it_index;
1028 Py_INCREF(item);
1029 return item;
1030 }
Raymond Hettinger48923c52002-08-09 01:30:17 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03001033 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 return NULL;
Raymond Hettinger48923c52002-08-09 01:30:17 +00001035}
1036
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001037static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301038tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Raymond Hettinger435bf582004-03-18 22:43:10 +00001039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 Py_ssize_t len = 0;
1041 if (it->it_seq)
1042 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
1043 return PyLong_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +00001044}
1045
Armin Rigof5b3e362006-02-11 21:32:43 +00001046PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001047
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001048static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301049tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001050{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001051 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001052 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001053 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001054 it->it_seq, it->it_index);
1055 else
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001056 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001057}
1058
1059static PyObject *
1060tupleiter_setstate(tupleiterobject *it, PyObject *state)
1061{
Victor Stinner7660b882013-06-24 23:59:24 +02001062 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001063 if (index == -1 && PyErr_Occurred())
1064 return NULL;
1065 if (it->it_seq != NULL) {
1066 if (index < 0)
1067 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00001068 else if (index > PyTuple_GET_SIZE(it->it_seq))
1069 index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001070 it->it_index = index;
1071 }
1072 Py_RETURN_NONE;
1073}
1074
1075PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1076PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
1077
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00001078static PyMethodDef tupleiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001080 {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc},
1081 {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00001083};
1084
Raymond Hettinger48923c52002-08-09 01:30:17 +00001085PyTypeObject PyTupleIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1087 "tuple_iterator", /* tp_name */
1088 sizeof(tupleiterobject), /* tp_basicsize */
1089 0, /* tp_itemsize */
1090 /* methods */
1091 (destructor)tupleiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001092 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 0, /* tp_getattr */
1094 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001095 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 0, /* tp_repr */
1097 0, /* tp_as_number */
1098 0, /* tp_as_sequence */
1099 0, /* tp_as_mapping */
1100 0, /* tp_hash */
1101 0, /* tp_call */
1102 0, /* tp_str */
1103 PyObject_GenericGetAttr, /* tp_getattro */
1104 0, /* tp_setattro */
1105 0, /* tp_as_buffer */
1106 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1107 0, /* tp_doc */
1108 (traverseproc)tupleiter_traverse, /* tp_traverse */
1109 0, /* tp_clear */
1110 0, /* tp_richcompare */
1111 0, /* tp_weaklistoffset */
1112 PyObject_SelfIter, /* tp_iter */
1113 (iternextfunc)tupleiter_next, /* tp_iternext */
1114 tupleiter_methods, /* tp_methods */
1115 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +00001116};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001117
1118static PyObject *
1119tuple_iter(PyObject *seq)
1120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 tupleiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (!PyTuple_Check(seq)) {
1124 PyErr_BadInternalCall();
1125 return NULL;
1126 }
1127 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
1128 if (it == NULL)
1129 return NULL;
1130 it->it_index = 0;
1131 Py_INCREF(seq);
1132 it->it_seq = (PyTupleObject *)seq;
1133 _PyObject_GC_TRACK(it);
1134 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135}