annotate Objects/tupleobject.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 396c8a24880c
children
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
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Tuple object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
4 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
6 /* 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
7 #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
8 #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
9 #endif
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
10 #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
11 #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
12 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
13
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
14 #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
15 /* 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
16 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
17 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
18 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
19 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
20 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
21 #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
22 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
23 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
24 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
25
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
26 PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
27 PyTuple_New(register int size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 register int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
30 register PyTupleObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 if (size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
32 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 }
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
35 #if MAXSAVESIZE > 0
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
36 if (size == 0 && free_tuples[0]) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
37 op = free_tuples[0];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
38 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
39 #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
40 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
41 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
42 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
43 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
44 if (0 < size && size < MAXSAVESIZE &&
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
45 (op = free_tuples[size]) != NULL)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
46 {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
47 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
48 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
49 #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
50 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
51 #endif
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
52 /* 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
53 #ifdef Py_TRACE_REFS
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
54 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
55 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
56 #endif
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
57 _Py_NewReference((PyObject *)op);
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
58 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
59 else
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
60 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
61 {
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
62 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
63 /* 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
64 if (nbytes / sizeof(PyObject *) != (size_t)size ||
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
65 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *))
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
66 <= 0)
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
67 {
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
68 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
69 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
70 op = PyObject_GC_NewVar(PyTupleObject, &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
71 if (op == NULL)
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
72 return NULL;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
73 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 for (i = 0; i < size; i++)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 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
76 #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
77 if (size == 0) {
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
78 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
79 ++num_free_tuples[0];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
80 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
81 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
82 #endif
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
83 _PyObject_GC_TRACK(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
84 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
88 PyTuple_Size(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
90 if (!PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
91 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
95 return ((PyTupleObject *)op)->ob_size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
98 PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
99 PyTuple_GetItem(register PyObject *op, register int i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
101 if (!PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
102 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
105 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
106 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
109 return ((PyTupleObject *)op) -> ob_item[i];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
113 PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
115 register PyObject *olditem;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
116 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
117 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
118 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
119 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
120 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
122 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
123 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
124 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
125 "tuple assignment index out of range");
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
126 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
128 p = ((PyTupleObject *)op) -> ob_item + i;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
129 olditem = *p;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
130 *p = newitem;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
131 Py_XDECREF(olditem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 static void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
138 tupledealloc(register PyTupleObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 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
141 register int len = op->ob_size;
22626
0446eeb787b7 Backport to 2.2.1:
Guido van Rossum <guido@python.org>
parents: 21293
diff changeset
142 PyObject_GC_UnTrack(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
143 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
144 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
145 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
146 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
147 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
148 #if MAXSAVESIZE > 0
19612
c396afef1aa9 More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents: 19592
diff changeset
149 if (len < MAXSAVESIZE &&
c396afef1aa9 More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents: 19592
diff changeset
150 num_free_tuples[len] < MAXSAVEDTUPLES &&
c396afef1aa9 More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents: 19592
diff changeset
151 op->ob_type == &PyTuple_Type)
c396afef1aa9 More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents: 19592
diff changeset
152 {
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
153 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
154 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
155 free_tuples[len] = op;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
156 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
157 }
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
158 #endif
8559
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
159 }
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19872
diff changeset
160 op->ob_type->tp_free((PyObject *)op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
161 done:
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
162 Py_TRASHCAN_SAFE_END(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
165 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
166 tupleprint(PyTupleObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 fprintf(fp, "(");
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
170 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
171 if (i > 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 fprintf(fp, ", ");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
173 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
174 return -1;
6
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 if (op->ob_size == 1)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 fprintf(fp, ",");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 fprintf(fp, ")");
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
179 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
182 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
183 tuplerepr(PyTupleObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 {
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
185 int i, n;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
186 PyObject *s, *temp;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
187 PyObject *pieces, *result = NULL;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
188
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
189 n = v->ob_size;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
190 if (n == 0)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
191 return PyString_FromString("()");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
192
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
193 pieces = PyTuple_New(n);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
194 if (pieces == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
195 return NULL;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
196
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
197 /* Do repr() on each element. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
198 for (i = 0; i < n; ++i) {
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
199 s = PyObject_Repr(v->ob_item[i]);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
200 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
201 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
202 PyTuple_SET_ITEM(pieces, i, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
203 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
204
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
205 /* Add "()" decorations to the first and last items. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
206 assert(n > 0);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
207 s = PyString_FromString("(");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
208 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
209 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
210 temp = PyTuple_GET_ITEM(pieces, 0);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
211 PyString_ConcatAndDel(&s, temp);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
212 PyTuple_SET_ITEM(pieces, 0, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
213 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
214 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
215
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
216 s = PyString_FromString(n == 1 ? ",)" : ")");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
217 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
218 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
219 temp = PyTuple_GET_ITEM(pieces, n-1);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
220 PyString_ConcatAndDel(&temp, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
221 PyTuple_SET_ITEM(pieces, n-1, temp);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
222 if (temp == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
223 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
224
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
225 /* Paste them all together with ", " between. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
226 s = PyString_FromString(", ");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
227 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
228 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
229 result = _PyString_Join(s, pieces);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
230 Py_DECREF(s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
231
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
232 Done:
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
233 Py_DECREF(pieces);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
234 return result;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
237 static long
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
238 tuplehash(PyTupleObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
239 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
240 register long x, y;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
241 register int len = v->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
242 register PyObject **p;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
243 x = 0x345678L;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
244 p = v->ob_item;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
245 while (--len >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
246 y = PyObject_Hash(*p++);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
247 if (y == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
248 return -1;
4733
3c7812bfa1a4 Better tuple hash function.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
249 x = (1000003*x) ^ y;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
250 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
251 x ^= v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
252 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
253 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
254 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
255 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
256
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
258 tuplelength(PyTupleObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
263 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
264 tuplecontains(PyTupleObject *a, PyObject *el)
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
265 {
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
266 int i, cmp;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
267
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
268 for (i = 0; i < a->ob_size; ++i) {
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
269 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
270 Py_EQ);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
271 if (cmp > 0)
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
272 return 1;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
273 else if (cmp < 0)
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
274 return -1;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
275 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
276 return 0;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
277 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
278
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
279 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
280 tupleitem(register PyTupleObject *a, register int i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 if (i < 0 || i >= a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
283 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
286 Py_INCREF(a->ob_item[i]);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 return a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
290 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
291 tupleslice(register PyTupleObject *a, register int ilow, register int ihigh)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
293 register PyTupleObject *np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 ihigh = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 ihigh = ilow;
19872
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
301 if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
302 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
303 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
305 np = (PyTupleObject *)PyTuple_New(ihigh - ilow);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 if (np == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 for (i = ilow; i < ihigh; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
309 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
310 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 np->ob_item[i - ilow] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
313 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
316 PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
317 PyTuple_GetSlice(PyObject *op, int i, int j)
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
318 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
319 if (op == NULL || !PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
320 PyErr_BadInternalCall();
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
321 return NULL;
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
322 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
323 return tupleslice((PyTupleObject *)op, i, j);
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
324 }
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
325
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
326 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
327 tupleconcat(register PyTupleObject *a, register PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 register int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 register int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
331 PyTupleObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
332 if (!PyTuple_Check(bb)) {
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
333 PyErr_Format(PyExc_TypeError,
12929
2eef636ba1b6 Michael Hudson <mwh21@cam.ac.uk>:
Marc-André Lemburg <mal@egenix.com>
parents: 12925
diff changeset
334 "can only concatenate tuple (not \"%.200s\") to tuple",
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
335 bb->ob_type->tp_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
338 #define b ((PyTupleObject *)bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 size = a->ob_size + b->ob_size;
25742
396c8a24880c Backport listobject.c 2.137 and tupleobject.c 2.75:
Guido van Rossum <guido@python.org>
parents: 22626
diff changeset
340 if (size < 0)
396c8a24880c Backport listobject.c 2.137 and tupleobject.c 2.75:
Guido van Rossum <guido@python.org>
parents: 22626
diff changeset
341 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
342 np = (PyTupleObject *) PyTuple_New(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 if (np == NULL) {
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
344 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 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
347 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
348 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 np->ob_item[i] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 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
352 PyObject *v = b->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
353 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 np->ob_item[i + a->ob_size] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
356 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
359
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
360 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
361 tuplerepeat(PyTupleObject *a, int n)
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
362 {
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
363 int i, j;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
364 int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
365 PyTupleObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
366 PyObject **p;
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
367 if (n < 0)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
368 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
369 if (a->ob_size == 0 || n == 1) {
19872
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
370 if (PyTuple_CheckExact(a)) {
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
371 /* Since tuples are immutable, we can return a shared
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
372 copy in this case */
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
373 Py_INCREF(a);
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
374 return (PyObject *)a;
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
375 }
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
376 if (a->ob_size == 0)
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
377 return PyTuple_New(0);
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
378 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
379 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
380 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
381 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
382 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
383 if (np == NULL)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
384 return NULL;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
385 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
386 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
387 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
388 *p = a->ob_item[j];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
389 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
390 p++;
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 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
393 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
394 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
395
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
396 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
397 tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
398 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
399 int i, err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
400 PyObject *x;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
401
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
402 for (i = o->ob_size; --i >= 0; ) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
403 x = o->ob_item[i];
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
404 if (x != NULL) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
405 err = visit(x, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
406 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
407 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
408 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
409 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
410 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
411 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
412
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
413 static PyObject *
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
414 tuplerichcompare(PyObject *v, PyObject *w, int op)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
415 {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
416 PyTupleObject *vt, *wt;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
417 int i;
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
418 int vlen, wlen;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
419
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
420 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
421 Py_INCREF(Py_NotImplemented);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
422 return Py_NotImplemented;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
423 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
424
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
425 vt = (PyTupleObject *)v;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
426 wt = (PyTupleObject *)w;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
427
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
428 vlen = vt->ob_size;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
429 wlen = wt->ob_size;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
430
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
431 /* Note: the corresponding code for lists has an "early out" test
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
432 * here when op is EQ or NE and the lengths differ. That pays there,
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
433 * but Tim was unable to find any real code where EQ/NE tuple
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
434 * compares don't have the same length, so testing for it here would
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
435 * have cost without benefit.
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
436 */
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
437
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
438 /* Search for the first index where items are different.
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
439 * Note that because tuples are immutable, it's safe to reuse
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
440 * vlen and wlen across the comparison calls.
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
441 */
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
442 for (i = 0; i < vlen && i < wlen; i++) {
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
443 int k = PyObject_RichCompareBool(vt->ob_item[i],
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
444 wt->ob_item[i], Py_EQ);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
445 if (k < 0)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
446 return NULL;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
447 if (!k)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
448 break;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
449 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
450
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
451 if (i >= vlen || i >= wlen) {
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
452 /* No more items to compare -- compare sizes */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
453 int cmp;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
454 PyObject *res;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
455 switch (op) {
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
456 case Py_LT: cmp = vlen < wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
457 case Py_LE: cmp = vlen <= wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
458 case Py_EQ: cmp = vlen == wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
459 case Py_NE: cmp = vlen != wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
460 case Py_GT: cmp = vlen > wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
461 case Py_GE: cmp = vlen >= wlen; break;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
462 default: return NULL; /* cannot happen */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
463 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
464 if (cmp)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
465 res = Py_True;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
466 else
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
467 res = Py_False;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
468 Py_INCREF(res);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
469 return res;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
470 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
471
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
472 /* We have an item that differs -- shortcuts for EQ/NE */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
473 if (op == Py_EQ) {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
474 Py_INCREF(Py_False);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
475 return Py_False;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
476 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
477 if (op == Py_NE) {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
478 Py_INCREF(Py_True);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
479 return Py_True;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
480 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
481
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
482 /* Compare the final item again using the proper operator */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
483 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
484 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
485
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
486 staticforward PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
487 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
488
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
489 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
490 tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
491 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
492 PyObject *arg = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
493 static char *kwlist[] = {"sequence", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
494
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
495 if (type != &PyTuple_Type)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
496 return tuple_subtype_new(type, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
497 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
498 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
499
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
500 if (arg == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
501 return PyTuple_New(0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
502 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
503 return PySequence_Tuple(arg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
504 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
505
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
506 static PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
507 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
508 {
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
509 PyObject *tmp, *new, *item;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
510 int i, n;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
511
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
512 assert(PyType_IsSubtype(type, &PyTuple_Type));
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
513 tmp = tuple_new(&PyTuple_Type, args, kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
514 if (tmp == NULL)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
515 return NULL;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
516 assert(PyTuple_Check(tmp));
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
517 new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
518 if (new == NULL)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
519 return NULL;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
520 for (i = 0; i < n; i++) {
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
521 item = PyTuple_GET_ITEM(tmp, i);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
522 Py_INCREF(item);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
523 PyTuple_SET_ITEM(new, i, item);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
524 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
525 Py_DECREF(tmp);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
526 return new;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
527 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
528
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
529 static char tuple_doc[] =
19657
937965516b54 Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents: 19656
diff changeset
530 "tuple() -> an empty tuple\n"
937965516b54 Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents: 19656
diff changeset
531 "tuple(sequence) -> tuple initialized from sequence's items\n"
937965516b54 Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents: 19656
diff changeset
532 "\n"
937965516b54 Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents: 19656
diff changeset
533 "If the argument is a tuple, the return value is the same object.";
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
534
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
535 static PySequenceMethods tuple_as_sequence = {
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
536 (inquiry)tuplelength, /* sq_length */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
537 (binaryfunc)tupleconcat, /* sq_concat */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
538 (intargfunc)tuplerepeat, /* sq_repeat */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
539 (intargfunc)tupleitem, /* sq_item */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
540 (intintargfunc)tupleslice, /* sq_slice */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
541 0, /* sq_ass_item */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
542 0, /* sq_ass_slice */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
543 (objobjproc)tuplecontains, /* sq_contains */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
546 PyTypeObject PyTuple_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
547 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 "tuple",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
550 sizeof(PyTupleObject) - sizeof(PyObject *),
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
551 sizeof(PyObject *),
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
552 (destructor)tupledealloc, /* tp_dealloc */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
553 (printfunc)tupleprint, /* tp_print */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
554 0, /* tp_getattr */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
555 0, /* tp_setattr */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
556 0, /* tp_compare */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
557 (reprfunc)tuplerepr, /* tp_repr */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
558 0, /* tp_as_number */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
559 &tuple_as_sequence, /* tp_as_sequence */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
560 0, /* tp_as_mapping */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
561 (hashfunc)tuplehash, /* tp_hash */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
562 0, /* tp_call */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
563 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
564 PyObject_GenericGetAttr, /* tp_getattro */
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
565 0, /* tp_setattro */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
566 0, /* tp_as_buffer */
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
567 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
568 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
569 tuple_doc, /* tp_doc */
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
570 (traverseproc)tupletraverse, /* tp_traverse */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
571 0, /* tp_clear */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
572 tuplerichcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
573 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
574 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
575 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
576 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
577 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
578 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
579 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
580 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
581 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
582 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
583 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
584 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
585 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
586 tuple_new, /* tp_new */
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19872
diff changeset
587 _PyObject_GC_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 };
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
589
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
590 /* 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
591 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
592 is only one module referencing the object. You can also think of it
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
593 as creating a new tuple object and destroying the old one, only more
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
594 efficiently. In any case, don't use this if the tuple may already be
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17975
diff changeset
595 known to some other part of the code. */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
596
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
597 int
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17975
diff changeset
598 _PyTuple_Resize(PyObject **pv, int newsize)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
599 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
600 register PyTupleObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
601 register PyTupleObject *sv;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
602 int i;
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
603 int oldsize;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
604
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
605 v = (PyTupleObject *) *pv;
19612
c396afef1aa9 More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents: 19592
diff changeset
606 if (v == NULL || v->ob_type != &PyTuple_Type ||
17975
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
607 (v->ob_size != 0 && v->ob_refcnt != 1)) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
608 *pv = 0;
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
609 Py_XDECREF(v);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
610 PyErr_BadInternalCall();
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
611 return -1;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
612 }
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
613 oldsize = v->ob_size;
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
614 if (oldsize == newsize)
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
615 return 0;
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
616
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
617 if (oldsize == 0) {
17975
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
618 /* Empty tuples are often shared, so we should never
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
619 resize them in-place even if we do own the only
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
620 (current) reference */
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
621 Py_DECREF(v);
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
622 *pv = PyTuple_New(newsize);
17979
192d36869445 _PyTuple_Resize: guard against PyTuple_New() returning NULL, using Tim's
Thomas Wouters <thomas@python.org>
parents: 17976
diff changeset
623 return *pv == NULL ? -1 : 0;
17975
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
624 }
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
625
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
626 /* XXX UNREF/NEWREF interface should be more symmetrical */
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 2888
diff changeset
627 #ifdef Py_REF_DEBUG
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2504
diff changeset
628 --_Py_RefTotal;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
629 #endif
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
630 _PyObject_GC_UNTRACK(v);
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
631 _Py_ForgetReference((PyObject *) v);
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
632 /* DECREF items deleted by shrinkage */
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
633 for (i = newsize; i < oldsize; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
634 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
635 v->ob_item[i] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
636 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
637 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
638 if (sv == NULL) {
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
639 *pv = NULL;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
640 PyObject_GC_Del(v);
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
641 return -1;
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
642 }
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
643 _Py_NewReference((PyObject *) sv);
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
644 /* Zero out items added by growing */
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
645 for (i = oldsize; i < newsize; i++)
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
646 sv->ob_item[i] = NULL;
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
647 *pv = (PyObject *) sv;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
648 _PyObject_GC_TRACK(sv);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
649 return 0;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
650 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
651
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
652 void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
653 PyTuple_Fini(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
654 {
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
655 #if MAXSAVESIZE > 0
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
656 int i;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
657
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
658 Py_XDECREF(free_tuples[0]);
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
659 free_tuples[0] = NULL;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
660
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
661 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
662 PyTupleObject *p, *q;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
663 p = free_tuples[i];
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
664 free_tuples[i] = NULL;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
665 while (p) {
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
666 q = p;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
667 p = (PyTupleObject *)(p->ob_item[0]);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
668 PyObject_GC_Del(q);
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
669 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
670 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
671 #endif
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
672 }