blob: d40f9474eed265c60f4e5e7073f2be303f07d813 [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{
29 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000030 register PyTupleObject *op;
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 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044 if (0 < size && size < MAXSAVESIZE &&
45 (op = free_tuples[size]) != NULL)
46 {
47 free_tuples[size] = (PyTupleObject *) op->ob_item[0];
Guido van Rossum5ce78f82000-04-21 21:15:05 +000048 num_free_tuples[size]--;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000049#ifdef COUNT_ALLOCS
50 fast_tuple_allocs++;
51#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +000052 /* PyObject_InitVar is inlined */
Guido van Rossum68055ce1998-12-11 14:56:38 +000053#ifdef Py_TRACE_REFS
Guido van Rossum68055ce1998-12-11 14:56:38 +000054 op->ob_size = size;
Guido van Rossumb18618d2000-05-03 23:44:39 +000055 op->ob_type = &PyTuple_Type;
Guido van Rossum68055ce1998-12-11 14:56:38 +000056#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +000057 _Py_NewReference((PyObject *)op);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +000058 }
59 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000060#endif
61 {
Guido van Rossum5bc51f21999-07-12 23:06:58 +000062 int nbytes = size * sizeof(PyObject *);
63 /* Check for overflow */
64 if (nbytes / sizeof(PyObject *) != (size_t)size ||
Jeremy Hyltond08b4c42000-06-23 19:37:02 +000065 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000066 + PyGC_HEAD_SIZE)
Guido van Rossum5bc51f21999-07-12 23:06:58 +000067 <= 0)
68 {
69 return PyErr_NoMemory();
70 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000071 /* PyObject_NewVar is inlined */
72 op = (PyTupleObject *) PyObject_MALLOC(nbytes);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000073 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000075 op = (PyTupleObject *) PyObject_FROM_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +000076 PyObject_INIT_VAR(op, &PyTuple_Type, size);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 for (i = 0; i < size; i++)
79 op->ob_item[i] = NULL;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#if MAXSAVESIZE > 0
81 if (size == 0) {
Sjoerd Mullender615194a1993-11-01 13:46:50 +000082 free_tuples[0] = op;
Guido van Rossum5ce78f82000-04-21 21:15:05 +000083 ++num_free_tuples[0];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084 Py_INCREF(op); /* extra INCREF so that this is never freed */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000085 }
86#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000087 PyObject_GC_Init(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
91int
Fred Drakeba096332000-07-09 07:04:36 +000092PyTuple_Size(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000094 if (!PyTuple_Check(op)) {
95 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 return -1;
97 }
98 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 return ((PyTupleObject *)op)->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000103PyTuple_GetItem(register PyObject *op, register int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 if (!PyTuple_Check(op)) {
106 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107 return NULL;
108 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
110 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 return NULL;
112 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114}
115
116int
Fred Drakeba096332000-07-09 07:04:36 +0000117PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119 register PyObject *olditem;
120 register PyObject **p;
Guido van Rossum787bdd31997-08-17 16:25:45 +0000121 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 Py_XDECREF(newitem);
123 PyErr_BadInternalCall();
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 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
127 Py_XDECREF(newitem);
128 PyErr_SetString(PyExc_IndexError,
129 "tuple assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000130 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132 p = ((PyTupleObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000133 olditem = *p;
134 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136 return 0;
137}
138
139/* Methods */
140
141static void
Fred Drakeba096332000-07-09 07:04:36 +0000142tupledealloc(register PyTupleObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143{
144 register int i;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000145 register int len = op->ob_size;
Guido van Rossumd724b232000-03-13 16:01:29 +0000146 Py_TRASHCAN_SAFE_BEGIN(op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000147 PyObject_GC_Fini(op);
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000148 if (len > 0) {
149 i = len;
Guido van Rossum1bb26871998-06-26 15:53:50 +0000150 while (--i >= 0)
151 Py_XDECREF(op->ob_item[i]);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000152#if MAXSAVESIZE > 0
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000153 if (len < MAXSAVESIZE && num_free_tuples[len] < MAXSAVEDTUPLES) {
154 op->ob_item[0] = (PyObject *) free_tuples[len];
155 num_free_tuples[len]++;
156 free_tuples[len] = op;
Guido van Rossumd724b232000-03-13 16:01:29 +0000157 goto done; /* return */
Guido van Rossum1bb26871998-06-26 15:53:50 +0000158 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000159#endif
Guido van Rossum1bb26871998-06-26 15:53:50 +0000160 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000161 op = (PyTupleObject *) PyObject_AS_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000162 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000163done:
164 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
166
Guido van Rossum49e85141991-06-07 22:59:30 +0000167static int
Fred Drakeba096332000-07-09 07:04:36 +0000168tupleprint(PyTupleObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169{
170 int i;
171 fprintf(fp, "(");
Guido van Rossum49e85141991-06-07 22:59:30 +0000172 for (i = 0; i < op->ob_size; i++) {
173 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174 fprintf(fp, ", ");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
Guido van Rossum49e85141991-06-07 22:59:30 +0000176 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177 }
178 if (op->ob_size == 1)
179 fprintf(fp, ",");
180 fprintf(fp, ")");
Guido van Rossum49e85141991-06-07 22:59:30 +0000181 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000185tuplerepr(PyTupleObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000189 s = PyString_FromString("(");
190 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191 for (i = 0; i < v->ob_size && s != NULL; i++) {
192 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 PyString_Concat(&s, comma);
194 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 Py_DECREF(comma);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000197 if (v->ob_size == 1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198 PyString_ConcatAndDel(&s, PyString_FromString(","));
199 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200 return s;
201}
202
Guido van Rossum9bfef441993-03-29 10:43:31 +0000203static long
Fred Drakeba096332000-07-09 07:04:36 +0000204tuplehash(PyTupleObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000205{
206 register long x, y;
207 register int len = v->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 register PyObject **p;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000209 x = 0x345678L;
210 p = v->ob_item;
211 while (--len >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 y = PyObject_Hash(*p++);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000213 if (y == -1)
214 return -1;
Guido van Rossum0969ad21996-12-16 17:55:46 +0000215 x = (1000003*x) ^ y;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000216 }
217 x ^= v->ob_size;
218 if (x == -1)
219 x = -2;
220 return x;
221}
222
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223static int
Fred Drakeba096332000-07-09 07:04:36 +0000224tuplelength(PyTupleObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
226 return a->ob_size;
227}
228
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000229static int
Fred Drakeba096332000-07-09 07:04:36 +0000230tuplecontains(PyTupleObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000231{
232 int i, cmp;
233
234 for (i = 0; i < a->ob_size; ++i) {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000235 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
236 Py_EQ);
237 if (cmp > 0)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000238 return 1;
Guido van Rossumf77bc622001-01-18 00:00:53 +0000239 else if (cmp < 0)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000240 return -1;
241 }
242 return 0;
243}
244
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000246tupleitem(register PyTupleObject *a, register int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247{
248 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000249 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 return NULL;
251 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253 return a->ob_item[i];
254}
255
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000257tupleslice(register PyTupleObject *a, register int ilow, register int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000259 register PyTupleObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260 register int i;
261 if (ilow < 0)
262 ilow = 0;
263 if (ihigh > a->ob_size)
264 ihigh = a->ob_size;
265 if (ihigh < ilow)
266 ihigh = ilow;
267 if (ilow == 0 && ihigh == a->ob_size) {
268 /* XXX can only do this if tuples are immutable! */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000269 Py_INCREF(a);
270 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000272 np = (PyTupleObject *)PyTuple_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273 if (np == NULL)
274 return NULL;
275 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 PyObject *v = a->ob_item[i];
277 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278 np->ob_item[i - ilow] = v;
279 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281}
282
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000284PyTuple_GetSlice(PyObject *op, int i, int j)
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000285{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000286 if (op == NULL || !PyTuple_Check(op)) {
287 PyErr_BadInternalCall();
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000288 return NULL;
289 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000291}
292
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000294tupleconcat(register PyTupleObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295{
296 register int size;
297 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000298 PyTupleObject *np;
299 if (!PyTuple_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000300 PyErr_Format(PyExc_TypeError,
Marc-André Lemburg29dc3812000-06-16 17:05:57 +0000301 "can only concatenate tuple (not \"%.200s\") to tuple",
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000302 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303 return NULL;
304 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000305#define b ((PyTupleObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308 if (np == NULL) {
Guido van Rossum49e85141991-06-07 22:59:30 +0000309 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310 }
311 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312 PyObject *v = a->ob_item[i];
313 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314 np->ob_item[i] = v;
315 }
316 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000317 PyObject *v = b->ob_item[i];
318 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319 np->ob_item[i + a->ob_size] = v;
320 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000321 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322#undef b
323}
324
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000326tuplerepeat(PyTupleObject *a, int n)
Guido van Rossumb8393da1991-06-04 19:35:24 +0000327{
328 int i, j;
329 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330 PyTupleObject *np;
331 PyObject **p;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000332 if (n < 0)
333 n = 0;
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000334 if (a->ob_size == 0 || n == 1) {
Guido van Rossumb8393da1991-06-04 19:35:24 +0000335 /* Since tuples are immutable, we can return a shared
336 copy in this case */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337 Py_INCREF(a);
338 return (PyObject *)a;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000339 }
340 size = a->ob_size * n;
Guido van Rossum0eb55ac1999-07-13 05:41:12 +0000341 if (size/a->ob_size != n)
Guido van Rossum5bc51f21999-07-12 23:06:58 +0000342 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000344 if (np == NULL)
345 return NULL;
346 p = np->ob_item;
347 for (i = 0; i < n; i++) {
348 for (j = 0; j < a->ob_size; j++) {
349 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350 Py_INCREF(*p);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000351 p++;
352 }
353 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000355}
356
Jeremy Hylton8caad492000-06-23 14:18:11 +0000357static int
358tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
359{
360 int i, err;
361 PyObject *x;
362
363 for (i = o->ob_size; --i >= 0; ) {
364 x = o->ob_item[i];
365 if (x != NULL) {
366 err = visit(x, arg);
367 if (err)
368 return err;
369 }
370 }
371 return 0;
372}
373
Guido van Rossumf77bc622001-01-18 00:00:53 +0000374static PyObject *
375tuplerichcompare(PyObject *v, PyObject *w, int op)
376{
377 PyTupleObject *vt, *wt;
378 int i;
379
380 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
381 Py_INCREF(Py_NotImplemented);
382 return Py_NotImplemented;
383 }
384
385 vt = (PyTupleObject *)v;
386 wt = (PyTupleObject *)w;
387
388 if (vt->ob_size != wt->ob_size && (op == Py_EQ || op == Py_NE)) {
389 /* Shortcut: if the lengths differ, the tuples differ */
390 PyObject *res;
391 if (op == Py_EQ)
392 res = Py_False;
393 else
394 res = Py_True;
395 Py_INCREF(res);
396 return res;
397 }
398
399 /* Search for the first index where items are different */
400 for (i = 0; i < vt->ob_size && i < wt->ob_size; i++) {
401 int k = PyObject_RichCompareBool(vt->ob_item[i],
402 wt->ob_item[i], Py_EQ);
403 if (k < 0)
404 return NULL;
405 if (!k)
406 break;
407 }
408
409 if (i >= vt->ob_size || i >= wt->ob_size) {
410 /* No more items to compare -- compare sizes */
411 int vs = vt->ob_size;
412 int ws = wt->ob_size;
413 int cmp;
414 PyObject *res;
415 switch (op) {
416 case Py_LT: cmp = vs < ws; break;
417 case Py_LE: cmp = ws <= ws; break;
418 case Py_EQ: cmp = vs == ws; break;
419 case Py_NE: cmp = vs != ws; break;
420 case Py_GT: cmp = vs > ws; break;
421 case Py_GE: cmp = vs >= ws; break;
422 default: return NULL; /* cannot happen */
423 }
424 if (cmp)
425 res = Py_True;
426 else
427 res = Py_False;
428 Py_INCREF(res);
429 return res;
430 }
431
432 /* We have an item that differs -- shortcuts for EQ/NE */
433 if (op == Py_EQ) {
434 Py_INCREF(Py_False);
435 return Py_False;
436 }
437 if (op == Py_NE) {
438 Py_INCREF(Py_True);
439 return Py_True;
440 }
441
442 /* Compare the final item again using the proper operator */
443 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
444}
445
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446static PySequenceMethods tuple_as_sequence = {
Guido van Rossumf77bc622001-01-18 00:00:53 +0000447 (inquiry)tuplelength, /* sq_length */
448 (binaryfunc)tupleconcat, /* sq_concat */
449 (intargfunc)tuplerepeat, /* sq_repeat */
450 (intargfunc)tupleitem, /* sq_item */
451 (intintargfunc)tupleslice, /* sq_slice */
452 0, /* sq_ass_item */
453 0, /* sq_ass_slice */
454 (objobjproc)tuplecontains, /* sq_contains */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455};
456
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457PyTypeObject PyTuple_Type = {
458 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459 0,
460 "tuple",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000461 sizeof(PyTupleObject) - sizeof(PyObject *) + PyGC_HEAD_SIZE,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462 sizeof(PyObject *),
Guido van Rossumf77bc622001-01-18 00:00:53 +0000463 (destructor)tupledealloc, /* tp_dealloc */
464 (printfunc)tupleprint, /* tp_print */
465 0, /* tp_getattr */
466 0, /* tp_setattr */
467 0, /* tp_compare */
468 (reprfunc)tuplerepr, /* tp_repr */
469 0, /* tp_as_number */
470 &tuple_as_sequence, /* tp_as_sequence */
471 0, /* tp_as_mapping */
472 (hashfunc)tuplehash, /* tp_hash */
473 0, /* tp_call */
474 0, /* tp_str */
475 0, /* tp_getattro */
476 0, /* tp_setattro */
477 0, /* tp_as_buffer */
478 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
479 0, /* tp_doc */
480 (traverseproc)tupletraverse, /* tp_traverse */
481 0, /* tp_clear */
482 tuplerichcompare, /* tp_richcompare */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000484
485/* The following function breaks the notion that tuples are immutable:
486 it changes the size of a tuple. We get away with this only if there
487 is only one module referencing the object. You can also think of it
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000488 as creating a new tuple object and destroying the old one, only more
489 efficiently. In any case, don't use this if the tuple may already be
490 known to some other part of the code. The last_is_sticky is not used
491 and must always be false. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000492
493int
Fred Drakeba096332000-07-09 07:04:36 +0000494_PyTuple_Resize(PyObject **pv, int newsize, int last_is_sticky)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000495{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496 register PyTupleObject *v;
497 register PyTupleObject *sv;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000498 int i;
499 int sizediff;
500
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 v = (PyTupleObject *) *pv;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000502 if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1 ||
503 last_is_sticky) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000504 *pv = 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000505 Py_XDECREF(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 PyErr_BadInternalCall();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000507 return -1;
508 }
Guido van Rossum055968c1995-08-04 04:05:10 +0000509 sizediff = newsize - v->ob_size;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000510 if (sizediff == 0)
511 return 0;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000512
Guido van Rossum12d12c51993-10-26 17:58:25 +0000513 /* XXX UNREF/NEWREF interface should be more symmetrical */
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000514#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000515 --_Py_RefTotal;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000516#endif
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000517 _Py_ForgetReference((PyObject *) v);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000518 for (i = newsize; i < v->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519 Py_XDECREF(v->ob_item[i]);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000520 v->ob_item[i] = NULL;
521 }
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000522 PyObject_GC_Fini(v);
523 v = (PyTupleObject *) PyObject_AS_GC(v);
524 sv = (PyTupleObject *) PyObject_REALLOC((char *)v,
525 sizeof(PyTupleObject)
526 + PyGC_HEAD_SIZE
527 + newsize * sizeof(PyObject *));
528 if (sv == NULL) {
529 *pv = NULL;
530 PyObject_DEL(v);
531 PyErr_NoMemory();
532 return -1;
Guido van Rossum5ce78f82000-04-21 21:15:05 +0000533 }
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000534 sv = (PyTupleObject *) PyObject_FROM_GC(sv);
535 _Py_NewReference((PyObject *) sv);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000536 for (i = sv->ob_size; i < newsize; i++)
537 sv->ob_item[i] = NULL;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000538 sv->ob_size = newsize;
Neil Schemenauer08b53e62000-10-05 19:36:49 +0000539 *pv = (PyObject *) sv;
540 PyObject_GC_Init(sv);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000541 return 0;
542}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000543
544void
Fred Drakeba096332000-07-09 07:04:36 +0000545PyTuple_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000546{
547#if MAXSAVESIZE > 0
548 int i;
549
550 Py_XDECREF(free_tuples[0]);
551 free_tuples[0] = NULL;
552
553 for (i = 1; i < MAXSAVESIZE; i++) {
554 PyTupleObject *p, *q;
555 p = free_tuples[i];
556 free_tuples[i] = NULL;
557 while (p) {
558 q = p;
559 p = (PyTupleObject *)(p->ob_item[0]);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000560 q = (PyTupleObject *) PyObject_AS_GC(q);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000561 PyObject_DEL(q);
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000562 }
563 }
564#endif
565}