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