blob: ea7d75302238d5e81eea50e781885bce8c462e6c [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;
Armin Rigo6fce78e2004-03-21 22:29:05 +000030 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000032 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033 return NULL;
34 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000035#if MAXSAVESIZE > 0
Sjoerd Mullender615194a1993-11-01 13:46:50 +000036 if (size == 0 && free_tuples[0]) {
37 op = free_tuples[0];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000039#ifdef COUNT_ALLOCS
40 tuple_zero_allocs++;
41#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042 return (PyObject *) op;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000043 }
Raymond Hettinger325d1692004-03-15 00:16:34 +000044 if (size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045 free_tuples[size] = (PyTupleObject *) op->ob_item[0];
Guido van Rossum5ce78f82000-04-21 21:15:05 +000046 num_free_tuples[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000047#ifdef COUNT_ALLOCS
48 fast_tuple_allocs++;
49#endif
Guido van Rossume3a8e7e2002-08-19 19:26:42 +000050 /* Inline PyObject_InitVar */
Guido van Rossum68055ce1998-12-11 14:56:38 +000051#ifdef Py_TRACE_REFS
Guido van Rossum68055ce1998-12-11 14:56:38 +000052 op->ob_size = size;
Guido van Rossumb18618d2000-05-03 23:44:39 +000053 op->ob_type = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000054#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +000055 _Py_NewReference((PyObject *)op);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +000056 }
57 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000058#endif
59 {
Guido van Rossum5bc51f21999-07-12 23:06:58 +000060 int nbytes = size * sizeof(PyObject *);
61 /* Check for overflow */
62 if (nbytes / sizeof(PyObject *) != (size_t)size ||
Neil Schemenauere83c00e2001-08-29 23:54:21 +000063 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *))
Guido van Rossum5bc51f21999-07-12 23:06:58 +000064 <= 0)
65 {
66 return PyErr_NoMemory();
67 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +000068 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069 if (op == NULL)
Neil Schemenauere83c00e2001-08-29 23:54:21 +000070 return NULL;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000071 }
Armin Rigo6fce78e2004-03-21 22:29:05 +000072 for (i=0; i < size; i++)
73 op->ob_item[i] = NULL;
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;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000139 PyObject **items;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000140 va_list vargs;
141
142 va_start(vargs, n);
143 result = PyTuple_New(n);
144 if (result == NULL)
145 return NULL;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000146 items = ((PyTupleObject *)result)->ob_item;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000147 for (i = 0; i < n; i++) {
148 o = va_arg(vargs, PyObject *);
149 Py_INCREF(o);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000150 items[i] = o;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000151 }
152 va_end(vargs);
153 return result;
154}
155
156
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157/* Methods */
158
159static void
Fred Drakeba096332000-07-09 07:04:36 +0000160tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161{
162 register int i;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000163 register int len = op->ob_size;
Guido van Rossumff413af2002-03-28 20:34:59 +0000164 PyObject_GC_UnTrack(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000165 Py_TRASHCAN_SAFE_BEGIN(op)
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000166 if (len > 0) {
167 i = len;
Armin Rigo6fce78e2004-03-21 22:29:05 +0000168 while (--i >= 0)
169 Py_XDECREF(op->ob_item[i]);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000170#if MAXSAVESIZE > 0
Guido van Rossum4b8c0f62001-08-30 18:31:30 +0000171 if (len < MAXSAVESIZE &&
172 num_free_tuples[len] < MAXSAVEDTUPLES &&
173 op->ob_type == &PyTuple_Type)
174 {
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000175 op->ob_item[0] = (PyObject *) free_tuples[len];
176 num_free_tuples[len]++;
177 free_tuples[len] = op;
Guido van Rossumd724b232000-03-13 16:01:29 +0000178 goto done; /* return */
Guido van Rossum1bb26871998-06-26 15:53:50 +0000179 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000180#endif
Guido van Rossum1bb26871998-06-26 15:53:50 +0000181 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000182 op->ob_type->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000183done:
184 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185}
186
Guido van Rossum49e85141991-06-07 22:59:30 +0000187static int
Fred Drakeba096332000-07-09 07:04:36 +0000188tupleprint(PyTupleObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
190 int i;
191 fprintf(fp, "(");
Guido van Rossum49e85141991-06-07 22:59:30 +0000192 for (i = 0; i < op->ob_size; i++) {
193 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194 fprintf(fp, ", ");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
Guido van Rossum49e85141991-06-07 22:59:30 +0000196 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 }
198 if (op->ob_size == 1)
199 fprintf(fp, ",");
200 fprintf(fp, ")");
Guido van Rossum49e85141991-06-07 22:59:30 +0000201 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202}
203
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000205tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206{
Tim Petersa7259592001-06-16 05:11:17 +0000207 int i, n;
208 PyObject *s, *temp;
209 PyObject *pieces, *result = NULL;
210
211 n = v->ob_size;
212 if (n == 0)
213 return PyString_FromString("()");
214
215 pieces = PyTuple_New(n);
216 if (pieces == NULL)
217 return NULL;
218
219 /* Do repr() on each element. */
220 for (i = 0; i < n; ++i) {
221 s = PyObject_Repr(v->ob_item[i]);
222 if (s == NULL)
223 goto Done;
224 PyTuple_SET_ITEM(pieces, i, s);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225 }
Tim Petersa7259592001-06-16 05:11:17 +0000226
227 /* Add "()" decorations to the first and last items. */
228 assert(n > 0);
229 s = PyString_FromString("(");
230 if (s == NULL)
231 goto Done;
232 temp = PyTuple_GET_ITEM(pieces, 0);
233 PyString_ConcatAndDel(&s, temp);
234 PyTuple_SET_ITEM(pieces, 0, s);
235 if (s == NULL)
236 goto Done;
237
238 s = PyString_FromString(n == 1 ? ",)" : ")");
239 if (s == NULL)
240 goto Done;
241 temp = PyTuple_GET_ITEM(pieces, n-1);
242 PyString_ConcatAndDel(&temp, s);
243 PyTuple_SET_ITEM(pieces, n-1, temp);
244 if (temp == NULL)
245 goto Done;
246
247 /* Paste them all together with ", " between. */
248 s = PyString_FromString(", ");
249 if (s == NULL)
250 goto Done;
251 result = _PyString_Join(s, pieces);
252 Py_DECREF(s);
253
254Done:
255 Py_DECREF(pieces);
256 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257}
258
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000259/* The addend 82520, was selected from the range(0, 1000000) for
260 generating the greatest number of prime multipliers for tuples
261 upto length eight:
262
263 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
264 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
265*/
266
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267static long
Fred Drakeba096332000-07-09 07:04:36 +0000268tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000269{
270 register long x, y;
271 register int len = v->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000272 register PyObject **p;
Raymond Hettinger41bd0222004-06-01 06:36:24 +0000273 long mult = 1000003L;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000274 x = 0x345678L;
275 p = v->ob_item;
276 while (--len >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277 y = PyObject_Hash(*p++);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000278 if (y == -1)
279 return -1;
Raymond Hettinger41bd0222004-06-01 06:36:24 +0000280 x = (x ^ y) * mult;
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000281 mult += 82520L + len + len;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000282 }
Raymond Hettinger57c2d932004-06-10 18:42:15 +0000283 x += 97531L;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000284 if (x == -1)
285 x = -2;
286 return x;
287}
288
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289static int
Fred Drakeba096332000-07-09 07:04:36 +0000290tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291{
292 return a->ob_size;
293}
294
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000295static int
Fred Drakeba096332000-07-09 07:04:36 +0000296tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000297{
298 int i, cmp;
299
Raymond Hettingerbd9adab2002-09-05 20:18:08 +0000300 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
Guido van Rossumf77bc622001-01-18 00:00:53 +0000301 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
Raymond Hettingerbd9adab2002-09-05 20:18:08 +0000302 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000303 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000304}
305
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000306static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000307tupleitem(register PyTupleObject *a, register int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308{
309 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000310 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 return NULL;
312 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314 return a->ob_item[i];
315}
316
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000317static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000318tupleslice(register PyTupleObject *a, register int ilow, register int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 register PyTupleObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000321 PyObject **src, **dest;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 register int i;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000323 int len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324 if (ilow < 0)
325 ilow = 0;
326 if (ihigh > a->ob_size)
327 ihigh = a->ob_size;
328 if (ihigh < ilow)
329 ihigh = ilow;
Tim Peters7b07a412001-09-11 19:48:03 +0000330 if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 Py_INCREF(a);
332 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333 }
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000334 len = ihigh - ilow;
335 np = (PyTupleObject *)PyTuple_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 if (np == NULL)
337 return NULL;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000338 src = a->ob_item + ilow;
339 dest = np->ob_item;
340 for (i = 0; i < len; i++) {
341 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000343 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000345 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346}
347
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000349PyTuple_GetSlice(PyObject *op, int i, int j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000350{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351 if (op == NULL || !PyTuple_Check(op)) {
352 PyErr_BadInternalCall();
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000353 return NULL;
354 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000356}
357
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000359tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360{
361 register int size;
362 register int i;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000363 PyObject **src, **dest;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 PyTupleObject *np;
365 if (!PyTuple_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000366 PyErr_Format(PyExc_TypeError,
Marc-André Lemburg29dc3812000-06-16 17:05:57 +0000367 "can only concatenate tuple (not \"%.200s\") to tuple",
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000368 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 return NULL;
370 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371#define b ((PyTupleObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372 size = a->ob_size + b->ob_size;
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000373 if (size < 0)
374 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376 if (np == NULL) {
Guido van Rossum49e85141991-06-07 22:59:30 +0000377 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000379 src = a->ob_item;
380 dest = np->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 for (i = 0; i < a->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000382 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000384 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000386 src = b->ob_item;
387 dest = np->ob_item + a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388 for (i = 0; i < b->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000389 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000391 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394#undef b
395}
396
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000398tuplerepeat(PyTupleObject *a, int n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000399{
400 int i, j;
401 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 PyTupleObject *np;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000403 PyObject **p, **items;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000404 if (n < 0)
405 n = 0;
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000406 if (a->ob_size == 0 || n == 1) {
Tim Peters7b07a412001-09-11 19:48:03 +0000407 if (PyTuple_CheckExact(a)) {
408 /* Since tuples are immutable, we can return a shared
409 copy in this case */
410 Py_INCREF(a);
411 return (PyObject *)a;
412 }
413 if (a->ob_size == 0)
414 return PyTuple_New(0);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000415 }
416 size = a->ob_size * n;
Guido van Rossum0eb55ac1999-07-13 05:41:12 +0000417 if (size/a->ob_size != n)
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000418 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000420 if (np == NULL)
421 return NULL;
422 p = np->ob_item;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000423 items = a->ob_item;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000424 for (i = 0; i < n; i++) {
425 for (j = 0; j < a->ob_size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000426 *p = items[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 Py_INCREF(*p);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000428 p++;
429 }
430 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000432}
433
Jeremy Hylton8caad492000-06-23 14:18:11 +0000434static int
435tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
436{
437 int i, err;
438 PyObject *x;
439
440 for (i = o->ob_size; --i >= 0; ) {
441 x = o->ob_item[i];
442 if (x != NULL) {
443 err = visit(x, arg);
444 if (err)
445 return err;
446 }
447 }
448 return 0;
449}
450
Guido van Rossumf77bc622001-01-18 00:00:53 +0000451static PyObject *
452tuplerichcompare(PyObject *v, PyObject *w, int op)
453{
454 PyTupleObject *vt, *wt;
455 int i;
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000456 int vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000457
458 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
459 Py_INCREF(Py_NotImplemented);
460 return Py_NotImplemented;
461 }
462
463 vt = (PyTupleObject *)v;
464 wt = (PyTupleObject *)w;
465
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000466 vlen = vt->ob_size;
467 wlen = wt->ob_size;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000468
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000469 /* Note: the corresponding code for lists has an "early out" test
470 * here when op is EQ or NE and the lengths differ. That pays there,
471 * but Tim was unable to find any real code where EQ/NE tuple
472 * compares don't have the same length, so testing for it here would
473 * have cost without benefit.
474 */
475
476 /* Search for the first index where items are different.
477 * Note that because tuples are immutable, it's safe to reuse
478 * vlen and wlen across the comparison calls.
479 */
480 for (i = 0; i < vlen && i < wlen; i++) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000481 int k = PyObject_RichCompareBool(vt->ob_item[i],
482 wt->ob_item[i], Py_EQ);
483 if (k < 0)
484 return NULL;
485 if (!k)
486 break;
487 }
488
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000489 if (i >= vlen || i >= wlen) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000490 /* No more items to compare -- compare sizes */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000491 int cmp;
492 PyObject *res;
493 switch (op) {
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000494 case Py_LT: cmp = vlen < wlen; break;
495 case Py_LE: cmp = vlen <= wlen; break;
496 case Py_EQ: cmp = vlen == wlen; break;
497 case Py_NE: cmp = vlen != wlen; break;
498 case Py_GT: cmp = vlen > wlen; break;
499 case Py_GE: cmp = vlen >= wlen; break;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000500 default: return NULL; /* cannot happen */
501 }
502 if (cmp)
503 res = Py_True;
504 else
505 res = Py_False;
506 Py_INCREF(res);
507 return res;
508 }
509
510 /* We have an item that differs -- shortcuts for EQ/NE */
511 if (op == Py_EQ) {
512 Py_INCREF(Py_False);
513 return Py_False;
514 }
515 if (op == Py_NE) {
516 Py_INCREF(Py_True);
517 return Py_True;
518 }
519
520 /* Compare the final item again using the proper operator */
521 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
522}
523
Jeremy Hylton938ace62002-07-17 16:30:39 +0000524static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000525tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
526
Tim Peters6d6c1a32001-08-02 04:15:00 +0000527static PyObject *
528tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
529{
530 PyObject *arg = NULL;
531 static char *kwlist[] = {"sequence", 0};
532
Guido van Rossumae960af2001-08-30 03:11:59 +0000533 if (type != &PyTuple_Type)
534 return tuple_subtype_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000535 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
536 return NULL;
537
538 if (arg == NULL)
539 return PyTuple_New(0);
540 else
541 return PySequence_Tuple(arg);
542}
543
Guido van Rossumae960af2001-08-30 03:11:59 +0000544static PyObject *
545tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
546{
547 PyObject *tmp, *new, *item;
548 int i, n;
549
550 assert(PyType_IsSubtype(type, &PyTuple_Type));
551 tmp = tuple_new(&PyTuple_Type, args, kwds);
552 if (tmp == NULL)
553 return NULL;
554 assert(PyTuple_Check(tmp));
555 new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
556 if (new == NULL)
557 return NULL;
558 for (i = 0; i < n; i++) {
559 item = PyTuple_GET_ITEM(tmp, i);
560 Py_INCREF(item);
561 PyTuple_SET_ITEM(new, i, item);
562 }
563 Py_DECREF(tmp);
564 return new;
565}
566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000567PyDoc_STRVAR(tuple_doc,
Tim Peters1b8ca0d2001-09-02 06:42:25 +0000568"tuple() -> an empty tuple\n"
569"tuple(sequence) -> tuple initialized from sequence's items\n"
570"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000571"If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000572
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573static PySequenceMethods tuple_as_sequence = {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000574 (inquiry)tuplelength, /* sq_length */
575 (binaryfunc)tupleconcat, /* sq_concat */
576 (intargfunc)tuplerepeat, /* sq_repeat */
577 (intargfunc)tupleitem, /* sq_item */
578 (intintargfunc)tupleslice, /* sq_slice */
579 0, /* sq_ass_item */
580 0, /* sq_ass_slice */
581 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000582};
583
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000584static PyObject*
585tuplesubscript(PyTupleObject* self, PyObject* item)
586{
587 if (PyInt_Check(item)) {
588 long i = PyInt_AS_LONG(item);
589 if (i < 0)
590 i += PyTuple_GET_SIZE(self);
591 return tupleitem(self, i);
592 }
593 else if (PyLong_Check(item)) {
594 long i = PyLong_AsLong(item);
595 if (i == -1 && PyErr_Occurred())
596 return NULL;
597 if (i < 0)
598 i += PyTuple_GET_SIZE(self);
599 return tupleitem(self, i);
600 }
601 else if (PySlice_Check(item)) {
602 int start, stop, step, slicelength, cur, i;
603 PyObject* result;
604 PyObject* it;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000605 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000606
607 if (PySlice_GetIndicesEx((PySliceObject*)item,
608 PyTuple_GET_SIZE(self),
609 &start, &stop, &step, &slicelength) < 0) {
610 return NULL;
611 }
612
613 if (slicelength <= 0) {
614 return PyTuple_New(0);
615 }
616 else {
617 result = PyTuple_New(slicelength);
618
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000619 src = self->ob_item;
620 dest = ((PyTupleObject *)result)->ob_item;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000621 for (cur = start, i = 0; i < slicelength;
622 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000623 it = src[cur];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000624 Py_INCREF(it);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000625 dest[i] = it;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000626 }
627
628 return result;
629 }
630 }
631 else {
632 PyErr_SetString(PyExc_TypeError,
633 "tuple indices must be integers");
634 return NULL;
635 }
636}
637
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000638static PyObject *
639tuple_getnewargs(PyTupleObject *v)
640{
641 return Py_BuildValue("(N)", tupleslice(v, 0, v->ob_size));
642
643}
644
645static PyMethodDef tuple_methods[] = {
646 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
647 {NULL, NULL} /* sentinel */
648};
649
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000650static PyMappingMethods tuple_as_mapping = {
651 (inquiry)tuplelength,
652 (binaryfunc)tuplesubscript,
653 0
654};
655
Raymond Hettinger48923c52002-08-09 01:30:17 +0000656static PyObject *tuple_iter(PyObject *seq);
657
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658PyTypeObject PyTuple_Type = {
659 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660 0,
661 "tuple",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000662 sizeof(PyTupleObject) - sizeof(PyObject *),
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663 sizeof(PyObject *),
Guido van Rossumf77bc622001-01-18 00:00:53 +0000664 (destructor)tupledealloc, /* tp_dealloc */
665 (printfunc)tupleprint, /* tp_print */
666 0, /* tp_getattr */
667 0, /* tp_setattr */
668 0, /* tp_compare */
669 (reprfunc)tuplerepr, /* tp_repr */
670 0, /* tp_as_number */
671 &tuple_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000672 &tuple_as_mapping, /* tp_as_mapping */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000673 (hashfunc)tuplehash, /* tp_hash */
674 0, /* tp_call */
675 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000677 0, /* tp_setattro */
678 0, /* tp_as_buffer */
Guido van Rossumae960af2001-08-30 03:11:59 +0000679 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
680 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681 tuple_doc, /* tp_doc */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000682 (traverseproc)tupletraverse, /* tp_traverse */
683 0, /* tp_clear */
684 tuplerichcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685 0, /* tp_weaklistoffset */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000686 tuple_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000687 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000688 tuple_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 0, /* tp_members */
690 0, /* tp_getset */
691 0, /* tp_base */
692 0, /* tp_dict */
693 0, /* tp_descr_get */
694 0, /* tp_descr_set */
695 0, /* tp_dictoffset */
696 0, /* tp_init */
697 0, /* tp_alloc */
698 tuple_new, /* tp_new */
Neil Schemenauer626d7742002-04-12 03:05:52 +0000699 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000701
702/* The following function breaks the notion that tuples are immutable:
703 it changes the size of a tuple. We get away with this only if there
704 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000705 as creating a new tuple object and destroying the old one, only more
706 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000707 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000708
709int
Tim Peters4324aa32001-05-28 22:30:08 +0000710_PyTuple_Resize(PyObject **pv, int newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000711{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 register PyTupleObject *v;
713 register PyTupleObject *sv;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000714 int i;
Guido van Rossumf70590f2001-12-07 20:00:04 +0000715 int oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000716
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 v = (PyTupleObject *) *pv;
Guido van Rossum4b8c0f62001-08-30 18:31:30 +0000718 if (v == NULL || v->ob_type != &PyTuple_Type ||
Thomas Wouters6a922372001-05-28 13:11:02 +0000719 (v->ob_size != 0 && v->ob_refcnt != 1)) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000720 *pv = 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000721 Py_XDECREF(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722 PyErr_BadInternalCall();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000723 return -1;
724 }
Guido van Rossumf70590f2001-12-07 20:00:04 +0000725 oldsize = v->ob_size;
726 if (oldsize == newsize)
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000727 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000728
Guido van Rossumf70590f2001-12-07 20:00:04 +0000729 if (oldsize == 0) {
Thomas Wouters6a922372001-05-28 13:11:02 +0000730 /* Empty tuples are often shared, so we should never
731 resize them in-place even if we do own the only
732 (current) reference */
733 Py_DECREF(v);
734 *pv = PyTuple_New(newsize);
Thomas Wouters0dcea592001-05-29 07:58:45 +0000735 return *pv == NULL ? -1 : 0;
Thomas Wouters6a922372001-05-28 13:11:02 +0000736 }
737
Guido van Rossum12d12c51993-10-26 17:58:25 +0000738 /* XXX UNREF/NEWREF interface should be more symmetrical */
Tim Peters34592512002-07-11 06:23:50 +0000739 _Py_DEC_REFTOTAL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000740 _PyObject_GC_UNTRACK(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000741 _Py_ForgetReference((PyObject *) v);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000742 /* DECREF items deleted by shrinkage */
743 for (i = newsize; i < oldsize; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 Py_XDECREF(v->ob_item[i]);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000745 v->ob_item[i] = NULL;
746 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000747 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000748 if (sv == NULL) {
749 *pv = NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000750 PyObject_GC_Del(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000751 return -1;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000752 }
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000753 _Py_NewReference((PyObject *) sv);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000754 /* Zero out items added by growing */
Neal Norwitz35fc7602002-06-13 21:11:11 +0000755 if (newsize > oldsize)
Jeremy Hylton8b47dff2002-06-20 23:13:17 +0000756 memset(&sv->ob_item[oldsize], 0,
757 sizeof(*sv->ob_item) * (newsize - oldsize));
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000758 *pv = (PyObject *) sv;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000759 _PyObject_GC_TRACK(sv);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000760 return 0;
761}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000762
763void
Fred Drakeba096332000-07-09 07:04:36 +0000764PyTuple_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000765{
766#if MAXSAVESIZE > 0
767 int i;
768
769 Py_XDECREF(free_tuples[0]);
770 free_tuples[0] = NULL;
771
772 for (i = 1; i < MAXSAVESIZE; i++) {
773 PyTupleObject *p, *q;
774 p = free_tuples[i];
775 free_tuples[i] = NULL;
776 while (p) {
777 q = p;
778 p = (PyTupleObject *)(p->ob_item[0]);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000779 PyObject_GC_Del(q);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000780 }
781 }
782#endif
783}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000784
785/*********************** Tuple Iterator **************************/
786
787typedef struct {
788 PyObject_HEAD
789 long it_index;
790 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
791} tupleiterobject;
792
793PyTypeObject PyTupleIter_Type;
794
795static PyObject *
796tuple_iter(PyObject *seq)
797{
798 tupleiterobject *it;
799
800 if (!PyTuple_Check(seq)) {
801 PyErr_BadInternalCall();
802 return NULL;
803 }
804 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
805 if (it == NULL)
806 return NULL;
807 it->it_index = 0;
808 Py_INCREF(seq);
809 it->it_seq = (PyTupleObject *)seq;
810 _PyObject_GC_TRACK(it);
811 return (PyObject *)it;
812}
813
814static void
815tupleiter_dealloc(tupleiterobject *it)
816{
817 _PyObject_GC_UNTRACK(it);
818 Py_XDECREF(it->it_seq);
819 PyObject_GC_Del(it);
820}
821
822static int
823tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
824{
825 if (it->it_seq == NULL)
826 return 0;
827 return visit((PyObject *)it->it_seq, arg);
828}
829
Raymond Hettinger48923c52002-08-09 01:30:17 +0000830static PyObject *
831tupleiter_next(tupleiterobject *it)
832{
833 PyTupleObject *seq;
834 PyObject *item;
835
836 assert(it != NULL);
837 seq = it->it_seq;
838 if (seq == NULL)
839 return NULL;
840 assert(PyTuple_Check(seq));
841
842 if (it->it_index < PyTuple_GET_SIZE(seq)) {
843 item = PyTuple_GET_ITEM(seq, it->it_index);
844 ++it->it_index;
845 Py_INCREF(item);
846 return item;
847 }
848
849 Py_DECREF(seq);
850 it->it_seq = NULL;
851 return NULL;
852}
853
Raymond Hettinger435bf582004-03-18 22:43:10 +0000854static int
855tupleiter_len(tupleiterobject *it)
856{
857 if (it->it_seq)
858 return PyTuple_GET_SIZE(it->it_seq) - it->it_index;
859 return 0;
860}
861
862static PySequenceMethods tupleiter_as_sequence = {
863 (inquiry)tupleiter_len, /* sq_length */
864 0, /* sq_concat */
865};
866
Raymond Hettinger48923c52002-08-09 01:30:17 +0000867PyTypeObject PyTupleIter_Type = {
868 PyObject_HEAD_INIT(&PyType_Type)
869 0, /* ob_size */
870 "tupleiterator", /* tp_name */
871 sizeof(tupleiterobject), /* tp_basicsize */
872 0, /* tp_itemsize */
873 /* methods */
874 (destructor)tupleiter_dealloc, /* tp_dealloc */
875 0, /* tp_print */
876 0, /* tp_getattr */
877 0, /* tp_setattr */
878 0, /* tp_compare */
879 0, /* tp_repr */
880 0, /* tp_as_number */
Raymond Hettinger435bf582004-03-18 22:43:10 +0000881 &tupleiter_as_sequence, /* tp_as_sequence */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000882 0, /* tp_as_mapping */
883 0, /* tp_hash */
884 0, /* tp_call */
885 0, /* tp_str */
886 PyObject_GenericGetAttr, /* tp_getattro */
887 0, /* tp_setattro */
888 0, /* tp_as_buffer */
889 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
890 0, /* tp_doc */
891 (traverseproc)tupleiter_traverse, /* tp_traverse */
892 0, /* tp_clear */
893 0, /* tp_richcompare */
894 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000895 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000896 (iternextfunc)tupleiter_next, /* tp_iternext */
897};