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