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