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