annotate Objects/tupleobject.c @ 12713:7efbb4e6afca legacy-trunk

macfsn should always be included, like exceptions and site.
author Jack Jansen <jack.jansen@cwi.nl>
date Wed, 17 May 2000 13:27:09 +0000
parents 1c7c2dd1beb1
children 4272b368bf69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* Tuple object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
34 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
36 /* Speed optimization to avoid frequent malloc/free of small tuples */
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
37 #ifndef MAXSAVESIZE
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
38 #define MAXSAVESIZE 20 /* Largest tuple to save on free list */
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
39 #endif
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
40 #ifndef MAXSAVEDTUPLES
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
41 #define MAXSAVEDTUPLES 2000 /* Maximum number of tuples of each size to save */
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
42 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
43
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
44 #if MAXSAVESIZE > 0
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
45 /* Entries 1 up to MAXSAVESIZE are free lists, entry 0 is the empty
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
46 tuple () of which at most one instance will be allocated.
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
47 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
48 static PyTupleObject *free_tuples[MAXSAVESIZE];
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
49 static int num_free_tuples[MAXSAVESIZE];
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
50 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
51 #ifdef COUNT_ALLOCS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
52 int fast_tuple_allocs;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
53 int tuple_zero_allocs;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
54 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
55
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
56 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
57 PyTuple_New(size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 register int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 register int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
61 register PyTupleObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if (size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
63 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 }
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
66 #if MAXSAVESIZE > 0
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
67 if (size == 0 && free_tuples[0]) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
68 op = free_tuples[0];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
69 Py_INCREF(op);
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
70 #ifdef COUNT_ALLOCS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
71 tuple_zero_allocs++;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
72 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
73 return (PyObject *) op;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
74 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
75 if (0 < size && size < MAXSAVESIZE &&
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
76 (op = free_tuples[size]) != NULL)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
77 {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
78 free_tuples[size] = (PyTupleObject *) op->ob_item[0];
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
79 num_free_tuples[size]--;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
80 #ifdef COUNT_ALLOCS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
81 fast_tuple_allocs++;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
82 #endif
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
83 /* PyObject_InitVar is inlined */
9632
ac5345814ba0 When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents: 9484
diff changeset
84 #ifdef Py_TRACE_REFS
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
85 op->ob_size = size;
9632
ac5345814ba0 When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents: 9484
diff changeset
86 op->ob_type = &PyTuple_Type;
ac5345814ba0 When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents: 9484
diff changeset
87 #endif
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
88 _Py_NewReference((PyObject *)op);
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
89 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
90 else
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
91 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
92 {
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
93 int nbytes = size * sizeof(PyObject *);
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
94 /* Check for overflow */
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
95 if (nbytes / sizeof(PyObject *) != (size_t)size ||
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
96 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *))
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
97 <= 0)
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
98 {
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
99 return PyErr_NoMemory();
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
100 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
101 /* PyObject_NewVar is inlined */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
102 op = (PyTupleObject *) PyObject_MALLOC(nbytes);
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
103 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
104 return PyErr_NoMemory();
8559
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
105
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
106 PyObject_INIT_VAR(op, &PyTuple_Type, size);
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
107 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 for (i = 0; i < size; i++)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 op->ob_item[i] = NULL;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
110 #if MAXSAVESIZE > 0
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
111 if (size == 0) {
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
112 free_tuples[0] = op;
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
113 ++num_free_tuples[0];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
114 Py_INCREF(op); /* extra INCREF so that this is never freed */
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
115 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
116 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
117 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
121 PyTuple_Size(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
122 register PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
124 if (!PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
125 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
129 return ((PyTupleObject *)op)->ob_size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
132 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
133 PyTuple_GetItem(op, i)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
134 register PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
137 if (!PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
138 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
141 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
142 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
145 return ((PyTupleObject *)op) -> ob_item[i];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
149 PyTuple_SetItem(op, i, newitem)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
150 register PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 register int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
152 PyObject *newitem;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
154 register PyObject *olditem;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
155 register PyObject **p;
6083
f159044530b1 PyTuple_SetItem should require that the tuple's refcnt is one!
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
156 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
157 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
158 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
159 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
161 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
162 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
163 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
164 "tuple assignment index out of range");
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
165 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
167 p = ((PyTupleObject *)op) -> ob_item + i;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
168 olditem = *p;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
169 *p = newitem;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
170 Py_XDECREF(olditem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 static void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 tupledealloc(op)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
178 register PyTupleObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 register int i;
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
181 register int len = op->ob_size;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
182 Py_TRASHCAN_SAFE_BEGIN(op)
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
183 if (len > 0) {
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
184 i = len;
8559
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
185 while (--i >= 0)
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
186 Py_XDECREF(op->ob_item[i]);
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
187 #if MAXSAVESIZE > 0
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
188 if (len < MAXSAVESIZE && num_free_tuples[len] < MAXSAVEDTUPLES) {
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
189 op->ob_item[0] = (PyObject *) free_tuples[len];
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
190 num_free_tuples[len]++;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
191 free_tuples[len] = op;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
192 goto done; /* return */
8559
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
193 }
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
194 #endif
8559
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
195 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
196 PyObject_DEL(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
197 done:
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
198 Py_TRASHCAN_SAFE_END(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
201 static int
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 tupleprint(op, fp, flags)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
203 PyTupleObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 int flags;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 fprintf(fp, "(");
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
209 for (i = 0; i < op->ob_size; i++) {
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
210 if (i > 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 fprintf(fp, ", ");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
212 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
213 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 if (op->ob_size == 1)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 fprintf(fp, ",");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 fprintf(fp, ")");
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
218 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
221 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 tuplerepr(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
223 PyTupleObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
225 PyObject *s, *comma;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
227 s = PyString_FromString("(");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
228 comma = PyString_FromString(", ");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 for (i = 0; i < v->ob_size && s != NULL; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 if (i > 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
231 PyString_Concat(&s, comma);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
232 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
234 Py_DECREF(comma);
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
235 if (v->ob_size == 1)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
236 PyString_ConcatAndDel(&s, PyString_FromString(","));
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
237 PyString_ConcatAndDel(&s, PyString_FromString(")"));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 return s;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 static int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 tuplecompare(v, w)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
243 register PyTupleObject *v, *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 register int len =
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 for (i = 0; i < len; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
249 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 if (cmp != 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 return cmp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 return v->ob_size - w->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
256 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
257 tuplehash(v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
258 PyTupleObject *v;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
259 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
260 register long x, y;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
261 register int len = v->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
262 register PyObject **p;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
263 x = 0x345678L;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
264 p = v->ob_item;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
265 while (--len >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
266 y = PyObject_Hash(*p++);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
267 if (y == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
268 return -1;
4733
3c7812bfa1a4 Better tuple hash function.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
269 x = (1000003*x) ^ y;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
270 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
271 x ^= v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
272 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
273 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
274 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
275 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
276
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 static int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 tuplelength(a)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
279 PyTupleObject *a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
284 static int
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
285 tuplecontains(a, el)
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
286 PyTupleObject *a;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
287 PyObject *el;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
288 {
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
289 int i, cmp;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
290
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
291 for (i = 0; i < a->ob_size; ++i) {
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
292 cmp = PyObject_Compare(el, PyTuple_GET_ITEM(a, i));
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
293 if (cmp == 0)
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
294 return 1;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
295 if (PyErr_Occurred())
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
296 return -1;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
297 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
298 return 0;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
299 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
300
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
301 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 tupleitem(a, i)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
303 register PyTupleObject *a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 if (i < 0 || i >= a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
307 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
310 Py_INCREF(a->ob_item[i]);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 return a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
314 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 tupleslice(a, ilow, ihigh)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
316 register PyTupleObject *a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 register int ilow, ihigh;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
319 register PyTupleObject *np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 ihigh = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 if (ilow == 0 && ihigh == a->ob_size) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 /* XXX can only do this if tuples are immutable! */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
329 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
330 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
332 np = (PyTupleObject *)PyTuple_New(ihigh - ilow);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 if (np == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 for (i = ilow; i < ihigh; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
336 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
337 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 np->ob_item[i - ilow] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
340 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
343 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
344 PyTuple_GetSlice(op, i, j)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
345 PyObject *op;
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
346 int i, j;
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
347 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
348 if (op == NULL || !PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
349 PyErr_BadInternalCall();
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
350 return NULL;
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
351 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
352 return tupleslice((PyTupleObject *)op, i, j);
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
353 }
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
354
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
355 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 tupleconcat(a, bb)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
357 register PyTupleObject *a;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
358 register PyObject *bb;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 register int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 register int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
362 PyTupleObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
363 if (!PyTuple_Check(bb)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
364 PyErr_BadArgument();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
367 #define b ((PyTupleObject *)bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 size = a->ob_size + b->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
369 np = (PyTupleObject *) PyTuple_New(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 if (np == NULL) {
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
371 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 for (i = 0; i < a->ob_size; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
374 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
375 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 np->ob_item[i] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 for (i = 0; i < b->ob_size; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
379 PyObject *v = b->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
380 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 np->ob_item[i + a->ob_size] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
383 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
386
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
387 static PyObject *
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
388 tuplerepeat(a, n)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
389 PyTupleObject *a;
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
390 int n;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
391 {
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
392 int i, j;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
393 int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
394 PyTupleObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
395 PyObject **p;
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
396 if (n < 0)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
397 n = 0;
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
398 if (a->ob_size == 0 || n == 1) {
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
399 /* Since tuples are immutable, we can return a shared
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
400 copy in this case */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
401 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
402 return (PyObject *)a;
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
403 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
404 size = a->ob_size * n;
11128
a4b7242a4b74 Mark Favas was quick to note that the last checkin divides by zero
Guido van Rossum <guido@python.org>
parents: 11127
diff changeset
405 if (size/a->ob_size != n)
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
406 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
407 np = (PyTupleObject *) PyTuple_New(size);
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
408 if (np == NULL)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
409 return NULL;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
410 p = np->ob_item;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
411 for (i = 0; i < n; i++) {
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
412 for (j = 0; j < a->ob_size; j++) {
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
413 *p = a->ob_item[j];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
414 Py_INCREF(*p);
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
415 p++;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
416 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
417 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
418 return (PyObject *) np;
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
419 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
420
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
421 static PySequenceMethods tuple_as_sequence = {
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
422 (inquiry)tuplelength, /*sq_length*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
423 (binaryfunc)tupleconcat, /*sq_concat*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
424 (intargfunc)tuplerepeat, /*sq_repeat*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
425 (intargfunc)tupleitem, /*sq_item*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
426 (intintargfunc)tupleslice, /*sq_slice*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 0, /*sq_ass_item*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 0, /*sq_ass_slice*/
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
429 (objobjproc)tuplecontains, /*sq_contains*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
432 PyTypeObject PyTuple_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
433 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 "tuple",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
436 sizeof(PyTupleObject) - sizeof(PyObject *),
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
437 sizeof(PyObject *),
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
438 (destructor)tupledealloc, /*tp_dealloc*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
439 (printfunc)tupleprint, /*tp_print*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 0, /*tp_getattr*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 0, /*tp_setattr*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
442 (cmpfunc)tuplecompare, /*tp_compare*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
443 (reprfunc)tuplerepr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 &tuple_as_sequence, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 0, /*tp_as_mapping*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
447 (hashfunc)tuplehash, /*tp_hash*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 };
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
449
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
450 /* The following function breaks the notion that tuples are immutable:
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
451 it changes the size of a tuple. We get away with this only if there
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
452 is only one module referencing the object. You can also think of it
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
453 as creating a new tuple object and destroying the old one, only
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
454 more efficiently. In any case, don't use this if the tuple may
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
455 already be known to some other part of the code...
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
456 If last_is_sticky is set, the tuple will grow or shrink at the
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
457 front, otherwise it will grow or shrink at the end. */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
458
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
459 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
460 _PyTuple_Resize(pv, newsize, last_is_sticky)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
461 PyObject **pv;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
462 int newsize;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
463 int last_is_sticky;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
464 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
465 register PyTupleObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
466 register PyTupleObject *sv;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
467 int i;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
468 int sizediff;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
469
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
470 v = (PyTupleObject *) *pv;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
471 if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
472 *pv = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
473 Py_DECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
474 PyErr_BadInternalCall();
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
475 return -1;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
476 }
2888
6abd14485884 better err checks in resizetuple
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
477 sizediff = newsize - v->ob_size;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
478 if (sizediff == 0)
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
479 return 0;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
480 /* XXX UNREF/NEWREF interface should be more symmetrical */
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 2888
diff changeset
481 #ifdef Py_REF_DEBUG
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2504
diff changeset
482 --_Py_RefTotal;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
483 #endif
11673
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11128
diff changeset
484 _Py_ForgetReference((PyObject *)v);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
485 if (last_is_sticky && sizediff < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
486 /* shrinking:
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
487 move entries to the front and zero moved entries */
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
488 for (i = 0; i < newsize; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
489 Py_XDECREF(v->ob_item[i]);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
490 v->ob_item[i] = v->ob_item[i - sizediff];
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
491 v->ob_item[i - sizediff] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
492 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
493 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
494 for (i = newsize; i < v->ob_size; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
495 Py_XDECREF(v->ob_item[i]);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
496 v->ob_item[i] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
497 }
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
498 #if MAXSAVESIZE > 0
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
499 if (newsize == 0 && free_tuples[0]) {
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
500 num_free_tuples[0]--;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
501 sv = free_tuples[0];
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
502 sv->ob_size = 0;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
503 Py_INCREF(sv);
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
504 #ifdef COUNT_ALLOCS
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
505 tuple_zero_allocs++;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
506 #endif
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
507 tupledealloc(v);
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
508 *pv = (PyObject*) sv;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
509 return 0;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
510 }
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
511 if (0 < newsize && newsize < MAXSAVESIZE &&
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
512 (sv = free_tuples[newsize]) != NULL)
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
513 {
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
514 free_tuples[newsize] = (PyTupleObject *) sv->ob_item[0];
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
515 num_free_tuples[newsize]--;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
516 #ifdef COUNT_ALLOCS
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
517 fast_tuple_allocs++;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
518 #endif
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
519 #ifdef Py_TRACE_REFS
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
520 sv->ob_type = &PyTuple_Type;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
521 #endif
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
522 for (i = 0; i < newsize; ++i){
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
523 sv->ob_item[i] = v->ob_item[i];
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
524 v->ob_item[i] = NULL;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
525 }
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
526 sv->ob_size = v->ob_size;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
527 tupledealloc(v);
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
528 *pv = (PyObject *) sv;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
529 } else
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
530 #endif
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
531 {
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
532 sv = (PyTupleObject *)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
533 PyObject_REALLOC((char *)v,
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
534 sizeof(PyTupleObject) + newsize * sizeof(PyObject *));
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
535 *pv = (PyObject *) sv;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
536 if (sv == NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
537 PyObject_DEL(v);
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
538 PyErr_NoMemory();
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
539 return -1;
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
540 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
541 }
11673
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11128
diff changeset
542 _Py_NewReference((PyObject *)sv);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
543 for (i = sv->ob_size; i < newsize; i++)
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
544 sv->ob_item[i] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
545 if (last_is_sticky && sizediff > 0) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
546 /* growing: move entries to the end and zero moved entries */
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
547 for (i = newsize - 1; i >= sizediff; i--) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
548 sv->ob_item[i] = sv->ob_item[i - sizediff];
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
549 sv->ob_item[i - sizediff] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
550 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
551 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
552 sv->ob_size = newsize;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
553 return 0;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
554 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
555
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
556 void
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
557 PyTuple_Fini()
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
558 {
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
559 #if MAXSAVESIZE > 0
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
560 int i;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
561
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
562 Py_XDECREF(free_tuples[0]);
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
563 free_tuples[0] = NULL;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
564
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
565 for (i = 1; i < MAXSAVESIZE; i++) {
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
566 PyTupleObject *p, *q;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
567 p = free_tuples[i];
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
568 free_tuples[i] = NULL;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
569 while (p) {
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
570 q = p;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
571 p = (PyTupleObject *)(p->ob_item[0]);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
572 PyObject_DEL(q);
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
573 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
574 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
575 #endif
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
576 }