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