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