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