blob: e9cb3efbe8e86aeb944cd29ff4bc3c82d17e5b98 [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 */
Christian Heimes5b970ad2008-02-06 13:33:44 +00007#ifndef PyTuple_MAXSAVESIZE
8#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
Guido van Rossum5ce78f82000-04-21 21:15:05 +00009#endif
Christian Heimes5b970ad2008-02-06 13:33:44 +000010#ifndef PyTuple_MAXFREELIST
11#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000012#endif
13
Christian Heimes5b970ad2008-02-06 13:33:44 +000014#if PyTuple_MAXSAVESIZE > 0
15/* Entries 1 up to PyTuple_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*/
Christian Heimes5b970ad2008-02-06 13:33:44 +000018static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
19static int numfree[PyTuple_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 }
Christian Heimes5b970ad2008-02-06 13:33:44 +000035#if PyTuple_MAXSAVESIZE > 0
36 if (size == 0 && free_list[0]) {
37 op = free_list[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 }
Christian Heimes5b970ad2008-02-06 13:33:44 +000044 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
45 free_list[size] = (PyTupleObject *) op->ob_item[0];
46 numfree[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
Christian Heimese93237d2007-12-19 02:37:44 +000052 Py_SIZE(op) = size;
53 Py_TYPE(op) = &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;
Christian Heimes5b970ad2008-02-06 13:33:44 +000074#if PyTuple_MAXSAVESIZE > 0
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000075 if (size == 0) {
Christian Heimes5b970ad2008-02-06 13:33:44 +000076 free_list[0] = op;
77 ++numfree[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
Christian Heimese93237d2007-12-19 02:37:44 +000093 return Py_SIZE(op);
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 }
Christian Heimese93237d2007-12-19 02:37:44 +0000103 if (i < 0 || i >= Py_SIZE(op)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000104 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 }
Christian Heimese93237d2007-12-19 02:37:44 +0000120 if (i < 0 || i >= Py_SIZE(op)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 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;
Christian Heimese93237d2007-12-19 02:37:44 +0000163 register Py_ssize_t len = Py_SIZE(op);
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]);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000170#if PyTuple_MAXSAVESIZE > 0
171 if (len < PyTuple_MAXSAVESIZE &&
172 numfree[len] < PyTuple_MAXFREELIST &&
Christian Heimese93237d2007-12-19 02:37:44 +0000173 Py_TYPE(op) == &PyTuple_Type)
Guido van Rossum4b8c0f62001-08-30 18:31:30 +0000174 {
Christian Heimes5b970ad2008-02-06 13:33:44 +0000175 op->ob_item[0] = (PyObject *) free_list[len];
176 numfree[len]++;
177 free_list[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 }
Christian Heimese93237d2007-12-19 02:37:44 +0000182 Py_TYPE(op)->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;
Brett Cannon01531592007-09-17 03:28:34 +0000191 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192 fprintf(fp, "(");
Brett Cannon01531592007-09-17 03:28:34 +0000193 Py_END_ALLOW_THREADS
Christian Heimese93237d2007-12-19 02:37:44 +0000194 for (i = 0; i < Py_SIZE(op); i++) {
Brett Cannon01531592007-09-17 03:28:34 +0000195 if (i > 0) {
196 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +0000198 Py_END_ALLOW_THREADS
199 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
Guido van Rossum49e85141991-06-07 22:59:30 +0000201 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 }
Christian Heimese93237d2007-12-19 02:37:44 +0000203 i = Py_SIZE(op);
Brett Cannon01531592007-09-17 03:28:34 +0000204 Py_BEGIN_ALLOW_THREADS
205 if (i == 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206 fprintf(fp, ",");
207 fprintf(fp, ")");
Brett Cannon01531592007-09-17 03:28:34 +0000208 Py_END_ALLOW_THREADS
Guido van Rossum49e85141991-06-07 22:59:30 +0000209 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210}
211
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000213tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000215 Py_ssize_t i, n;
Tim Petersa7259592001-06-16 05:11:17 +0000216 PyObject *s, *temp;
217 PyObject *pieces, *result = NULL;
218
Christian Heimese93237d2007-12-19 02:37:44 +0000219 n = Py_SIZE(v);
Brett Cannon31ba8482007-09-30 20:37:19 +0000220 if (n == 0)
221 return PyString_FromString("()");
222
Brett Cannon0b14f242007-09-30 19:45:10 +0000223 /* While not mutable, it is still possible to end up with a cycle in a
224 tuple through an object that stores itself within a tuple (and thus
225 infinitely asks for the repr of itself). This should only be
226 possible within a type. */
227 i = Py_ReprEnter((PyObject *)v);
228 if (i != 0) {
229 return i > 0 ? PyString_FromString("(...)") : NULL;
230 }
231
Tim Petersa7259592001-06-16 05:11:17 +0000232 pieces = PyTuple_New(n);
233 if (pieces == NULL)
234 return NULL;
235
236 /* Do repr() on each element. */
237 for (i = 0; i < n; ++i) {
Brett Cannon0b14f242007-09-30 19:45:10 +0000238 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
239 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +0000240 s = PyObject_Repr(v->ob_item[i]);
Brett Cannon0b14f242007-09-30 19:45:10 +0000241 Py_LeaveRecursiveCall();
Tim Petersa7259592001-06-16 05:11:17 +0000242 if (s == NULL)
243 goto Done;
244 PyTuple_SET_ITEM(pieces, i, s);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245 }
Tim Petersa7259592001-06-16 05:11:17 +0000246
247 /* Add "()" decorations to the first and last items. */
248 assert(n > 0);
249 s = PyString_FromString("(");
250 if (s == NULL)
251 goto Done;
252 temp = PyTuple_GET_ITEM(pieces, 0);
253 PyString_ConcatAndDel(&s, temp);
254 PyTuple_SET_ITEM(pieces, 0, s);
255 if (s == NULL)
256 goto Done;
257
258 s = PyString_FromString(n == 1 ? ",)" : ")");
259 if (s == NULL)
260 goto Done;
261 temp = PyTuple_GET_ITEM(pieces, n-1);
262 PyString_ConcatAndDel(&temp, s);
263 PyTuple_SET_ITEM(pieces, n-1, temp);
264 if (temp == NULL)
265 goto Done;
266
267 /* Paste them all together with ", " between. */
268 s = PyString_FromString(", ");
269 if (s == NULL)
270 goto Done;
271 result = _PyString_Join(s, pieces);
272 Py_DECREF(s);
273
274Done:
275 Py_DECREF(pieces);
Brett Cannon0b14f242007-09-30 19:45:10 +0000276 Py_ReprLeave((PyObject *)v);
Tim Petersa7259592001-06-16 05:11:17 +0000277 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278}
279
Raymond Hettinger4ec44e82004-06-04 06:35:20 +0000280/* The addend 82520, was selected from the range(0, 1000000) for
281 generating the greatest number of prime multipliers for tuples
282 upto length eight:
283
284 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
285 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
286*/
287
Guido van Rossum9bfef441993-03-29 10:43:31 +0000288static long
Fred Drakeba096332000-07-09 07:04:36 +0000289tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000290{
291 register long x, y;
Christian Heimese93237d2007-12-19 02:37:44 +0000292 register Py_ssize_t len = Py_SIZE(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 register PyObject **p;
Raymond Hettinger41bd0222004-06-01 06:36:24 +0000294 long mult = 1000003L;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000295 x = 0x345678L;
296 p = v->ob_item;
297 while (--len >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000298 y = PyObject_Hash(*p++);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000299 if (y == -1)
300 return -1;
Raymond Hettinger41bd0222004-06-01 06:36:24 +0000301 x = (x ^ y) * mult;
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000302 /* the cast might truncate len; that doesn't change hash stability */
303 mult += (long)(82520L + len + len);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000304 }
Raymond Hettinger57c2d932004-06-10 18:42:15 +0000305 x += 97531L;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000306 if (x == -1)
307 x = -2;
308 return x;
309}
310
Martin v. Löwis18e16552006-02-15 17:27:45 +0000311static Py_ssize_t
Fred Drakeba096332000-07-09 07:04:36 +0000312tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313{
Christian Heimese93237d2007-12-19 02:37:44 +0000314 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315}
316
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000317static int
Fred Drakeba096332000-07-09 07:04:36 +0000318tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000319{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320 Py_ssize_t i;
321 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000322
Christian Heimese93237d2007-12-19 02:37:44 +0000323 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
Guido van Rossumf77bc622001-01-18 00:00:53 +0000324 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
Raymond Hettingerbd9adab2002-09-05 20:18:08 +0000325 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000326 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000327}
328
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000329static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000330tupleitem(register PyTupleObject *a, register Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331{
Christian Heimese93237d2007-12-19 02:37:44 +0000332 if (i < 0 || i >= Py_SIZE(a)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334 return NULL;
335 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337 return a->ob_item[i];
338}
339
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000341tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
342 register Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344 register PyTupleObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000345 PyObject **src, **dest;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000346 register Py_ssize_t i;
347 Py_ssize_t len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348 if (ilow < 0)
349 ilow = 0;
Christian Heimese93237d2007-12-19 02:37:44 +0000350 if (ihigh > Py_SIZE(a))
351 ihigh = Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 if (ihigh < ilow)
353 ihigh = ilow;
Christian Heimese93237d2007-12-19 02:37:44 +0000354 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355 Py_INCREF(a);
356 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 }
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000358 len = ihigh - ilow;
359 np = (PyTupleObject *)PyTuple_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360 if (np == NULL)
361 return NULL;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000362 src = a->ob_item + ilow;
363 dest = np->ob_item;
364 for (i = 0; i < len; i++) {
365 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000367 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370}
371
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000373PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000374{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 if (op == NULL || !PyTuple_Check(op)) {
376 PyErr_BadInternalCall();
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000377 return NULL;
378 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000380}
381
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000383tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000385 register Py_ssize_t size;
386 register Py_ssize_t i;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000387 PyObject **src, **dest;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000388 PyTupleObject *np;
389 if (!PyTuple_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000390 PyErr_Format(PyExc_TypeError,
Marc-André Lemburg29dc3812000-06-16 17:05:57 +0000391 "can only concatenate tuple (not \"%.200s\") to tuple",
Christian Heimese93237d2007-12-19 02:37:44 +0000392 Py_TYPE(bb)->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393 return NULL;
394 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395#define b ((PyTupleObject *)bb)
Christian Heimese93237d2007-12-19 02:37:44 +0000396 size = Py_SIZE(a) + Py_SIZE(b);
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000397 if (size < 0)
398 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400 if (np == NULL) {
Guido van Rossum49e85141991-06-07 22:59:30 +0000401 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000403 src = a->ob_item;
404 dest = np->ob_item;
Christian Heimese93237d2007-12-19 02:37:44 +0000405 for (i = 0; i < Py_SIZE(a); i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000406 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000408 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000410 src = b->ob_item;
Christian Heimese93237d2007-12-19 02:37:44 +0000411 dest = np->ob_item + Py_SIZE(a);
412 for (i = 0; i < Py_SIZE(b); i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000413 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000415 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418#undef b
419}
420
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000422tuplerepeat(PyTupleObject *a, Py_ssize_t n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000423{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000424 Py_ssize_t i, j;
425 Py_ssize_t size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426 PyTupleObject *np;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000427 PyObject **p, **items;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000428 if (n < 0)
429 n = 0;
Christian Heimese93237d2007-12-19 02:37:44 +0000430 if (Py_SIZE(a) == 0 || n == 1) {
Tim Peters7b07a412001-09-11 19:48:03 +0000431 if (PyTuple_CheckExact(a)) {
432 /* Since tuples are immutable, we can return a shared
433 copy in this case */
434 Py_INCREF(a);
435 return (PyObject *)a;
436 }
Christian Heimese93237d2007-12-19 02:37:44 +0000437 if (Py_SIZE(a) == 0)
Tim Peters7b07a412001-09-11 19:48:03 +0000438 return PyTuple_New(0);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000439 }
Christian Heimese93237d2007-12-19 02:37:44 +0000440 size = Py_SIZE(a) * n;
441 if (size/Py_SIZE(a) != n)
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000442 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000444 if (np == NULL)
445 return NULL;
446 p = np->ob_item;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000447 items = a->ob_item;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000448 for (i = 0; i < n; i++) {
Christian Heimese93237d2007-12-19 02:37:44 +0000449 for (j = 0; j < Py_SIZE(a); j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000450 *p = items[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 Py_INCREF(*p);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000452 p++;
453 }
454 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000456}
457
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000458static PyObject *
459tupleindex(PyTupleObject *self, PyObject *args)
460{
461 Py_ssize_t i, start=0, stop=Py_SIZE(self);
462 PyObject *v;
463
464 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
465 _PyEval_SliceIndex, &start,
466 _PyEval_SliceIndex, &stop))
467 return NULL;
468 if (start < 0) {
469 start += Py_SIZE(self);
470 if (start < 0)
471 start = 0;
472 }
473 if (stop < 0) {
474 stop += Py_SIZE(self);
475 if (stop < 0)
476 stop = 0;
477 }
478 for (i = start; i < stop && i < Py_SIZE(self); i++) {
479 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
480 if (cmp > 0)
Raymond Hettinger55285ef2008-02-07 02:12:52 +0000481 return PyInt_FromSsize_t(i);
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000482 else if (cmp < 0)
483 return NULL;
484 }
485 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in list");
486 return NULL;
487}
488
489static PyObject *
490tuplecount(PyTupleObject *self, PyObject *v)
491{
492 Py_ssize_t count = 0;
493 Py_ssize_t i;
494
495 for (i = 0; i < Py_SIZE(self); i++) {
496 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
497 if (cmp > 0)
498 count++;
499 else if (cmp < 0)
500 return NULL;
501 }
Raymond Hettinger55285ef2008-02-07 02:12:52 +0000502 return PyInt_FromSsize_t(count);
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000503}
504
Jeremy Hylton8caad492000-06-23 14:18:11 +0000505static int
506tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
507{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000508 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000509
Christian Heimese93237d2007-12-19 02:37:44 +0000510 for (i = Py_SIZE(o); --i >= 0; )
Thomas Woutersc6e55062006-04-15 21:47:09 +0000511 Py_VISIT(o->ob_item[i]);
Jeremy Hylton8caad492000-06-23 14:18:11 +0000512 return 0;
513}
514
Guido van Rossumf77bc622001-01-18 00:00:53 +0000515static PyObject *
516tuplerichcompare(PyObject *v, PyObject *w, int op)
517{
518 PyTupleObject *vt, *wt;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000519 Py_ssize_t i;
520 Py_ssize_t vlen, wlen;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000521
522 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
523 Py_INCREF(Py_NotImplemented);
524 return Py_NotImplemented;
525 }
526
527 vt = (PyTupleObject *)v;
528 wt = (PyTupleObject *)w;
529
Christian Heimese93237d2007-12-19 02:37:44 +0000530 vlen = Py_SIZE(vt);
531 wlen = Py_SIZE(wt);
Guido van Rossumf77bc622001-01-18 00:00:53 +0000532
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000533 /* Note: the corresponding code for lists has an "early out" test
534 * here when op is EQ or NE and the lengths differ. That pays there,
535 * but Tim was unable to find any real code where EQ/NE tuple
536 * compares don't have the same length, so testing for it here would
537 * have cost without benefit.
538 */
539
540 /* Search for the first index where items are different.
541 * Note that because tuples are immutable, it's safe to reuse
542 * vlen and wlen across the comparison calls.
543 */
544 for (i = 0; i < vlen && i < wlen; i++) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000545 int k = PyObject_RichCompareBool(vt->ob_item[i],
546 wt->ob_item[i], Py_EQ);
547 if (k < 0)
548 return NULL;
549 if (!k)
550 break;
551 }
552
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000553 if (i >= vlen || i >= wlen) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000554 /* No more items to compare -- compare sizes */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000555 int cmp;
556 PyObject *res;
557 switch (op) {
Tim Petersd7ed3bf2001-05-15 20:12:59 +0000558 case Py_LT: cmp = vlen < wlen; break;
559 case Py_LE: cmp = vlen <= wlen; break;
560 case Py_EQ: cmp = vlen == wlen; break;
561 case Py_NE: cmp = vlen != wlen; break;
562 case Py_GT: cmp = vlen > wlen; break;
563 case Py_GE: cmp = vlen >= wlen; break;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000564 default: return NULL; /* cannot happen */
565 }
566 if (cmp)
567 res = Py_True;
568 else
569 res = Py_False;
570 Py_INCREF(res);
571 return res;
572 }
573
574 /* We have an item that differs -- shortcuts for EQ/NE */
575 if (op == Py_EQ) {
576 Py_INCREF(Py_False);
577 return Py_False;
578 }
579 if (op == Py_NE) {
580 Py_INCREF(Py_True);
581 return Py_True;
582 }
583
584 /* Compare the final item again using the proper operator */
585 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
586}
587
Jeremy Hylton938ace62002-07-17 16:30:39 +0000588static PyObject *
Guido van Rossumae960af2001-08-30 03:11:59 +0000589tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
590
Tim Peters6d6c1a32001-08-02 04:15:00 +0000591static PyObject *
592tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
593{
594 PyObject *arg = NULL;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000595 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000596
Guido van Rossumae960af2001-08-30 03:11:59 +0000597 if (type != &PyTuple_Type)
598 return tuple_subtype_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000599 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
600 return NULL;
601
602 if (arg == NULL)
603 return PyTuple_New(0);
604 else
605 return PySequence_Tuple(arg);
606}
607
Guido van Rossumae960af2001-08-30 03:11:59 +0000608static PyObject *
609tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
610{
Anthony Baxtera6286212006-04-11 07:42:36 +0000611 PyObject *tmp, *newobj, *item;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000612 Py_ssize_t i, n;
Guido van Rossumae960af2001-08-30 03:11:59 +0000613
614 assert(PyType_IsSubtype(type, &PyTuple_Type));
615 tmp = tuple_new(&PyTuple_Type, args, kwds);
616 if (tmp == NULL)
617 return NULL;
618 assert(PyTuple_Check(tmp));
Anthony Baxtera6286212006-04-11 07:42:36 +0000619 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
620 if (newobj == NULL)
Guido van Rossumae960af2001-08-30 03:11:59 +0000621 return NULL;
622 for (i = 0; i < n; i++) {
623 item = PyTuple_GET_ITEM(tmp, i);
624 Py_INCREF(item);
Anthony Baxtera6286212006-04-11 07:42:36 +0000625 PyTuple_SET_ITEM(newobj, i, item);
Guido van Rossumae960af2001-08-30 03:11:59 +0000626 }
627 Py_DECREF(tmp);
Anthony Baxtera6286212006-04-11 07:42:36 +0000628 return newobj;
Guido van Rossumae960af2001-08-30 03:11:59 +0000629}
630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000631PyDoc_STRVAR(tuple_doc,
Tim Peters1b8ca0d2001-09-02 06:42:25 +0000632"tuple() -> an empty tuple\n"
633"tuple(sequence) -> tuple initialized from sequence's items\n"
634"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000635"If the argument is a tuple, the return value is the same object.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637static PySequenceMethods tuple_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638 (lenfunc)tuplelength, /* sq_length */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000639 (binaryfunc)tupleconcat, /* sq_concat */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640 (ssizeargfunc)tuplerepeat, /* sq_repeat */
641 (ssizeargfunc)tupleitem, /* sq_item */
642 (ssizessizeargfunc)tupleslice, /* sq_slice */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000643 0, /* sq_ass_item */
644 0, /* sq_ass_slice */
645 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646};
647
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000648static PyObject*
649tuplesubscript(PyTupleObject* self, PyObject* item)
650{
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000651 if (PyIndex_Check(item)) {
652 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000653 if (i == -1 && PyErr_Occurred())
654 return NULL;
655 if (i < 0)
656 i += PyTuple_GET_SIZE(self);
657 return tupleitem(self, i);
658 }
659 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000661 PyObject* result;
662 PyObject* it;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000663 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000664
665 if (PySlice_GetIndicesEx((PySliceObject*)item,
666 PyTuple_GET_SIZE(self),
667 &start, &stop, &step, &slicelength) < 0) {
668 return NULL;
669 }
670
671 if (slicelength <= 0) {
672 return PyTuple_New(0);
673 }
Thomas Wouters3ccec682007-08-28 15:28:19 +0000674 else if (start == 0 && step == 1 &&
675 slicelength == PyTuple_GET_SIZE(self) &&
676 PyTuple_CheckExact(self)) {
677 Py_INCREF(self);
678 return (PyObject *)self;
679 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000680 else {
681 result = PyTuple_New(slicelength);
Georg Brandl5c170fd2006-03-17 19:03:25 +0000682 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000683
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000684 src = self->ob_item;
685 dest = ((PyTupleObject *)result)->ob_item;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000686 for (cur = start, i = 0; i < slicelength;
687 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000688 it = src[cur];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000689 Py_INCREF(it);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000690 dest[i] = it;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000691 }
692
693 return result;
694 }
695 }
696 else {
Georg Brandl283a1352006-11-19 08:48:30 +0000697 PyErr_Format(PyExc_TypeError,
698 "tuple indices must be integers, not %.200s",
Christian Heimese93237d2007-12-19 02:37:44 +0000699 Py_TYPE(item)->tp_name);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000700 return NULL;
701 }
702}
703
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000704static PyObject *
705tuple_getnewargs(PyTupleObject *v)
706{
Christian Heimese93237d2007-12-19 02:37:44 +0000707 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000708
709}
710
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000711PyDoc_STRVAR(index_doc,
712"T.index(value, [start, [stop]]) -> integer -- return first index of value");
713PyDoc_STRVAR(count_doc,
714"T.count(value) -> integer -- return number of occurrences of value");
715
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000716static PyMethodDef tuple_methods[] = {
717 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
Raymond Hettinger5b07ebc2008-02-07 00:54:20 +0000718 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
719 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000720 {NULL, NULL} /* sentinel */
721};
722
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000723static PyMappingMethods tuple_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000724 (lenfunc)tuplelength,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000725 (binaryfunc)tuplesubscript,
726 0
727};
728
Raymond Hettinger48923c52002-08-09 01:30:17 +0000729static PyObject *tuple_iter(PyObject *seq);
730
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731PyTypeObject PyTuple_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000732 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000733 "tuple",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000734 sizeof(PyTupleObject) - sizeof(PyObject *),
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 sizeof(PyObject *),
Guido van Rossumf77bc622001-01-18 00:00:53 +0000736 (destructor)tupledealloc, /* tp_dealloc */
737 (printfunc)tupleprint, /* tp_print */
738 0, /* tp_getattr */
739 0, /* tp_setattr */
740 0, /* tp_compare */
741 (reprfunc)tuplerepr, /* tp_repr */
742 0, /* tp_as_number */
743 &tuple_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000744 &tuple_as_mapping, /* tp_as_mapping */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000745 (hashfunc)tuplehash, /* tp_hash */
746 0, /* tp_call */
747 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000748 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000749 0, /* tp_setattro */
750 0, /* tp_as_buffer */
Guido van Rossumae960af2001-08-30 03:11:59 +0000751 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000752 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753 tuple_doc, /* tp_doc */
Guido van Rossumf77bc622001-01-18 00:00:53 +0000754 (traverseproc)tupletraverse, /* tp_traverse */
755 0, /* tp_clear */
756 tuplerichcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757 0, /* tp_weaklistoffset */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000758 tuple_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000760 tuple_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000761 0, /* tp_members */
762 0, /* tp_getset */
763 0, /* tp_base */
764 0, /* tp_dict */
765 0, /* tp_descr_get */
766 0, /* tp_descr_set */
767 0, /* tp_dictoffset */
768 0, /* tp_init */
769 0, /* tp_alloc */
770 tuple_new, /* tp_new */
Neil Schemenauer626d7742002-04-12 03:05:52 +0000771 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000773
774/* The following function breaks the notion that tuples are immutable:
775 it changes the size of a tuple. We get away with this only if there
776 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000777 as creating a new tuple object and destroying the old one, only more
778 efficiently. In any case, don't use this if the tuple may already be
Tim Peters4324aa32001-05-28 22:30:08 +0000779 known to some other part of the code. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000780
781int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000782_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000783{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784 register PyTupleObject *v;
785 register PyTupleObject *sv;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000786 Py_ssize_t i;
787 Py_ssize_t oldsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000788
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789 v = (PyTupleObject *) *pv;
Christian Heimese93237d2007-12-19 02:37:44 +0000790 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
791 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000792 *pv = 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000793 Py_XDECREF(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794 PyErr_BadInternalCall();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000795 return -1;
796 }
Christian Heimese93237d2007-12-19 02:37:44 +0000797 oldsize = Py_SIZE(v);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000798 if (oldsize == newsize)
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000799 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000800
Guido van Rossumf70590f2001-12-07 20:00:04 +0000801 if (oldsize == 0) {
Thomas Wouters6a922372001-05-28 13:11:02 +0000802 /* Empty tuples are often shared, so we should never
803 resize them in-place even if we do own the only
804 (current) reference */
805 Py_DECREF(v);
806 *pv = PyTuple_New(newsize);
Thomas Wouters0dcea592001-05-29 07:58:45 +0000807 return *pv == NULL ? -1 : 0;
Thomas Wouters6a922372001-05-28 13:11:02 +0000808 }
809
Guido van Rossum12d12c51993-10-26 17:58:25 +0000810 /* XXX UNREF/NEWREF interface should be more symmetrical */
Tim Peters34592512002-07-11 06:23:50 +0000811 _Py_DEC_REFTOTAL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000812 _PyObject_GC_UNTRACK(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000813 _Py_ForgetReference((PyObject *) v);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000814 /* DECREF items deleted by shrinkage */
815 for (i = newsize; i < oldsize; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816 Py_XDECREF(v->ob_item[i]);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000817 v->ob_item[i] = NULL;
818 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000819 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000820 if (sv == NULL) {
821 *pv = NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000822 PyObject_GC_Del(v);
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000823 return -1;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000824 }
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000825 _Py_NewReference((PyObject *) sv);
Guido van Rossumf70590f2001-12-07 20:00:04 +0000826 /* Zero out items added by growing */
Neal Norwitz35fc7602002-06-13 21:11:11 +0000827 if (newsize > oldsize)
Jeremy Hylton8b47dff2002-06-20 23:13:17 +0000828 memset(&sv->ob_item[oldsize], 0,
829 sizeof(*sv->ob_item) * (newsize - oldsize));
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000830 *pv = (PyObject *) sv;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000831 _PyObject_GC_TRACK(sv);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000832 return 0;
833}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000834
Christian Heimes3b718a72008-02-14 12:47:33 +0000835int
836PyTuple_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000837{
Christian Heimes3b718a72008-02-14 12:47:33 +0000838 int freelist_size = 0;
Christian Heimes5b970ad2008-02-06 13:33:44 +0000839#if PyTuple_MAXSAVESIZE > 0
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000840 int i;
Christian Heimes5b970ad2008-02-06 13:33:44 +0000841 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000842 PyTupleObject *p, *q;
Christian Heimes5b970ad2008-02-06 13:33:44 +0000843 p = free_list[i];
Christian Heimes3b718a72008-02-14 12:47:33 +0000844 freelist_size += numfree[i];
Christian Heimes5b970ad2008-02-06 13:33:44 +0000845 free_list[i] = NULL;
Christian Heimes3b718a72008-02-14 12:47:33 +0000846 numfree[i] = 0;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000847 while (p) {
848 q = p;
849 p = (PyTupleObject *)(p->ob_item[0]);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000850 PyObject_GC_Del(q);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000851 }
852 }
853#endif
Christian Heimes3b718a72008-02-14 12:47:33 +0000854 return freelist_size;
855}
856
857void
858PyTuple_Fini(void)
859{
860#if PyTuple_MAXSAVESIZE > 0
861 /* empty tuples are used all over the place and applications may
862 * rely on the fact that an empty tuple is a singleton. */
863 Py_XDECREF(free_list[0]);
864 free_list[0] = NULL;
865
866 (void)PyTuple_ClearFreeList();
867#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000868}
Raymond Hettinger48923c52002-08-09 01:30:17 +0000869
870/*********************** Tuple Iterator **************************/
871
872typedef struct {
873 PyObject_HEAD
874 long it_index;
875 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
876} tupleiterobject;
877
Raymond Hettinger48923c52002-08-09 01:30:17 +0000878static void
879tupleiter_dealloc(tupleiterobject *it)
880{
881 _PyObject_GC_UNTRACK(it);
882 Py_XDECREF(it->it_seq);
883 PyObject_GC_Del(it);
884}
885
886static int
887tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
888{
Thomas Woutersc6e55062006-04-15 21:47:09 +0000889 Py_VISIT(it->it_seq);
890 return 0;
Raymond Hettinger48923c52002-08-09 01:30:17 +0000891}
892
Raymond Hettinger48923c52002-08-09 01:30:17 +0000893static PyObject *
894tupleiter_next(tupleiterobject *it)
895{
896 PyTupleObject *seq;
897 PyObject *item;
898
899 assert(it != NULL);
900 seq = it->it_seq;
901 if (seq == NULL)
902 return NULL;
903 assert(PyTuple_Check(seq));
904
905 if (it->it_index < PyTuple_GET_SIZE(seq)) {
906 item = PyTuple_GET_ITEM(seq, it->it_index);
907 ++it->it_index;
908 Py_INCREF(item);
909 return item;
910 }
911
912 Py_DECREF(seq);
913 it->it_seq = NULL;
914 return NULL;
915}
916
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000917static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +0000918tupleiter_len(tupleiterobject *it)
919{
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000920 Py_ssize_t len = 0;
Raymond Hettinger435bf582004-03-18 22:43:10 +0000921 if (it->it_seq)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000922 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000923 return PyInt_FromSsize_t(len);
Raymond Hettinger435bf582004-03-18 22:43:10 +0000924}
925
Armin Rigof5b3e362006-02-11 21:32:43 +0000926PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000927
928static PyMethodDef tupleiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000929 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000930 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +0000931};
932
Raymond Hettinger48923c52002-08-09 01:30:17 +0000933PyTypeObject PyTupleIter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000934 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger48923c52002-08-09 01:30:17 +0000935 "tupleiterator", /* tp_name */
936 sizeof(tupleiterobject), /* tp_basicsize */
937 0, /* tp_itemsize */
938 /* methods */
939 (destructor)tupleiter_dealloc, /* tp_dealloc */
940 0, /* tp_print */
941 0, /* tp_getattr */
942 0, /* tp_setattr */
943 0, /* tp_compare */
944 0, /* tp_repr */
945 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000946 0, /* tp_as_sequence */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000947 0, /* tp_as_mapping */
948 0, /* tp_hash */
949 0, /* tp_call */
950 0, /* tp_str */
951 PyObject_GenericGetAttr, /* tp_getattro */
952 0, /* tp_setattro */
953 0, /* tp_as_buffer */
954 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
955 0, /* tp_doc */
956 (traverseproc)tupleiter_traverse, /* tp_traverse */
957 0, /* tp_clear */
958 0, /* tp_richcompare */
959 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000960 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger48923c52002-08-09 01:30:17 +0000961 (iternextfunc)tupleiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000962 tupleiter_methods, /* tp_methods */
963 0,
Raymond Hettinger48923c52002-08-09 01:30:17 +0000964};
Martin v. Löwis72d20672006-04-11 09:04:12 +0000965
966static PyObject *
967tuple_iter(PyObject *seq)
968{
969 tupleiterobject *it;
970
971 if (!PyTuple_Check(seq)) {
972 PyErr_BadInternalCall();
973 return NULL;
974 }
975 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
976 if (it == NULL)
977 return NULL;
978 it->it_index = 0;
979 Py_INCREF(seq);
980 it->it_seq = (PyTupleObject *)seq;
981 _PyObject_GC_TRACK(it);
982 return (PyObject *)it;
983}