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