blob: e28158e59b330e11c8b6fe03609634f7beda5970 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum9bfef441993-03-29 10:43:31 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum3f5da241990-12-20 15:06:42 +000029#ifdef REF_DEBUG
30long ref_total;
31#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032
Guido van Rossum3f5da241990-12-20 15:06:42 +000033/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
34 These are used by the individual routines for object creation.
35 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
37object *
38newobject(tp)
39 typeobject *tp;
40{
41 object *op = (object *) malloc(tp->tp_basicsize);
42 if (op == NULL)
43 return err_nomem();
44 NEWREF(op);
45 op->ob_type = tp;
46 return op;
47}
48
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049varobject *
50newvarobject(tp, size)
51 typeobject *tp;
52 unsigned int size;
53{
54 varobject *op = (varobject *)
55 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
56 if (op == NULL)
Guido van Rossum05ab1111991-05-05 20:10:41 +000057 return (varobject *)err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 NEWREF(op);
59 op->ob_type = tp;
60 op->ob_size = size;
61 return op;
62}
63
Guido van Rossum90933611991-06-07 16:10:43 +000064int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065printobject(op, fp, flags)
66 object *op;
67 FILE *fp;
68 int flags;
69{
Guido van Rossum278ef591991-07-27 21:40:24 +000070 int ret = 0;
Guido van Rossum90933611991-06-07 16:10:43 +000071 if (intrcheck()) {
72 err_set(KeyboardInterrupt);
73 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 }
Guido van Rossum90933611991-06-07 16:10:43 +000075 if (op == NULL) {
76 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 }
Guido van Rossum90933611991-06-07 16:10:43 +000078 else {
79 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +000080 fprintf(fp, "<refcnt %u at %lx>",
81 op->ob_refcnt, (long)op);
82 else if (op->ob_type->tp_print == NULL) {
83 if (op->ob_type->tp_repr == NULL) {
84 fprintf(fp, "<%s object at %lx>",
85 op->ob_type->tp_name, (long)op);
86 }
87 else {
88 object *s = reprobject(op);
89 if (s == NULL)
90 ret = -1;
91 else if (!is_stringobject(s)) {
92 err_setstr(TypeError,
93 "repr not string");
94 ret = -1;
95 }
96 else {
97 fprintf(fp, "%s", getstringvalue(s));
98 }
99 XDECREF(s);
100 }
101 }
Guido van Rossum90933611991-06-07 16:10:43 +0000102 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000103 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000104 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000105 if (ret == 0) {
106 if (ferror(fp)) {
Guido van Rossum2912f221991-12-10 13:59:09 +0000107 err_errno(IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000108 clearerr(fp);
109 ret = -1;
110 }
111 }
112 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113}
114
115object *
116reprobject(v)
117 object *v;
118{
Guido van Rossum90933611991-06-07 16:10:43 +0000119 if (intrcheck()) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 err_set(KeyboardInterrupt);
Guido van Rossum90933611991-06-07 16:10:43 +0000121 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122 }
Guido van Rossum90933611991-06-07 16:10:43 +0000123 if (v == NULL)
124 return newstringobject("<NULL>");
125 else if (v->ob_type->tp_repr == NULL) {
126 char buf[120];
127 sprintf(buf, "<%.80s object at %lx>",
128 v->ob_type->tp_name, (long)v);
129 return newstringobject(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130 }
Guido van Rossum90933611991-06-07 16:10:43 +0000131 else
132 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
135int
136cmpobject(v, w)
137 object *v, *w;
138{
139 typeobject *tp;
140 if (v == w)
141 return 0;
142 if (v == NULL)
143 return -1;
144 if (w == NULL)
145 return 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000146 if ((tp = v->ob_type) != w->ob_type) {
147 if (tp->tp_as_number != NULL &&
148 w->ob_type->tp_as_number != NULL) {
149 if (coerce(&v, &w) != 0) {
150 err_clear();
151 /* XXX Should report the error,
152 XXX but the interface isn't there... */
153 }
154 else {
155 int cmp = (*v->ob_type->tp_compare)(v, w);
156 DECREF(v);
157 DECREF(w);
158 return cmp;
159 }
160 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000162 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163 if (tp->tp_compare == NULL)
164 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000165 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
167
Guido van Rossum9bfef441993-03-29 10:43:31 +0000168long
169hashobject(v)
170 object *v;
171{
172 typeobject *tp = v->ob_type;
173 if (tp->tp_hash != NULL)
174 return (*tp->tp_hash)(v);
175 if (tp->tp_compare == NULL)
176 return (long) v; /* Use address as hash value */
177 /* If there's a cmp but no hash defined, the object can't be hashed */
178 err_setstr(TypeError, "unhashable type");
179 return -1;
180}
181
Guido van Rossum3f5da241990-12-20 15:06:42 +0000182object *
183getattr(v, name)
184 object *v;
185 char *name;
186{
187 if (v->ob_type->tp_getattr == NULL) {
188 err_setstr(TypeError, "attribute-less object");
189 return NULL;
190 }
191 else {
192 return (*v->ob_type->tp_getattr)(v, name);
193 }
194}
195
196int
197setattr(v, name, w)
198 object *v;
199 char *name;
200 object *w;
201{
202 if (v->ob_type->tp_setattr == NULL) {
203 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000204 err_setstr(TypeError,
205 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000206 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000207 err_setstr(TypeError,
208 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000209 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000210 }
211 else {
212 return (*v->ob_type->tp_setattr)(v, name, w);
213 }
214}
215
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000216/* Test a value used as condition, e.g., in a for or if statement.
217 Return -1 if an error occurred */
218
219int
220testbool(v)
221 object *v;
222{
223 int res;
224 if (v == None)
225 res = 0;
226 else if (v->ob_type->tp_as_number != NULL)
227 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
228 else if (v->ob_type->tp_as_mapping != NULL)
229 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
230 else if (v->ob_type->tp_as_sequence != NULL)
231 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
232 else
233 res = 1;
234 if (res > 0)
235 res = 1;
236 return res;
237}
238
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239
240/*
241NoObject is usable as a non-NULL undefined value, used by the macro None.
242There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000243so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244*/
245
Guido van Rossum0c182a11992-03-27 17:26:13 +0000246/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247static object *
248none_repr(op)
249 object *op;
250{
251 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
254static typeobject Notype = {
255 OB_HEAD_INIT(&Typetype)
256 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258 0,
259 0,
260 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000261 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000262 0, /*tp_getattr*/
263 0, /*tp_setattr*/
264 0, /*tp_compare*/
265 none_repr, /*tp_repr*/
266 0, /*tp_as_number*/
267 0, /*tp_as_sequence*/
268 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000269 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270};
271
272object NoObject = {
273 OB_HEAD_INIT(&Notype)
274};
275
276
277#ifdef TRACE_REFS
278
279static object refchain = {&refchain, &refchain};
280
281NEWREF(op)
282 object *op;
283{
284 ref_total++;
285 op->ob_refcnt = 1;
286 op->_ob_next = refchain._ob_next;
287 op->_ob_prev = &refchain;
288 refchain._ob_next->_ob_prev = op;
289 refchain._ob_next = op;
290}
291
Guido van Rossum3f5da241990-12-20 15:06:42 +0000292UNREF(op)
293 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000295 register object *p;
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000296 if (op->ob_refcnt < 0) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000297 fprintf(stderr, "UNREF negative refcnt\n");
298 abort();
299 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000300 if (op == &refchain ||
301 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
302 fprintf(stderr, "UNREF invalid object\n");
303 abort();
304 }
305#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000306 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
307 if (p == op)
308 break;
309 }
310 if (p == &refchain) { /* Not found */
311 fprintf(stderr, "UNREF unknown object\n");
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000312 abort();
313 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000314#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 op->_ob_next->_ob_prev = op->_ob_prev;
316 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000317 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000318}
319
320DELREF(op)
321 object *op;
322{
323 UNREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324 (*(op)->ob_type->tp_dealloc)(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000325 op->ob_type = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
328printrefs(fp)
329 FILE *fp;
330{
331 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000332 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000334 if (op->ob_refcnt == 1 && is_stringobject(op))
335 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000337 if (printobject(op, fp, 0) != 0)
338 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339 putc('\n', fp);
340 }
341}
342
343#endif