blob: be3cce7e809909a3a94d9783733c536bf94b3ada [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000037#ifdef COUNT_ALLOCS
38static typeobject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000039extern int tuple_zero_allocs, fast_tuple_allocs;
40extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000041extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000042void
43dump_counts()
44{
45 typeobject *tp;
46
47 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000048 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
49 tp->tp_name, tp->tp_alloc, tp->tp_free,
50 tp->tp_maxalloc);
51 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
52 fast_tuple_allocs, tuple_zero_allocs);
53 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
54 quick_int_allocs, quick_neg_int_allocs);
55 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
56 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000057}
58
59void
60inc_count(tp)
61 typeobject *tp;
62{
63 if (tp->tp_alloc == 0) {
64 /* first time; hang in linked list */
65 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumd7047b31995-01-02 19:07:15 +000066 fatal("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000067 tp->tp_next = type_list;
68 type_list = tp;
69 }
70 tp->tp_alloc++;
71 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
72 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
73}
74#endif
75
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076object *
77newobject(tp)
78 typeobject *tp;
79{
80 object *op = (object *) malloc(tp->tp_basicsize);
81 if (op == NULL)
82 return err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 op->ob_type = tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000084 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 return op;
86}
87
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088varobject *
89newvarobject(tp, size)
90 typeobject *tp;
91 unsigned int size;
92{
93 varobject *op = (varobject *)
94 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
95 if (op == NULL)
Guido van Rossum05ab1111991-05-05 20:10:41 +000096 return (varobject *)err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 op->ob_type = tp;
98 op->ob_size = size;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000099 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 return op;
101}
102
Guido van Rossum90933611991-06-07 16:10:43 +0000103int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104printobject(op, fp, flags)
105 object *op;
106 FILE *fp;
107 int flags;
108{
Guido van Rossum278ef591991-07-27 21:40:24 +0000109 int ret = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000110 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000111 return -1;
Guido van Rossum90933611991-06-07 16:10:43 +0000112 if (op == NULL) {
113 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 }
Guido van Rossum90933611991-06-07 16:10:43 +0000115 else {
116 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000117 fprintf(fp, "<refcnt %u at %lx>",
118 op->ob_refcnt, (long)op);
119 else if (op->ob_type->tp_print == NULL) {
120 if (op->ob_type->tp_repr == NULL) {
121 fprintf(fp, "<%s object at %lx>",
122 op->ob_type->tp_name, (long)op);
123 }
124 else {
Guido van Rossumc6004111993-11-05 10:22:19 +0000125 object *s;
126 if (flags & PRINT_RAW)
127 s = strobject(op);
128 else
129 s = reprobject(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000130 if (s == NULL)
131 ret = -1;
132 else if (!is_stringobject(s)) {
133 err_setstr(TypeError,
134 "repr not string");
135 ret = -1;
136 }
137 else {
138 fprintf(fp, "%s", getstringvalue(s));
139 }
140 XDECREF(s);
141 }
142 }
Guido van Rossum90933611991-06-07 16:10:43 +0000143 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000144 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000145 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000146 if (ret == 0) {
147 if (ferror(fp)) {
Guido van Rossum2912f221991-12-10 13:59:09 +0000148 err_errno(IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000149 clearerr(fp);
150 ret = -1;
151 }
152 }
153 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154}
155
156object *
157reprobject(v)
158 object *v;
159{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000160 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000161 return NULL;
Guido van Rossum90933611991-06-07 16:10:43 +0000162 if (v == NULL)
163 return newstringobject("<NULL>");
164 else if (v->ob_type->tp_repr == NULL) {
165 char buf[120];
166 sprintf(buf, "<%.80s object at %lx>",
167 v->ob_type->tp_name, (long)v);
168 return newstringobject(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 }
Guido van Rossum90933611991-06-07 16:10:43 +0000170 else
171 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172}
173
Guido van Rossumc6004111993-11-05 10:22:19 +0000174object *
175strobject(v)
176 object *v;
177{
178 if (v == NULL)
179 return newstringobject("<NULL>");
180 if (is_stringobject(v)) {
181 INCREF(v);
182 return v;
183 }
184 else {
185 object *func = getattr(v, "__str__");
186 object *args;
187 object *res;
188 if (func == NULL) {
189 err_clear();
190 return reprobject(v);
191 }
192 args = newtupleobject(0);
193 if (args == NULL)
194 res = NULL;
195 else {
196 res = call_object(func, args);
197 DECREF(args);
198 }
199 DECREF(func);
200 return res;
201 }
202}
203
Guido van Rossum20566841995-01-12 11:26:10 +0000204static object *
205do_cmp(v, w)
206 object *v, *w;
207{
208 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
209 because the check in cmpobject() reverses the objects first.
210 This is intentional -- it makes no sense to define cmp(x,y) different
211 than -cmp(y,x). */
212 if (is_instanceobject(v) || is_instanceobject(w))
213 return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp);
214 return newintobject((long)cmpobject(v, w));
215}
216
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217int
218cmpobject(v, w)
219 object *v, *w;
220{
221 typeobject *tp;
222 if (v == w)
223 return 0;
224 if (v == NULL)
225 return -1;
226 if (w == NULL)
227 return 1;
Guido van Rossum20566841995-01-12 11:26:10 +0000228 if (is_instanceobject(v) || is_instanceobject(w)) {
229 object *res;
230 int c;
231 if (!is_instanceobject(v))
232 return -cmpobject(w, v);
233 res = do_cmp(v, w);
234 if (res == NULL) {
235 err_clear();
236 return (v < w) ? -1 : 1;
237 }
238 if (!is_intobject(res)) {
239 DECREF(res);
240 return (v < w) ? -1 : 1;
241 }
242 c = getintvalue(res);
243 DECREF(res);
244 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
245 }
Guido van Rossum9fb03681991-07-01 18:48:04 +0000246 if ((tp = v->ob_type) != w->ob_type) {
247 if (tp->tp_as_number != NULL &&
248 w->ob_type->tp_as_number != NULL) {
249 if (coerce(&v, &w) != 0) {
250 err_clear();
251 /* XXX Should report the error,
252 XXX but the interface isn't there... */
253 }
254 else {
255 int cmp = (*v->ob_type->tp_compare)(v, w);
256 DECREF(v);
257 DECREF(w);
258 return cmp;
259 }
260 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000262 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263 if (tp->tp_compare == NULL)
264 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000265 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266}
267
Guido van Rossum9bfef441993-03-29 10:43:31 +0000268long
269hashobject(v)
270 object *v;
271{
272 typeobject *tp = v->ob_type;
273 if (tp->tp_hash != NULL)
274 return (*tp->tp_hash)(v);
275 if (tp->tp_compare == NULL)
276 return (long) v; /* Use address as hash value */
277 /* If there's a cmp but no hash defined, the object can't be hashed */
278 err_setstr(TypeError, "unhashable type");
279 return -1;
280}
281
Guido van Rossum3f5da241990-12-20 15:06:42 +0000282object *
283getattr(v, name)
284 object *v;
285 char *name;
286{
287 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossum016564a1995-01-07 11:54:44 +0000288 err_setstr(AttributeError, "attribute-less object");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000289 return NULL;
290 }
291 else {
292 return (*v->ob_type->tp_getattr)(v, name);
293 }
294}
295
296int
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000297hasattr(v, name)
298 object *v;
299 char *name;
300{
301 object *res = getattr(v, name);
302 if (res != NULL) {
303 DECREF(res);
304 return 1;
305 }
306 err_clear();
307 return 0;
308}
309
310int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000311setattr(v, name, w)
312 object *v;
313 char *name;
314 object *w;
315{
316 if (v->ob_type->tp_setattr == NULL) {
317 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000318 err_setstr(TypeError,
319 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000320 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000321 err_setstr(TypeError,
322 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000323 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000324 }
325 else {
326 return (*v->ob_type->tp_setattr)(v, name, w);
327 }
328}
329
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000330/* Test a value used as condition, e.g., in a for or if statement.
331 Return -1 if an error occurred */
332
333int
334testbool(v)
335 object *v;
336{
337 int res;
338 if (v == None)
339 res = 0;
340 else if (v->ob_type->tp_as_number != NULL)
341 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
342 else if (v->ob_type->tp_as_mapping != NULL)
343 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
344 else if (v->ob_type->tp_as_sequence != NULL)
345 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
346 else
347 res = 1;
348 if (res > 0)
349 res = 1;
350 return res;
351}
352
Guido van Rossum5524a591995-01-10 15:26:20 +0000353/* Coerce two numeric types to the "larger" one.
354 Increment the reference count on each argument.
355 Return -1 and raise an exception if no coercion is possible
356 (and then no reference count is incremented).
357*/
358
359int
360coerce(pv, pw)
361 object **pv, **pw;
362{
363 register object *v = *pv;
364 register object *w = *pw;
365 int res;
366
367 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
368 INCREF(v);
369 INCREF(w);
370 return 0;
371 }
372 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
373 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
374 if (res <= 0)
375 return res;
376 }
377 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
378 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
379 if (res <= 0)
380 return res;
381 }
382 err_setstr(TypeError, "number coercion failed");
383 return -1;
384}
385
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386
387/*
388NoObject is usable as a non-NULL undefined value, used by the macro None.
389There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000390so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391*/
392
Guido van Rossum0c182a11992-03-27 17:26:13 +0000393/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000394static object *
395none_repr(op)
396 object *op;
397{
398 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399}
400
401static typeobject Notype = {
402 OB_HEAD_INIT(&Typetype)
403 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000404 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 0,
406 0,
407 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000408 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000409 0, /*tp_getattr*/
410 0, /*tp_setattr*/
411 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000412 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000413 0, /*tp_as_number*/
414 0, /*tp_as_sequence*/
415 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000416 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417};
418
419object NoObject = {
420 OB_HEAD_INIT(&Notype)
421};
422
423
424#ifdef TRACE_REFS
425
426static object refchain = {&refchain, &refchain};
427
428NEWREF(op)
429 object *op;
430{
431 ref_total++;
432 op->ob_refcnt = 1;
433 op->_ob_next = refchain._ob_next;
434 op->_ob_prev = &refchain;
435 refchain._ob_next->_ob_prev = op;
436 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000437#ifdef COUNT_ALLOCS
438 inc_count(op->ob_type);
439#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440}
441
Guido van Rossum3f5da241990-12-20 15:06:42 +0000442UNREF(op)
443 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000445 register object *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000446 if (op->ob_refcnt < 0)
447 fatal("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000448 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000449 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
450 fatal("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000451#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000452 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
453 if (p == op)
454 break;
455 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000456 if (p == &refchain) /* Not found */
457 fatal("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000458#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459 op->_ob_next->_ob_prev = op->_ob_prev;
460 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000461 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000462}
463
464DELREF(op)
465 object *op;
466{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000467 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000468 UNREF(op);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000469#ifdef COUNT_ALLOCS
470 op->ob_type->tp_free++;
471#endif
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000472 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000473 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474}
475
476printrefs(fp)
477 FILE *fp;
478{
479 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000480 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000482 if (op->ob_refcnt == 1 && is_stringobject(op))
483 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000485 if (printobject(op, fp, 0) != 0)
486 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487 putc('\n', fp);
488 }
489}
490
491#endif