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