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 * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 27 | PyTuple_New(register Py_ssize_t 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; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 30 | Py_ssize_t 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 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 60 | Py_ssize_t nbytes = size * sizeof(PyObject *); |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 85 | Py_ssize_t |
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 * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 97 | PyTuple_GetItem(register PyObject *op, register Py_ssize_t 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 |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 111 | 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] | 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 * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 134 | PyTuple_Pack(Py_ssize_t n, ...) |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 135 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 136 | Py_ssize_t i; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 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 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 162 | register Py_ssize_t i; |
| 163 | register Py_ssize_t 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 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 190 | Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 207 | Py_ssize_t i, n; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 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; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 271 | register Py_ssize_t 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; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 281 | /* the cast might truncate len; that doesn't change hash stability */ |
| 282 | mult += (long)(82520L + len + len); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 283 | } |
Raymond Hettinger | 57c2d93 | 2004-06-10 18:42:15 +0000 | [diff] [blame] | 284 | x += 97531L; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 285 | if (x == -1) |
| 286 | x = -2; |
| 287 | return x; |
| 288 | } |
| 289 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 290 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 291 | tuplelength(PyTupleObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | { |
| 293 | return a->ob_size; |
| 294 | } |
| 295 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 296 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 297 | tuplecontains(PyTupleObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 299 | Py_ssize_t i; |
| 300 | int cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 301 | |
Raymond Hettinger | bd9adab | 2002-09-05 20:18:08 +0000 | [diff] [blame] | 302 | 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] | 303 | cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i), |
Raymond Hettinger | bd9adab | 2002-09-05 20:18:08 +0000 | [diff] [blame] | 304 | Py_EQ); |
Neal Norwitz | bb9c5f5 | 2002-09-05 21:32:55 +0000 | [diff] [blame] | 305 | return cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 309 | tupleitem(register PyTupleObject *a, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | { |
| 311 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 312 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | return NULL; |
| 314 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 315 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 316 | return a->ob_item[i]; |
| 317 | } |
| 318 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 319 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 320 | tupleslice(register PyTupleObject *a, register Py_ssize_t ilow, |
| 321 | register Py_ssize_t ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 323 | register PyTupleObject *np; |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 324 | PyObject **src, **dest; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 325 | register Py_ssize_t i; |
| 326 | Py_ssize_t len; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 327 | if (ilow < 0) |
| 328 | ilow = 0; |
| 329 | if (ihigh > a->ob_size) |
| 330 | ihigh = a->ob_size; |
| 331 | if (ihigh < ilow) |
| 332 | ihigh = ilow; |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 333 | if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 334 | Py_INCREF(a); |
| 335 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | } |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 337 | len = ihigh - ilow; |
| 338 | np = (PyTupleObject *)PyTuple_New(len); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | if (np == NULL) |
| 340 | return NULL; |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 341 | src = a->ob_item + ilow; |
| 342 | dest = np->ob_item; |
| 343 | for (i = 0; i < len; i++) { |
| 344 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 345 | Py_INCREF(v); |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 346 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 348 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 352 | 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] | 353 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | if (op == NULL || !PyTuple_Check(op)) { |
| 355 | PyErr_BadInternalCall(); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | return tupleslice((PyTupleObject *)op, i, j); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 361 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 362 | tupleconcat(register PyTupleObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 363 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 364 | register Py_ssize_t size; |
| 365 | register Py_ssize_t i; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 366 | PyObject **src, **dest; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 367 | PyTupleObject *np; |
| 368 | if (!PyTuple_Check(bb)) { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 369 | PyErr_Format(PyExc_TypeError, |
Marc-André Lemburg | 29dc381 | 2000-06-16 17:05:57 +0000 | [diff] [blame] | 370 | "can only concatenate tuple (not \"%.200s\") to tuple", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 371 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | #define b ((PyTupleObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 375 | size = a->ob_size + b->ob_size; |
Guido van Rossum | a5c0e6d | 2002-10-11 21:05:56 +0000 | [diff] [blame] | 376 | if (size < 0) |
| 377 | return PyErr_NoMemory(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 378 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 379 | if (np == NULL) { |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 380 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | } |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 382 | src = a->ob_item; |
| 383 | dest = np->ob_item; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 384 | for (i = 0; i < a->ob_size; i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 385 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 386 | Py_INCREF(v); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 387 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | } |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 389 | src = b->ob_item; |
| 390 | dest = np->ob_item + a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 391 | for (i = 0; i < b->ob_size; i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 392 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 393 | Py_INCREF(v); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 394 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 395 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 396 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | #undef b |
| 398 | } |
| 399 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 400 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 401 | tuplerepeat(PyTupleObject *a, Py_ssize_t n) |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 402 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 403 | Py_ssize_t i, j; |
| 404 | Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 405 | PyTupleObject *np; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 406 | PyObject **p, **items; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 407 | if (n < 0) |
| 408 | n = 0; |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 409 | if (a->ob_size == 0 || n == 1) { |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 410 | if (PyTuple_CheckExact(a)) { |
| 411 | /* Since tuples are immutable, we can return a shared |
| 412 | copy in this case */ |
| 413 | Py_INCREF(a); |
| 414 | return (PyObject *)a; |
| 415 | } |
| 416 | if (a->ob_size == 0) |
| 417 | return PyTuple_New(0); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 418 | } |
| 419 | size = a->ob_size * n; |
Guido van Rossum | 0eb55ac | 1999-07-13 05:41:12 +0000 | [diff] [blame] | 420 | if (size/a->ob_size != n) |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 421 | return PyErr_NoMemory(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 422 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 423 | if (np == NULL) |
| 424 | return NULL; |
| 425 | p = np->ob_item; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 426 | items = a->ob_item; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 427 | for (i = 0; i < n; i++) { |
| 428 | for (j = 0; j < a->ob_size; j++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 429 | *p = items[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 430 | Py_INCREF(*p); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 431 | p++; |
| 432 | } |
| 433 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 434 | return (PyObject *) np; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 437 | static int |
| 438 | tupletraverse(PyTupleObject *o, visitproc visit, void *arg) |
| 439 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 440 | Py_ssize_t i; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 441 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 442 | for (i = o->ob_size; --i >= 0; ) |
| 443 | Py_VISIT(o->ob_item[i]); |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 447 | static PyObject * |
| 448 | tuplerichcompare(PyObject *v, PyObject *w, int op) |
| 449 | { |
| 450 | PyTupleObject *vt, *wt; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 451 | Py_ssize_t i; |
| 452 | Py_ssize_t vlen, wlen; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 453 | |
| 454 | if (!PyTuple_Check(v) || !PyTuple_Check(w)) { |
| 455 | Py_INCREF(Py_NotImplemented); |
| 456 | return Py_NotImplemented; |
| 457 | } |
| 458 | |
| 459 | vt = (PyTupleObject *)v; |
| 460 | wt = (PyTupleObject *)w; |
| 461 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 462 | vlen = vt->ob_size; |
| 463 | wlen = wt->ob_size; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 464 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 465 | /* Note: the corresponding code for lists has an "early out" test |
| 466 | * here when op is EQ or NE and the lengths differ. That pays there, |
| 467 | * but Tim was unable to find any real code where EQ/NE tuple |
| 468 | * compares don't have the same length, so testing for it here would |
| 469 | * have cost without benefit. |
| 470 | */ |
| 471 | |
| 472 | /* Search for the first index where items are different. |
| 473 | * Note that because tuples are immutable, it's safe to reuse |
| 474 | * vlen and wlen across the comparison calls. |
| 475 | */ |
| 476 | for (i = 0; i < vlen && i < wlen; i++) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 477 | int k = PyObject_RichCompareBool(vt->ob_item[i], |
| 478 | wt->ob_item[i], Py_EQ); |
| 479 | if (k < 0) |
| 480 | return NULL; |
| 481 | if (!k) |
| 482 | break; |
| 483 | } |
| 484 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 485 | if (i >= vlen || i >= wlen) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 486 | /* No more items to compare -- compare sizes */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 487 | int cmp; |
| 488 | PyObject *res; |
| 489 | switch (op) { |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 490 | case Py_LT: cmp = vlen < wlen; break; |
| 491 | case Py_LE: cmp = vlen <= wlen; break; |
| 492 | case Py_EQ: cmp = vlen == wlen; break; |
| 493 | case Py_NE: cmp = vlen != wlen; break; |
| 494 | case Py_GT: cmp = vlen > wlen; break; |
| 495 | case Py_GE: cmp = vlen >= wlen; break; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 496 | default: return NULL; /* cannot happen */ |
| 497 | } |
| 498 | if (cmp) |
| 499 | res = Py_True; |
| 500 | else |
| 501 | res = Py_False; |
| 502 | Py_INCREF(res); |
| 503 | return res; |
| 504 | } |
| 505 | |
| 506 | /* We have an item that differs -- shortcuts for EQ/NE */ |
| 507 | if (op == Py_EQ) { |
| 508 | Py_INCREF(Py_False); |
| 509 | return Py_False; |
| 510 | } |
| 511 | if (op == Py_NE) { |
| 512 | Py_INCREF(Py_True); |
| 513 | return Py_True; |
| 514 | } |
| 515 | |
| 516 | /* Compare the final item again using the proper operator */ |
| 517 | return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op); |
| 518 | } |
| 519 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 520 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 521 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 522 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 523 | static PyObject * |
| 524 | tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 525 | { |
| 526 | PyObject *arg = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 527 | static char *kwlist[] = {"sequence", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 528 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 529 | if (type != &PyTuple_Type) |
| 530 | return tuple_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 531 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg)) |
| 532 | return NULL; |
| 533 | |
| 534 | if (arg == NULL) |
| 535 | return PyTuple_New(0); |
| 536 | else |
| 537 | return PySequence_Tuple(arg); |
| 538 | } |
| 539 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 540 | static PyObject * |
| 541 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 542 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 543 | PyObject *tmp, *newobj, *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 544 | Py_ssize_t i, n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 545 | |
| 546 | assert(PyType_IsSubtype(type, &PyTuple_Type)); |
| 547 | tmp = tuple_new(&PyTuple_Type, args, kwds); |
| 548 | if (tmp == NULL) |
| 549 | return NULL; |
| 550 | assert(PyTuple_Check(tmp)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 551 | newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
| 552 | if (newobj == NULL) |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 553 | return NULL; |
| 554 | for (i = 0; i < n; i++) { |
| 555 | item = PyTuple_GET_ITEM(tmp, i); |
| 556 | Py_INCREF(item); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 557 | PyTuple_SET_ITEM(newobj, i, item); |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 558 | } |
| 559 | Py_DECREF(tmp); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 560 | return newobj; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 563 | PyDoc_STRVAR(tuple_doc, |
Tim Peters | 1b8ca0d | 2001-09-02 06:42:25 +0000 | [diff] [blame] | 564 | "tuple() -> an empty tuple\n" |
| 565 | "tuple(sequence) -> tuple initialized from sequence's items\n" |
| 566 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 567 | "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] | 568 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 569 | static PySequenceMethods tuple_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 570 | (lenfunc)tuplelength, /* sq_length */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 571 | (binaryfunc)tupleconcat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 572 | (ssizeargfunc)tuplerepeat, /* sq_repeat */ |
| 573 | (ssizeargfunc)tupleitem, /* sq_item */ |
| 574 | (ssizessizeargfunc)tupleslice, /* sq_slice */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 575 | 0, /* sq_ass_item */ |
| 576 | 0, /* sq_ass_slice */ |
| 577 | (objobjproc)tuplecontains, /* sq_contains */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 578 | }; |
| 579 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 580 | static PyObject* |
| 581 | tuplesubscript(PyTupleObject* self, PyObject* item) |
| 582 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 583 | if (PyIndex_Check(item)) { |
| 584 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 585 | if (i == -1 && PyErr_Occurred()) |
| 586 | return NULL; |
| 587 | if (i < 0) |
| 588 | i += PyTuple_GET_SIZE(self); |
| 589 | return tupleitem(self, i); |
| 590 | } |
| 591 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 592 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 593 | PyObject* result; |
| 594 | PyObject* it; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 595 | PyObject **src, **dest; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 596 | |
| 597 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
| 598 | PyTuple_GET_SIZE(self), |
| 599 | &start, &stop, &step, &slicelength) < 0) { |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | if (slicelength <= 0) { |
| 604 | return PyTuple_New(0); |
| 605 | } |
| 606 | else { |
| 607 | result = PyTuple_New(slicelength); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 608 | if (!result) return NULL; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 609 | |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 610 | src = self->ob_item; |
| 611 | dest = ((PyTupleObject *)result)->ob_item; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 612 | for (cur = start, i = 0; i < slicelength; |
| 613 | cur += step, i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 614 | it = src[cur]; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 615 | Py_INCREF(it); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 616 | dest[i] = it; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | return result; |
| 620 | } |
| 621 | } |
| 622 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 623 | PyErr_Format(PyExc_TypeError, |
| 624 | "tuple indices must be integers, not %.200s", |
| 625 | item->ob_type->tp_name); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 626 | return NULL; |
| 627 | } |
| 628 | } |
| 629 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 630 | static PyObject * |
| 631 | tuple_getnewargs(PyTupleObject *v) |
| 632 | { |
| 633 | return Py_BuildValue("(N)", tupleslice(v, 0, v->ob_size)); |
| 634 | |
| 635 | } |
| 636 | |
| 637 | static PyMethodDef tuple_methods[] = { |
| 638 | {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS}, |
| 639 | {NULL, NULL} /* sentinel */ |
| 640 | }; |
| 641 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 642 | static PyMappingMethods tuple_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 643 | (lenfunc)tuplelength, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 644 | (binaryfunc)tuplesubscript, |
| 645 | 0 |
| 646 | }; |
| 647 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 648 | static PyObject *tuple_iter(PyObject *seq); |
| 649 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 650 | PyTypeObject PyTuple_Type = { |
| 651 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | 0, |
| 653 | "tuple", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 654 | sizeof(PyTupleObject) - sizeof(PyObject *), |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 655 | sizeof(PyObject *), |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 656 | (destructor)tupledealloc, /* tp_dealloc */ |
| 657 | (printfunc)tupleprint, /* tp_print */ |
| 658 | 0, /* tp_getattr */ |
| 659 | 0, /* tp_setattr */ |
| 660 | 0, /* tp_compare */ |
| 661 | (reprfunc)tuplerepr, /* tp_repr */ |
| 662 | 0, /* tp_as_number */ |
| 663 | &tuple_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 664 | &tuple_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 665 | (hashfunc)tuplehash, /* tp_hash */ |
| 666 | 0, /* tp_call */ |
| 667 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 668 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 669 | 0, /* tp_setattro */ |
| 670 | 0, /* tp_as_buffer */ |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 671 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 672 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 673 | tuple_doc, /* tp_doc */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 674 | (traverseproc)tupletraverse, /* tp_traverse */ |
| 675 | 0, /* tp_clear */ |
| 676 | tuplerichcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 677 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 678 | tuple_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 679 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 680 | tuple_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 681 | 0, /* tp_members */ |
| 682 | 0, /* tp_getset */ |
| 683 | 0, /* tp_base */ |
| 684 | 0, /* tp_dict */ |
| 685 | 0, /* tp_descr_get */ |
| 686 | 0, /* tp_descr_set */ |
| 687 | 0, /* tp_dictoffset */ |
| 688 | 0, /* tp_init */ |
| 689 | 0, /* tp_alloc */ |
| 690 | tuple_new, /* tp_new */ |
Neil Schemenauer | 626d774 | 2002-04-12 03:05:52 +0000 | [diff] [blame] | 691 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 692 | }; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 693 | |
| 694 | /* The following function breaks the notion that tuples are immutable: |
| 695 | it changes the size of a tuple. We get away with this only if there |
| 696 | 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] | 697 | as creating a new tuple object and destroying the old one, only more |
| 698 | 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] | 699 | known to some other part of the code. */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 700 | |
| 701 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 702 | _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 703 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 704 | register PyTupleObject *v; |
| 705 | register PyTupleObject *sv; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 706 | Py_ssize_t i; |
| 707 | Py_ssize_t oldsize; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 708 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 709 | v = (PyTupleObject *) *pv; |
Guido van Rossum | 4b8c0f6 | 2001-08-30 18:31:30 +0000 | [diff] [blame] | 710 | if (v == NULL || v->ob_type != &PyTuple_Type || |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 711 | (v->ob_size != 0 && v->ob_refcnt != 1)) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 712 | *pv = 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 713 | Py_XDECREF(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 714 | PyErr_BadInternalCall(); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 715 | return -1; |
| 716 | } |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 717 | oldsize = v->ob_size; |
| 718 | if (oldsize == newsize) |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 719 | return 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 720 | |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 721 | if (oldsize == 0) { |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 722 | /* Empty tuples are often shared, so we should never |
| 723 | resize them in-place even if we do own the only |
| 724 | (current) reference */ |
| 725 | Py_DECREF(v); |
| 726 | *pv = PyTuple_New(newsize); |
Thomas Wouters | 0dcea59 | 2001-05-29 07:58:45 +0000 | [diff] [blame] | 727 | return *pv == NULL ? -1 : 0; |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 730 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 731 | _Py_DEC_REFTOTAL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 732 | _PyObject_GC_UNTRACK(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 733 | _Py_ForgetReference((PyObject *) v); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 734 | /* DECREF items deleted by shrinkage */ |
| 735 | for (i = newsize; i < oldsize; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 736 | Py_XDECREF(v->ob_item[i]); |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 737 | v->ob_item[i] = NULL; |
| 738 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 739 | sv = PyObject_GC_Resize(PyTupleObject, v, newsize); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 740 | if (sv == NULL) { |
| 741 | *pv = NULL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 742 | PyObject_GC_Del(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 743 | return -1; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 744 | } |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 745 | _Py_NewReference((PyObject *) sv); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 746 | /* Zero out items added by growing */ |
Neal Norwitz | 35fc760 | 2002-06-13 21:11:11 +0000 | [diff] [blame] | 747 | if (newsize > oldsize) |
Jeremy Hylton | 8b47dff | 2002-06-20 23:13:17 +0000 | [diff] [blame] | 748 | memset(&sv->ob_item[oldsize], 0, |
| 749 | sizeof(*sv->ob_item) * (newsize - oldsize)); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 750 | *pv = (PyObject *) sv; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 751 | _PyObject_GC_TRACK(sv); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 752 | return 0; |
| 753 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 754 | |
| 755 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 756 | PyTuple_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 757 | { |
| 758 | #if MAXSAVESIZE > 0 |
| 759 | int i; |
| 760 | |
| 761 | Py_XDECREF(free_tuples[0]); |
| 762 | free_tuples[0] = NULL; |
| 763 | |
| 764 | for (i = 1; i < MAXSAVESIZE; i++) { |
| 765 | PyTupleObject *p, *q; |
| 766 | p = free_tuples[i]; |
| 767 | free_tuples[i] = NULL; |
| 768 | while (p) { |
| 769 | q = p; |
| 770 | p = (PyTupleObject *)(p->ob_item[0]); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 771 | PyObject_GC_Del(q); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | #endif |
| 775 | } |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 776 | |
| 777 | /*********************** Tuple Iterator **************************/ |
| 778 | |
| 779 | typedef struct { |
| 780 | PyObject_HEAD |
| 781 | long it_index; |
| 782 | PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 783 | } tupleiterobject; |
| 784 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 785 | static void |
| 786 | tupleiter_dealloc(tupleiterobject *it) |
| 787 | { |
| 788 | _PyObject_GC_UNTRACK(it); |
| 789 | Py_XDECREF(it->it_seq); |
| 790 | PyObject_GC_Del(it); |
| 791 | } |
| 792 | |
| 793 | static int |
| 794 | tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg) |
| 795 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 796 | Py_VISIT(it->it_seq); |
| 797 | return 0; |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 800 | static PyObject * |
| 801 | tupleiter_next(tupleiterobject *it) |
| 802 | { |
| 803 | PyTupleObject *seq; |
| 804 | PyObject *item; |
| 805 | |
| 806 | assert(it != NULL); |
| 807 | seq = it->it_seq; |
| 808 | if (seq == NULL) |
| 809 | return NULL; |
| 810 | assert(PyTuple_Check(seq)); |
| 811 | |
| 812 | if (it->it_index < PyTuple_GET_SIZE(seq)) { |
| 813 | item = PyTuple_GET_ITEM(seq, it->it_index); |
| 814 | ++it->it_index; |
| 815 | Py_INCREF(item); |
| 816 | return item; |
| 817 | } |
| 818 | |
| 819 | Py_DECREF(seq); |
| 820 | it->it_seq = NULL; |
| 821 | return NULL; |
| 822 | } |
| 823 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 824 | static PyObject * |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 825 | tupleiter_len(tupleiterobject *it) |
| 826 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 827 | Py_ssize_t len = 0; |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 828 | if (it->it_seq) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 829 | len = PyTuple_GET_SIZE(it->it_seq) - it->it_index; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 830 | return PyInt_FromSsize_t(len); |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 833 | 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] | 834 | |
| 835 | static PyMethodDef tupleiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 836 | {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 837 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 838 | }; |
| 839 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 840 | PyTypeObject PyTupleIter_Type = { |
| 841 | PyObject_HEAD_INIT(&PyType_Type) |
| 842 | 0, /* ob_size */ |
| 843 | "tupleiterator", /* tp_name */ |
| 844 | sizeof(tupleiterobject), /* tp_basicsize */ |
| 845 | 0, /* tp_itemsize */ |
| 846 | /* methods */ |
| 847 | (destructor)tupleiter_dealloc, /* tp_dealloc */ |
| 848 | 0, /* tp_print */ |
| 849 | 0, /* tp_getattr */ |
| 850 | 0, /* tp_setattr */ |
| 851 | 0, /* tp_compare */ |
| 852 | 0, /* tp_repr */ |
| 853 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 854 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 855 | 0, /* tp_as_mapping */ |
| 856 | 0, /* tp_hash */ |
| 857 | 0, /* tp_call */ |
| 858 | 0, /* tp_str */ |
| 859 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 860 | 0, /* tp_setattro */ |
| 861 | 0, /* tp_as_buffer */ |
| 862 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 863 | 0, /* tp_doc */ |
| 864 | (traverseproc)tupleiter_traverse, /* tp_traverse */ |
| 865 | 0, /* tp_clear */ |
| 866 | 0, /* tp_richcompare */ |
| 867 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 868 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 869 | (iternextfunc)tupleiter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 870 | tupleiter_methods, /* tp_methods */ |
| 871 | 0, |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 872 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 873 | |
| 874 | static PyObject * |
| 875 | tuple_iter(PyObject *seq) |
| 876 | { |
| 877 | tupleiterobject *it; |
| 878 | |
| 879 | if (!PyTuple_Check(seq)) { |
| 880 | PyErr_BadInternalCall(); |
| 881 | return NULL; |
| 882 | } |
| 883 | it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type); |
| 884 | if (it == NULL) |
| 885 | return NULL; |
| 886 | it->it_index = 0; |
| 887 | Py_INCREF(seq); |
| 888 | it->it_seq = (PyTupleObject *)seq; |
| 889 | _PyObject_GC_TRACK(it); |
| 890 | return (PyObject *)it; |
| 891 | } |