blob: a469797f58304e11119090a108b2a430018fddaf [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
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000197hasattr(v, name)
198 object *v;
199 char *name;
200{
201 object *res = getattr(v, name);
202 if (res != NULL) {
203 DECREF(res);
204 return 1;
205 }
206 err_clear();
207 return 0;
208}
209
210int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000211setattr(v, name, w)
212 object *v;
213 char *name;
214 object *w;
215{
216 if (v->ob_type->tp_setattr == NULL) {
217 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000218 err_setstr(TypeError,
219 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000220 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000221 err_setstr(TypeError,
222 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000223 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224 }
225 else {
226 return (*v->ob_type->tp_setattr)(v, name, w);
227 }
228}
229
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000230/* Test a value used as condition, e.g., in a for or if statement.
231 Return -1 if an error occurred */
232
233int
234testbool(v)
235 object *v;
236{
237 int res;
238 if (v == None)
239 res = 0;
240 else if (v->ob_type->tp_as_number != NULL)
241 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
242 else if (v->ob_type->tp_as_mapping != NULL)
243 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
244 else if (v->ob_type->tp_as_sequence != NULL)
245 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
246 else
247 res = 1;
248 if (res > 0)
249 res = 1;
250 return res;
251}
252
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253
254/*
255NoObject is usable as a non-NULL undefined value, used by the macro None.
256There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258*/
259
Guido van Rossum0c182a11992-03-27 17:26:13 +0000260/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000261static object *
262none_repr(op)
263 object *op;
264{
265 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266}
267
268static typeobject Notype = {
269 OB_HEAD_INIT(&Typetype)
270 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000271 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272 0,
273 0,
274 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000275 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000276 0, /*tp_getattr*/
277 0, /*tp_setattr*/
278 0, /*tp_compare*/
279 none_repr, /*tp_repr*/
280 0, /*tp_as_number*/
281 0, /*tp_as_sequence*/
282 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000283 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284};
285
286object NoObject = {
287 OB_HEAD_INIT(&Notype)
288};
289
290
291#ifdef TRACE_REFS
292
293static object refchain = {&refchain, &refchain};
294
295NEWREF(op)
296 object *op;
297{
298 ref_total++;
299 op->ob_refcnt = 1;
300 op->_ob_next = refchain._ob_next;
301 op->_ob_prev = &refchain;
302 refchain._ob_next->_ob_prev = op;
303 refchain._ob_next = op;
304}
305
Guido van Rossum3f5da241990-12-20 15:06:42 +0000306UNREF(op)
307 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000309 register object *p;
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000310 if (op->ob_refcnt < 0) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000311 fprintf(stderr, "UNREF negative refcnt\n");
312 abort();
313 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000314 if (op == &refchain ||
315 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
316 fprintf(stderr, "UNREF invalid object\n");
317 abort();
318 }
319#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000320 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
321 if (p == op)
322 break;
323 }
324 if (p == &refchain) { /* Not found */
325 fprintf(stderr, "UNREF unknown object\n");
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000326 abort();
327 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000328#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 op->_ob_next->_ob_prev = op->_ob_prev;
330 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000331 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000332}
333
334DELREF(op)
335 object *op;
336{
337 UNREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338 (*(op)->ob_type->tp_dealloc)(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000339 op->ob_type = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340}
341
342printrefs(fp)
343 FILE *fp;
344{
345 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000346 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000348 if (op->ob_refcnt == 1 && is_stringobject(op))
349 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000351 if (printobject(op, fp, 0) != 0)
352 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353 putc('\n', fp);
354 }
355}
356
357#endif