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