annotate Objects/tupleobject.c @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 65d2d8e67da3
children 3c7812bfa1a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* Tuple object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
34 #include "allobjects.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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 */
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
44 static tupleobject *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
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 newtupleobject(size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 register int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 register tupleobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 if (size < 0) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
58 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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];
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
64 INCREF(op);
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
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
68 return (object *) op;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
69 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
70 if (0 < size && size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
71 free_tuples[size] = (tupleobject *) 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
72 #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
73 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
74 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
75 } else
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
76 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
77 {
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
78 op = (tupleobject *)
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
79 malloc(sizeof(tupleobject) + size * sizeof(object *));
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
80 if (op == NULL)
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
81 return err_nomem();
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
82 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 op->ob_type = &Tupletype;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 op->ob_size = size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 for (i = 0; i < size; i++)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 op->ob_item[i] = NULL;
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1313
diff changeset
87 NEWREF(op);
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
88 #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
89 if (size == 0) {
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
90 free_tuples[0] = 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 INCREF(op); /* extra INCREF so that this is never freed */
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
92 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
93 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 return (object *) op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 gettuplesize(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 register object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 if (!is_tupleobject(op)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
102 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 else
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 return ((tupleobject *)op)->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 gettupleitem(op, i)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 register object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 if (!is_tupleobject(op)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
115 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
119 err_setstr(IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return ((tupleobject *)op) -> ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 settupleitem(op, i, newitem)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 register object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 register int i;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
129 object *newitem;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 register object *olditem;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
132 register object **p;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 if (!is_tupleobject(op)) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
134 XDECREF(newitem);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
135 err_badcall();
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
136 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
139 XDECREF(newitem);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
140 err_setstr(IndexError, "tuple assignment index out of range");
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
141 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 }
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
143 p = ((tupleobject *)op) -> ob_item + i;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
144 olditem = *p;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
145 *p = newitem;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
146 XDECREF(olditem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 static void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 tupledealloc(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 register tupleobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 register int i;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
157 for (i = 0; i < op->ob_size; i++)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
158 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
159 #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
160 if (0 < op->ob_size && op->ob_size < MAXSAVESIZE) {
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
161 op->ob_item[0] = (object *) free_tuples[op->ob_size];
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
162 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
163 } else
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
164 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
165 free((ANY *)op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
168 static int
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 tupleprint(op, fp, flags)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 tupleobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 int flags;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 fprintf(fp, "(");
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
176 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
177 if (i > 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 fprintf(fp, ", ");
1405
90c5e2bb4da5 * posixmodule.c: added set{uid,gid}.
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
179 if (printobject(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
180 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 if (op->ob_size == 1)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 fprintf(fp, ",");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 fprintf(fp, ")");
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
185 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
188 static object *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 tuplerepr(v)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 tupleobject *v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 {
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
192 object *s, *comma;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 s = newstringobject("(");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 comma = newstringobject(", ");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 for (i = 0; i < v->ob_size && s != NULL; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 if (i > 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 joinstring(&s, comma);
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
199 joinstring_decref(&s, reprobject(v->ob_item[i]));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 DECREF(comma);
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
202 if (v->ob_size == 1)
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
203 joinstring_decref(&s, newstringobject(","));
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
204 joinstring_decref(&s, newstringobject(")"));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 return s;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 static int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 tuplecompare(v, w)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 register tupleobject *v, *w;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 register int len =
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 for (i = 0; i < len; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 int cmp = cmpobject(v->ob_item[i], w->ob_item[i]);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 if (cmp != 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 return cmp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 return v->ob_size - w->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
223 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
224 tuplehash(v)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
225 tupleobject *v;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
226 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
227 register long x, y;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
228 register int len = v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
229 register object **p;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
230 x = 0x345678L;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
231 p = v->ob_item;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
232 while (--len >= 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
233 y = hashobject(*p++);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
234 if (y == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
235 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
236 x = (x + x + x) ^ y;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
237 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
238 x ^= v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
239 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
240 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
241 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
242 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
243
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 static int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 tuplelength(a)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 tupleobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 static object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 tupleitem(a, i)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 register tupleobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 if (i < 0 || i >= a->ob_size) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
257 err_setstr(IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 INCREF(a->ob_item[i]);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 return a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 static object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 tupleslice(a, ilow, ihigh)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 register tupleobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 register int ilow, ihigh;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 register tupleobject *np;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 ihigh = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 if (ilow == 0 && ihigh == a->ob_size) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 /* XXX can only do this if tuples are immutable! */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 INCREF(a);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 return (object *)a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 np = (tupleobject *)newtupleobject(ihigh - ilow);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 if (np == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 for (i = ilow; i < ihigh; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 object *v = a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 INCREF(v);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 np->ob_item[i - ilow] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 return (object *)np;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
293 object *
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
294 gettupleslice(op, i, j)
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
295 object *op;
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
296 int i, j;
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
297 {
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
298 if (op == NULL || !is_tupleobject(op)) {
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
299 err_badcall();
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
300 return NULL;
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
301 }
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
302 return tupleslice((tupleobject *)op, i, j);
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
303 }
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
304
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 static object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 tupleconcat(a, bb)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 register tupleobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 register object *bb;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 register int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 tupleobject *np;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 if (!is_tupleobject(bb)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
314 err_badarg();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 #define b ((tupleobject *)bb)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 size = a->ob_size + b->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 np = (tupleobject *) newtupleobject(size);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 if (np == NULL) {
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
321 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 for (i = 0; i < a->ob_size; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 object *v = a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 INCREF(v);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 np->ob_item[i] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 for (i = 0; i < b->ob_size; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 object *v = b->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 INCREF(v);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 np->ob_item[i + a->ob_size] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 return (object *)np;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
337 static object *
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
338 tuplerepeat(a, n)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
339 tupleobject *a;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
340 int n;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
341 {
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
342 int i, j;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
343 int size;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
344 tupleobject *np;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
345 object **p;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
346 if (n < 0)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
347 n = 0;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
348 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
349 /* 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
350 copy in this case */
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
351 INCREF(a);
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
352 return (object *)a;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
353 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
354 size = a->ob_size * n;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
355 np = (tupleobject *) newtupleobject(size);
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
356 if (np == NULL)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
357 return NULL;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
358 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
359 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
360 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
361 *p = a->ob_item[j];
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
362 INCREF(*p);
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
363 p++;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
364 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
365 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
366 return (object *) np;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
367 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
368
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 static sequence_methods tuple_as_sequence = {
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
370 (inquiry)tuplelength, /*sq_length*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
371 (binaryfunc)tupleconcat, /*sq_concat*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
372 (intargfunc)tuplerepeat, /*sq_repeat*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
373 (intargfunc)tupleitem, /*sq_item*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
374 (intintargfunc)tupleslice, /*sq_slice*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 0, /*sq_ass_item*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 0, /*sq_ass_slice*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 typeobject Tupletype = {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 "tuple",
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 sizeof(tupleobject) - sizeof(object *),
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 sizeof(object *),
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
385 (destructor)tupledealloc, /*tp_dealloc*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
386 (printfunc)tupleprint, /*tp_print*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 0, /*tp_getattr*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 0, /*tp_setattr*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
389 (cmpfunc)tuplecompare, /*tp_compare*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
390 (reprfunc)tuplerepr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 &tuple_as_sequence, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 0, /*tp_as_mapping*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
394 (hashfunc)tuplehash, /*tp_hash*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 };
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
396
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
397 /* 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
398 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
399 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
400 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
401 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
402 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
403 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
404 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
405
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
406 int
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
407 resizetuple(pv, newsize, last_is_sticky)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
408 object **pv;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
409 int newsize;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
410 int last_is_sticky;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
411 {
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
412 register tupleobject *v;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
413 register tupleobject *sv;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
414 int i;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
415 int sizediff;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
416
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
417 v = (tupleobject *) *pv;
2888
6abd14485884 better err checks in resizetuple
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
418 if (v == NULL || !is_tupleobject(v) || v->ob_refcnt != 1) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
419 *pv = 0;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
420 DECREF(v);
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
421 err_badcall();
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
422 return -1;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
423 }
2888
6abd14485884 better err checks in resizetuple
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
424 sizediff = newsize - v->ob_size;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
425 if (sizediff == 0)
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
426 return 0;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
427 /* XXX UNREF/NEWREF interface should be more symmetrical */
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 2888
diff changeset
428 #ifdef Py_REF_DEBUG
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2504
diff changeset
429 --_Py_RefTotal;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
430 #endif
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
431 UNREF(v);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
432 if (last_is_sticky && sizediff < 0) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
433 /* shrinking: move entries to the front and zero moved entries */
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
434 for (i = 0; i < newsize; i++) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
435 XDECREF(v->ob_item[i]);
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
436 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
437 v->ob_item[i - sizediff] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
438 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
439 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
440 for (i = newsize; i < v->ob_size; i++) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
441 XDECREF(v->ob_item[i]);
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
442 v->ob_item[i] = 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 sv = (tupleobject *)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
445 realloc((char *)v,
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
446 sizeof(tupleobject) + newsize * sizeof(object *));
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
447 *pv = (object *) sv;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
448 if (sv == NULL) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
449 DEL(v);
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
450 err_nomem();
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
451 return -1;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
452 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
453 NEWREF(sv);
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
454 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
455 sv->ob_item[i] = NULL;
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) {
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
457 /* 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
458 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
459 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
460 sv->ob_item[i - sizediff] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
461 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
462 }
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
463 sv->ob_size = newsize;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
464 return 0;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
465 }