Mercurial > cpython-lru_cache-weakref
annotate Objects/tupleobject.c @ 11239:577ea261bdf2 legacy-trunk
Find the help.txt file relative to __file__ or ".", not in sys.path.
(Suggested by Moshe Zadka, but implemented differently.)
Add <<python-docs>> event which, on Unix, brings up Netscape pointing
to http://www.python.doc/current/ (a local copy would be nice but its
location can't be predicted). Windows solution TBD.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 26 Aug 1999 23:06:05 +0000 |
| parents | a4b7242a4b74 |
| children | 5e6e9e893acd |
| 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 |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
36 #ifndef MAXSAVESIZE |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
37 #define MAXSAVESIZE 20 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
38 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
39 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
40 #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
|
41 /* Entries 1 upto MAXSAVESIZE are free lists, entry 0 is the empty |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
42 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
|
43 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
44 static PyTupleObject *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
|
45 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
46 #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
|
47 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
|
48 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
|
49 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
50 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
51 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
52 PyTuple_New(size) |
| 6 | 53 register int size; |
| 54 { | |
| 55 register int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
56 register PyTupleObject *op; |
| 6 | 57 if (size < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
58 PyErr_BadInternalCall(); |
| 6 | 59 return NULL; |
| 60 } | |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
61 #if MAXSAVESIZE > 0 |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
62 if (size == 0 && free_tuples[0]) { |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
63 op = free_tuples[0]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
64 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
|
65 #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
|
66 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
|
67 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
68 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
|
69 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
70 if (0 < size && size < MAXSAVESIZE && |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
71 (op = free_tuples[size]) != NULL) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
72 { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
73 free_tuples[size] = (PyTupleObject *) op->ob_item[0]; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
74 #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
|
75 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
|
76 #endif |
|
9632
ac5345814ba0
When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents:
9484
diff
changeset
|
77 #ifdef Py_TRACE_REFS |
|
ac5345814ba0
When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents:
9484
diff
changeset
|
78 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
|
79 op->ob_size = size; |
|
ac5345814ba0
When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents:
9484
diff
changeset
|
80 #endif |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
81 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
82 else |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
83 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
84 { |
|
11127
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
85 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
|
86 /* 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
|
87 if (nbytes / sizeof(PyObject *) != (size_t)size || |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
88 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)) |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
89 <= 0) |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
90 { |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
91 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
|
92 } |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
93 ; |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
94 op = (PyTupleObject *) 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
|
95 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
96 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
|
97 |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
98 op->ob_type = &PyTuple_Type; |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
99 op->ob_size = size; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
100 } |
| 6 | 101 for (i = 0; i < size; i++) |
| 102 op->ob_item[i] = NULL; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
103 _Py_NewReference(op); |
|
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 MAXSAVESIZE > 0 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
105 if (size == 0) { |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
106 free_tuples[0] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
107 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
|
108 } |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
109 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
110 return (PyObject *) op; |
| 6 | 111 } |
| 112 | |
| 113 int | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
114 PyTuple_Size(op) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
115 register PyObject *op; |
| 6 | 116 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
117 if (!PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
118 PyErr_BadInternalCall(); |
| 6 | 119 return -1; |
| 120 } | |
| 121 else | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
122 return ((PyTupleObject *)op)->ob_size; |
| 6 | 123 } |
| 124 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
125 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
126 PyTuple_GetItem(op, i) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
127 register PyObject *op; |
| 6 | 128 register int i; |
| 129 { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
130 if (!PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
131 PyErr_BadInternalCall(); |
| 6 | 132 return NULL; |
| 133 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
134 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
135 PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 6 | 136 return NULL; |
| 137 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
138 return ((PyTupleObject *)op) -> ob_item[i]; |
| 6 | 139 } |
| 140 | |
| 141 int | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
142 PyTuple_SetItem(op, i, newitem) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
143 register PyObject *op; |
| 6 | 144 register int i; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
145 PyObject *newitem; |
| 6 | 146 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
147 register PyObject *olditem; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
148 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
|
149 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
|
150 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
151 PyErr_BadInternalCall(); |
| 27 | 152 return -1; |
| 6 | 153 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
154 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
155 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
156 PyErr_SetString(PyExc_IndexError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
157 "tuple assignment index out of range"); |
| 27 | 158 return -1; |
| 6 | 159 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
160 p = ((PyTupleObject *)op) -> ob_item + i; |
|
2504
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
161 olditem = *p; |
|
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
162 *p = newitem; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
163 Py_XDECREF(olditem); |
| 6 | 164 return 0; |
| 165 } | |
| 166 | |
| 167 /* Methods */ | |
| 168 | |
| 169 static void | |
| 170 tupledealloc(op) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
171 register PyTupleObject *op; |
| 6 | 172 { |
| 173 register int i; | |
|
8559
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
174 |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
175 if (op->ob_size > 0) { |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
176 i = op->ob_size; |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
177 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
|
178 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
|
179 #if MAXSAVESIZE > 0 |
|
8559
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
180 if (op->ob_size < MAXSAVESIZE) { |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
181 op->ob_item[0] = (PyObject *) free_tuples[op->ob_size]; |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
182 free_tuples[op->ob_size] = op; |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
183 return; |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
184 } |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
185 #endif |
|
8559
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
186 } |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
187 free((ANY *)op); |
| 6 | 188 } |
| 189 | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
190 static int |
| 6 | 191 tupleprint(op, fp, flags) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
192 PyTupleObject *op; |
| 6 | 193 FILE *fp; |
| 194 int flags; | |
| 195 { | |
| 196 int i; | |
| 197 fprintf(fp, "("); | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
198 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
|
199 if (i > 0) |
| 6 | 200 fprintf(fp, ", "); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
201 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
|
202 return -1; |
| 6 | 203 } |
| 204 if (op->ob_size == 1) | |
| 205 fprintf(fp, ","); | |
| 206 fprintf(fp, ")"); | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
207 return 0; |
| 6 | 208 } |
| 209 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
210 static PyObject * |
| 6 | 211 tuplerepr(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
212 PyTupleObject *v; |
| 6 | 213 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
214 PyObject *s, *comma; |
| 6 | 215 int i; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
216 s = PyString_FromString("("); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
217 comma = PyString_FromString(", "); |
| 6 | 218 for (i = 0; i < v->ob_size && s != NULL; i++) { |
| 219 if (i > 0) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
220 PyString_Concat(&s, comma); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
221 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); |
| 6 | 222 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
223 Py_DECREF(comma); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
224 if (v->ob_size == 1) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
225 PyString_ConcatAndDel(&s, PyString_FromString(",")); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
226 PyString_ConcatAndDel(&s, PyString_FromString(")")); |
| 6 | 227 return s; |
| 228 } | |
| 229 | |
| 230 static int | |
| 231 tuplecompare(v, w) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
232 register PyTupleObject *v, *w; |
| 6 | 233 { |
| 234 register int len = | |
| 235 (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; | |
| 236 register int i; | |
| 237 for (i = 0; i < len; i++) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
238 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]); |
| 6 | 239 if (cmp != 0) |
| 240 return cmp; | |
| 241 } | |
| 242 return v->ob_size - w->ob_size; | |
| 243 } | |
| 244 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
245 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
246 tuplehash(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
247 PyTupleObject *v; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
248 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
249 register long x, y; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
250 register int len = v->ob_size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
251 register PyObject **p; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
252 x = 0x345678L; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
253 p = v->ob_item; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
254 while (--len >= 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
255 y = PyObject_Hash(*p++); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
256 if (y == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
257 return -1; |
|
4733
3c7812bfa1a4
Better tuple hash function.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
258 x = (1000003*x) ^ y; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
259 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
260 x ^= v->ob_size; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
261 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
262 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
263 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
264 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
265 |
| 6 | 266 static int |
| 267 tuplelength(a) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
268 PyTupleObject *a; |
| 6 | 269 { |
| 270 return a->ob_size; | |
| 271 } | |
| 272 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
273 static PyObject * |
| 6 | 274 tupleitem(a, i) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
275 register PyTupleObject *a; |
| 6 | 276 register int i; |
| 277 { | |
| 278 if (i < 0 || i >= a->ob_size) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
279 PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 6 | 280 return NULL; |
| 281 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
282 Py_INCREF(a->ob_item[i]); |
| 6 | 283 return a->ob_item[i]; |
| 284 } | |
| 285 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
286 static PyObject * |
| 6 | 287 tupleslice(a, ilow, ihigh) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
288 register PyTupleObject *a; |
| 6 | 289 register int ilow, ihigh; |
| 290 { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
291 register PyTupleObject *np; |
| 6 | 292 register int i; |
| 293 if (ilow < 0) | |
| 294 ilow = 0; | |
| 295 if (ihigh > a->ob_size) | |
| 296 ihigh = a->ob_size; | |
| 297 if (ihigh < ilow) | |
| 298 ihigh = ilow; | |
| 299 if (ilow == 0 && ihigh == a->ob_size) { | |
| 300 /* 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
|
301 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
302 return (PyObject *)a; |
| 6 | 303 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
304 np = (PyTupleObject *)PyTuple_New(ihigh - ilow); |
| 6 | 305 if (np == NULL) |
| 306 return NULL; | |
| 307 for (i = ilow; i < ihigh; i++) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
308 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
309 Py_INCREF(v); |
| 6 | 310 np->ob_item[i - ilow] = v; |
| 311 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
312 return (PyObject *)np; |
| 6 | 313 } |
| 314 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
315 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
316 PyTuple_GetSlice(op, i, j) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
317 PyObject *op; |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
318 int i, j; |
|
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
319 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
320 if (op == NULL || !PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
321 PyErr_BadInternalCall(); |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
322 return NULL; |
|
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
323 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
324 return tupleslice((PyTupleObject *)op, i, j); |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
325 } |
|
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
326 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
327 static PyObject * |
| 6 | 328 tupleconcat(a, bb) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
329 register PyTupleObject *a; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
330 register PyObject *bb; |
| 6 | 331 { |
| 332 register int size; | |
| 333 register int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
334 PyTupleObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
335 if (!PyTuple_Check(bb)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
336 PyErr_BadArgument(); |
| 6 | 337 return NULL; |
| 338 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
339 #define b ((PyTupleObject *)bb) |
| 6 | 340 size = a->ob_size + b->ob_size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
341 np = (PyTupleObject *) PyTuple_New(size); |
| 6 | 342 if (np == NULL) { |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
343 return NULL; |
| 6 | 344 } |
| 345 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
|
346 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
347 Py_INCREF(v); |
| 6 | 348 np->ob_item[i] = v; |
| 349 } | |
| 350 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
|
351 PyObject *v = b->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
352 Py_INCREF(v); |
| 6 | 353 np->ob_item[i + a->ob_size] = v; |
| 354 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
355 return (PyObject *)np; |
| 6 | 356 #undef b |
| 357 } | |
| 358 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
359 static PyObject * |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
360 tuplerepeat(a, n) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
361 PyTupleObject *a; |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
362 int n; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
363 { |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
364 int i, j; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
365 int size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
366 PyTupleObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
367 PyObject **p; |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
368 if (n < 0) |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
369 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
|
370 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
|
371 /* 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
|
372 copy in this case */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
373 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
374 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
|
375 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
376 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
|
377 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
|
378 return PyErr_NoMemory(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
379 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
|
380 if (np == NULL) |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
381 return NULL; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
382 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
|
383 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
|
384 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
|
385 *p = a->ob_item[j]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
386 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
|
387 p++; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
388 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
389 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
390 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
|
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 static PySequenceMethods tuple_as_sequence = { |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
394 (inquiry)tuplelength, /*sq_length*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
395 (binaryfunc)tupleconcat, /*sq_concat*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
396 (intargfunc)tuplerepeat, /*sq_repeat*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
397 (intargfunc)tupleitem, /*sq_item*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
398 (intintargfunc)tupleslice, /*sq_slice*/ |
| 6 | 399 0, /*sq_ass_item*/ |
| 400 0, /*sq_ass_slice*/ | |
| 401 }; | |
| 402 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
403 PyTypeObject PyTuple_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
404 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 405 0, |
| 406 "tuple", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
407 sizeof(PyTupleObject) - sizeof(PyObject *), |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
408 sizeof(PyObject *), |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
409 (destructor)tupledealloc, /*tp_dealloc*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
410 (printfunc)tupleprint, /*tp_print*/ |
| 6 | 411 0, /*tp_getattr*/ |
| 412 0, /*tp_setattr*/ | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
413 (cmpfunc)tuplecompare, /*tp_compare*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
414 (reprfunc)tuplerepr, /*tp_repr*/ |
| 6 | 415 0, /*tp_as_number*/ |
| 416 &tuple_as_sequence, /*tp_as_sequence*/ | |
| 417 0, /*tp_as_mapping*/ | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
418 (hashfunc)tuplehash, /*tp_hash*/ |
| 6 | 419 }; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
420 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
421 /* 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
|
422 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
|
423 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
|
424 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
|
425 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
|
426 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
|
427 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
|
428 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
|
429 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
430 int |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
431 _PyTuple_Resize(pv, newsize, last_is_sticky) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
432 PyObject **pv; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
433 int newsize; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
434 int last_is_sticky; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
435 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
436 register PyTupleObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
437 register PyTupleObject *sv; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
438 int i; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
439 int sizediff; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
440 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
441 v = (PyTupleObject *) *pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
442 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
|
443 *pv = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
444 Py_DECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
445 PyErr_BadInternalCall(); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
446 return -1; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
447 } |
|
2888
6abd14485884
better err checks in resizetuple
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
448 sizediff = newsize - v->ob_size; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
449 if (sizediff == 0) |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
450 return 0; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
451 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
2888
diff
changeset
|
452 #ifdef Py_REF_DEBUG |
|
2612
ca6b60d4ea90
fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents:
2504
diff
changeset
|
453 --_Py_RefTotal; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
454 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
455 _Py_ForgetReference(v); |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
456 if (last_is_sticky && sizediff < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
457 /* shrinking: |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
458 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
|
459 for (i = 0; i < newsize; i++) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
460 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
|
461 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
|
462 v->ob_item[i - sizediff] = NULL; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
463 } |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
464 } |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
465 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
|
466 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
|
467 v->ob_item[i] = NULL; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
468 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
469 sv = (PyTupleObject *) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
470 realloc((char *)v, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
471 sizeof(PyTupleObject) + newsize * sizeof(PyObject *)); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
472 *pv = (PyObject *) sv; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
473 if (sv == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
474 PyMem_DEL(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
475 PyErr_NoMemory(); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
476 return -1; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
477 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
478 _Py_NewReference(sv); |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
479 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
|
480 sv->ob_item[i] = NULL; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
481 if (last_is_sticky && sizediff > 0) { |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
482 /* 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
|
483 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
|
484 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
|
485 sv->ob_item[i - sizediff] = NULL; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
486 } |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
487 } |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
488 sv->ob_size = newsize; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
489 return 0; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
490 } |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
491 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
492 void |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
493 PyTuple_Fini() |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
494 { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
495 #if MAXSAVESIZE > 0 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
496 int i; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
497 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
498 Py_XDECREF(free_tuples[0]); |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
499 free_tuples[0] = NULL; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
500 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
501 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
|
502 PyTupleObject *p, *q; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
503 p = free_tuples[i]; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
504 free_tuples[i] = NULL; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
505 while (p) { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
506 q = p; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
507 p = (PyTupleObject *)(p->ob_item[0]); |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
508 PyMem_DEL(q); |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
509 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
510 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
511 #endif |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
512 } |
