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