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