Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Tuple object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 5 | #include "pycore_abstract.h" // _PyIndex_Check() |
Victor Stinner | e281f7d | 2018-11-01 02:30:36 +0100 | [diff] [blame] | 6 | #include "pycore_accu.h" |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 7 | #include "pycore_gc.h" // _PyObject_GC_IS_TRACKED() |
| 8 | #include "pycore_object.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 10 | /*[clinic input] |
| 11 | class 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 | |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 17 | /* Speed optimization to avoid frequent malloc/free of small tuples */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 18 | #ifndef PyTuple_MAXSAVESIZE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | #define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */ |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 20 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 21 | #ifndef PyTuple_MAXFREELIST |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 22 | #define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */ |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 25 | /* bpo-40521: tuple free lists are shared by all interpreters. */ |
| 26 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 27 | # undef PyTuple_MAXSAVESIZE |
| 28 | # define PyTuple_MAXSAVESIZE 0 |
| 29 | #endif |
| 30 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 31 | #if PyTuple_MAXSAVESIZE > 0 |
| 32 | /* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 33 | tuple () of which at most one instance will be allocated. |
| 34 | */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 35 | static PyTupleObject *free_list[PyTuple_MAXSAVESIZE]; |
| 36 | static int numfree[PyTuple_MAXSAVESIZE]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 37 | #endif |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 38 | |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 39 | static inline void |
| 40 | tuple_gc_track(PyTupleObject *op) |
| 41 | { |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 42 | _PyObject_GC_TRACK(op); |
| 43 | } |
| 44 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 45 | /* Print summary info about the state of the optimized allocator */ |
| 46 | void |
| 47 | _PyTuple_DebugMallocStats(FILE *out) |
| 48 | { |
| 49 | #if PyTuple_MAXSAVESIZE > 0 |
| 50 | int i; |
| 51 | char buf[128]; |
| 52 | for (i = 1; i < PyTuple_MAXSAVESIZE; i++) { |
| 53 | PyOS_snprintf(buf, sizeof(buf), |
| 54 | "free %d-sized PyTupleObject", i); |
| 55 | _PyDebugAllocatorStats(out, |
| 56 | buf, |
| 57 | numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i)); |
| 58 | } |
| 59 | #endif |
| 60 | } |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 61 | |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 62 | /* Allocate an uninitialized tuple object. Before making it public following |
| 63 | steps must be done: |
| 64 | - initialize its items |
| 65 | - call tuple_gc_track() on it |
| 66 | Because the empty tuple is always reused and it's already tracked by GC, |
| 67 | this function must not be called with size == 0 (unless from PyTuple_New() |
| 68 | which wraps this function). |
| 69 | */ |
| 70 | static PyTupleObject * |
| 71 | tuple_alloc(Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 73 | PyTupleObject *op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | if (size < 0) { |
| 75 | PyErr_BadInternalCall(); |
| 76 | return NULL; |
| 77 | } |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 78 | #if PyTuple_MAXSAVESIZE > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) { |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 80 | assert(size != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | free_list[size] = (PyTupleObject *) op->ob_item[0]; |
| 82 | numfree[size]--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | /* Inline PyObject_InitVar */ |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 84 | #ifdef Py_TRACE_REFS |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame^] | 85 | Py_SET_SIZE(op, size); |
Dong-hee Na | 7d847e2 | 2020-05-26 02:25:28 +0900 | [diff] [blame] | 86 | Py_SET_TYPE(op, &PyTuple_Type); |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 87 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | _Py_NewReference((PyObject *)op); |
| 89 | } |
| 90 | else |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 91 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | /* Check for overflow */ |
Sergey Fedoseev | 755d4ef | 2019-09-10 01:40:58 +0500 | [diff] [blame] | 94 | if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) - |
| 95 | sizeof(PyObject *))) / sizeof(PyObject *)) { |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 96 | return (PyTupleObject *)PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); |
| 99 | if (op == NULL) |
| 100 | return NULL; |
| 101 | } |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 102 | return op; |
| 103 | } |
| 104 | |
| 105 | PyObject * |
| 106 | PyTuple_New(Py_ssize_t size) |
| 107 | { |
| 108 | PyTupleObject *op; |
| 109 | #if PyTuple_MAXSAVESIZE > 0 |
| 110 | if (size == 0 && free_list[0]) { |
| 111 | op = free_list[0]; |
| 112 | Py_INCREF(op); |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 113 | return (PyObject *) op; |
| 114 | } |
| 115 | #endif |
| 116 | op = tuple_alloc(size); |
Zackery Spytz | 60bd1f8 | 2019-09-04 07:58:05 -0600 | [diff] [blame] | 117 | if (op == NULL) { |
| 118 | return NULL; |
| 119 | } |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 120 | for (Py_ssize_t i = 0; i < size; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | op->ob_item[i] = NULL; |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 122 | } |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 123 | #if PyTuple_MAXSAVESIZE > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | if (size == 0) { |
| 125 | free_list[0] = op; |
| 126 | ++numfree[0]; |
| 127 | Py_INCREF(op); /* extra INCREF so that this is never freed */ |
| 128 | } |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 129 | #endif |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 130 | tuple_gc_track(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 134 | Py_ssize_t |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 135 | PyTuple_Size(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | if (!PyTuple_Check(op)) { |
| 138 | PyErr_BadInternalCall(); |
| 139 | return -1; |
| 140 | } |
| 141 | else |
| 142 | return Py_SIZE(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 146 | PyTuple_GetItem(PyObject *op, Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | if (!PyTuple_Check(op)) { |
| 149 | PyErr_BadInternalCall(); |
| 150 | return NULL; |
| 151 | } |
| 152 | if (i < 0 || i >= Py_SIZE(op)) { |
| 153 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 154 | return NULL; |
| 155 | } |
| 156 | return ((PyTupleObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | int |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 160 | PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 162 | PyObject **p; |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 163 | if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | Py_XDECREF(newitem); |
| 165 | PyErr_BadInternalCall(); |
| 166 | return -1; |
| 167 | } |
| 168 | if (i < 0 || i >= Py_SIZE(op)) { |
| 169 | Py_XDECREF(newitem); |
| 170 | PyErr_SetString(PyExc_IndexError, |
| 171 | "tuple assignment index out of range"); |
| 172 | return -1; |
| 173 | } |
| 174 | p = ((PyTupleObject *)op) -> ob_item + i; |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 175 | Py_XSETREF(*p, newitem); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 179 | void |
| 180 | _PyTuple_MaybeUntrack(PyObject *op) |
| 181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | PyTupleObject *t; |
| 183 | Py_ssize_t i, n; |
| 184 | |
| 185 | if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op)) |
| 186 | return; |
| 187 | t = (PyTupleObject *) op; |
| 188 | n = Py_SIZE(t); |
| 189 | for (i = 0; i < n; i++) { |
| 190 | PyObject *elt = PyTuple_GET_ITEM(t, i); |
| 191 | /* Tuple with NULL elements aren't |
| 192 | fully constructed, don't untrack |
| 193 | them yet. */ |
| 194 | if (!elt || |
| 195 | _PyObject_GC_MAY_BE_TRACKED(elt)) |
| 196 | return; |
| 197 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | _PyObject_GC_UNTRACK(op); |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 201 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 202 | PyTuple_Pack(Py_ssize_t n, ...) |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | Py_ssize_t i; |
| 205 | PyObject *o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | PyObject **items; |
| 207 | va_list vargs; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 208 | |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 209 | if (n == 0) { |
| 210 | return PyTuple_New(0); |
| 211 | } |
| 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | va_start(vargs, n); |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 214 | PyTupleObject *result = tuple_alloc(n); |
Christian Heimes | d5a8804 | 2012-09-10 02:54:51 +0200 | [diff] [blame] | 215 | if (result == NULL) { |
| 216 | va_end(vargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | return NULL; |
Christian Heimes | d5a8804 | 2012-09-10 02:54:51 +0200 | [diff] [blame] | 218 | } |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 219 | items = result->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | for (i = 0; i < n; i++) { |
| 221 | o = va_arg(vargs, PyObject *); |
| 222 | Py_INCREF(o); |
| 223 | items[i] = o; |
| 224 | } |
| 225 | va_end(vargs); |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 226 | tuple_gc_track(result); |
| 227 | return (PyObject *)result; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | /* Methods */ |
| 232 | |
| 233 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 234 | tupledealloc(PyTupleObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 236 | Py_ssize_t i; |
| 237 | Py_ssize_t len = Py_SIZE(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | PyObject_GC_UnTrack(op); |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 239 | Py_TRASHCAN_BEGIN(op, tupledealloc) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | if (len > 0) { |
| 241 | i = len; |
| 242 | while (--i >= 0) |
| 243 | Py_XDECREF(op->ob_item[i]); |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 244 | #if PyTuple_MAXSAVESIZE > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | if (len < PyTuple_MAXSAVESIZE && |
| 246 | numfree[len] < PyTuple_MAXFREELIST && |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 247 | Py_IS_TYPE(op, &PyTuple_Type)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | { |
| 249 | op->ob_item[0] = (PyObject *) free_list[len]; |
| 250 | numfree[len]++; |
| 251 | free_list[len] = op; |
| 252 | goto done; /* return */ |
| 253 | } |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 254 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | } |
| 256 | Py_TYPE(op)->tp_free((PyObject *)op); |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 257 | #if PyTuple_MAXSAVESIZE > 0 |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 258 | done: |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 259 | #endif |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 260 | Py_TRASHCAN_END |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 263 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 264 | tuplerepr(PyTupleObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | Py_ssize_t i, n; |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 267 | _PyUnicodeWriter writer; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | n = Py_SIZE(v); |
| 270 | if (n == 0) |
| 271 | return PyUnicode_FromString("()"); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | /* While not mutable, it is still possible to end up with a cycle in a |
| 274 | tuple through an object that stores itself within a tuple (and thus |
| 275 | infinitely asks for the repr of itself). This should only be |
| 276 | possible within a type. */ |
| 277 | i = Py_ReprEnter((PyObject *)v); |
| 278 | if (i != 0) { |
| 279 | return i > 0 ? PyUnicode_FromString("(...)") : NULL; |
| 280 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 281 | |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 282 | _PyUnicodeWriter_Init(&writer); |
| 283 | writer.overallocate = 1; |
| 284 | if (Py_SIZE(v) > 1) { |
| 285 | /* "(" + "1" + ", 2" * (len - 1) + ")" */ |
| 286 | writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1; |
| 287 | } |
| 288 | else { |
| 289 | /* "(1,)" */ |
| 290 | writer.min_length = 4; |
| 291 | } |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 292 | |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 293 | if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 294 | goto error; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | /* Do repr() on each element. */ |
| 297 | for (i = 0; i < n; ++i) { |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 298 | PyObject *s; |
| 299 | |
| 300 | if (i > 0) { |
| 301 | if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) |
| 302 | goto error; |
| 303 | } |
| 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | s = PyObject_Repr(v->ob_item[i]); |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 306 | if (s == NULL) |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 307 | goto error; |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 308 | |
| 309 | if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) { |
| 310 | Py_DECREF(s); |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 311 | goto error; |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 312 | } |
| 313 | Py_DECREF(s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | } |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 315 | |
| 316 | writer.overallocate = 0; |
| 317 | if (n > 1) { |
| 318 | if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) |
| 319 | goto error; |
| 320 | } |
| 321 | else { |
| 322 | if (_PyUnicodeWriter_WriteASCIIString(&writer, ",)", 2) < 0) |
| 323 | goto error; |
| 324 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | Py_ReprLeave((PyObject *)v); |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 327 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 328 | |
| 329 | error: |
Victor Stinner | 88a9cd9 | 2013-11-19 12:59:46 +0100 | [diff] [blame] | 330 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 331 | Py_ReprLeave((PyObject *)v); |
| 332 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Raymond Hettinger | 4ec44e8 | 2004-06-04 06:35:20 +0000 | [diff] [blame] | 335 | |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 336 | /* Hash for tuples. This is a slightly simplified version of the xxHash |
| 337 | non-cryptographic hash: |
| 338 | - we do not use any parallellism, there is only 1 accumulator. |
| 339 | - we drop the final mixing since this is just a permutation of the |
| 340 | output space: it does not help against collisions. |
| 341 | - at the end, we mangle the length with a single constant. |
| 342 | For the xxHash specification, see |
| 343 | https://github.com/Cyan4973/xxHash/blob/master/doc/xxhash_spec.md |
Christian Heimes | 34bdeb5 | 2013-01-07 21:24:18 +0100 | [diff] [blame] | 344 | |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 345 | Below are the official constants from the xxHash specification. Optimizing |
| 346 | compilers should emit a single "rotate" instruction for the |
| 347 | _PyHASH_XXROTATE() expansion. If that doesn't happen for some important |
| 348 | platform, the macro could be changed to expand to a platform-specific rotate |
| 349 | spelling instead. |
Raymond Hettinger | 4ec44e8 | 2004-06-04 06:35:20 +0000 | [diff] [blame] | 350 | */ |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 351 | #if SIZEOF_PY_UHASH_T > 4 |
| 352 | #define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL) |
| 353 | #define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL) |
| 354 | #define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL) |
| 355 | #define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */ |
| 356 | #else |
| 357 | #define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL) |
| 358 | #define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL) |
| 359 | #define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL) |
| 360 | #define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */ |
| 361 | #endif |
Raymond Hettinger | 4ec44e8 | 2004-06-04 06:35:20 +0000 | [diff] [blame] | 362 | |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 363 | /* Tests have shown that it's not worth to cache the hash value, see |
| 364 | https://bugs.python.org/issue9685 */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 365 | static Py_hash_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 366 | tuplehash(PyTupleObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 367 | { |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 368 | Py_ssize_t i, len = Py_SIZE(v); |
| 369 | PyObject **item = v->ob_item; |
| 370 | |
| 371 | Py_uhash_t acc = _PyHASH_XXPRIME_5; |
| 372 | for (i = 0; i < len; i++) { |
| 373 | Py_uhash_t lane = PyObject_Hash(item[i]); |
| 374 | if (lane == (Py_uhash_t)-1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | return -1; |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 376 | } |
| 377 | acc += lane * _PyHASH_XXPRIME_2; |
| 378 | acc = _PyHASH_XXROTATE(acc); |
| 379 | acc *= _PyHASH_XXPRIME_1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | } |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 381 | |
| 382 | /* Add input length, mangled to keep the historical value of hash(()). */ |
| 383 | acc += len ^ (_PyHASH_XXPRIME_5 ^ 3527539UL); |
| 384 | |
| 385 | if (acc == (Py_uhash_t)-1) { |
| 386 | return 1546275796; |
| 387 | } |
| 388 | return acc; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 391 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 392 | tuplelength(PyTupleObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | return Py_SIZE(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 397 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 398 | tuplecontains(PyTupleObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | Py_ssize_t i; |
| 401 | int cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) |
Serhiy Storchaka | 18b711c | 2019-08-04 14:12:48 +0300 | [diff] [blame] | 404 | cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | return cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 408 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 409 | tupleitem(PyTupleObject *a, Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | if (i < 0 || i >= Py_SIZE(a)) { |
| 412 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 413 | return NULL; |
| 414 | } |
| 415 | Py_INCREF(a->ob_item[i]); |
| 416 | return a->ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 419 | PyObject * |
| 420 | _PyTuple_FromArray(PyObject *const *src, Py_ssize_t n) |
| 421 | { |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 422 | if (n == 0) { |
| 423 | return PyTuple_New(0); |
| 424 | } |
| 425 | |
| 426 | PyTupleObject *tuple = tuple_alloc(n); |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 427 | if (tuple == NULL) { |
| 428 | return NULL; |
| 429 | } |
| 430 | PyObject **dst = tuple->ob_item; |
| 431 | for (Py_ssize_t i = 0; i < n; i++) { |
| 432 | PyObject *item = src[i]; |
| 433 | Py_INCREF(item); |
| 434 | dst[i] = item; |
| 435 | } |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 436 | tuple_gc_track(tuple); |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 437 | return (PyObject *)tuple; |
| 438 | } |
| 439 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 440 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 441 | tupleslice(PyTupleObject *a, Py_ssize_t ilow, |
| 442 | Py_ssize_t ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | if (ilow < 0) |
| 445 | ilow = 0; |
| 446 | if (ihigh > Py_SIZE(a)) |
| 447 | ihigh = Py_SIZE(a); |
| 448 | if (ihigh < ilow) |
| 449 | ihigh = ilow; |
| 450 | if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) { |
| 451 | Py_INCREF(a); |
| 452 | return (PyObject *)a; |
| 453 | } |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 454 | return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 457 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 458 | PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j) |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | if (op == NULL || !PyTuple_Check(op)) { |
| 461 | PyErr_BadInternalCall(); |
| 462 | return NULL; |
| 463 | } |
| 464 | return tupleslice((PyTupleObject *)op, i, j); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 467 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 468 | tupleconcat(PyTupleObject *a, PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 469 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 470 | Py_ssize_t size; |
| 471 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | PyObject **src, **dest; |
| 473 | PyTupleObject *np; |
Serhiy Storchaka | 98e80c2 | 2017-03-06 23:39:35 +0200 | [diff] [blame] | 474 | if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) { |
| 475 | Py_INCREF(bb); |
| 476 | return bb; |
| 477 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | if (!PyTuple_Check(bb)) { |
| 479 | PyErr_Format(PyExc_TypeError, |
| 480 | "can only concatenate tuple (not \"%.200s\") to tuple", |
| 481 | Py_TYPE(bb)->tp_name); |
| 482 | return NULL; |
| 483 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 484 | #define b ((PyTupleObject *)bb) |
Serhiy Storchaka | 98e80c2 | 2017-03-06 23:39:35 +0200 | [diff] [blame] | 485 | if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) { |
| 486 | Py_INCREF(a); |
| 487 | return (PyObject *)a; |
| 488 | } |
Sergey Fedoseev | e682b26 | 2020-05-25 19:54:40 +0500 | [diff] [blame] | 489 | assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX); |
Martin Panter | b93d863 | 2016-07-25 02:39:20 +0000 | [diff] [blame] | 490 | size = Py_SIZE(a) + Py_SIZE(b); |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 491 | if (size == 0) { |
| 492 | return PyTuple_New(0); |
| 493 | } |
| 494 | |
| 495 | np = tuple_alloc(size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | if (np == NULL) { |
| 497 | return NULL; |
| 498 | } |
| 499 | src = a->ob_item; |
| 500 | dest = np->ob_item; |
| 501 | for (i = 0; i < Py_SIZE(a); i++) { |
| 502 | PyObject *v = src[i]; |
| 503 | Py_INCREF(v); |
| 504 | dest[i] = v; |
| 505 | } |
| 506 | src = b->ob_item; |
| 507 | dest = np->ob_item + Py_SIZE(a); |
| 508 | for (i = 0; i < Py_SIZE(b); i++) { |
| 509 | PyObject *v = src[i]; |
| 510 | Py_INCREF(v); |
| 511 | dest[i] = v; |
| 512 | } |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 513 | tuple_gc_track(np); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 515 | #undef b |
| 516 | } |
| 517 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 518 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 519 | tuplerepeat(PyTupleObject *a, Py_ssize_t n) |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | Py_ssize_t i, j; |
| 522 | Py_ssize_t size; |
| 523 | PyTupleObject *np; |
| 524 | PyObject **p, **items; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | if (Py_SIZE(a) == 0 || n == 1) { |
| 526 | if (PyTuple_CheckExact(a)) { |
| 527 | /* Since tuples are immutable, we can return a shared |
| 528 | copy in this case */ |
| 529 | Py_INCREF(a); |
| 530 | return (PyObject *)a; |
| 531 | } |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 532 | } |
| 533 | if (Py_SIZE(a) == 0 || n <= 0) { |
| 534 | return PyTuple_New(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 536 | if (n > PY_SSIZE_T_MAX / Py_SIZE(a)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 538 | size = Py_SIZE(a) * n; |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 539 | np = tuple_alloc(size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | if (np == NULL) |
| 541 | return NULL; |
| 542 | p = np->ob_item; |
| 543 | items = a->ob_item; |
| 544 | for (i = 0; i < n; i++) { |
| 545 | for (j = 0; j < Py_SIZE(a); j++) { |
| 546 | *p = items[j]; |
| 547 | Py_INCREF(*p); |
| 548 | p++; |
| 549 | } |
| 550 | } |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 551 | tuple_gc_track(np); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | return (PyObject *) np; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 555 | /*[clinic input] |
| 556 | tuple.index |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 557 | |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 558 | value: object |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 559 | start: slice_index(accept={int}) = 0 |
| 560 | stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 561 | / |
| 562 | |
| 563 | Return first index of value. |
| 564 | |
| 565 | Raises ValueError if the value is not present. |
| 566 | [clinic start generated code]*/ |
| 567 | |
| 568 | static PyObject * |
| 569 | tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start, |
| 570 | Py_ssize_t stop) |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 571 | /*[clinic end generated code: output=07b6f9f3cb5c33eb input=fb39e9874a21fe3f]*/ |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 572 | { |
| 573 | Py_ssize_t i; |
| 574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | if (start < 0) { |
| 576 | start += Py_SIZE(self); |
| 577 | if (start < 0) |
| 578 | start = 0; |
| 579 | } |
| 580 | if (stop < 0) { |
| 581 | stop += Py_SIZE(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | } |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 583 | else if (stop > Py_SIZE(self)) { |
| 584 | stop = Py_SIZE(self); |
| 585 | } |
| 586 | for (i = start; i < stop; i++) { |
| 587 | int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | if (cmp > 0) |
| 589 | return PyLong_FromSsize_t(i); |
| 590 | else if (cmp < 0) |
| 591 | return NULL; |
| 592 | } |
| 593 | PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple"); |
| 594 | return NULL; |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 597 | /*[clinic input] |
| 598 | tuple.count |
| 599 | |
| 600 | value: object |
| 601 | / |
| 602 | |
| 603 | Return number of occurrences of value. |
| 604 | [clinic start generated code]*/ |
| 605 | |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 606 | static PyObject * |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 607 | tuple_count(PyTupleObject *self, PyObject *value) |
| 608 | /*[clinic end generated code: output=aa927affc5a97605 input=531721aff65bd772]*/ |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | Py_ssize_t count = 0; |
| 611 | Py_ssize_t i; |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | for (i = 0; i < Py_SIZE(self); i++) { |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 614 | int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | if (cmp > 0) |
| 616 | count++; |
| 617 | else if (cmp < 0) |
| 618 | return NULL; |
| 619 | } |
| 620 | return PyLong_FromSsize_t(count); |
Raymond Hettinger | 65baa34 | 2008-02-07 00:41:02 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 623 | static int |
| 624 | tupletraverse(PyTupleObject *o, visitproc visit, void *arg) |
| 625 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | Py_ssize_t i; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | for (i = Py_SIZE(o); --i >= 0; ) |
| 629 | Py_VISIT(o->ob_item[i]); |
| 630 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 633 | static PyObject * |
| 634 | tuplerichcompare(PyObject *v, PyObject *w, int op) |
| 635 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | PyTupleObject *vt, *wt; |
| 637 | Py_ssize_t i; |
| 638 | Py_ssize_t vlen, wlen; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 639 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 640 | if (!PyTuple_Check(v) || !PyTuple_Check(w)) |
| 641 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | vt = (PyTupleObject *)v; |
| 644 | wt = (PyTupleObject *)w; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | vlen = Py_SIZE(vt); |
| 647 | wlen = Py_SIZE(wt); |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | /* Note: the corresponding code for lists has an "early out" test |
| 650 | * here when op is EQ or NE and the lengths differ. That pays there, |
| 651 | * but Tim was unable to find any real code where EQ/NE tuple |
| 652 | * compares don't have the same length, so testing for it here would |
| 653 | * have cost without benefit. |
| 654 | */ |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | /* Search for the first index where items are different. |
| 657 | * Note that because tuples are immutable, it's safe to reuse |
| 658 | * vlen and wlen across the comparison calls. |
| 659 | */ |
| 660 | for (i = 0; i < vlen && i < wlen; i++) { |
| 661 | int k = PyObject_RichCompareBool(vt->ob_item[i], |
| 662 | wt->ob_item[i], Py_EQ); |
| 663 | if (k < 0) |
| 664 | return NULL; |
| 665 | if (!k) |
| 666 | break; |
| 667 | } |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | if (i >= vlen || i >= wlen) { |
| 670 | /* No more items to compare -- compare sizes */ |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 671 | Py_RETURN_RICHCOMPARE(vlen, wlen, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | } |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | /* We have an item that differs -- shortcuts for EQ/NE */ |
| 675 | if (op == Py_EQ) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 676 | Py_RETURN_FALSE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | } |
| 678 | if (op == Py_NE) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 679 | Py_RETURN_TRUE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | } |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | /* Compare the final item again using the proper operator */ |
| 683 | return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op); |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 686 | static PyObject * |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 687 | tuple_subtype_new(PyTypeObject *type, PyObject *iterable); |
| 688 | |
| 689 | /*[clinic input] |
| 690 | @classmethod |
| 691 | tuple.__new__ as tuple_new |
| 692 | iterable: object(c_default="NULL") = () |
| 693 | / |
| 694 | |
| 695 | Built-in immutable sequence. |
| 696 | |
| 697 | If no argument is given, the constructor returns an empty tuple. |
| 698 | If iterable is specified the tuple is initialized from iterable's items. |
| 699 | |
| 700 | If the argument is a tuple, the return value is the same object. |
| 701 | [clinic start generated code]*/ |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 702 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 703 | static PyObject * |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 704 | tuple_new_impl(PyTypeObject *type, PyObject *iterable) |
| 705 | /*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | if (type != &PyTuple_Type) |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 708 | return tuple_subtype_new(type, iterable); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 709 | |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 710 | if (iterable == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | return PyTuple_New(0); |
| 712 | else |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 713 | return PySequence_Tuple(iterable); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 716 | static PyObject * |
Dong-hee Na | 9ee88cd | 2020-03-13 22:57:00 +0900 | [diff] [blame] | 717 | tuple_vectorcall(PyObject *type, PyObject * const*args, |
| 718 | size_t nargsf, PyObject *kwnames) |
| 719 | { |
Dong-hee Na | 87ec86c | 2020-03-16 23:06:20 +0900 | [diff] [blame] | 720 | if (!_PyArg_NoKwnames("tuple", kwnames)) { |
Dong-hee Na | 9ee88cd | 2020-03-13 22:57:00 +0900 | [diff] [blame] | 721 | return NULL; |
| 722 | } |
Dong-hee Na | c98f87f | 2020-03-16 23:04:14 +0900 | [diff] [blame] | 723 | |
Dong-hee Na | 9ee88cd | 2020-03-13 22:57:00 +0900 | [diff] [blame] | 724 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
Dong-hee Na | c98f87f | 2020-03-16 23:04:14 +0900 | [diff] [blame] | 725 | if (!_PyArg_CheckPositional("tuple", nargs, 0, 1)) { |
Dong-hee Na | 9ee88cd | 2020-03-13 22:57:00 +0900 | [diff] [blame] | 726 | return NULL; |
| 727 | } |
| 728 | |
| 729 | if (nargs) { |
| 730 | return tuple_new_impl((PyTypeObject *)type, args[0]); |
| 731 | } |
| 732 | return PyTuple_New(0); |
| 733 | } |
| 734 | |
| 735 | static PyObject * |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 736 | tuple_subtype_new(PyTypeObject *type, PyObject *iterable) |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | PyObject *tmp, *newobj, *item; |
| 739 | Py_ssize_t i, n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | assert(PyType_IsSubtype(type, &PyTuple_Type)); |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 742 | tmp = tuple_new_impl(&PyTuple_Type, iterable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | if (tmp == NULL) |
| 744 | return NULL; |
| 745 | assert(PyTuple_Check(tmp)); |
| 746 | newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
Hai Shi | c81609e | 2020-03-16 03:37:49 +0800 | [diff] [blame] | 747 | if (newobj == NULL) { |
| 748 | Py_DECREF(tmp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | return NULL; |
Hai Shi | c81609e | 2020-03-16 03:37:49 +0800 | [diff] [blame] | 750 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | for (i = 0; i < n; i++) { |
| 752 | item = PyTuple_GET_ITEM(tmp, i); |
| 753 | Py_INCREF(item); |
| 754 | PyTuple_SET_ITEM(newobj, i, item); |
| 755 | } |
| 756 | Py_DECREF(tmp); |
| 757 | return newobj; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 760 | static PySequenceMethods tuple_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | (lenfunc)tuplelength, /* sq_length */ |
| 762 | (binaryfunc)tupleconcat, /* sq_concat */ |
| 763 | (ssizeargfunc)tuplerepeat, /* sq_repeat */ |
| 764 | (ssizeargfunc)tupleitem, /* sq_item */ |
| 765 | 0, /* sq_slice */ |
| 766 | 0, /* sq_ass_item */ |
| 767 | 0, /* sq_ass_slice */ |
| 768 | (objobjproc)tuplecontains, /* sq_contains */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 769 | }; |
| 770 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 771 | static PyObject* |
| 772 | tuplesubscript(PyTupleObject* self, PyObject* item) |
| 773 | { |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 774 | if (_PyIndex_Check(item)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 776 | if (i == -1 && PyErr_Occurred()) |
| 777 | return NULL; |
| 778 | if (i < 0) |
| 779 | i += PyTuple_GET_SIZE(self); |
| 780 | return tupleitem(self, i); |
| 781 | } |
| 782 | else if (PySlice_Check(item)) { |
Zackery Spytz | 14514d9 | 2019-05-17 01:13:03 -0600 | [diff] [blame] | 783 | Py_ssize_t start, stop, step, slicelength, i; |
| 784 | size_t cur; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | PyObject* it; |
| 786 | PyObject **src, **dest; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 787 | |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 788 | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | return NULL; |
| 790 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 791 | slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start, |
| 792 | &stop, step); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | if (slicelength <= 0) { |
| 795 | return PyTuple_New(0); |
| 796 | } |
| 797 | else if (start == 0 && step == 1 && |
| 798 | slicelength == PyTuple_GET_SIZE(self) && |
| 799 | PyTuple_CheckExact(self)) { |
| 800 | Py_INCREF(self); |
| 801 | return (PyObject *)self; |
| 802 | } |
| 803 | else { |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 804 | PyTupleObject* result = tuple_alloc(slicelength); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | if (!result) return NULL; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | src = self->ob_item; |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 808 | dest = result->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | for (cur = start, i = 0; i < slicelength; |
| 810 | cur += step, i++) { |
| 811 | it = src[cur]; |
| 812 | Py_INCREF(it); |
| 813 | dest[i] = it; |
| 814 | } |
| 815 | |
Sergey Fedoseev | 4fa10dd | 2019-08-14 19:10:33 +0500 | [diff] [blame] | 816 | tuple_gc_track(result); |
| 817 | return (PyObject *)result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | else { |
| 821 | PyErr_Format(PyExc_TypeError, |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 822 | "tuple indices must be integers or slices, not %.200s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | Py_TYPE(item)->tp_name); |
| 824 | return NULL; |
| 825 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 828 | /*[clinic input] |
| 829 | tuple.__getnewargs__ |
| 830 | [clinic start generated code]*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 832 | static PyObject * |
| 833 | tuple___getnewargs___impl(PyTupleObject *self) |
| 834 | /*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/ |
| 835 | { |
| 836 | return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self))); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static PyMethodDef tuple_methods[] = { |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 840 | TUPLE___GETNEWARGS___METHODDEF |
| 841 | TUPLE_INDEX_METHODDEF |
| 842 | TUPLE_COUNT_METHODDEF |
Guido van Rossum | 48b069a | 2020-04-07 09:50:06 -0700 | [diff] [blame] | 843 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 845 | }; |
| 846 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 847 | static PyMappingMethods tuple_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | (lenfunc)tuplelength, |
| 849 | (binaryfunc)tuplesubscript, |
| 850 | 0 |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 851 | }; |
| 852 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 853 | static PyObject *tuple_iter(PyObject *seq); |
| 854 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 855 | PyTypeObject PyTuple_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 857 | "tuple", |
| 858 | sizeof(PyTupleObject) - sizeof(PyObject *), |
| 859 | sizeof(PyObject *), |
| 860 | (destructor)tupledealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 861 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | 0, /* tp_getattr */ |
| 863 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 864 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | (reprfunc)tuplerepr, /* tp_repr */ |
| 866 | 0, /* tp_as_number */ |
| 867 | &tuple_as_sequence, /* tp_as_sequence */ |
| 868 | &tuple_as_mapping, /* tp_as_mapping */ |
| 869 | (hashfunc)tuplehash, /* tp_hash */ |
| 870 | 0, /* tp_call */ |
| 871 | 0, /* tp_str */ |
| 872 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 873 | 0, /* tp_setattro */ |
| 874 | 0, /* tp_as_buffer */ |
| 875 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 876 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */ |
Serhiy Storchaka | 0b56159 | 2017-03-19 08:47:58 +0200 | [diff] [blame] | 877 | tuple_new__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | (traverseproc)tupletraverse, /* tp_traverse */ |
| 879 | 0, /* tp_clear */ |
| 880 | tuplerichcompare, /* tp_richcompare */ |
| 881 | 0, /* tp_weaklistoffset */ |
| 882 | tuple_iter, /* tp_iter */ |
| 883 | 0, /* tp_iternext */ |
| 884 | tuple_methods, /* tp_methods */ |
| 885 | 0, /* tp_members */ |
| 886 | 0, /* tp_getset */ |
| 887 | 0, /* tp_base */ |
| 888 | 0, /* tp_dict */ |
| 889 | 0, /* tp_descr_get */ |
| 890 | 0, /* tp_descr_set */ |
| 891 | 0, /* tp_dictoffset */ |
| 892 | 0, /* tp_init */ |
| 893 | 0, /* tp_alloc */ |
| 894 | tuple_new, /* tp_new */ |
| 895 | PyObject_GC_Del, /* tp_free */ |
Dong-hee Na | 9ee88cd | 2020-03-13 22:57:00 +0900 | [diff] [blame] | 896 | .tp_vectorcall = tuple_vectorcall, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 897 | }; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 898 | |
| 899 | /* The following function breaks the notion that tuples are immutable: |
| 900 | it changes the size of a tuple. We get away with this only if there |
| 901 | is only one module referencing the object. You can also think of it |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 902 | as creating a new tuple object and destroying the old one, only more |
| 903 | efficiently. In any case, don't use this if the tuple may already be |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 904 | known to some other part of the code. */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 905 | |
| 906 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 907 | _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 908 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 909 | PyTupleObject *v; |
| 910 | PyTupleObject *sv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | Py_ssize_t i; |
| 912 | Py_ssize_t oldsize; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | v = (PyTupleObject *) *pv; |
Andy Lester | 5572870 | 2020-03-06 16:53:17 -0600 | [diff] [blame] | 915 | if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) { |
| 917 | *pv = 0; |
| 918 | Py_XDECREF(v); |
| 919 | PyErr_BadInternalCall(); |
| 920 | return -1; |
| 921 | } |
| 922 | oldsize = Py_SIZE(v); |
| 923 | if (oldsize == newsize) |
| 924 | return 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | if (oldsize == 0) { |
| 927 | /* Empty tuples are often shared, so we should never |
| 928 | resize them in-place even if we do own the only |
| 929 | (current) reference */ |
| 930 | Py_DECREF(v); |
| 931 | *pv = PyTuple_New(newsize); |
| 932 | return *pv == NULL ? -1 : 0; |
| 933 | } |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 936 | #ifdef Py_REF_DEBUG |
| 937 | _Py_RefTotal--; |
| 938 | #endif |
| 939 | if (_PyObject_GC_IS_TRACKED(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | _PyObject_GC_UNTRACK(v); |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 941 | } |
| 942 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | _Py_ForgetReference((PyObject *) v); |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 944 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | /* DECREF items deleted by shrinkage */ |
| 946 | for (i = newsize; i < oldsize; i++) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 947 | Py_CLEAR(v->ob_item[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | } |
| 949 | sv = PyObject_GC_Resize(PyTupleObject, v, newsize); |
| 950 | if (sv == NULL) { |
| 951 | *pv = NULL; |
| 952 | PyObject_GC_Del(v); |
| 953 | return -1; |
| 954 | } |
| 955 | _Py_NewReference((PyObject *) sv); |
| 956 | /* Zero out items added by growing */ |
| 957 | if (newsize > oldsize) |
| 958 | memset(&sv->ob_item[oldsize], 0, |
| 959 | sizeof(*sv->ob_item) * (newsize - oldsize)); |
| 960 | *pv = (PyObject *) sv; |
| 961 | _PyObject_GC_TRACK(sv); |
| 962 | return 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 963 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 964 | |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 965 | void |
| 966 | _PyTuple_ClearFreeList(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 967 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 968 | #if PyTuple_MAXSAVESIZE > 0 |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 969 | for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) { |
| 970 | PyTupleObject *p = free_list[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | free_list[i] = NULL; |
| 972 | numfree[i] = 0; |
| 973 | while (p) { |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 974 | PyTupleObject *q = p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | p = (PyTupleObject *)(p->ob_item[0]); |
| 976 | PyObject_GC_Del(q); |
| 977 | } |
| 978 | } |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 979 | // the empty tuple singleton is only cleared by _PyTuple_Fini() |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 980 | #endif |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 981 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 983 | void |
Victor Stinner | bed4817 | 2019-08-27 00:12:32 +0200 | [diff] [blame] | 984 | _PyTuple_Fini(void) |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 985 | { |
| 986 | #if PyTuple_MAXSAVESIZE > 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | /* empty tuples are used all over the place and applications may |
| 988 | * rely on the fact that an empty tuple is a singleton. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 989 | Py_CLEAR(free_list[0]); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 990 | |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 991 | _PyTuple_ClearFreeList(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 992 | #endif |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 993 | } |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 994 | |
| 995 | /*********************** Tuple Iterator **************************/ |
| 996 | |
| 997 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | PyObject_HEAD |
Victor Stinner | a2d5698 | 2013-06-05 00:11:34 +0200 | [diff] [blame] | 999 | Py_ssize_t it_index; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1001 | } tupleiterobject; |
| 1002 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1003 | static void |
| 1004 | tupleiter_dealloc(tupleiterobject *it) |
| 1005 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | _PyObject_GC_UNTRACK(it); |
| 1007 | Py_XDECREF(it->it_seq); |
| 1008 | PyObject_GC_Del(it); |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static int |
| 1012 | tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg) |
| 1013 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | Py_VISIT(it->it_seq); |
| 1015 | return 0; |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1018 | static PyObject * |
| 1019 | tupleiter_next(tupleiterobject *it) |
| 1020 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | PyTupleObject *seq; |
| 1022 | PyObject *item; |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | assert(it != NULL); |
| 1025 | seq = it->it_seq; |
| 1026 | if (seq == NULL) |
| 1027 | return NULL; |
| 1028 | assert(PyTuple_Check(seq)); |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | if (it->it_index < PyTuple_GET_SIZE(seq)) { |
| 1031 | item = PyTuple_GET_ITEM(seq, it->it_index); |
| 1032 | ++it->it_index; |
| 1033 | Py_INCREF(item); |
| 1034 | return item; |
| 1035 | } |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 1038 | Py_DECREF(seq); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | return NULL; |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1042 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1043 | tupleiter_len(tupleiterobject *it, PyObject *Py_UNUSED(ignored)) |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 1044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | Py_ssize_t len = 0; |
| 1046 | if (it->it_seq) |
| 1047 | len = PyTuple_GET_SIZE(it->it_seq) - it->it_index; |
| 1048 | return PyLong_FromSsize_t(len); |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 1051 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1052 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1053 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1054 | tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1055 | { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 1056 | _Py_IDENTIFIER(iter); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1057 | if (it->it_seq) |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 1058 | return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1059 | it->it_seq, it->it_index); |
| 1060 | else |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 1061 | return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | static PyObject * |
| 1065 | tupleiter_setstate(tupleiterobject *it, PyObject *state) |
| 1066 | { |
Victor Stinner | 7660b88 | 2013-06-24 23:59:24 +0200 | [diff] [blame] | 1067 | Py_ssize_t index = PyLong_AsSsize_t(state); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1068 | if (index == -1 && PyErr_Occurred()) |
| 1069 | return NULL; |
| 1070 | if (it->it_seq != NULL) { |
| 1071 | if (index < 0) |
| 1072 | index = 0; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 1073 | else if (index > PyTuple_GET_SIZE(it->it_seq)) |
| 1074 | index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1075 | it->it_index = index; |
| 1076 | } |
| 1077 | Py_RETURN_NONE; |
| 1078 | } |
| 1079 | |
| 1080 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 1081 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 1082 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1083 | static PyMethodDef tupleiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1085 | {"__reduce__", (PyCFunction)tupleiter_reduce, METH_NOARGS, reduce_doc}, |
| 1086 | {"__setstate__", (PyCFunction)tupleiter_setstate, METH_O, setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 1088 | }; |
| 1089 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1090 | PyTypeObject PyTupleIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1092 | "tuple_iterator", /* tp_name */ |
| 1093 | sizeof(tupleiterobject), /* tp_basicsize */ |
| 1094 | 0, /* tp_itemsize */ |
| 1095 | /* methods */ |
| 1096 | (destructor)tupleiter_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1097 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | 0, /* tp_getattr */ |
| 1099 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1100 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | 0, /* tp_repr */ |
| 1102 | 0, /* tp_as_number */ |
| 1103 | 0, /* tp_as_sequence */ |
| 1104 | 0, /* tp_as_mapping */ |
| 1105 | 0, /* tp_hash */ |
| 1106 | 0, /* tp_call */ |
| 1107 | 0, /* tp_str */ |
| 1108 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1109 | 0, /* tp_setattro */ |
| 1110 | 0, /* tp_as_buffer */ |
| 1111 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1112 | 0, /* tp_doc */ |
| 1113 | (traverseproc)tupleiter_traverse, /* tp_traverse */ |
| 1114 | 0, /* tp_clear */ |
| 1115 | 0, /* tp_richcompare */ |
| 1116 | 0, /* tp_weaklistoffset */ |
| 1117 | PyObject_SelfIter, /* tp_iter */ |
| 1118 | (iternextfunc)tupleiter_next, /* tp_iternext */ |
| 1119 | tupleiter_methods, /* tp_methods */ |
| 1120 | 0, |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 1121 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1122 | |
| 1123 | static PyObject * |
| 1124 | tuple_iter(PyObject *seq) |
| 1125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | tupleiterobject *it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | if (!PyTuple_Check(seq)) { |
| 1129 | PyErr_BadInternalCall(); |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type); |
| 1133 | if (it == NULL) |
| 1134 | return NULL; |
| 1135 | it->it_index = 0; |
| 1136 | Py_INCREF(seq); |
| 1137 | it->it_seq = (PyTupleObject *)seq; |
| 1138 | _PyObject_GC_TRACK(it); |
| 1139 | return (PyObject *)it; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1140 | } |