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