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