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