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 | { |
| 29 | register int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 30 | register PyTupleObject *op; |
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 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 44 | if (0 < size && size < MAXSAVESIZE && |
| 45 | (op = free_tuples[size]) != NULL) |
| 46 | { |
| 47 | free_tuples[size] = (PyTupleObject *) op->ob_item[0]; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 48 | num_free_tuples[size]--; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 49 | #ifdef COUNT_ALLOCS |
| 50 | fast_tuple_allocs++; |
| 51 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 52 | /* PyObject_InitVar is inlined */ |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 53 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 54 | op->ob_size = size; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 55 | op->ob_type = &PyTuple_Type; |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 56 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 57 | _Py_NewReference((PyObject *)op); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 58 | } |
| 59 | else |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 60 | #endif |
| 61 | { |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 62 | int nbytes = size * sizeof(PyObject *); |
| 63 | /* Check for overflow */ |
| 64 | if (nbytes / sizeof(PyObject *) != (size_t)size || |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 65 | (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)) |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 66 | <= 0) |
| 67 | { |
| 68 | return PyErr_NoMemory(); |
| 69 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 70 | op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 71 | if (op == NULL) |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 72 | return NULL; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 73 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | for (i = 0; i < size; i++) |
| 75 | op->ob_item[i] = NULL; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 76 | #if MAXSAVESIZE > 0 |
| 77 | if (size == 0) { |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 78 | free_tuples[0] = op; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 79 | ++num_free_tuples[0]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 80 | Py_INCREF(op); /* extra INCREF so that this is never freed */ |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 81 | } |
| 82 | #endif |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 83 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 84 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 88 | PyTuple_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | if (!PyTuple_Check(op)) { |
| 91 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | return -1; |
| 93 | } |
| 94 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | return ((PyTupleObject *)op)->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 98 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 99 | PyTuple_GetItem(register PyObject *op, register int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 101 | if (!PyTuple_Check(op)) { |
| 102 | PyErr_BadInternalCall(); |
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 | if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
| 106 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | return ((PyTupleObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 113 | PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | register PyObject *olditem; |
| 116 | register PyObject **p; |
Guido van Rossum | 787bdd3 | 1997-08-17 16:25:45 +0000 | [diff] [blame] | 117 | if (!PyTuple_Check(op) || op->ob_refcnt != 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | Py_XDECREF(newitem); |
| 119 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 120 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
| 123 | Py_XDECREF(newitem); |
| 124 | PyErr_SetString(PyExc_IndexError, |
| 125 | "tuple assignment index out of range"); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 126 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 128 | p = ((PyTupleObject *)op) -> ob_item + i; |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 129 | olditem = *p; |
| 130 | *p = newitem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 131 | Py_XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* Methods */ |
| 136 | |
| 137 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 138 | tupledealloc(register PyTupleObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | { |
| 140 | register int i; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 141 | register int len = op->ob_size; |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 142 | PyObject_GC_UnTrack(op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 143 | Py_TRASHCAN_SAFE_BEGIN(op) |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 144 | if (len > 0) { |
| 145 | i = len; |
Guido van Rossum | 1bb2687 | 1998-06-26 15:53:50 +0000 | [diff] [blame] | 146 | while (--i >= 0) |
| 147 | Py_XDECREF(op->ob_item[i]); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 148 | #if MAXSAVESIZE > 0 |
Guido van Rossum | 4b8c0f6 | 2001-08-30 18:31:30 +0000 | [diff] [blame] | 149 | if (len < MAXSAVESIZE && |
| 150 | num_free_tuples[len] < MAXSAVEDTUPLES && |
| 151 | op->ob_type == &PyTuple_Type) |
| 152 | { |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 153 | op->ob_item[0] = (PyObject *) free_tuples[len]; |
| 154 | num_free_tuples[len]++; |
| 155 | free_tuples[len] = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 156 | goto done; /* return */ |
Guido van Rossum | 1bb2687 | 1998-06-26 15:53:50 +0000 | [diff] [blame] | 157 | } |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 158 | #endif |
Guido van Rossum | 1bb2687 | 1998-06-26 15:53:50 +0000 | [diff] [blame] | 159 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 160 | op->ob_type->tp_free((PyObject *)op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 161 | done: |
| 162 | Py_TRASHCAN_SAFE_END(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 165 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 166 | tupleprint(PyTupleObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | { |
| 168 | int i; |
| 169 | fprintf(fp, "("); |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 170 | for (i = 0; i < op->ob_size; i++) { |
| 171 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | fprintf(fp, ", "); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 173 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 174 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 175 | } |
| 176 | if (op->ob_size == 1) |
| 177 | fprintf(fp, ","); |
| 178 | fprintf(fp, ")"); |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 179 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 183 | tuplerepr(PyTupleObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 185 | int i, n; |
| 186 | PyObject *s, *temp; |
| 187 | PyObject *pieces, *result = NULL; |
| 188 | |
| 189 | n = v->ob_size; |
| 190 | if (n == 0) |
| 191 | return PyString_FromString("()"); |
| 192 | |
| 193 | pieces = PyTuple_New(n); |
| 194 | if (pieces == NULL) |
| 195 | return NULL; |
| 196 | |
| 197 | /* Do repr() on each element. */ |
| 198 | for (i = 0; i < n; ++i) { |
| 199 | s = PyObject_Repr(v->ob_item[i]); |
| 200 | if (s == NULL) |
| 201 | goto Done; |
| 202 | PyTuple_SET_ITEM(pieces, i, s); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 204 | |
| 205 | /* Add "()" decorations to the first and last items. */ |
| 206 | assert(n > 0); |
| 207 | s = PyString_FromString("("); |
| 208 | if (s == NULL) |
| 209 | goto Done; |
| 210 | temp = PyTuple_GET_ITEM(pieces, 0); |
| 211 | PyString_ConcatAndDel(&s, temp); |
| 212 | PyTuple_SET_ITEM(pieces, 0, s); |
| 213 | if (s == NULL) |
| 214 | goto Done; |
| 215 | |
| 216 | s = PyString_FromString(n == 1 ? ",)" : ")"); |
| 217 | if (s == NULL) |
| 218 | goto Done; |
| 219 | temp = PyTuple_GET_ITEM(pieces, n-1); |
| 220 | PyString_ConcatAndDel(&temp, s); |
| 221 | PyTuple_SET_ITEM(pieces, n-1, temp); |
| 222 | if (temp == NULL) |
| 223 | goto Done; |
| 224 | |
| 225 | /* Paste them all together with ", " between. */ |
| 226 | s = PyString_FromString(", "); |
| 227 | if (s == NULL) |
| 228 | goto Done; |
| 229 | result = _PyString_Join(s, pieces); |
| 230 | Py_DECREF(s); |
| 231 | |
| 232 | Done: |
| 233 | Py_DECREF(pieces); |
| 234 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 237 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 238 | tuplehash(PyTupleObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 239 | { |
| 240 | register long x, y; |
| 241 | register int len = v->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | register PyObject **p; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 243 | x = 0x345678L; |
| 244 | p = v->ob_item; |
| 245 | while (--len >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 246 | y = PyObject_Hash(*p++); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 247 | if (y == -1) |
| 248 | return -1; |
Guido van Rossum | 0969ad2 | 1996-12-16 17:55:46 +0000 | [diff] [blame] | 249 | x = (1000003*x) ^ y; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 250 | } |
| 251 | x ^= v->ob_size; |
| 252 | if (x == -1) |
| 253 | x = -2; |
| 254 | return x; |
| 255 | } |
| 256 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 258 | tuplelength(PyTupleObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | { |
| 260 | return a->ob_size; |
| 261 | } |
| 262 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 263 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 264 | tuplecontains(PyTupleObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 265 | { |
| 266 | int i, cmp; |
| 267 | |
| 268 | for (i = 0; i < a->ob_size; ++i) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 269 | cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i), |
| 270 | Py_EQ); |
| 271 | if (cmp > 0) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 272 | return 1; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 273 | else if (cmp < 0) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 279 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 280 | tupleitem(register PyTupleObject *a, register int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | { |
| 282 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 283 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 284 | return NULL; |
| 285 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 286 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | return a->ob_item[i]; |
| 288 | } |
| 289 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 291 | tupleslice(register PyTupleObject *a, register int ilow, register int ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 293 | register PyTupleObject *np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | register int i; |
| 295 | if (ilow < 0) |
| 296 | ilow = 0; |
| 297 | if (ihigh > a->ob_size) |
| 298 | ihigh = a->ob_size; |
| 299 | if (ihigh < ilow) |
| 300 | ihigh = ilow; |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 301 | if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | Py_INCREF(a); |
| 303 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 305 | np = (PyTupleObject *)PyTuple_New(ihigh - ilow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 306 | if (np == NULL) |
| 307 | return NULL; |
| 308 | for (i = ilow; i < ihigh; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 309 | PyObject *v = a->ob_item[i]; |
| 310 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | np->ob_item[i - ilow] = v; |
| 312 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 316 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 317 | PyTuple_GetSlice(PyObject *op, int i, int j) |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 318 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 319 | if (op == NULL || !PyTuple_Check(op)) { |
| 320 | PyErr_BadInternalCall(); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 321 | return NULL; |
| 322 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 323 | return tupleslice((PyTupleObject *)op, i, j); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 326 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 327 | tupleconcat(register PyTupleObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | { |
| 329 | register int size; |
| 330 | register int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 331 | PyTupleObject *np; |
| 332 | if (!PyTuple_Check(bb)) { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 333 | PyErr_Format(PyExc_TypeError, |
Marc-André Lemburg | 29dc381 | 2000-06-16 17:05:57 +0000 | [diff] [blame] | 334 | "can only concatenate tuple (not \"%.200s\") to tuple", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 335 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | return NULL; |
| 337 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 338 | #define b ((PyTupleObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | if (np == NULL) { |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 342 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | } |
| 344 | for (i = 0; i < a->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 345 | PyObject *v = a->ob_item[i]; |
| 346 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | np->ob_item[i] = v; |
| 348 | } |
| 349 | for (i = 0; i < b->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 350 | PyObject *v = b->ob_item[i]; |
| 351 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | np->ob_item[i + a->ob_size] = v; |
| 353 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 355 | #undef b |
| 356 | } |
| 357 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 359 | tuplerepeat(PyTupleObject *a, int n) |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 360 | { |
| 361 | int i, j; |
| 362 | int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 363 | PyTupleObject *np; |
| 364 | PyObject **p; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 365 | if (n < 0) |
| 366 | n = 0; |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 367 | if (a->ob_size == 0 || n == 1) { |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 368 | if (PyTuple_CheckExact(a)) { |
| 369 | /* Since tuples are immutable, we can return a shared |
| 370 | copy in this case */ |
| 371 | Py_INCREF(a); |
| 372 | return (PyObject *)a; |
| 373 | } |
| 374 | if (a->ob_size == 0) |
| 375 | return PyTuple_New(0); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 376 | } |
| 377 | size = a->ob_size * n; |
Guido van Rossum | 0eb55ac | 1999-07-13 05:41:12 +0000 | [diff] [blame] | 378 | if (size/a->ob_size != n) |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 379 | return PyErr_NoMemory(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 380 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 381 | if (np == NULL) |
| 382 | return NULL; |
| 383 | p = np->ob_item; |
| 384 | for (i = 0; i < n; i++) { |
| 385 | for (j = 0; j < a->ob_size; j++) { |
| 386 | *p = a->ob_item[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 387 | Py_INCREF(*p); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 388 | p++; |
| 389 | } |
| 390 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 391 | return (PyObject *) np; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 394 | static int |
| 395 | tupletraverse(PyTupleObject *o, visitproc visit, void *arg) |
| 396 | { |
| 397 | int i, err; |
| 398 | PyObject *x; |
| 399 | |
| 400 | for (i = o->ob_size; --i >= 0; ) { |
| 401 | x = o->ob_item[i]; |
| 402 | if (x != NULL) { |
| 403 | err = visit(x, arg); |
| 404 | if (err) |
| 405 | return err; |
| 406 | } |
| 407 | } |
| 408 | return 0; |
| 409 | } |
| 410 | |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 411 | static PyObject * |
| 412 | tuplerichcompare(PyObject *v, PyObject *w, int op) |
| 413 | { |
| 414 | PyTupleObject *vt, *wt; |
| 415 | int i; |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 416 | int vlen, wlen; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 417 | |
| 418 | if (!PyTuple_Check(v) || !PyTuple_Check(w)) { |
| 419 | Py_INCREF(Py_NotImplemented); |
| 420 | return Py_NotImplemented; |
| 421 | } |
| 422 | |
| 423 | vt = (PyTupleObject *)v; |
| 424 | wt = (PyTupleObject *)w; |
| 425 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 426 | vlen = vt->ob_size; |
| 427 | wlen = wt->ob_size; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 428 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 429 | /* Note: the corresponding code for lists has an "early out" test |
| 430 | * here when op is EQ or NE and the lengths differ. That pays there, |
| 431 | * but Tim was unable to find any real code where EQ/NE tuple |
| 432 | * compares don't have the same length, so testing for it here would |
| 433 | * have cost without benefit. |
| 434 | */ |
| 435 | |
| 436 | /* Search for the first index where items are different. |
| 437 | * Note that because tuples are immutable, it's safe to reuse |
| 438 | * vlen and wlen across the comparison calls. |
| 439 | */ |
| 440 | for (i = 0; i < vlen && i < wlen; i++) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 441 | int k = PyObject_RichCompareBool(vt->ob_item[i], |
| 442 | wt->ob_item[i], Py_EQ); |
| 443 | if (k < 0) |
| 444 | return NULL; |
| 445 | if (!k) |
| 446 | break; |
| 447 | } |
| 448 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 449 | if (i >= vlen || i >= wlen) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 450 | /* No more items to compare -- compare sizes */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 451 | int cmp; |
| 452 | PyObject *res; |
| 453 | switch (op) { |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 454 | case Py_LT: cmp = vlen < wlen; break; |
| 455 | case Py_LE: cmp = vlen <= wlen; break; |
| 456 | case Py_EQ: cmp = vlen == wlen; break; |
| 457 | case Py_NE: cmp = vlen != wlen; break; |
| 458 | case Py_GT: cmp = vlen > wlen; break; |
| 459 | case Py_GE: cmp = vlen >= wlen; break; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 460 | default: return NULL; /* cannot happen */ |
| 461 | } |
| 462 | if (cmp) |
| 463 | res = Py_True; |
| 464 | else |
| 465 | res = Py_False; |
| 466 | Py_INCREF(res); |
| 467 | return res; |
| 468 | } |
| 469 | |
| 470 | /* We have an item that differs -- shortcuts for EQ/NE */ |
| 471 | if (op == Py_EQ) { |
| 472 | Py_INCREF(Py_False); |
| 473 | return Py_False; |
| 474 | } |
| 475 | if (op == Py_NE) { |
| 476 | Py_INCREF(Py_True); |
| 477 | return Py_True; |
| 478 | } |
| 479 | |
| 480 | /* Compare the final item again using the proper operator */ |
| 481 | return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op); |
| 482 | } |
| 483 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 484 | staticforward PyObject * |
| 485 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 486 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 487 | static PyObject * |
| 488 | tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 489 | { |
| 490 | PyObject *arg = NULL; |
| 491 | static char *kwlist[] = {"sequence", 0}; |
| 492 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 493 | if (type != &PyTuple_Type) |
| 494 | return tuple_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 495 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg)) |
| 496 | return NULL; |
| 497 | |
| 498 | if (arg == NULL) |
| 499 | return PyTuple_New(0); |
| 500 | else |
| 501 | return PySequence_Tuple(arg); |
| 502 | } |
| 503 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 504 | static PyObject * |
| 505 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 506 | { |
| 507 | PyObject *tmp, *new, *item; |
| 508 | int i, n; |
| 509 | |
| 510 | assert(PyType_IsSubtype(type, &PyTuple_Type)); |
| 511 | tmp = tuple_new(&PyTuple_Type, args, kwds); |
| 512 | if (tmp == NULL) |
| 513 | return NULL; |
| 514 | assert(PyTuple_Check(tmp)); |
| 515 | new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
| 516 | if (new == NULL) |
| 517 | return NULL; |
| 518 | for (i = 0; i < n; i++) { |
| 519 | item = PyTuple_GET_ITEM(tmp, i); |
| 520 | Py_INCREF(item); |
| 521 | PyTuple_SET_ITEM(new, i, item); |
| 522 | } |
| 523 | Py_DECREF(tmp); |
| 524 | return new; |
| 525 | } |
| 526 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 527 | static char tuple_doc[] = |
Tim Peters | 1b8ca0d | 2001-09-02 06:42:25 +0000 | [diff] [blame] | 528 | "tuple() -> an empty tuple\n" |
| 529 | "tuple(sequence) -> tuple initialized from sequence's items\n" |
| 530 | "\n" |
| 531 | "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] | 532 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 533 | static PySequenceMethods tuple_as_sequence = { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 534 | (inquiry)tuplelength, /* sq_length */ |
| 535 | (binaryfunc)tupleconcat, /* sq_concat */ |
| 536 | (intargfunc)tuplerepeat, /* sq_repeat */ |
| 537 | (intargfunc)tupleitem, /* sq_item */ |
| 538 | (intintargfunc)tupleslice, /* sq_slice */ |
| 539 | 0, /* sq_ass_item */ |
| 540 | 0, /* sq_ass_slice */ |
| 541 | (objobjproc)tuplecontains, /* sq_contains */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 544 | PyTypeObject PyTuple_Type = { |
| 545 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 546 | 0, |
| 547 | "tuple", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 548 | sizeof(PyTupleObject) - sizeof(PyObject *), |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 549 | sizeof(PyObject *), |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 550 | (destructor)tupledealloc, /* tp_dealloc */ |
| 551 | (printfunc)tupleprint, /* tp_print */ |
| 552 | 0, /* tp_getattr */ |
| 553 | 0, /* tp_setattr */ |
| 554 | 0, /* tp_compare */ |
| 555 | (reprfunc)tuplerepr, /* tp_repr */ |
| 556 | 0, /* tp_as_number */ |
| 557 | &tuple_as_sequence, /* tp_as_sequence */ |
| 558 | 0, /* tp_as_mapping */ |
| 559 | (hashfunc)tuplehash, /* tp_hash */ |
| 560 | 0, /* tp_call */ |
| 561 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 562 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 563 | 0, /* tp_setattro */ |
| 564 | 0, /* tp_as_buffer */ |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 565 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 566 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 567 | tuple_doc, /* tp_doc */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 568 | (traverseproc)tupletraverse, /* tp_traverse */ |
| 569 | 0, /* tp_clear */ |
| 570 | tuplerichcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 571 | 0, /* tp_weaklistoffset */ |
| 572 | 0, /* tp_iter */ |
| 573 | 0, /* tp_iternext */ |
| 574 | 0, /* tp_methods */ |
| 575 | 0, /* tp_members */ |
| 576 | 0, /* tp_getset */ |
| 577 | 0, /* tp_base */ |
| 578 | 0, /* tp_dict */ |
| 579 | 0, /* tp_descr_get */ |
| 580 | 0, /* tp_descr_set */ |
| 581 | 0, /* tp_dictoffset */ |
| 582 | 0, /* tp_init */ |
| 583 | 0, /* tp_alloc */ |
| 584 | tuple_new, /* tp_new */ |
Neil Schemenauer | 626d774 | 2002-04-12 03:05:52 +0000 | [diff] [blame] | 585 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 586 | }; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 587 | |
| 588 | /* The following function breaks the notion that tuples are immutable: |
| 589 | it changes the size of a tuple. We get away with this only if there |
| 590 | 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] | 591 | as creating a new tuple object and destroying the old one, only more |
| 592 | 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] | 593 | known to some other part of the code. */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 594 | |
| 595 | int |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 596 | _PyTuple_Resize(PyObject **pv, int newsize) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 597 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 598 | register PyTupleObject *v; |
| 599 | register PyTupleObject *sv; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 600 | int i; |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 601 | int oldsize; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 602 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 603 | v = (PyTupleObject *) *pv; |
Guido van Rossum | 4b8c0f6 | 2001-08-30 18:31:30 +0000 | [diff] [blame] | 604 | if (v == NULL || v->ob_type != &PyTuple_Type || |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 605 | (v->ob_size != 0 && v->ob_refcnt != 1)) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 606 | *pv = 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 607 | Py_XDECREF(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 608 | PyErr_BadInternalCall(); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 609 | return -1; |
| 610 | } |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 611 | oldsize = v->ob_size; |
| 612 | if (oldsize == newsize) |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 613 | return 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 614 | |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 615 | if (oldsize == 0) { |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 616 | /* Empty tuples are often shared, so we should never |
| 617 | resize them in-place even if we do own the only |
| 618 | (current) reference */ |
| 619 | Py_DECREF(v); |
| 620 | *pv = PyTuple_New(newsize); |
Thomas Wouters | 0dcea59 | 2001-05-29 07:58:45 +0000 | [diff] [blame] | 621 | return *pv == NULL ? -1 : 0; |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 624 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 625 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 626 | --_Py_RefTotal; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 627 | #endif |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 628 | _PyObject_GC_UNTRACK(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 629 | _Py_ForgetReference((PyObject *) v); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 630 | /* DECREF items deleted by shrinkage */ |
| 631 | for (i = newsize; i < oldsize; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 632 | Py_XDECREF(v->ob_item[i]); |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 633 | v->ob_item[i] = NULL; |
| 634 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 635 | sv = PyObject_GC_Resize(PyTupleObject, v, newsize); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 636 | if (sv == NULL) { |
| 637 | *pv = NULL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 638 | PyObject_GC_Del(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 639 | return -1; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 640 | } |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 641 | _Py_NewReference((PyObject *) sv); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 642 | /* Zero out items added by growing */ |
| 643 | for (i = oldsize; i < newsize; i++) |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 644 | sv->ob_item[i] = NULL; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 645 | *pv = (PyObject *) sv; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 646 | _PyObject_GC_TRACK(sv); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 647 | return 0; |
| 648 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 649 | |
| 650 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 651 | PyTuple_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 652 | { |
| 653 | #if MAXSAVESIZE > 0 |
| 654 | int i; |
| 655 | |
| 656 | Py_XDECREF(free_tuples[0]); |
| 657 | free_tuples[0] = NULL; |
| 658 | |
| 659 | for (i = 1; i < MAXSAVESIZE; i++) { |
| 660 | PyTupleObject *p, *q; |
| 661 | p = free_tuples[i]; |
| 662 | free_tuples[i] = NULL; |
| 663 | while (p) { |
| 664 | q = p; |
| 665 | p = (PyTupleObject *)(p->ob_item[0]); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 666 | PyObject_GC_Del(q); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | #endif |
| 670 | } |