blob: eaf3c88b1d4770ddc16b788b17e40c40db826fad [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Tuple object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum5ce78f82000-04-21 21:15:05 +00006/* Speed optimization to avoid frequent malloc/free of small tuples */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +00007#ifndef MAXSAVESIZE
Guido van Rossum5ce78f82000-04-21 21:15:05 +00008#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 Mullender842d2cc1993-10-15 16:18:48 +000012#endif
13
14#if MAXSAVESIZE > 0
Guido van Rossum5ce78f82000-04-21 21:15:05 +000015/* Entries 1 up to MAXSAVESIZE are free lists, entry 0 is the empty
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000016 tuple () of which at most one instance will be allocated.
17*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000018static PyTupleObject *free_tuples[MAXSAVESIZE];
Guido van Rossum5ce78f82000-04-21 21:15:05 +000019static int num_free_tuples[MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000020#endif
21#ifdef COUNT_ALLOCS
22int fast_tuple_allocs;
23int tuple_zero_allocs;
24#endif
25
Guido van Rossumc0b618a1997-05-02 03:12:38 +000026PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000027PyTuple_New(register int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 register PyTupleObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032 return NULL;
33 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000034#if MAXSAVESIZE > 0
Sjoerd Mullender615194a1993-11-01 13:46:50 +000035 if (size == 0 && free_tuples[0]) {
36 op = free_tuples[0];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000037 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000038#ifdef COUNT_ALLOCS
39 tuple_zero_allocs++;
40#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000041 return (PyObject *) op;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000042 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043 if (0 < size && size < MAXSAVESIZE &&
44 (op = free_tuples[size]) != NULL)
45 {
46 free_tuples[size] = (PyTupleObject *) op->ob_item[0];
Guido van Rossum5ce78f82000-04-21 21:15:05 +000047 num_free_tuples[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000048#ifdef COUNT_ALLOCS
49 fast_tuple_allocs++;
50#endif
Guido van Rossume3a8e7e2002-08-19 19:26:42 +000051 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000052#ifdef Py_TRACE_REFS
Guido van Rossum68055ce1998-12-11 14:56:38 +000053 op->ob_size = size;
Guido van Rossumb18618d2000-05-03 23:44:39 +000054 op->ob_type = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000055#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +000056 _Py_NewReference((PyObject *)op);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +000057 }
58 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000059#endif
60 {
Guido van Rossum5bc51f21999-07-12 23:06:58 +000061 int nbytes = size * sizeof(PyObject *);
62 /* Check for overflow */
63 if (nbytes / sizeof(PyObject *) != (size_t)size ||
Neil Schemenauere83c00e2001-08-29 23:54:21 +000064 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *))
Guido van Rossum5bc51f21999-07-12 23:06:58 +000065 <= 0)
66 {
67 return PyErr_NoMemory();
68 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +000069 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000070 if (op == NULL)
Neil Schemenauere83c00e2001-08-29 23:54:21 +000071 return NULL;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000072 }
Neal Norwitz35fc7602002-06-13 21:11:11 +000073 memset(op->ob_item, 0, sizeof(*op->ob_item) * size);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000074#if MAXSAVESIZE > 0
75 if (size == 0) {
Sjoerd Mullender615194a1993-11-01 13:46:50 +000076 free_tuples[0] = op;
Guido van Rossum5ce78f82000-04-21 21:15:05 +000077 ++num_free_tuples[0];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 Py_INCREF(op); /* extra INCREF so that this is never freed */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000079 }
80#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +000081 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
85int
Fred Drakeba096332000-07-09 07:04:36 +000086PyTuple_Size(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 if (!PyTuple_Check(op)) {
89 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 return -1;
91 }
92 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093 return ((PyTupleObject *)op)->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094}
95
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000097PyTuple_GetItem(register PyObject *op, register int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 if (!PyTuple_Check(op)) {
100 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 return NULL;
102 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
104 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105 return NULL;
106 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108}
109
110int
Fred Drakeba096332000-07-09 07:04:36 +0000111PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 register PyObject *olditem;
114 register PyObject **p;
Guido van Rossum787bdd31997-08-17 16:25:45 +0000115 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000116 Py_XDECREF(newitem);
117 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000118 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
121 Py_XDECREF(newitem);
122 PyErr_SetString(PyExc_IndexError,
123 "tuple assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000124 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126 p = ((PyTupleObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000127 olditem = *p;
128 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130 return 0;
131}
132
Raymond Hettingercb2da432003-10-12 18:24:34 +0000133PyObject *
134PyTuple_Pack(int n, ...)
135{
136 int i;
137 PyObject *o;
138 PyObject *result;
139 va_list vargs;
140
141 va_start(vargs, n);
142 result = PyTuple_New(n);
143 if (result == NULL)
144 return NULL;
145 for (i = 0; i < n; i++) {
146 o = va_arg(vargs, PyObject *);
147 Py_INCREF(o);
148 PyTuple_SET_ITEM(result, i, o);
149 }
150 va_end(vargs);
151 return result;
152}
153
154
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155/* Methods */
156
157static void
Fred Drakeba096332000-07-09 07:04:36 +0000158tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159{
160 register int i;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000161 register int len = op->ob_size;
Guido van Rossumff413af2002-03-28 20:34:59 +0000162 PyObject_GC_UnTrack(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000163 Py_TRASHCAN_SAFE_BEGIN(op)
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000164 if (len > 0) {
165 i = len;
Guido van Rossum1bb26871998-06-26 15:53:50 +0000166 while (--i >= 0)
167 Py_XDECREF(op->ob_item[i]);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000168#if MAXSAVESIZE > 0
Guido van Rossum4b8c0f62001-08-30 18:31:30 +0000169 if (len < MAXSAVESIZE &&
170 num_free_tuples[len] < MAXSAVEDTUPLES &&
171 op->ob_type == &PyTuple_Type)
172 {
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000173 op->ob_item[0] = (PyObject *) free_tuples[len];
174 num_free_tuples[len]++;
175 free_tuples[len] = op;
Guido van Rossumd724b232000-03-13 16:01:29 +0000176 goto done; /* return */
Guido van Rossum1bb26871998-06-26 15:53:50 +0000177 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000178#endif
Guido van Rossum1bb26871998-06-26 15:53:50 +0000179 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000180 op->ob_type->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000181done:
182 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183}
184
Guido van Rossum49e85141991-06-07 22:59:30 +0000185static int
Fred Drakeba096332000-07-09 07:04:36 +0000186tupleprint(PyTupleObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
188 int i;
189 fprintf(fp, "(");
Guido van Rossum49e85141991-06-07 22:59:30 +0000190 for (i = 0; i < op->ob_size; i++) {
191 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192 fprintf(fp, ", ");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
Guido van Rossum49e85141991-06-07 22:59:30 +0000194 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195 }
196 if (op->ob_size == 1)
197 fprintf(fp, ",");
198 fprintf(fp, ")");
Guido van Rossum49e85141991-06-07 22:59:30 +0000199 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200}
201
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000203tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204{
Tim Petersa7259592001-06-16 05:11:17 +0000205 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 Rossum85a5fbb1990-10-14 12:07:46 +0000223 }
Tim Petersa7259592001-06-16 05:11:17 +0000224
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
252Done:
253 Py_DECREF(pieces);
254 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255}
256
Guido van Rossum9bfef441993-03-29 10:43:31 +0000257static long
Fred Drakeba096332000-07-09 07:04:36 +0000258tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000259{
260 register long x, y;
261 register int len = v->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 register PyObject **p;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000263 x = 0x345678L;
264 p = v->ob_item;
265 while (--len >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266 y = PyObject_Hash(*p++);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267 if (y == -1)
268 return -1;
Guido van Rossum0969ad21996-12-16 17:55:46 +0000269 x = (1000003*x) ^ y;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270 }
271 x ^= v->ob_size;
272 if (x == -1)
273 x = -2;
274 return x;
275}
276
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277static int
Fred Drakeba096332000-07-09 07:04:36 +0000278tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279{
280 return a->ob_size;
281}
282
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000283static int
Fred Drakeba096332000-07-09 07:04:36 +0000284tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000285{
286 int i, cmp;
287
Raymond Hettingerbd9adab2002-09-05 20:18:08 +0000288 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
Guido van Rossumf77bc622001-01-18 00:00:53 +0000289 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
Raymond Hettingerbd9adab2002-09-05 20:18:08 +0000290 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000291 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000292}
293
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000295tupleitem(register PyTupleObject *a, register int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296{
297 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000298 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 return NULL;
300 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000301 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302 return a->ob_item[i];
303}
304
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000305static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000306tupleslice(register PyTupleObject *a, register int ilow, register int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 register PyTupleObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000309 PyObject **src, **dest;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310 register int i;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000311 int len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312 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 Peters7b07a412001-09-11 19:48:03 +0000318 if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319 Py_INCREF(a);
320 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321 }
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000322 len = ihigh - ilow;
323 np = (PyTupleObject *)PyTuple_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324 if (np == NULL)
325 return NULL;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000326 src = a->ob_item + ilow;
327 dest = np->ob_item;
328 for (i = 0; i < len; i++) {
329 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000331 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334}
335
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000337PyTuple_GetSlice(PyObject *op, int i, int j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000338{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 if (op == NULL || !PyTuple_Check(op)) {
340 PyErr_BadInternalCall();
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000341 return NULL;
342 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000344}
345
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000347tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348{
349 register int size;
350 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351 PyTupleObject *np;
352 if (!PyTuple_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000353 PyErr_Format(PyExc_TypeError,
Marc-André Lemburg29dc3812000-06-16 17:05:57 +0000354 "can only concatenate tuple (not \"%.200s\") to tuple",
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000355 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356 return NULL;
357 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358#define b ((PyTupleObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359 size = a->ob_size + b->ob_size;
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000360 if (size < 0)
361 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363 if (np == NULL) {
Guido van Rossum49e85141991-06-07 22:59:30 +0000364 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 }
366 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 PyObject *v = a->ob_item[i];
368 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 np->ob_item[i] = v;
370 }
371 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372 PyObject *v = b->ob_item[i];
373 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374 np->ob_item[i + a->ob_size] = v;
375 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377#undef b
378}
379
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000381tuplerepeat(PyTupleObject *a, int n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000382{
383 int i, j;
384 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000385 PyTupleObject *np;
386 PyObject **p;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000387 if (n < 0)
388 n = 0;
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000389 if (a->ob_size == 0 || n == 1) {
Tim Peters7b07a412001-09-11 19:48:03 +0000390 if (PyTuple_CheckExact(a)) {
391 /* Since tuples are immutable, we can return a shared
392 copy in this case */
393 Py_INCREF(a);
394 return (PyObject *)a;
395 }
396 if (a->ob_size == 0)
397 return PyTuple_New(0);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000398 }
399 size = a->ob_size * n;
Guido van Rossum0eb55ac1999-07-13 05:41:12 +0000400 if (size/a->ob_size != n)
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000401 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000403 if (np == NULL)
404 return NULL;
405 p = np->ob_item;
406 for (i = 0; i < n; i++) {
407 for (j = 0; j < a->ob_size; j++) {
408 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409 Py_INCREF(*p);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000410 p++;
411 }
412 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000414}
415
Jeremy Hylton8caad492000-06-23 14:18:11 +0000416static int
417tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
418{
419 int i, err;
420 PyObject *x;
421
422 for (i = o->ob_size; --i >= 0; ) {
423 x = o->ob_item[i];
424 if (x != NULL) {
425 err = visit(x, arg);
426 if (err)
427 return err;
428 }
429 }
430 return 0;
431}
432
Guido van Rossumf77bc622001-01-18 00:00:53 +0000433static PyObject *
434tuplerichcompare(PyObject *v, PyObject *w, int op)
435{
436 PyTupleObject *vt, *wt;
437 int i;
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000438 int vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000439
440 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
441 Py_INCREF(Py_NotImplemented);
442 return Py_NotImplemented;
443 }
444
445 vt = (PyTupleObject *)v;
446 wt = (PyTupleObject *)w;
447
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000448 vlen = vt->ob_size;
449 wlen = wt->ob_size;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000450
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000451 /* Note: the corresponding code for lists has an "early out" test
452 * here when op is EQ or NE and the lengths differ. That pays there,
453 * but Tim was unable to find any real code where EQ/NE tuple
454 * compares don't have the same length, so testing for it here would
455 * have cost without benefit.
456 */
457
458 /* Search for the first index where items are different.
459 * Note that because tuples are immutable, it's safe to reuse
460 * vlen and wlen across the comparison calls.
461 */
462 for (i = 0; i < vlen && i < wlen; i++) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000463 int k = PyObject_RichCompareBool(vt->ob_item[i],
464 wt->ob_item[i], Py_EQ);
465 if (k < 0)
466 return NULL;
467 if (!k)
468 break;
469 }
470
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000471 if (i >= vlen || i >= wlen) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000472 /* No more items to compare -- compare sizes */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000473 int cmp;
474 PyObject *res;
475 switch (op) {
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000476 case Py_LT: cmp = vlen < wlen; break;
477 case Py_LE: cmp = vlen <= wlen; break;
478 case Py_EQ: cmp = vlen == wlen; break;
479 case Py_NE: cmp = vlen != wlen; break;
480 case Py_GT: cmp = vlen > wlen; break;
481 case Py_GE: cmp = vlen >= wlen; break;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000482 default: return NULL; /* cannot happen */
483 }
484 if (cmp)
485 res = Py_True;
486 else
487 res = Py_False;
488 Py_INCREF(res);
489 return res;
490 }
491
492 /* We have an item that differs -- shortcuts for EQ/NE */
493 if (op == Py_EQ) {
494 Py_INCREF(Py_False);
495 return Py_False;
496 }
497 if (op == Py_NE) {
498 Py_INCREF(Py_True);
499 return Py_True;
500 }
501
502 /* Compare the final item again using the proper operator */
503 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
504}
505
Jeremy Hylton938ace62002-07-17 16:30:39 +0000506static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000507tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
508
Tim Peters6d6c1a32001-08-02 04:15:00 +0000509static PyObject *
510tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
511{
512 PyObject *arg = NULL;
513 static char *kwlist[] = {"sequence", 0};
514
Guido van Rossumae960af2001-08-30 03:11:59 +0000515 if (type != &PyTuple_Type)
516 return tuple_subtype_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000517 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
518 return NULL;
519
520 if (arg == NULL)
521 return PyTuple_New(0);
522 else
523 return PySequence_Tuple(arg);
524}
525
Guido van Rossumae960af2001-08-30 03:11:59 +0000526static PyObject *
527tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
528{
529 PyObject *tmp, *new, *item;
530 int i, n;
531
532 assert(PyType_IsSubtype(type, &PyTuple_Type));
533 tmp = tuple_new(&PyTuple_Type, args, kwds);
534 if (tmp == NULL)
535 return NULL;
536 assert(PyTuple_Check(tmp));
537 new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
538 if (new == NULL)
539 return NULL;
540 for (i = 0; i < n; i++) {
541 item = PyTuple_GET_ITEM(tmp, i);
542 Py_INCREF(item);
543 PyTuple_SET_ITEM(new, i, item);
544 }
545 Py_DECREF(tmp);
546 return new;
547}
548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000549PyDoc_STRVAR(tuple_doc,
Tim Peters1b8ca0d2001-09-02 06:42:25 +0000550"tuple() -> an empty tuple\n"
551"tuple(sequence) -> tuple initialized from sequence's items\n"
552"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000553"If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000554
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555static PySequenceMethods tuple_as_sequence = {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000556 (inquiry)tuplelength, /* sq_length */
557 (binaryfunc)tupleconcat, /* sq_concat */
558 (intargfunc)tuplerepeat, /* sq_repeat */
559 (intargfunc)tupleitem, /* sq_item */
560 (intintargfunc)tupleslice, /* sq_slice */
561 0, /* sq_ass_item */
562 0, /* sq_ass_slice */
563 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564};
565
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000566static PyObject*
567tuplesubscript(PyTupleObject* self, PyObject* item)
568{
569 if (PyInt_Check(item)) {
570 long i = PyInt_AS_LONG(item);
571 if (i < 0)
572 i += PyTuple_GET_SIZE(self);
573 return tupleitem(self, i);
574 }
575 else if (PyLong_Check(item)) {
576 long i = PyLong_AsLong(item);
577 if (i == -1 && PyErr_Occurred())
578 return NULL;
579 if (i < 0)
580 i += PyTuple_GET_SIZE(self);
581 return tupleitem(self, i);
582 }
583 else if (PySlice_Check(item)) {
584 int start, stop, step, slicelength, cur, i;
585 PyObject* result;
586 PyObject* it;
587
588 if (PySlice_GetIndicesEx((PySliceObject*)item,
589 PyTuple_GET_SIZE(self),
590 &start, &stop, &step, &slicelength) < 0) {
591 return NULL;
592 }
593
594 if (slicelength <= 0) {
595 return PyTuple_New(0);
596 }
597 else {
598 result = PyTuple_New(slicelength);
599
600 for (cur = start, i = 0; i < slicelength;
601 cur += step, i++) {
602 it = PyTuple_GET_ITEM(self, cur);
603 Py_INCREF(it);
604 PyTuple_SET_ITEM(result, i, it);
605 }
606
607 return result;
608 }
609 }
610 else {
611 PyErr_SetString(PyExc_TypeError,
612 "tuple indices must be integers");
613 return NULL;
614 }
615}
616
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000617static PyObject *
618tuple_getnewargs(PyTupleObject *v)
619{
620 return Py_BuildValue("(N)", tupleslice(v, 0, v->ob_size));
621
622}
623
624static PyMethodDef tuple_methods[] = {
625 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
626 {NULL, NULL} /* sentinel */
627};
628
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000629static PyMappingMethods tuple_as_mapping = {
630 (inquiry)tuplelength,
631 (binaryfunc)tuplesubscript,
632 0
633};
634
Raymond Hettinger48923c52002-08-09 01:30:17 +0000635static PyObject *tuple_iter(PyObject *seq);
636
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637PyTypeObject PyTuple_Type = {
638 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639 0,
640 "tuple",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000641 sizeof(PyTupleObject) - sizeof(PyObject *),
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642 sizeof(PyObject *),
Guido van Rossumf77bc622001-01-18 00:00:53 +0000643 (destructor)tupledealloc, /* tp_dealloc */
644 (printfunc)tupleprint, /* tp_print */
645 0, /* tp_getattr */
646 0, /* tp_setattr */
647 0, /* tp_compare */
648 (reprfunc)tuplerepr, /* tp_repr */
649 0, /* tp_as_number */
650 &tuple_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000651 &tuple_as_mapping, /* tp_as_mapping */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000652 (hashfunc)tuplehash, /* tp_hash */
653 0, /* tp_call */
654 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000655 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000656 0, /* tp_setattro */
657 0, /* tp_as_buffer */
Guido van Rossumae960af2001-08-30 03:11:59 +0000658 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
659 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000660 tuple_doc, /* tp_doc */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000661 (traverseproc)tupletraverse, /* tp_traverse */
662 0, /* tp_clear */
663 tuplerichcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000664 0, /* tp_weaklistoffset */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000665 tuple_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000667 tuple_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668 0, /* tp_members */
669 0, /* tp_getset */
670 0, /* tp_base */
671 0, /* tp_dict */
672 0, /* tp_descr_get */
673 0, /* tp_descr_set */
674 0, /* tp_dictoffset */
675 0, /* tp_init */
676 0, /* tp_alloc */
677 tuple_new, /* tp_new */
Neil Schemenauer626d7742002-04-12 03:05:52 +0000678 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000680
681/* The following function breaks the notion that tuples are immutable:
682 it changes the size of a tuple. We get away with this only if there
683 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000684 as creating a new tuple object and destroying the old one, only more
685 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000686 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000687
688int
Tim Peters4324aa32001-05-28 22:30:08 +0000689_PyTuple_Resize(PyObject **pv, int newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000690{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 register PyTupleObject *v;
692 register PyTupleObject *sv;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000693 int i;
Guido van Rossumf70590f2001-12-07 20:00:04 +0000694 int oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000695
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 v = (PyTupleObject *) *pv;
Guido van Rossum4b8c0f62001-08-30 18:31:30 +0000697 if (v == NULL || v->ob_type != &PyTuple_Type ||
Thomas Wouters6a922372001-05-28 13:11:02 +0000698 (v->ob_size != 0 && v->ob_refcnt != 1)) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000699 *pv = 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000700 Py_XDECREF(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701 PyErr_BadInternalCall();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000702 return -1;
703 }
Guido van Rossumf70590f2001-12-07 20:00:04 +0000704 oldsize = v->ob_size;
705 if (oldsize == newsize)
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000706 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000707
Guido van Rossumf70590f2001-12-07 20:00:04 +0000708 if (oldsize == 0) {
Thomas Wouters6a922372001-05-28 13:11:02 +0000709 /* Empty tuples are often shared, so we should never
710 resize them in-place even if we do own the only
711 (current) reference */
712 Py_DECREF(v);
713 *pv = PyTuple_New(newsize);
Thomas Wouters0dcea592001-05-29 07:58:45 +0000714 return *pv == NULL ? -1 : 0;
Thomas Wouters6a922372001-05-28 13:11:02 +0000715 }
716
Guido van Rossum12d12c51993-10-26 17:58:25 +0000717 /* XXX UNREF/NEWREF interface should be more symmetrical */
Tim Peters34592512002-07-11 06:23:50 +0000718 _Py_DEC_REFTOTAL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000719 _PyObject_GC_UNTRACK(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000720 _Py_ForgetReference((PyObject *) v);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000721 /* DECREF items deleted by shrinkage */
722 for (i = newsize; i < oldsize; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 Py_XDECREF(v->ob_item[i]);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000724 v->ob_item[i] = NULL;
725 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000726 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000727 if (sv == NULL) {
728 *pv = NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000729 PyObject_GC_Del(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000730 return -1;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000731 }
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000732 _Py_NewReference((PyObject *) sv);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000733 /* Zero out items added by growing */
Neal Norwitz35fc7602002-06-13 21:11:11 +0000734 if (newsize > oldsize)
Jeremy Hylton8b47dff2002-06-20 23:13:17 +0000735 memset(&sv->ob_item[oldsize], 0,
736 sizeof(*sv->ob_item) * (newsize - oldsize));
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000737 *pv = (PyObject *) sv;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000738 _PyObject_GC_TRACK(sv);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000739 return 0;
740}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000741
742void
Fred Drakeba096332000-07-09 07:04:36 +0000743PyTuple_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000744{
745#if MAXSAVESIZE > 0
746 int i;
747
748 Py_XDECREF(free_tuples[0]);
749 free_tuples[0] = NULL;
750
751 for (i = 1; i < MAXSAVESIZE; i++) {
752 PyTupleObject *p, *q;
753 p = free_tuples[i];
754 free_tuples[i] = NULL;
755 while (p) {
756 q = p;
757 p = (PyTupleObject *)(p->ob_item[0]);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000758 PyObject_GC_Del(q);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000759 }
760 }
761#endif
762}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000763
764/*********************** Tuple Iterator **************************/
765
766typedef struct {
767 PyObject_HEAD
768 long it_index;
769 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
770} tupleiterobject;
771
772PyTypeObject PyTupleIter_Type;
773
774static PyObject *
775tuple_iter(PyObject *seq)
776{
777 tupleiterobject *it;
778
779 if (!PyTuple_Check(seq)) {
780 PyErr_BadInternalCall();
781 return NULL;
782 }
783 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
784 if (it == NULL)
785 return NULL;
786 it->it_index = 0;
787 Py_INCREF(seq);
788 it->it_seq = (PyTupleObject *)seq;
789 _PyObject_GC_TRACK(it);
790 return (PyObject *)it;
791}
792
793static void
794tupleiter_dealloc(tupleiterobject *it)
795{
796 _PyObject_GC_UNTRACK(it);
797 Py_XDECREF(it->it_seq);
798 PyObject_GC_Del(it);
799}
800
801static int
802tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
803{
804 if (it->it_seq == NULL)
805 return 0;
806 return visit((PyObject *)it->it_seq, arg);
807}
808
Raymond Hettinger48923c52002-08-09 01:30:17 +0000809static PyObject *
810tupleiter_next(tupleiterobject *it)
811{
812 PyTupleObject *seq;
813 PyObject *item;
814
815 assert(it != NULL);
816 seq = it->it_seq;
817 if (seq == NULL)
818 return NULL;
819 assert(PyTuple_Check(seq));
820
821 if (it->it_index < PyTuple_GET_SIZE(seq)) {
822 item = PyTuple_GET_ITEM(seq, it->it_index);
823 ++it->it_index;
824 Py_INCREF(item);
825 return item;
826 }
827
828 Py_DECREF(seq);
829 it->it_seq = NULL;
830 return NULL;
831}
832
833PyTypeObject PyTupleIter_Type = {
834 PyObject_HEAD_INIT(&PyType_Type)
835 0, /* ob_size */
836 "tupleiterator", /* tp_name */
837 sizeof(tupleiterobject), /* tp_basicsize */
838 0, /* tp_itemsize */
839 /* methods */
840 (destructor)tupleiter_dealloc, /* tp_dealloc */
841 0, /* tp_print */
842 0, /* tp_getattr */
843 0, /* tp_setattr */
844 0, /* tp_compare */
845 0, /* tp_repr */
846 0, /* tp_as_number */
847 0, /* tp_as_sequence */
848 0, /* tp_as_mapping */
849 0, /* tp_hash */
850 0, /* tp_call */
851 0, /* tp_str */
852 PyObject_GenericGetAttr, /* tp_getattro */
853 0, /* tp_setattro */
854 0, /* tp_as_buffer */
855 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
856 0, /* tp_doc */
857 (traverseproc)tupleiter_traverse, /* tp_traverse */
858 0, /* tp_clear */
859 0, /* tp_richcompare */
860 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000861 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000862 (iternextfunc)tupleiter_next, /* tp_iternext */
863};