blob: 1643ec6fc1c8968269f474d2ae7da5c13cd27a7a [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 Rossum6f9e4331995-03-29 16:57:48 +000029#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossum3f5da241990-12-20 15:06:42 +000030long 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) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000064 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000065 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;
Guido van Rossum2497ead1995-02-10 17:00:27 +000091 int size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
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>");
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000180 else if (is_stringobject(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000181 INCREF(v);
182 return v;
183 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000184 else if (v->ob_type->tp_str != NULL)
185 return (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000186 else {
Guido van Rossum32b582b1995-01-17 16:35:13 +0000187 object *func;
Guido van Rossumc6004111993-11-05 10:22:19 +0000188 object *res;
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000189 if (!is_instanceobject(v) ||
190 (func = getattr(v, "__str__")) == NULL) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000191 err_clear();
192 return reprobject(v);
193 }
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000194 res = call_object(func, (object *)NULL);
Guido van Rossumc6004111993-11-05 10:22:19 +0000195 DECREF(func);
196 return res;
197 }
198}
199
Guido van Rossum20566841995-01-12 11:26:10 +0000200static object *
201do_cmp(v, w)
202 object *v, *w;
203{
204 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
205 because the check in cmpobject() reverses the objects first.
206 This is intentional -- it makes no sense to define cmp(x,y) different
207 than -cmp(y,x). */
208 if (is_instanceobject(v) || is_instanceobject(w))
209 return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp);
210 return newintobject((long)cmpobject(v, w));
211}
212
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213int
214cmpobject(v, w)
215 object *v, *w;
216{
217 typeobject *tp;
218 if (v == w)
219 return 0;
220 if (v == NULL)
221 return -1;
222 if (w == NULL)
223 return 1;
Guido van Rossum20566841995-01-12 11:26:10 +0000224 if (is_instanceobject(v) || is_instanceobject(w)) {
225 object *res;
226 int c;
227 if (!is_instanceobject(v))
228 return -cmpobject(w, v);
229 res = do_cmp(v, w);
230 if (res == NULL) {
231 err_clear();
232 return (v < w) ? -1 : 1;
233 }
234 if (!is_intobject(res)) {
235 DECREF(res);
236 return (v < w) ? -1 : 1;
237 }
238 c = getintvalue(res);
239 DECREF(res);
240 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
241 }
Guido van Rossum9fb03681991-07-01 18:48:04 +0000242 if ((tp = v->ob_type) != w->ob_type) {
243 if (tp->tp_as_number != NULL &&
244 w->ob_type->tp_as_number != NULL) {
245 if (coerce(&v, &w) != 0) {
246 err_clear();
247 /* XXX Should report the error,
248 XXX but the interface isn't there... */
249 }
250 else {
251 int cmp = (*v->ob_type->tp_compare)(v, w);
252 DECREF(v);
253 DECREF(w);
254 return cmp;
255 }
256 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000258 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259 if (tp->tp_compare == NULL)
260 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000261 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
Guido van Rossum9bfef441993-03-29 10:43:31 +0000264long
265hashobject(v)
266 object *v;
267{
268 typeobject *tp = v->ob_type;
269 if (tp->tp_hash != NULL)
270 return (*tp->tp_hash)(v);
271 if (tp->tp_compare == NULL)
272 return (long) v; /* Use address as hash value */
273 /* If there's a cmp but no hash defined, the object can't be hashed */
274 err_setstr(TypeError, "unhashable type");
275 return -1;
276}
277
Guido van Rossum3f5da241990-12-20 15:06:42 +0000278object *
279getattr(v, name)
280 object *v;
281 char *name;
282{
283 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossum016564a1995-01-07 11:54:44 +0000284 err_setstr(AttributeError, "attribute-less object");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000285 return NULL;
286 }
287 else {
288 return (*v->ob_type->tp_getattr)(v, name);
289 }
290}
291
292int
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000293hasattr(v, name)
294 object *v;
295 char *name;
296{
297 object *res = getattr(v, name);
298 if (res != NULL) {
299 DECREF(res);
300 return 1;
301 }
302 err_clear();
303 return 0;
304}
305
306int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000307setattr(v, name, w)
308 object *v;
309 char *name;
310 object *w;
311{
312 if (v->ob_type->tp_setattr == NULL) {
313 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000314 err_setstr(TypeError,
315 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000316 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000317 err_setstr(TypeError,
318 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000319 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000320 }
321 else {
322 return (*v->ob_type->tp_setattr)(v, name, w);
323 }
324}
325
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000326/* Test a value used as condition, e.g., in a for or if statement.
327 Return -1 if an error occurred */
328
329int
330testbool(v)
331 object *v;
332{
333 int res;
334 if (v == None)
335 res = 0;
336 else if (v->ob_type->tp_as_number != NULL)
337 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
338 else if (v->ob_type->tp_as_mapping != NULL)
339 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
340 else if (v->ob_type->tp_as_sequence != NULL)
341 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
342 else
343 res = 1;
344 if (res > 0)
345 res = 1;
346 return res;
347}
348
Guido van Rossum5524a591995-01-10 15:26:20 +0000349/* Coerce two numeric types to the "larger" one.
350 Increment the reference count on each argument.
351 Return -1 and raise an exception if no coercion is possible
352 (and then no reference count is incremented).
353*/
354
355int
356coerce(pv, pw)
357 object **pv, **pw;
358{
359 register object *v = *pv;
360 register object *w = *pw;
361 int res;
362
363 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
364 INCREF(v);
365 INCREF(w);
366 return 0;
367 }
368 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
369 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
370 if (res <= 0)
371 return res;
372 }
373 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
374 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
375 if (res <= 0)
376 return res;
377 }
378 err_setstr(TypeError, "number coercion failed");
379 return -1;
380}
381
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000383/* Test whether an object can be called */
384
385int
386callable(x)
387 object *x;
388{
389 if (x == NULL)
390 return 0;
391 if (x->ob_type->tp_call != NULL ||
392 is_funcobject(x) ||
393 is_instancemethodobject(x) ||
394 is_methodobject(x) ||
395 is_classobject(x))
396 return 1;
397 if (is_instanceobject(x)) {
398 object *call = getattr(x, "__call__");
399 if (call == NULL) {
400 err_clear();
401 return 0;
402 }
403 /* Could test recursively but don't, for fear of endless
404 recursion if some joker sets self.__call__ = self */
405 DECREF(call);
406 return 1;
407 }
408 return 0;
409}
410
411
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412/*
413NoObject is usable as a non-NULL undefined value, used by the macro None.
414There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000415so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416*/
417
Guido van Rossum0c182a11992-03-27 17:26:13 +0000418/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000419static object *
420none_repr(op)
421 object *op;
422{
423 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424}
425
426static typeobject Notype = {
427 OB_HEAD_INIT(&Typetype)
428 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000429 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 0,
431 0,
432 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000433 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000434 0, /*tp_getattr*/
435 0, /*tp_setattr*/
436 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000437 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000438 0, /*tp_as_number*/
439 0, /*tp_as_sequence*/
440 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000441 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442};
443
444object NoObject = {
445 OB_HEAD_INIT(&Notype)
446};
447
448
449#ifdef TRACE_REFS
450
451static object refchain = {&refchain, &refchain};
452
453NEWREF(op)
454 object *op;
455{
456 ref_total++;
457 op->ob_refcnt = 1;
458 op->_ob_next = refchain._ob_next;
459 op->_ob_prev = &refchain;
460 refchain._ob_next->_ob_prev = op;
461 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000462#ifdef COUNT_ALLOCS
463 inc_count(op->ob_type);
464#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465}
466
Guido van Rossum3f5da241990-12-20 15:06:42 +0000467UNREF(op)
468 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000470 register object *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000471 if (op->ob_refcnt < 0)
472 fatal("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000473 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000474 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
475 fatal("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000476#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000477 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
478 if (p == op)
479 break;
480 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000481 if (p == &refchain) /* Not found */
482 fatal("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000483#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 op->_ob_next->_ob_prev = op->_ob_prev;
485 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000486 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000487#ifdef COUNT_ALLOCS
488 op->ob_type->tp_free++;
489#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000490}
491
492DELREF(op)
493 object *op;
494{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000495 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000496 UNREF(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000497 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000498 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000499}
500
501printrefs(fp)
502 FILE *fp;
503{
504 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000505 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000507 if (op->ob_refcnt == 1 && is_stringobject(op))
508 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000510 if (printobject(op, fp, 0) != 0)
511 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 putc('\n', fp);
513 }
514}
515
516#endif