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