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