Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Tuple object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 6 | /* Speed optimization to avoid frequent malloc/free of small tuples */ |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 7 | #ifndef MAXSAVESIZE |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 8 | #define MAXSAVESIZE 20 /* Largest tuple to save on free list */ |
| 9 | #endif |
| 10 | #ifndef MAXSAVEDTUPLES |
| 11 | #define MAXSAVEDTUPLES 2000 /* Maximum number of tuples of each size to save */ |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
| 14 | #if MAXSAVESIZE > 0 |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 15 | /* Entries 1 up to MAXSAVESIZE are free lists, entry 0 is the empty |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 16 | tuple () of which at most one instance will be allocated. |
| 17 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 18 | static PyTupleObject *free_tuples[MAXSAVESIZE]; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 19 | static int num_free_tuples[MAXSAVESIZE]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 20 | #endif |
| 21 | #ifdef COUNT_ALLOCS |
| 22 | int fast_tuple_allocs; |
| 23 | int tuple_zero_allocs; |
| 24 | #endif |
| 25 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 26 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 27 | PyTuple_New(register int size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 29 | register PyTupleObject *op; |
Armin Rigo | 6fce78e | 2004-03-21 22:29:05 +0000 | [diff] [blame] | 30 | int i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | if (size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 32 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | return NULL; |
| 34 | } |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 35 | #if MAXSAVESIZE > 0 |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 36 | if (size == 0 && free_tuples[0]) { |
| 37 | op = free_tuples[0]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | Py_INCREF(op); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 39 | #ifdef COUNT_ALLOCS |
| 40 | tuple_zero_allocs++; |
| 41 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | return (PyObject *) op; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 43 | } |
Raymond Hettinger | 325d169 | 2004-03-15 00:16:34 +0000 | [diff] [blame] | 44 | if (size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | free_tuples[size] = (PyTupleObject *) op->ob_item[0]; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 46 | num_free_tuples[size]--; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 47 | #ifdef COUNT_ALLOCS |
| 48 | fast_tuple_allocs++; |
| 49 | #endif |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 50 | /* Inline PyObject_InitVar */ |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 51 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 52 | op->ob_size = size; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 53 | op->ob_type = &PyTuple_Type; |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 54 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 55 | _Py_NewReference((PyObject *)op); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 56 | } |
| 57 | else |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 58 | #endif |
| 59 | { |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 60 | int nbytes = size * sizeof(PyObject *); |
| 61 | /* Check for overflow */ |
| 62 | if (nbytes / sizeof(PyObject *) != (size_t)size || |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 63 | (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)) |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 64 | <= 0) |
| 65 | { |
| 66 | return PyErr_NoMemory(); |
| 67 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 68 | op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 69 | if (op == NULL) |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 70 | return NULL; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 71 | } |
Armin Rigo | 6fce78e | 2004-03-21 22:29:05 +0000 | [diff] [blame] | 72 | for (i=0; i < size; i++) |
| 73 | op->ob_item[i] = NULL; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 74 | #if MAXSAVESIZE > 0 |
| 75 | if (size == 0) { |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 76 | free_tuples[0] = op; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 77 | ++num_free_tuples[0]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | Py_INCREF(op); /* extra INCREF so that this is never freed */ |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 79 | } |
| 80 | #endif |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 81 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 86 | PyTuple_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | if (!PyTuple_Check(op)) { |
| 89 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | return ((PyTupleObject *)op)->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 97 | PyTuple_GetItem(register PyObject *op, register int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | if (!PyTuple_Check(op)) { |
| 100 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 103 | if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
| 104 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | return ((PyTupleObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 111 | PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | register PyObject *olditem; |
| 114 | register PyObject **p; |
Guido van Rossum | 787bdd3 | 1997-08-17 16:25:45 +0000 | [diff] [blame] | 115 | if (!PyTuple_Check(op) || op->ob_refcnt != 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 116 | Py_XDECREF(newitem); |
| 117 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 118 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
| 121 | Py_XDECREF(newitem); |
| 122 | PyErr_SetString(PyExc_IndexError, |
| 123 | "tuple assignment index out of range"); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 124 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | p = ((PyTupleObject *)op) -> ob_item + i; |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 127 | olditem = *p; |
| 128 | *p = newitem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 129 | Py_XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 133 | PyObject * |
| 134 | PyTuple_Pack(int n, ...) |
| 135 | { |
| 136 | int i; |
| 137 | PyObject *o; |
| 138 | PyObject *result; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 139 | PyObject **items; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 140 | va_list vargs; |
| 141 | |
| 142 | va_start(vargs, n); |
| 143 | result = PyTuple_New(n); |
| 144 | if (result == NULL) |
| 145 | return NULL; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 146 | items = ((PyTupleObject *)result)->ob_item; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 147 | for (i = 0; i < n; i++) { |
| 148 | o = va_arg(vargs, PyObject *); |
| 149 | Py_INCREF(o); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 150 | items[i] = o; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 151 | } |
| 152 | va_end(vargs); |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | /* Methods */ |
| 158 | |
| 159 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 160 | tupledealloc(register PyTupleObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | { |
| 162 | register int i; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 163 | register int len = op->ob_size; |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 164 | PyObject_GC_UnTrack(op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 165 | Py_TRASHCAN_SAFE_BEGIN(op) |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 166 | if (len > 0) { |
| 167 | i = len; |
Armin Rigo | 6fce78e | 2004-03-21 22:29:05 +0000 | [diff] [blame] | 168 | while (--i >= 0) |
| 169 | Py_XDECREF(op->ob_item[i]); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 170 | #if MAXSAVESIZE > 0 |
Guido van Rossum | 4b8c0f6 | 2001-08-30 18:31:30 +0000 | [diff] [blame] | 171 | if (len < MAXSAVESIZE && |
| 172 | num_free_tuples[len] < MAXSAVEDTUPLES && |
| 173 | op->ob_type == &PyTuple_Type) |
| 174 | { |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 175 | op->ob_item[0] = (PyObject *) free_tuples[len]; |
| 176 | num_free_tuples[len]++; |
| 177 | free_tuples[len] = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 178 | goto done; /* return */ |
Guido van Rossum | 1bb2687 | 1998-06-26 15:53:50 +0000 | [diff] [blame] | 179 | } |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 180 | #endif |
Guido van Rossum | 1bb2687 | 1998-06-26 15:53:50 +0000 | [diff] [blame] | 181 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 182 | op->ob_type->tp_free((PyObject *)op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 183 | done: |
| 184 | Py_TRASHCAN_SAFE_END(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 187 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 188 | tupleprint(PyTupleObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | { |
| 190 | int i; |
| 191 | fprintf(fp, "("); |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 192 | for (i = 0; i < op->ob_size; i++) { |
| 193 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | fprintf(fp, ", "); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 195 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 196 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | } |
| 198 | if (op->ob_size == 1) |
| 199 | fprintf(fp, ","); |
| 200 | fprintf(fp, ")"); |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 201 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 205 | tuplerepr(PyTupleObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 207 | int i, n; |
| 208 | PyObject *s, *temp; |
| 209 | PyObject *pieces, *result = NULL; |
| 210 | |
| 211 | n = v->ob_size; |
| 212 | if (n == 0) |
| 213 | return PyString_FromString("()"); |
| 214 | |
| 215 | pieces = PyTuple_New(n); |
| 216 | if (pieces == NULL) |
| 217 | return NULL; |
| 218 | |
| 219 | /* Do repr() on each element. */ |
| 220 | for (i = 0; i < n; ++i) { |
| 221 | s = PyObject_Repr(v->ob_item[i]); |
| 222 | if (s == NULL) |
| 223 | goto Done; |
| 224 | PyTuple_SET_ITEM(pieces, i, s); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 226 | |
| 227 | /* Add "()" decorations to the first and last items. */ |
| 228 | assert(n > 0); |
| 229 | s = PyString_FromString("("); |
| 230 | if (s == NULL) |
| 231 | goto Done; |
| 232 | temp = PyTuple_GET_ITEM(pieces, 0); |
| 233 | PyString_ConcatAndDel(&s, temp); |
| 234 | PyTuple_SET_ITEM(pieces, 0, s); |
| 235 | if (s == NULL) |
| 236 | goto Done; |
| 237 | |
| 238 | s = PyString_FromString(n == 1 ? ",)" : ")"); |
| 239 | if (s == NULL) |
| 240 | goto Done; |
| 241 | temp = PyTuple_GET_ITEM(pieces, n-1); |
| 242 | PyString_ConcatAndDel(&temp, s); |
| 243 | PyTuple_SET_ITEM(pieces, n-1, temp); |
| 244 | if (temp == NULL) |
| 245 | goto Done; |
| 246 | |
| 247 | /* Paste them all together with ", " between. */ |
| 248 | s = PyString_FromString(", "); |
| 249 | if (s == NULL) |
| 250 | goto Done; |
| 251 | result = _PyString_Join(s, pieces); |
| 252 | Py_DECREF(s); |
| 253 | |
| 254 | Done: |
| 255 | Py_DECREF(pieces); |
| 256 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Raymond Hettinger | 4ec44e8 | 2004-06-04 06:35:20 +0000 | [diff] [blame] | 259 | /* The addend 82520, was selected from the range(0, 1000000) for |
| 260 | generating the greatest number of prime multipliers for tuples |
| 261 | upto length eight: |
| 262 | |
| 263 | 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533, |
| 264 | 1330111, 1412633, 1165069, 1247599, 1495177, 1577699 |
| 265 | */ |
| 266 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 267 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 268 | tuplehash(PyTupleObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 269 | { |
| 270 | register long x, y; |
| 271 | register int len = v->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 272 | register PyObject **p; |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 273 | long mult = 1000003L; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 274 | x = 0x345678L; |
| 275 | p = v->ob_item; |
| 276 | while (--len >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 277 | y = PyObject_Hash(*p++); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 278 | if (y == -1) |
| 279 | return -1; |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 280 | x = (x ^ y) * mult; |
Raymond Hettinger | 4ec44e8 | 2004-06-04 06:35:20 +0000 | [diff] [blame] | 281 | mult += 82520L + len + len; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 282 | } |
Raymond Hettinger | 57c2d93 | 2004-06-10 18:42:15 +0000 | [diff] [blame] | 283 | x += 97531L; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 284 | if (x == -1) |
| 285 | x = -2; |
| 286 | return x; |
| 287 | } |
| 288 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 289 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 290 | tuplelength(PyTupleObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 291 | { |
| 292 | return a->ob_size; |
| 293 | } |
| 294 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 295 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 296 | tuplecontains(PyTupleObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 297 | { |
| 298 | int i, cmp; |
| 299 | |
Raymond Hettinger | bd9adab | 2002-09-05 20:18:08 +0000 | [diff] [blame] | 300 | for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i) |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 301 | cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i), |
Raymond Hettinger | bd9adab | 2002-09-05 20:18:08 +0000 | [diff] [blame] | 302 | Py_EQ); |
Neal Norwitz | bb9c5f5 | 2002-09-05 21:32:55 +0000 | [diff] [blame] | 303 | return cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 306 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 307 | tupleitem(register PyTupleObject *a, register int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | { |
| 309 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 310 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | return NULL; |
| 312 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | return a->ob_item[i]; |
| 315 | } |
| 316 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 317 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 318 | tupleslice(register PyTupleObject *a, register int ilow, register int ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | register PyTupleObject *np; |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 321 | PyObject **src, **dest; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | register int i; |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 323 | int len; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 324 | if (ilow < 0) |
| 325 | ilow = 0; |
| 326 | if (ihigh > a->ob_size) |
| 327 | ihigh = a->ob_size; |
| 328 | if (ihigh < ilow) |
| 329 | ihigh = ilow; |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 330 | if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 331 | Py_INCREF(a); |
| 332 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | } |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 334 | len = ihigh - ilow; |
| 335 | np = (PyTupleObject *)PyTuple_New(len); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | if (np == NULL) |
| 337 | return NULL; |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 338 | src = a->ob_item + ilow; |
| 339 | dest = np->ob_item; |
| 340 | for (i = 0; i < len; i++) { |
| 341 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | Py_INCREF(v); |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 343 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 344 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 345 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 348 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 349 | PyTuple_GetSlice(PyObject *op, int i, int j) |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 350 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | if (op == NULL || !PyTuple_Check(op)) { |
| 352 | PyErr_BadInternalCall(); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 353 | return NULL; |
| 354 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | return tupleslice((PyTupleObject *)op, i, j); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 359 | tupleconcat(register PyTupleObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | { |
| 361 | register int size; |
| 362 | register int i; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 363 | PyObject **src, **dest; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | PyTupleObject *np; |
| 365 | if (!PyTuple_Check(bb)) { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 366 | PyErr_Format(PyExc_TypeError, |
Marc-André Lemburg | 29dc381 | 2000-06-16 17:05:57 +0000 | [diff] [blame] | 367 | "can only concatenate tuple (not \"%.200s\") to tuple", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 368 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | return NULL; |
| 370 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 371 | #define b ((PyTupleObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 372 | size = a->ob_size + b->ob_size; |
Guido van Rossum | a5c0e6d | 2002-10-11 21:05:56 +0000 | [diff] [blame] | 373 | if (size < 0) |
| 374 | return PyErr_NoMemory(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | if (np == NULL) { |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 377 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 378 | } |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 379 | src = a->ob_item; |
| 380 | dest = np->ob_item; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | for (i = 0; i < a->ob_size; i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 382 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 383 | Py_INCREF(v); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 384 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 385 | } |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 386 | src = b->ob_item; |
| 387 | dest = np->ob_item + a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | for (i = 0; i < b->ob_size; i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 389 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 390 | Py_INCREF(v); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 391 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 393 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 394 | #undef b |
| 395 | } |
| 396 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 397 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 398 | tuplerepeat(PyTupleObject *a, int n) |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 399 | { |
| 400 | int i, j; |
| 401 | int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 402 | PyTupleObject *np; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 403 | PyObject **p, **items; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 404 | if (n < 0) |
| 405 | n = 0; |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 406 | if (a->ob_size == 0 || n == 1) { |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 407 | if (PyTuple_CheckExact(a)) { |
| 408 | /* Since tuples are immutable, we can return a shared |
| 409 | copy in this case */ |
| 410 | Py_INCREF(a); |
| 411 | return (PyObject *)a; |
| 412 | } |
| 413 | if (a->ob_size == 0) |
| 414 | return PyTuple_New(0); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 415 | } |
| 416 | size = a->ob_size * n; |
Guido van Rossum | 0eb55ac | 1999-07-13 05:41:12 +0000 | [diff] [blame] | 417 | if (size/a->ob_size != n) |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 418 | return PyErr_NoMemory(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 419 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 420 | if (np == NULL) |
| 421 | return NULL; |
| 422 | p = np->ob_item; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 423 | items = a->ob_item; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 424 | for (i = 0; i < n; i++) { |
| 425 | for (j = 0; j < a->ob_size; j++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 426 | *p = items[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 427 | Py_INCREF(*p); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 428 | p++; |
| 429 | } |
| 430 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 431 | return (PyObject *) np; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 434 | static int |
| 435 | tupletraverse(PyTupleObject *o, visitproc visit, void *arg) |
| 436 | { |
| 437 | int i, err; |
| 438 | PyObject *x; |
| 439 | |
| 440 | for (i = o->ob_size; --i >= 0; ) { |
| 441 | x = o->ob_item[i]; |
| 442 | if (x != NULL) { |
| 443 | err = visit(x, arg); |
| 444 | if (err) |
| 445 | return err; |
| 446 | } |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 451 | static PyObject * |
| 452 | tuplerichcompare(PyObject *v, PyObject *w, int op) |
| 453 | { |
| 454 | PyTupleObject *vt, *wt; |
| 455 | int i; |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 456 | int vlen, wlen; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 457 | |
| 458 | if (!PyTuple_Check(v) || !PyTuple_Check(w)) { |
| 459 | Py_INCREF(Py_NotImplemented); |
| 460 | return Py_NotImplemented; |
| 461 | } |
| 462 | |
| 463 | vt = (PyTupleObject *)v; |
| 464 | wt = (PyTupleObject *)w; |
| 465 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 466 | vlen = vt->ob_size; |
| 467 | wlen = wt->ob_size; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 468 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 469 | /* Note: the corresponding code for lists has an "early out" test |
| 470 | * here when op is EQ or NE and the lengths differ. That pays there, |
| 471 | * but Tim was unable to find any real code where EQ/NE tuple |
| 472 | * compares don't have the same length, so testing for it here would |
| 473 | * have cost without benefit. |
| 474 | */ |
| 475 | |
| 476 | /* Search for the first index where items are different. |
| 477 | * Note that because tuples are immutable, it's safe to reuse |
| 478 | * vlen and wlen across the comparison calls. |
| 479 | */ |
| 480 | for (i = 0; i < vlen && i < wlen; i++) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 481 | int k = PyObject_RichCompareBool(vt->ob_item[i], |
| 482 | wt->ob_item[i], Py_EQ); |
| 483 | if (k < 0) |
| 484 | return NULL; |
| 485 | if (!k) |
| 486 | break; |
| 487 | } |
| 488 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 489 | if (i >= vlen || i >= wlen) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 490 | /* No more items to compare -- compare sizes */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 491 | int cmp; |
| 492 | PyObject *res; |
| 493 | switch (op) { |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 494 | case Py_LT: cmp = vlen < wlen; break; |
| 495 | case Py_LE: cmp = vlen <= wlen; break; |
| 496 | case Py_EQ: cmp = vlen == wlen; break; |
| 497 | case Py_NE: cmp = vlen != wlen; break; |
| 498 | case Py_GT: cmp = vlen > wlen; break; |
| 499 | case Py_GE: cmp = vlen >= wlen; break; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 500 | default: return NULL; /* cannot happen */ |
| 501 | } |
| 502 | if (cmp) |
| 503 | res = Py_True; |
| 504 | else |
| 505 | res = Py_False; |
| 506 | Py_INCREF(res); |
| 507 | return res; |
| 508 | } |
| 509 | |
| 510 | /* We have an item that differs -- shortcuts for EQ/NE */ |
| 511 | if (op == Py_EQ) { |
| 512 | Py_INCREF(Py_False); |
| 513 | return Py_False; |
| 514 | } |
| 515 | if (op == Py_NE) { |
| 516 | Py_INCREF(Py_True); |
| 517 | return Py_True; |
| 518 | } |
| 519 | |
| 520 | /* Compare the final item again using the proper operator */ |
| 521 | return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op); |
| 522 | } |
| 523 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 524 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 525 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 526 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 527 | static PyObject * |
| 528 | tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 529 | { |
| 530 | PyObject *arg = NULL; |
| 531 | static char *kwlist[] = {"sequence", 0}; |
| 532 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 533 | if (type != &PyTuple_Type) |
| 534 | return tuple_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 535 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg)) |
| 536 | return NULL; |
| 537 | |
| 538 | if (arg == NULL) |
| 539 | return PyTuple_New(0); |
| 540 | else |
| 541 | return PySequence_Tuple(arg); |
| 542 | } |
| 543 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 544 | static PyObject * |
| 545 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 546 | { |
| 547 | PyObject *tmp, *new, *item; |
| 548 | int i, n; |
| 549 | |
| 550 | assert(PyType_IsSubtype(type, &PyTuple_Type)); |
| 551 | tmp = tuple_new(&PyTuple_Type, args, kwds); |
| 552 | if (tmp == NULL) |
| 553 | return NULL; |
| 554 | assert(PyTuple_Check(tmp)); |
| 555 | new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
| 556 | if (new == NULL) |
| 557 | return NULL; |
| 558 | for (i = 0; i < n; i++) { |
| 559 | item = PyTuple_GET_ITEM(tmp, i); |
| 560 | Py_INCREF(item); |
| 561 | PyTuple_SET_ITEM(new, i, item); |
| 562 | } |
| 563 | Py_DECREF(tmp); |
| 564 | return new; |
| 565 | } |
| 566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 567 | PyDoc_STRVAR(tuple_doc, |
Tim Peters | 1b8ca0d | 2001-09-02 06:42:25 +0000 | [diff] [blame] | 568 | "tuple() -> an empty tuple\n" |
| 569 | "tuple(sequence) -> tuple initialized from sequence's items\n" |
| 570 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 571 | "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] | 572 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 573 | static PySequenceMethods tuple_as_sequence = { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 574 | (inquiry)tuplelength, /* sq_length */ |
| 575 | (binaryfunc)tupleconcat, /* sq_concat */ |
| 576 | (intargfunc)tuplerepeat, /* sq_repeat */ |
| 577 | (intargfunc)tupleitem, /* sq_item */ |
| 578 | (intintargfunc)tupleslice, /* sq_slice */ |
| 579 | 0, /* sq_ass_item */ |
| 580 | 0, /* sq_ass_slice */ |
| 581 | (objobjproc)tuplecontains, /* sq_contains */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 582 | }; |
| 583 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 584 | static PyObject* |
| 585 | tuplesubscript(PyTupleObject* self, PyObject* item) |
| 586 | { |
| 587 | if (PyInt_Check(item)) { |
| 588 | long i = PyInt_AS_LONG(item); |
| 589 | if (i < 0) |
| 590 | i += PyTuple_GET_SIZE(self); |
| 591 | return tupleitem(self, i); |
| 592 | } |
| 593 | else if (PyLong_Check(item)) { |
| 594 | long i = PyLong_AsLong(item); |
| 595 | if (i == -1 && PyErr_Occurred()) |
| 596 | return NULL; |
| 597 | if (i < 0) |
| 598 | i += PyTuple_GET_SIZE(self); |
| 599 | return tupleitem(self, i); |
| 600 | } |
| 601 | else if (PySlice_Check(item)) { |
| 602 | int start, stop, step, slicelength, cur, i; |
| 603 | PyObject* result; |
| 604 | PyObject* it; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 605 | PyObject **src, **dest; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 606 | |
| 607 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
| 608 | PyTuple_GET_SIZE(self), |
| 609 | &start, &stop, &step, &slicelength) < 0) { |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | if (slicelength <= 0) { |
| 614 | return PyTuple_New(0); |
| 615 | } |
| 616 | else { |
| 617 | result = PyTuple_New(slicelength); |
| 618 | |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 619 | src = self->ob_item; |
| 620 | dest = ((PyTupleObject *)result)->ob_item; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 621 | for (cur = start, i = 0; i < slicelength; |
| 622 | cur += step, i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 623 | it = src[cur]; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 624 | Py_INCREF(it); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 625 | dest[i] = it; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return result; |
| 629 | } |
| 630 | } |
| 631 | else { |
| 632 | PyErr_SetString(PyExc_TypeError, |
| 633 | "tuple indices must be integers"); |
| 634 | return NULL; |
| 635 | } |
| 636 | } |
| 637 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 638 | static PyObject * |
| 639 | tuple_getnewargs(PyTupleObject *v) |
| 640 | { |
| 641 | return Py_BuildValue("(N)", tupleslice(v, 0, v->ob_size)); |
| 642 | |
| 643 | } |
| 644 | |
| 645 | static PyMethodDef tuple_methods[] = { |
| 646 | {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS}, |
| 647 | {NULL, NULL} /* sentinel */ |
| 648 | }; |
| 649 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 650 | static PyMappingMethods tuple_as_mapping = { |
| 651 | (inquiry)tuplelength, |
| 652 | (binaryfunc)tuplesubscript, |
| 653 | 0 |
| 654 | }; |
| 655 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 656 | static PyObject *tuple_iter(PyObject *seq); |
| 657 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 658 | PyTypeObject PyTuple_Type = { |
| 659 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 660 | 0, |
| 661 | "tuple", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 662 | sizeof(PyTupleObject) - sizeof(PyObject *), |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 663 | sizeof(PyObject *), |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 664 | (destructor)tupledealloc, /* tp_dealloc */ |
| 665 | (printfunc)tupleprint, /* tp_print */ |
| 666 | 0, /* tp_getattr */ |
| 667 | 0, /* tp_setattr */ |
| 668 | 0, /* tp_compare */ |
| 669 | (reprfunc)tuplerepr, /* tp_repr */ |
| 670 | 0, /* tp_as_number */ |
| 671 | &tuple_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 672 | &tuple_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 673 | (hashfunc)tuplehash, /* tp_hash */ |
| 674 | 0, /* tp_call */ |
| 675 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 676 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 677 | 0, /* tp_setattro */ |
| 678 | 0, /* tp_as_buffer */ |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 679 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 680 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 681 | tuple_doc, /* tp_doc */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 682 | (traverseproc)tupletraverse, /* tp_traverse */ |
| 683 | 0, /* tp_clear */ |
| 684 | tuplerichcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 685 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 686 | tuple_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 687 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 688 | tuple_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 689 | 0, /* tp_members */ |
| 690 | 0, /* tp_getset */ |
| 691 | 0, /* tp_base */ |
| 692 | 0, /* tp_dict */ |
| 693 | 0, /* tp_descr_get */ |
| 694 | 0, /* tp_descr_set */ |
| 695 | 0, /* tp_dictoffset */ |
| 696 | 0, /* tp_init */ |
| 697 | 0, /* tp_alloc */ |
| 698 | tuple_new, /* tp_new */ |
Neil Schemenauer | 626d774 | 2002-04-12 03:05:52 +0000 | [diff] [blame] | 699 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 700 | }; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 701 | |
| 702 | /* The following function breaks the notion that tuples are immutable: |
| 703 | it changes the size of a tuple. We get away with this only if there |
| 704 | 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] | 705 | as creating a new tuple object and destroying the old one, only more |
| 706 | 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] | 707 | known to some other part of the code. */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 708 | |
| 709 | int |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 710 | _PyTuple_Resize(PyObject **pv, int newsize) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 711 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 712 | register PyTupleObject *v; |
| 713 | register PyTupleObject *sv; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 714 | int i; |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 715 | int oldsize; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 716 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 717 | v = (PyTupleObject *) *pv; |
Guido van Rossum | 4b8c0f6 | 2001-08-30 18:31:30 +0000 | [diff] [blame] | 718 | if (v == NULL || v->ob_type != &PyTuple_Type || |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 719 | (v->ob_size != 0 && v->ob_refcnt != 1)) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 720 | *pv = 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 721 | Py_XDECREF(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 722 | PyErr_BadInternalCall(); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 723 | return -1; |
| 724 | } |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 725 | oldsize = v->ob_size; |
| 726 | if (oldsize == newsize) |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 727 | return 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 729 | if (oldsize == 0) { |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 730 | /* Empty tuples are often shared, so we should never |
| 731 | resize them in-place even if we do own the only |
| 732 | (current) reference */ |
| 733 | Py_DECREF(v); |
| 734 | *pv = PyTuple_New(newsize); |
Thomas Wouters | 0dcea59 | 2001-05-29 07:58:45 +0000 | [diff] [blame] | 735 | return *pv == NULL ? -1 : 0; |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 738 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 739 | _Py_DEC_REFTOTAL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 740 | _PyObject_GC_UNTRACK(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 741 | _Py_ForgetReference((PyObject *) v); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 742 | /* DECREF items deleted by shrinkage */ |
| 743 | for (i = newsize; i < oldsize; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 744 | Py_XDECREF(v->ob_item[i]); |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 745 | v->ob_item[i] = NULL; |
| 746 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 747 | sv = PyObject_GC_Resize(PyTupleObject, v, newsize); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 748 | if (sv == NULL) { |
| 749 | *pv = NULL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 750 | PyObject_GC_Del(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 751 | return -1; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 752 | } |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 753 | _Py_NewReference((PyObject *) sv); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 754 | /* Zero out items added by growing */ |
Neal Norwitz | 35fc760 | 2002-06-13 21:11:11 +0000 | [diff] [blame] | 755 | if (newsize > oldsize) |
Jeremy Hylton | 8b47dff | 2002-06-20 23:13:17 +0000 | [diff] [blame] | 756 | memset(&sv->ob_item[oldsize], 0, |
| 757 | sizeof(*sv->ob_item) * (newsize - oldsize)); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 758 | *pv = (PyObject *) sv; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 759 | _PyObject_GC_TRACK(sv); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 760 | return 0; |
| 761 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 762 | |
| 763 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 764 | PyTuple_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 765 | { |
| 766 | #if MAXSAVESIZE > 0 |
| 767 | int i; |
| 768 | |
| 769 | Py_XDECREF(free_tuples[0]); |
| 770 | free_tuples[0] = NULL; |
| 771 | |
| 772 | for (i = 1; i < MAXSAVESIZE; i++) { |
| 773 | PyTupleObject *p, *q; |
| 774 | p = free_tuples[i]; |
| 775 | free_tuples[i] = NULL; |
| 776 | while (p) { |
| 777 | q = p; |
| 778 | p = (PyTupleObject *)(p->ob_item[0]); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 779 | PyObject_GC_Del(q); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | #endif |
| 783 | } |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 784 | |
| 785 | /*********************** Tuple Iterator **************************/ |
| 786 | |
| 787 | typedef struct { |
| 788 | PyObject_HEAD |
| 789 | long it_index; |
| 790 | PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 791 | } tupleiterobject; |
| 792 | |
| 793 | PyTypeObject PyTupleIter_Type; |
| 794 | |
| 795 | static PyObject * |
| 796 | tuple_iter(PyObject *seq) |
| 797 | { |
| 798 | tupleiterobject *it; |
| 799 | |
| 800 | if (!PyTuple_Check(seq)) { |
| 801 | PyErr_BadInternalCall(); |
| 802 | return NULL; |
| 803 | } |
| 804 | it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type); |
| 805 | if (it == NULL) |
| 806 | return NULL; |
| 807 | it->it_index = 0; |
| 808 | Py_INCREF(seq); |
| 809 | it->it_seq = (PyTupleObject *)seq; |
| 810 | _PyObject_GC_TRACK(it); |
| 811 | return (PyObject *)it; |
| 812 | } |
| 813 | |
| 814 | static void |
| 815 | tupleiter_dealloc(tupleiterobject *it) |
| 816 | { |
| 817 | _PyObject_GC_UNTRACK(it); |
| 818 | Py_XDECREF(it->it_seq); |
| 819 | PyObject_GC_Del(it); |
| 820 | } |
| 821 | |
| 822 | static int |
| 823 | tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg) |
| 824 | { |
| 825 | if (it->it_seq == NULL) |
| 826 | return 0; |
| 827 | return visit((PyObject *)it->it_seq, arg); |
| 828 | } |
| 829 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 830 | static PyObject * |
| 831 | tupleiter_next(tupleiterobject *it) |
| 832 | { |
| 833 | PyTupleObject *seq; |
| 834 | PyObject *item; |
| 835 | |
| 836 | assert(it != NULL); |
| 837 | seq = it->it_seq; |
| 838 | if (seq == NULL) |
| 839 | return NULL; |
| 840 | assert(PyTuple_Check(seq)); |
| 841 | |
| 842 | if (it->it_index < PyTuple_GET_SIZE(seq)) { |
| 843 | item = PyTuple_GET_ITEM(seq, it->it_index); |
| 844 | ++it->it_index; |
| 845 | Py_INCREF(item); |
| 846 | return item; |
| 847 | } |
| 848 | |
| 849 | Py_DECREF(seq); |
| 850 | it->it_seq = NULL; |
| 851 | return NULL; |
| 852 | } |
| 853 | |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 854 | static int |
| 855 | tupleiter_len(tupleiterobject *it) |
| 856 | { |
| 857 | if (it->it_seq) |
| 858 | return PyTuple_GET_SIZE(it->it_seq) - it->it_index; |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static PySequenceMethods tupleiter_as_sequence = { |
| 863 | (inquiry)tupleiter_len, /* sq_length */ |
| 864 | 0, /* sq_concat */ |
| 865 | }; |
| 866 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 867 | PyTypeObject PyTupleIter_Type = { |
| 868 | PyObject_HEAD_INIT(&PyType_Type) |
| 869 | 0, /* ob_size */ |
| 870 | "tupleiterator", /* tp_name */ |
| 871 | sizeof(tupleiterobject), /* tp_basicsize */ |
| 872 | 0, /* tp_itemsize */ |
| 873 | /* methods */ |
| 874 | (destructor)tupleiter_dealloc, /* tp_dealloc */ |
| 875 | 0, /* tp_print */ |
| 876 | 0, /* tp_getattr */ |
| 877 | 0, /* tp_setattr */ |
| 878 | 0, /* tp_compare */ |
| 879 | 0, /* tp_repr */ |
| 880 | 0, /* tp_as_number */ |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 881 | &tupleiter_as_sequence, /* tp_as_sequence */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 882 | 0, /* tp_as_mapping */ |
| 883 | 0, /* tp_hash */ |
| 884 | 0, /* tp_call */ |
| 885 | 0, /* tp_str */ |
| 886 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 887 | 0, /* tp_setattro */ |
| 888 | 0, /* tp_as_buffer */ |
| 889 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 890 | 0, /* tp_doc */ |
| 891 | (traverseproc)tupleiter_traverse, /* tp_traverse */ |
| 892 | 0, /* tp_clear */ |
| 893 | 0, /* tp_richcompare */ |
| 894 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 895 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 896 | (iternextfunc)tupleiter_next, /* tp_iternext */ |
| 897 | }; |