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