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