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