blob: 4b7714c9cf6287596871ae6091916f94c7477da3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Tuple object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000036#ifndef MAXSAVESIZE
37#define MAXSAVESIZE 20
38#endif
39
40#if MAXSAVESIZE > 0
41/* Entries 1 upto MAXSAVESIZE are free lists, entry 0 is the empty
42 tuple () of which at most one instance will be allocated.
43*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044static PyTupleObject *free_tuples[MAXSAVESIZE];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000045#endif
46#ifdef COUNT_ALLOCS
47int fast_tuple_allocs;
48int tuple_zero_allocs;
49#endif
50
Guido van Rossumc0b618a1997-05-02 03:12:38 +000051PyObject *
52PyTuple_New(size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053 register int size;
54{
55 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 register PyTupleObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000058 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 return NULL;
60 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000061#if MAXSAVESIZE > 0
Sjoerd Mullender615194a1993-11-01 13:46:50 +000062 if (size == 0 && free_tuples[0]) {
63 op = free_tuples[0];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064 Py_INCREF(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000065#ifdef COUNT_ALLOCS
66 tuple_zero_allocs++;
67#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 return (PyObject *) op;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070 if (0 < size && size < MAXSAVESIZE &&
71 (op = free_tuples[size]) != NULL)
72 {
73 free_tuples[size] = (PyTupleObject *) op->ob_item[0];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000074#ifdef COUNT_ALLOCS
75 fast_tuple_allocs++;
76#endif
Guido van Rossum68055ce1998-12-11 14:56:38 +000077#ifdef Py_TRACE_REFS
78 op->ob_type = &PyTuple_Type;
79 op->ob_size = size;
80#endif
Guido van Rossumfbbd57e1997-08-05 02:16:08 +000081 }
82 else
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000083#endif
84 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 op = (PyTupleObject *) malloc(
Guido van Rossum017f7781998-11-16 18:56:03 +000086 sizeof(PyTupleObject) + (size-1) * sizeof(PyObject *));
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000087 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 return PyErr_NoMemory();
Guido van Rossum1bb26871998-06-26 15:53:50 +000089
90 op->ob_type = &PyTuple_Type;
91 op->ob_size = size;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000092 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 for (i = 0; i < size; i++)
94 op->ob_item[i] = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 _Py_NewReference(op);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000096#if MAXSAVESIZE > 0
97 if (size == 0) {
Sjoerd Mullender615194a1993-11-01 13:46:50 +000098 free_tuples[0] = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 Py_INCREF(op); /* extra INCREF so that this is never freed */
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000100 }
101#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103}
104
105int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106PyTuple_Size(op)
107 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 if (!PyTuple_Check(op)) {
110 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 return -1;
112 }
113 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 return ((PyTupleObject *)op)->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115}
116
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117PyObject *
118PyTuple_GetItem(op, i)
119 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 register int i;
121{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 if (!PyTuple_Check(op)) {
123 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 return NULL;
125 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
127 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return NULL;
129 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 return ((PyTupleObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131}
132
133int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134PyTuple_SetItem(op, i, newitem)
135 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139 register PyObject *olditem;
140 register PyObject **p;
Guido van Rossum787bdd31997-08-17 16:25:45 +0000141 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 Py_XDECREF(newitem);
143 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000144 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000146 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
147 Py_XDECREF(newitem);
148 PyErr_SetString(PyExc_IndexError,
149 "tuple assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000150 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 p = ((PyTupleObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000153 olditem = *p;
154 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 return 0;
157}
158
159/* Methods */
160
161static void
162tupledealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 register PyTupleObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164{
165 register int i;
Guido van Rossum1bb26871998-06-26 15:53:50 +0000166
167 if (op->ob_size > 0) {
168 i = op->ob_size;
169 while (--i >= 0)
170 Py_XDECREF(op->ob_item[i]);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000171#if MAXSAVESIZE > 0
Guido van Rossum1bb26871998-06-26 15:53:50 +0000172 if (op->ob_size < MAXSAVESIZE) {
173 op->ob_item[0] = (PyObject *) free_tuples[op->ob_size];
174 free_tuples[op->ob_size] = op;
175 return;
176 }
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000177#endif
Guido van Rossum1bb26871998-06-26 15:53:50 +0000178 }
179 free((ANY *)op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Guido van Rossum49e85141991-06-07 22:59:30 +0000182static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183tupleprint(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 PyTupleObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185 FILE *fp;
186 int flags;
187{
188 int i;
189 fprintf(fp, "(");
Guido van Rossum49e85141991-06-07 22:59:30 +0000190 for (i = 0; i < op->ob_size; i++) {
191 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192 fprintf(fp, ", ");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
Guido van Rossum49e85141991-06-07 22:59:30 +0000194 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195 }
196 if (op->ob_size == 1)
197 fprintf(fp, ",");
198 fprintf(fp, ")");
Guido van Rossum49e85141991-06-07 22:59:30 +0000199 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200}
201
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203tuplerepr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 PyTupleObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 s = PyString_FromString("(");
209 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 for (i = 0; i < v->ob_size && s != NULL; i++) {
211 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 PyString_Concat(&s, comma);
213 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 Py_DECREF(comma);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000216 if (v->ob_size == 1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000217 PyString_ConcatAndDel(&s, PyString_FromString(","));
218 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219 return s;
220}
221
222static int
223tuplecompare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 register PyTupleObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
226 register int len =
227 (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
228 register int i;
229 for (i = 0; i < len; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231 if (cmp != 0)
232 return cmp;
233 }
234 return v->ob_size - w->ob_size;
235}
236
Guido van Rossum9bfef441993-03-29 10:43:31 +0000237static long
238tuplehash(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239 PyTupleObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000240{
241 register long x, y;
242 register int len = v->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243 register PyObject **p;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000244 x = 0x345678L;
245 p = v->ob_item;
246 while (--len >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 y = PyObject_Hash(*p++);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000248 if (y == -1)
249 return -1;
Guido van Rossum0969ad21996-12-16 17:55:46 +0000250 x = (1000003*x) ^ y;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000251 }
252 x ^= v->ob_size;
253 if (x == -1)
254 x = -2;
255 return x;
256}
257
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258static int
259tuplelength(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260 PyTupleObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261{
262 return a->ob_size;
263}
264
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266tupleitem(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267 register PyTupleObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 register int i;
269{
270 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000271 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272 return NULL;
273 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000274 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275 return a->ob_item[i];
276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279tupleslice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 register PyTupleObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 register int ilow, ihigh;
282{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283 register PyTupleObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 register int i;
285 if (ilow < 0)
286 ilow = 0;
287 if (ihigh > a->ob_size)
288 ihigh = a->ob_size;
289 if (ihigh < ilow)
290 ihigh = ilow;
291 if (ilow == 0 && ihigh == a->ob_size) {
292 /* XXX can only do this if tuples are immutable! */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 Py_INCREF(a);
294 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 np = (PyTupleObject *)PyTuple_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297 if (np == NULL)
298 return NULL;
299 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300 PyObject *v = a->ob_item[i];
301 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302 np->ob_item[i - ilow] = v;
303 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305}
306
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307PyObject *
308PyTuple_GetSlice(op, i, j)
309 PyObject *op;
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000310 int i, j;
311{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312 if (op == NULL || !PyTuple_Check(op)) {
313 PyErr_BadInternalCall();
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000314 return NULL;
315 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000316 return tupleslice((PyTupleObject *)op, i, j);
Guido van Rossum7c36ad71992-01-14 18:45:33 +0000317}
318
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320tupleconcat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000321 register PyTupleObject *a;
322 register PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323{
324 register int size;
325 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 PyTupleObject *np;
327 if (!PyTuple_Check(bb)) {
328 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 return NULL;
330 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331#define b ((PyTupleObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334 if (np == NULL) {
Guido van Rossum49e85141991-06-07 22:59:30 +0000335 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 }
337 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338 PyObject *v = a->ob_item[i];
339 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340 np->ob_item[i] = v;
341 }
342 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 PyObject *v = b->ob_item[i];
344 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 np->ob_item[i + a->ob_size] = v;
346 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348#undef b
349}
350
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351static PyObject *
Guido van Rossumb8393da1991-06-04 19:35:24 +0000352tuplerepeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353 PyTupleObject *a;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000354 int n;
355{
356 int i, j;
357 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 PyTupleObject *np;
359 PyObject **p;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000360 if (n < 0)
361 n = 0;
362 if (a->ob_size*n == a->ob_size) {
363 /* Since tuples are immutable, we can return a shared
364 copy in this case */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365 Py_INCREF(a);
366 return (PyObject *)a;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000367 }
368 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 np = (PyTupleObject *) PyTuple_New(size);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000370 if (np == NULL)
371 return NULL;
372 p = np->ob_item;
373 for (i = 0; i < n; i++) {
374 for (j = 0; j < a->ob_size; j++) {
375 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376 Py_INCREF(*p);
Guido van Rossumb8393da1991-06-04 19:35:24 +0000377 p++;
378 }
379 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 return (PyObject *) np;
Guido van Rossumb8393da1991-06-04 19:35:24 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PySequenceMethods tuple_as_sequence = {
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000384 (inquiry)tuplelength, /*sq_length*/
385 (binaryfunc)tupleconcat, /*sq_concat*/
386 (intargfunc)tuplerepeat, /*sq_repeat*/
387 (intargfunc)tupleitem, /*sq_item*/
388 (intintargfunc)tupleslice, /*sq_slice*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389 0, /*sq_ass_item*/
390 0, /*sq_ass_slice*/
391};
392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393PyTypeObject PyTuple_Type = {
394 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395 0,
396 "tuple",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 sizeof(PyTupleObject) - sizeof(PyObject *),
398 sizeof(PyObject *),
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000399 (destructor)tupledealloc, /*tp_dealloc*/
400 (printfunc)tupleprint, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 0, /*tp_getattr*/
402 0, /*tp_setattr*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000403 (cmpfunc)tuplecompare, /*tp_compare*/
404 (reprfunc)tuplerepr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 0, /*tp_as_number*/
406 &tuple_as_sequence, /*tp_as_sequence*/
407 0, /*tp_as_mapping*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000408 (hashfunc)tuplehash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409};
Guido van Rossum12d12c51993-10-26 17:58:25 +0000410
411/* The following function breaks the notion that tuples are immutable:
412 it changes the size of a tuple. We get away with this only if there
413 is only one module referencing the object. You can also think of it
414 as creating a new tuple object and destroying the old one, only
415 more efficiently. In any case, don't use this if the tuple may
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000416 already be known to some other part of the code...
417 If last_is_sticky is set, the tuple will grow or shrink at the
418 front, otherwise it will grow or shrink at the end. */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000419
420int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421_PyTuple_Resize(pv, newsize, last_is_sticky)
422 PyObject **pv;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000423 int newsize;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000424 int last_is_sticky;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000425{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426 register PyTupleObject *v;
427 register PyTupleObject *sv;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000428 int i;
429 int sizediff;
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 v = (PyTupleObject *) *pv;
432 if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000433 *pv = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434 Py_DECREF(v);
435 PyErr_BadInternalCall();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000436 return -1;
437 }
Guido van Rossum055968c1995-08-04 04:05:10 +0000438 sizediff = newsize - v->ob_size;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000439 if (sizediff == 0)
440 return 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000441 /* XXX UNREF/NEWREF interface should be more symmetrical */
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000442#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000443 --_Py_RefTotal;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000444#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 _Py_ForgetReference(v);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000446 if (last_is_sticky && sizediff < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 /* shrinking:
448 move entries to the front and zero moved entries */
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000449 for (i = 0; i < newsize; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000450 Py_XDECREF(v->ob_item[i]);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000451 v->ob_item[i] = v->ob_item[i - sizediff];
452 v->ob_item[i - sizediff] = NULL;
453 }
454 }
455 for (i = newsize; i < v->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456 Py_XDECREF(v->ob_item[i]);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000457 v->ob_item[i] = NULL;
458 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459 sv = (PyTupleObject *)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000460 realloc((char *)v,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 sizeof(PyTupleObject) + newsize * sizeof(PyObject *));
462 *pv = (PyObject *) sv;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000463 if (sv == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464 PyMem_DEL(v);
465 PyErr_NoMemory();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000466 return -1;
467 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 _Py_NewReference(sv);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000469 for (i = sv->ob_size; i < newsize; i++)
470 sv->ob_item[i] = NULL;
471 if (last_is_sticky && sizediff > 0) {
472 /* growing: move entries to the end and zero moved entries */
473 for (i = newsize - 1; i >= sizediff; i--) {
474 sv->ob_item[i] = sv->ob_item[i - sizediff];
475 sv->ob_item[i - sizediff] = NULL;
476 }
477 }
478 sv->ob_size = newsize;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000479 return 0;
480}
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000481
482void
483PyTuple_Fini()
484{
485#if MAXSAVESIZE > 0
486 int i;
487
488 Py_XDECREF(free_tuples[0]);
489 free_tuples[0] = NULL;
490
491 for (i = 1; i < MAXSAVESIZE; i++) {
492 PyTupleObject *p, *q;
493 p = free_tuples[i];
494 free_tuples[i] = NULL;
495 while (p) {
496 q = p;
497 p = (PyTupleObject *)(p->ob_item[0]);
498 PyMem_DEL(q);
499 }
500 }
501#endif
502}