annotate Objects/tupleobject.c @ 3044:7e7bb00a0a2e legacy-trunk

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