blob: c85b35a6037396e95c558d2775903a97a3e847f2 [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 *
Martin v. Löwis18e16552006-02-15 17:27:45 +000027PyTuple_New(register Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 register PyTupleObject *op;
Martin v. Löwis18e16552006-02-15 17:27:45 +000030 Py_ssize_t 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 {
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t nbytes = size * sizeof(PyObject *);
Guido van Rossum5bc51f21999-07-12 23:06:58 +000061 /* 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
Martin v. Löwis18e16552006-02-15 17:27:45 +000085Py_ssize_t
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 *
Martin v. Löwis18e16552006-02-15 17:27:45 +000097PyTuple_GetItem(register PyObject *op, register Py_ssize_t 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
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111PyTuple_SetItem(register PyObject *op, register Py_ssize_t 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 *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134PyTuple_Pack(Py_ssize_t n, ...)
Raymond Hettingercb2da432003-10-12 18:24:34 +0000135{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136 Py_ssize_t i;
Raymond Hettingercb2da432003-10-12 18:24:34 +0000137 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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000162 register Py_ssize_t i;
163 register Py_ssize_t 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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000190 Py_ssize_t i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191 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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000207 Py_ssize_t i, n;
Tim Petersa7259592001-06-16 05:11:17 +0000208 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;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000271 register Py_ssize_t 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;
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000281 /* the cast might truncate len; that doesn't change hash stability */
282 mult += (long)(82520L + len + len);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000283 }
Raymond Hettinger57c2d932004-06-10 18:42:15 +0000284 x += 97531L;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000285 if (x == -1)
286 x = -2;
287 return x;
288}
289
Martin v. Löwis18e16552006-02-15 17:27:45 +0000290static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000291tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292{
293 return a->ob_size;
294}
295
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000296static int
Fred Drakeba096332000-07-09 07:04:36 +0000297tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000298{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000299 Py_ssize_t i;
300 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000301
Raymond Hettingerbd9adab2002-09-05 20:18:08 +0000302 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
Guido van Rossumf77bc622001-01-18 00:00:53 +0000303 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
Raymond Hettingerbd9adab2002-09-05 20:18:08 +0000304 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000305 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000306}
307
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000309tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310{
311 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 return NULL;
314 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000315 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316 return a->ob_item[i];
317}
318
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
321 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323 register PyTupleObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000324 PyObject **src, **dest;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000325 register Py_ssize_t i;
326 Py_ssize_t len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327 if (ilow < 0)
328 ilow = 0;
329 if (ihigh > a->ob_size)
330 ihigh = a->ob_size;
331 if (ihigh < ilow)
332 ihigh = ilow;
Tim Peters7b07a412001-09-11 19:48:03 +0000333 if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334 Py_INCREF(a);
335 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 }
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000337 len = ihigh - ilow;
338 np = (PyTupleObject *)PyTuple_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339 if (np == NULL)
340 return NULL;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000341 src = a->ob_item + ilow;
342 dest = np->ob_item;
343 for (i = 0; i < len; i++) {
344 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000345 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000346 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349}
350
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000352PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000353{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 if (op == NULL || !PyTuple_Check(op)) {
355 PyErr_BadInternalCall();
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000356 return NULL;
357 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000359}
360
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000362tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000364 register Py_ssize_t size;
365 register Py_ssize_t i;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000366 PyObject **src, **dest;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 PyTupleObject *np;
368 if (!PyTuple_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000369 PyErr_Format(PyExc_TypeError,
Marc-André Lemburg29dc3812000-06-16 17:05:57 +0000370 "can only concatenate tuple (not \"%.200s\") to tuple",
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000371 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372 return NULL;
373 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374#define b ((PyTupleObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 size = a->ob_size + b->ob_size;
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000376 if (size < 0)
377 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000378 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379 if (np == NULL) {
Guido van Rossum49e85141991-06-07 22:59:30 +0000380 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000382 src = a->ob_item;
383 dest = np->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384 for (i = 0; i < a->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000385 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000387 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000389 src = b->ob_item;
390 dest = np->ob_item + a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391 for (i = 0; i < b->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000392 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000394 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397#undef b
398}
399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000401tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000402{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000403 Py_ssize_t i, j;
404 Py_ssize_t size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 PyTupleObject *np;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000406 PyObject **p, **items;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000407 if (n < 0)
408 n = 0;
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000409 if (a->ob_size == 0 || n == 1) {
Tim Peters7b07a412001-09-11 19:48:03 +0000410 if (PyTuple_CheckExact(a)) {
411 /* Since tuples are immutable, we can return a shared
412 copy in this case */
413 Py_INCREF(a);
414 return (PyObject *)a;
415 }
416 if (a->ob_size == 0)
417 return PyTuple_New(0);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000418 }
419 size = a->ob_size * n;
Guido van Rossum0eb55ac1999-07-13 05:41:12 +0000420 if (size/a->ob_size != n)
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000421 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000423 if (np == NULL)
424 return NULL;
425 p = np->ob_item;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000426 items = a->ob_item;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000427 for (i = 0; i < n; i++) {
428 for (j = 0; j < a->ob_size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000429 *p = items[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000430 Py_INCREF(*p);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000431 p++;
432 }
433 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000435}
436
Jeremy Hylton8caad492000-06-23 14:18:11 +0000437static int
438tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
439{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000440 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000441
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442 for (i = o->ob_size; --i >= 0; )
443 Py_VISIT(o->ob_item[i]);
Jeremy Hylton8caad492000-06-23 14:18:11 +0000444 return 0;
445}
446
Guido van Rossumf77bc622001-01-18 00:00:53 +0000447static PyObject *
448tuplerichcompare(PyObject *v, PyObject *w, int op)
449{
450 PyTupleObject *vt, *wt;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451 Py_ssize_t i;
452 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000453
454 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
455 Py_INCREF(Py_NotImplemented);
456 return Py_NotImplemented;
457 }
458
459 vt = (PyTupleObject *)v;
460 wt = (PyTupleObject *)w;
461
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000462 vlen = vt->ob_size;
463 wlen = wt->ob_size;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000464
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000465 /* Note: the corresponding code for lists has an "early out" test
466 * here when op is EQ or NE and the lengths differ. That pays there,
467 * but Tim was unable to find any real code where EQ/NE tuple
468 * compares don't have the same length, so testing for it here would
469 * have cost without benefit.
470 */
471
472 /* Search for the first index where items are different.
473 * Note that because tuples are immutable, it's safe to reuse
474 * vlen and wlen across the comparison calls.
475 */
476 for (i = 0; i < vlen && i < wlen; i++) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000477 int k = PyObject_RichCompareBool(vt->ob_item[i],
478 wt->ob_item[i], Py_EQ);
479 if (k < 0)
480 return NULL;
481 if (!k)
482 break;
483 }
484
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000485 if (i >= vlen || i >= wlen) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000486 /* No more items to compare -- compare sizes */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000487 int cmp;
488 PyObject *res;
489 switch (op) {
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000490 case Py_LT: cmp = vlen < wlen; break;
491 case Py_LE: cmp = vlen <= wlen; break;
492 case Py_EQ: cmp = vlen == wlen; break;
493 case Py_NE: cmp = vlen != wlen; break;
494 case Py_GT: cmp = vlen > wlen; break;
495 case Py_GE: cmp = vlen >= wlen; break;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000496 default: return NULL; /* cannot happen */
497 }
498 if (cmp)
499 res = Py_True;
500 else
501 res = Py_False;
502 Py_INCREF(res);
503 return res;
504 }
505
506 /* We have an item that differs -- shortcuts for EQ/NE */
507 if (op == Py_EQ) {
508 Py_INCREF(Py_False);
509 return Py_False;
510 }
511 if (op == Py_NE) {
512 Py_INCREF(Py_True);
513 return Py_True;
514 }
515
516 /* Compare the final item again using the proper operator */
517 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
518}
519
Jeremy Hylton938ace62002-07-17 16:30:39 +0000520static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000521tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
522
Tim Peters6d6c1a32001-08-02 04:15:00 +0000523static PyObject *
524tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
525{
526 PyObject *arg = NULL;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000527 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000528
Guido van Rossumae960af2001-08-30 03:11:59 +0000529 if (type != &PyTuple_Type)
530 return tuple_subtype_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000531 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
532 return NULL;
533
534 if (arg == NULL)
535 return PyTuple_New(0);
536 else
537 return PySequence_Tuple(arg);
538}
539
Guido van Rossumae960af2001-08-30 03:11:59 +0000540static PyObject *
541tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
542{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 PyObject *tmp, *newobj, *item;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000544 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000545
546 assert(PyType_IsSubtype(type, &PyTuple_Type));
547 tmp = tuple_new(&PyTuple_Type, args, kwds);
548 if (tmp == NULL)
549 return NULL;
550 assert(PyTuple_Check(tmp));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
552 if (newobj == NULL)
Guido van Rossumae960af2001-08-30 03:11:59 +0000553 return NULL;
554 for (i = 0; i < n; i++) {
555 item = PyTuple_GET_ITEM(tmp, i);
556 Py_INCREF(item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000557 PyTuple_SET_ITEM(newobj, i, item);
Guido van Rossumae960af2001-08-30 03:11:59 +0000558 }
559 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000561}
562
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000563PyDoc_STRVAR(tuple_doc,
Tim Peters1b8ca0d2001-09-02 06:42:25 +0000564"tuple() -> an empty tuple\n"
565"tuple(sequence) -> tuple initialized from sequence's items\n"
566"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000567"If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000568
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569static PySequenceMethods tuple_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000570 (lenfunc)tuplelength, /* sq_length */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000571 (binaryfunc)tupleconcat, /* sq_concat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572 (ssizeargfunc)tuplerepeat, /* sq_repeat */
573 (ssizeargfunc)tupleitem, /* sq_item */
574 (ssizessizeargfunc)tupleslice, /* sq_slice */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000575 0, /* sq_ass_item */
576 0, /* sq_ass_slice */
577 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578};
579
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000580static PyObject*
581tuplesubscript(PyTupleObject* self, PyObject* item)
582{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000583 if (PyIndex_Check(item)) {
584 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000585 if (i == -1 && PyErr_Occurred())
586 return NULL;
587 if (i < 0)
588 i += PyTuple_GET_SIZE(self);
589 return tupleitem(self, i);
590 }
591 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000592 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000593 PyObject* result;
594 PyObject* it;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000595 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000596
597 if (PySlice_GetIndicesEx((PySliceObject*)item,
598 PyTuple_GET_SIZE(self),
599 &start, &stop, &step, &slicelength) < 0) {
600 return NULL;
601 }
602
603 if (slicelength <= 0) {
604 return PyTuple_New(0);
605 }
606 else {
607 result = PyTuple_New(slicelength);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000608 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000609
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000610 src = self->ob_item;
611 dest = ((PyTupleObject *)result)->ob_item;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000612 for (cur = start, i = 0; i < slicelength;
613 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000614 it = src[cur];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000615 Py_INCREF(it);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000616 dest[i] = it;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000617 }
618
619 return result;
620 }
621 }
622 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000623 PyErr_Format(PyExc_TypeError,
624 "tuple indices must be integers, not %.200s",
625 item->ob_type->tp_name);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000626 return NULL;
627 }
628}
629
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000630static PyObject *
631tuple_getnewargs(PyTupleObject *v)
632{
633 return Py_BuildValue("(N)", tupleslice(v, 0, v->ob_size));
634
635}
636
637static PyMethodDef tuple_methods[] = {
638 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
639 {NULL, NULL} /* sentinel */
640};
641
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000642static PyMappingMethods tuple_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000643 (lenfunc)tuplelength,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000644 (binaryfunc)tuplesubscript,
645 0
646};
647
Raymond Hettinger48923c52002-08-09 01:30:17 +0000648static PyObject *tuple_iter(PyObject *seq);
649
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650PyTypeObject PyTuple_Type = {
651 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652 0,
653 "tuple",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000654 sizeof(PyTupleObject) - sizeof(PyObject *),
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 sizeof(PyObject *),
Guido van Rossumf77bc622001-01-18 00:00:53 +0000656 (destructor)tupledealloc, /* tp_dealloc */
657 (printfunc)tupleprint, /* tp_print */
658 0, /* tp_getattr */
659 0, /* tp_setattr */
660 0, /* tp_compare */
661 (reprfunc)tuplerepr, /* tp_repr */
662 0, /* tp_as_number */
663 &tuple_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000664 &tuple_as_mapping, /* tp_as_mapping */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000665 (hashfunc)tuplehash, /* tp_hash */
666 0, /* tp_call */
667 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000669 0, /* tp_setattro */
670 0, /* tp_as_buffer */
Guido van Rossumae960af2001-08-30 03:11:59 +0000671 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
672 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000673 tuple_doc, /* tp_doc */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000674 (traverseproc)tupletraverse, /* tp_traverse */
675 0, /* tp_clear */
676 tuplerichcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677 0, /* tp_weaklistoffset */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000678 tuple_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000679 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000680 tuple_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681 0, /* tp_members */
682 0, /* tp_getset */
683 0, /* tp_base */
684 0, /* tp_dict */
685 0, /* tp_descr_get */
686 0, /* tp_descr_set */
687 0, /* tp_dictoffset */
688 0, /* tp_init */
689 0, /* tp_alloc */
690 tuple_new, /* tp_new */
Neil Schemenauer626d7742002-04-12 03:05:52 +0000691 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000693
694/* The following function breaks the notion that tuples are immutable:
695 it changes the size of a tuple. We get away with this only if there
696 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000697 as creating a new tuple object and destroying the old one, only more
698 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000699 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000700
701int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000702_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000703{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000704 register PyTupleObject *v;
705 register PyTupleObject *sv;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000706 Py_ssize_t i;
707 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000708
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 v = (PyTupleObject *) *pv;
Guido van Rossum4b8c0f62001-08-30 18:31:30 +0000710 if (v == NULL || v->ob_type != &PyTuple_Type ||
Thomas Wouters6a922372001-05-28 13:11:02 +0000711 (v->ob_size != 0 && v->ob_refcnt != 1)) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000712 *pv = 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000713 Py_XDECREF(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 PyErr_BadInternalCall();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000715 return -1;
716 }
Guido van Rossumf70590f2001-12-07 20:00:04 +0000717 oldsize = v->ob_size;
718 if (oldsize == newsize)
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000719 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000720
Guido van Rossumf70590f2001-12-07 20:00:04 +0000721 if (oldsize == 0) {
Thomas Wouters6a922372001-05-28 13:11:02 +0000722 /* Empty tuples are often shared, so we should never
723 resize them in-place even if we do own the only
724 (current) reference */
725 Py_DECREF(v);
726 *pv = PyTuple_New(newsize);
Thomas Wouters0dcea592001-05-29 07:58:45 +0000727 return *pv == NULL ? -1 : 0;
Thomas Wouters6a922372001-05-28 13:11:02 +0000728 }
729
Guido van Rossum12d12c51993-10-26 17:58:25 +0000730 /* XXX UNREF/NEWREF interface should be more symmetrical */
Tim Peters34592512002-07-11 06:23:50 +0000731 _Py_DEC_REFTOTAL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000732 _PyObject_GC_UNTRACK(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000733 _Py_ForgetReference((PyObject *) v);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000734 /* DECREF items deleted by shrinkage */
735 for (i = newsize; i < oldsize; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736 Py_XDECREF(v->ob_item[i]);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000737 v->ob_item[i] = NULL;
738 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000739 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000740 if (sv == NULL) {
741 *pv = NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000742 PyObject_GC_Del(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000743 return -1;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000744 }
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000745 _Py_NewReference((PyObject *) sv);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000746 /* Zero out items added by growing */
Neal Norwitz35fc7602002-06-13 21:11:11 +0000747 if (newsize > oldsize)
Jeremy Hylton8b47dff2002-06-20 23:13:17 +0000748 memset(&sv->ob_item[oldsize], 0,
749 sizeof(*sv->ob_item) * (newsize - oldsize));
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000750 *pv = (PyObject *) sv;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000751 _PyObject_GC_TRACK(sv);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000752 return 0;
753}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000754
755void
Fred Drakeba096332000-07-09 07:04:36 +0000756PyTuple_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000757{
758#if MAXSAVESIZE > 0
759 int i;
760
761 Py_XDECREF(free_tuples[0]);
762 free_tuples[0] = NULL;
763
764 for (i = 1; i < MAXSAVESIZE; i++) {
765 PyTupleObject *p, *q;
766 p = free_tuples[i];
767 free_tuples[i] = NULL;
768 while (p) {
769 q = p;
770 p = (PyTupleObject *)(p->ob_item[0]);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000771 PyObject_GC_Del(q);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000772 }
773 }
774#endif
775}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000776
777/*********************** Tuple Iterator **************************/
778
779typedef struct {
780 PyObject_HEAD
781 long it_index;
782 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
783} tupleiterobject;
784
Raymond Hettinger48923c52002-08-09 01:30:17 +0000785static void
786tupleiter_dealloc(tupleiterobject *it)
787{
788 _PyObject_GC_UNTRACK(it);
789 Py_XDECREF(it->it_seq);
790 PyObject_GC_Del(it);
791}
792
793static int
794tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
795{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000796 Py_VISIT(it->it_seq);
797 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000798}
799
Raymond Hettinger48923c52002-08-09 01:30:17 +0000800static PyObject *
801tupleiter_next(tupleiterobject *it)
802{
803 PyTupleObject *seq;
804 PyObject *item;
805
806 assert(it != NULL);
807 seq = it->it_seq;
808 if (seq == NULL)
809 return NULL;
810 assert(PyTuple_Check(seq));
811
812 if (it->it_index < PyTuple_GET_SIZE(seq)) {
813 item = PyTuple_GET_ITEM(seq, it->it_index);
814 ++it->it_index;
815 Py_INCREF(item);
816 return item;
817 }
818
819 Py_DECREF(seq);
820 it->it_seq = NULL;
821 return NULL;
822}
823
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000824static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000825tupleiter_len(tupleiterobject *it)
826{
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000827 Py_ssize_t len = 0;
Raymond Hettinger435bf582004-03-18 22:43:10 +0000828 if (it->it_seq)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000829 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000830 return PyInt_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000831}
832
Armin Rigof5b3e362006-02-11 21:32:43 +0000833PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000834
835static PyMethodDef tupleiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000836 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000837 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +0000838};
839
Raymond Hettinger48923c52002-08-09 01:30:17 +0000840PyTypeObject PyTupleIter_Type = {
841 PyObject_HEAD_INIT(&PyType_Type)
842 0, /* ob_size */
843 "tupleiterator", /* tp_name */
844 sizeof(tupleiterobject), /* tp_basicsize */
845 0, /* tp_itemsize */
846 /* methods */
847 (destructor)tupleiter_dealloc, /* tp_dealloc */
848 0, /* tp_print */
849 0, /* tp_getattr */
850 0, /* tp_setattr */
851 0, /* tp_compare */
852 0, /* tp_repr */
853 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000854 0, /* tp_as_sequence */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000855 0, /* tp_as_mapping */
856 0, /* tp_hash */
857 0, /* tp_call */
858 0, /* tp_str */
859 PyObject_GenericGetAttr, /* tp_getattro */
860 0, /* tp_setattro */
861 0, /* tp_as_buffer */
862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
863 0, /* tp_doc */
864 (traverseproc)tupleiter_traverse, /* tp_traverse */
865 0, /* tp_clear */
866 0, /* tp_richcompare */
867 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000868 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000869 (iternextfunc)tupleiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000870 tupleiter_methods, /* tp_methods */
871 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +0000872};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873
874static PyObject *
875tuple_iter(PyObject *seq)
876{
877 tupleiterobject *it;
878
879 if (!PyTuple_Check(seq)) {
880 PyErr_BadInternalCall();
881 return NULL;
882 }
883 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
884 if (it == NULL)
885 return NULL;
886 it->it_index = 0;
887 Py_INCREF(seq);
888 it->it_seq = (PyTupleObject *)seq;
889 _PyObject_GC_TRACK(it);
890 return (PyObject *)it;
891}