blob: 9c99ea2d19ed6e148f4c5e54afdcbdcec910df3d [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
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000059PyObject *
60get_counts()
61{
62 PyTypeObject *tp;
63 PyObject *result;
64 PyObject *v;
65
66 result = PyList_New(0);
67 if (result == NULL)
68 return NULL;
69 for (tp = type_list; tp; tp = tp->tp_next) {
70 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
71 tp->tp_free, tp->tp_maxalloc);
72 if (v == NULL) {
73 Py_DECREF(result);
74 return NULL;
75 }
76 if (PyList_Append(result, v) < 0) {
77 Py_DECREF(v);
78 Py_DECREF(result);
79 return NULL;
80 }
81 Py_DECREF(v);
82 }
83 return result;
84}
85
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000086void
87inc_count(tp)
88 typeobject *tp;
89{
90 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000091 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000092 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumd7047b31995-01-02 19:07:15 +000093 fatal("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000094 tp->tp_next = type_list;
95 type_list = tp;
96 }
97 tp->tp_alloc++;
98 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
99 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
100}
101#endif
102
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000103#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104object *
105newobject(tp)
106 typeobject *tp;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000107#else
108object *
109newobject(tp,op)
110 typeobject *tp;
111 PyObject *op;
112#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000114#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 object *op = (object *) malloc(tp->tp_basicsize);
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000116#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 if (op == NULL)
118 return err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 op->ob_type = tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000120 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121 return op;
122}
123
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000124#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125varobject *
126newvarobject(tp, size)
127 typeobject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000128 int size;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000129#else
130varobject *
131newvarobject(tp, size, op)
132 typeobject *tp;
133 int size;
134 varobject *op;
135#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136{
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000137#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 varobject *op = (varobject *)
139 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000140#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141 if (op == NULL)
Guido van Rossum05ab1111991-05-05 20:10:41 +0000142 return (varobject *)err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143 op->ob_type = tp;
144 op->ob_size = size;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000145 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 return op;
147}
148
Guido van Rossum90933611991-06-07 16:10:43 +0000149int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150printobject(op, fp, flags)
151 object *op;
152 FILE *fp;
153 int flags;
154{
Guido van Rossum278ef591991-07-27 21:40:24 +0000155 int ret = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000156 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000157 return -1;
Guido van Rossum90933611991-06-07 16:10:43 +0000158 if (op == NULL) {
159 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 }
Guido van Rossum90933611991-06-07 16:10:43 +0000161 else {
162 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000163 fprintf(fp, "<refcnt %u at %lx>",
164 op->ob_refcnt, (long)op);
165 else if (op->ob_type->tp_print == NULL) {
166 if (op->ob_type->tp_repr == NULL) {
167 fprintf(fp, "<%s object at %lx>",
168 op->ob_type->tp_name, (long)op);
169 }
170 else {
Guido van Rossumc6004111993-11-05 10:22:19 +0000171 object *s;
172 if (flags & PRINT_RAW)
173 s = strobject(op);
174 else
175 s = reprobject(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000176 if (s == NULL)
177 ret = -1;
178 else if (!is_stringobject(s)) {
179 err_setstr(TypeError,
180 "repr not string");
181 ret = -1;
182 }
183 else {
184 fprintf(fp, "%s", getstringvalue(s));
185 }
186 XDECREF(s);
187 }
188 }
Guido van Rossum90933611991-06-07 16:10:43 +0000189 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000190 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000191 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000192 if (ret == 0) {
193 if (ferror(fp)) {
Guido van Rossum2912f221991-12-10 13:59:09 +0000194 err_errno(IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000195 clearerr(fp);
196 ret = -1;
197 }
198 }
199 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200}
201
202object *
203reprobject(v)
204 object *v;
205{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000206 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000207 return NULL;
Guido van Rossum90933611991-06-07 16:10:43 +0000208 if (v == NULL)
209 return newstringobject("<NULL>");
210 else if (v->ob_type->tp_repr == NULL) {
211 char buf[120];
212 sprintf(buf, "<%.80s object at %lx>",
213 v->ob_type->tp_name, (long)v);
214 return newstringobject(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215 }
Guido van Rossum90933611991-06-07 16:10:43 +0000216 else
217 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
Guido van Rossumc6004111993-11-05 10:22:19 +0000220object *
221strobject(v)
222 object *v;
223{
224 if (v == NULL)
225 return newstringobject("<NULL>");
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000226 else if (is_stringobject(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000227 INCREF(v);
228 return v;
229 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000230 else if (v->ob_type->tp_str != NULL)
231 return (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000232 else {
Guido van Rossum32b582b1995-01-17 16:35:13 +0000233 object *func;
Guido van Rossumc6004111993-11-05 10:22:19 +0000234 object *res;
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000235 if (!is_instanceobject(v) ||
236 (func = getattr(v, "__str__")) == NULL) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000237 err_clear();
238 return reprobject(v);
239 }
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000240 res = call_object(func, (object *)NULL);
Guido van Rossumc6004111993-11-05 10:22:19 +0000241 DECREF(func);
242 return res;
243 }
244}
245
Guido van Rossum20566841995-01-12 11:26:10 +0000246static object *
247do_cmp(v, w)
248 object *v, *w;
249{
250 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
251 because the check in cmpobject() reverses the objects first.
252 This is intentional -- it makes no sense to define cmp(x,y) different
253 than -cmp(y,x). */
254 if (is_instanceobject(v) || is_instanceobject(w))
255 return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp);
256 return newintobject((long)cmpobject(v, w));
257}
258
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259int
260cmpobject(v, w)
261 object *v, *w;
262{
263 typeobject *tp;
264 if (v == w)
265 return 0;
266 if (v == NULL)
267 return -1;
268 if (w == NULL)
269 return 1;
Guido van Rossum20566841995-01-12 11:26:10 +0000270 if (is_instanceobject(v) || is_instanceobject(w)) {
271 object *res;
272 int c;
273 if (!is_instanceobject(v))
274 return -cmpobject(w, v);
275 res = do_cmp(v, w);
276 if (res == NULL) {
277 err_clear();
278 return (v < w) ? -1 : 1;
279 }
280 if (!is_intobject(res)) {
281 DECREF(res);
282 return (v < w) ? -1 : 1;
283 }
284 c = getintvalue(res);
285 DECREF(res);
286 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
287 }
Guido van Rossum9fb03681991-07-01 18:48:04 +0000288 if ((tp = v->ob_type) != w->ob_type) {
289 if (tp->tp_as_number != NULL &&
290 w->ob_type->tp_as_number != NULL) {
291 if (coerce(&v, &w) != 0) {
292 err_clear();
293 /* XXX Should report the error,
294 XXX but the interface isn't there... */
295 }
296 else {
297 int cmp = (*v->ob_type->tp_compare)(v, w);
298 DECREF(v);
299 DECREF(w);
300 return cmp;
301 }
302 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000304 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 if (tp->tp_compare == NULL)
306 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000307 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308}
309
Guido van Rossum9bfef441993-03-29 10:43:31 +0000310long
311hashobject(v)
312 object *v;
313{
314 typeobject *tp = v->ob_type;
315 if (tp->tp_hash != NULL)
316 return (*tp->tp_hash)(v);
317 if (tp->tp_compare == NULL)
318 return (long) v; /* Use address as hash value */
319 /* If there's a cmp but no hash defined, the object can't be hashed */
320 err_setstr(TypeError, "unhashable type");
321 return -1;
322}
323
Guido van Rossum3f5da241990-12-20 15:06:42 +0000324object *
325getattr(v, name)
326 object *v;
327 char *name;
328{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000329 if (v->ob_type->tp_getattro != NULL) {
330 object *w, *res;
331 w = newstringobject(name);
332 if (w == NULL)
333 return NULL;
334 res = (*v->ob_type->tp_getattro)(v, w);
335 XDECREF(w);
336 return res;
337 }
338
Guido van Rossum3f5da241990-12-20 15:06:42 +0000339 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossum016564a1995-01-07 11:54:44 +0000340 err_setstr(AttributeError, "attribute-less object");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000341 return NULL;
342 }
343 else {
344 return (*v->ob_type->tp_getattr)(v, name);
345 }
346}
347
348int
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000349hasattr(v, name)
350 object *v;
351 char *name;
352{
353 object *res = getattr(v, name);
354 if (res != NULL) {
355 DECREF(res);
356 return 1;
357 }
358 err_clear();
359 return 0;
360}
361
362int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000363setattr(v, name, w)
364 object *v;
365 char *name;
366 object *w;
367{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000368 if (v->ob_type->tp_setattro != NULL) {
369 object *s;
370 int res;
371 s = newstringobject(name);
372 if (s == NULL)
373 return NULL;
374 res = (*v->ob_type->tp_setattro)(v, s, w);
375 XDECREF(s);
376 return res;
377 }
378
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379 if (v->ob_type->tp_setattr == NULL) {
380 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000381 err_setstr(TypeError,
382 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000383 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000384 err_setstr(TypeError,
385 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000386 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000387 }
388 else {
389 return (*v->ob_type->tp_setattr)(v, name, w);
390 }
391}
392
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000393/* Test a value used as condition, e.g., in a for or if statement.
394 Return -1 if an error occurred */
395
396int
397testbool(v)
398 object *v;
399{
400 int res;
401 if (v == None)
402 res = 0;
403 else if (v->ob_type->tp_as_number != NULL)
404 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
405 else if (v->ob_type->tp_as_mapping != NULL)
406 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
407 else if (v->ob_type->tp_as_sequence != NULL)
408 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
409 else
410 res = 1;
411 if (res > 0)
412 res = 1;
413 return res;
414}
415
Guido van Rossum5524a591995-01-10 15:26:20 +0000416/* Coerce two numeric types to the "larger" one.
417 Increment the reference count on each argument.
418 Return -1 and raise an exception if no coercion is possible
419 (and then no reference count is incremented).
420*/
421
422int
423coerce(pv, pw)
424 object **pv, **pw;
425{
426 register object *v = *pv;
427 register object *w = *pw;
428 int res;
429
430 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
431 INCREF(v);
432 INCREF(w);
433 return 0;
434 }
435 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
436 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
437 if (res <= 0)
438 return res;
439 }
440 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
441 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
442 if (res <= 0)
443 return res;
444 }
445 err_setstr(TypeError, "number coercion failed");
446 return -1;
447}
448
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000450/* Test whether an object can be called */
451
452int
453callable(x)
454 object *x;
455{
456 if (x == NULL)
457 return 0;
458 if (x->ob_type->tp_call != NULL ||
459 is_funcobject(x) ||
460 is_instancemethodobject(x) ||
461 is_methodobject(x) ||
462 is_classobject(x))
463 return 1;
464 if (is_instanceobject(x)) {
465 object *call = getattr(x, "__call__");
466 if (call == NULL) {
467 err_clear();
468 return 0;
469 }
470 /* Could test recursively but don't, for fear of endless
471 recursion if some joker sets self.__call__ = self */
472 DECREF(call);
473 return 1;
474 }
475 return 0;
476}
477
478
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479/*
480NoObject is usable as a non-NULL undefined value, used by the macro None.
481There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000482so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483*/
484
Guido van Rossum0c182a11992-03-27 17:26:13 +0000485/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000486static object *
487none_repr(op)
488 object *op;
489{
490 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491}
492
493static typeobject Notype = {
494 OB_HEAD_INIT(&Typetype)
495 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000496 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497 0,
498 0,
499 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000500 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000501 0, /*tp_getattr*/
502 0, /*tp_setattr*/
503 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000504 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000505 0, /*tp_as_number*/
506 0, /*tp_as_sequence*/
507 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000508 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509};
510
511object NoObject = {
512 OB_HEAD_INIT(&Notype)
513};
514
515
Guido van Rossum84a90321996-05-22 16:34:47 +0000516#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517
518static object refchain = {&refchain, &refchain};
519
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000520void
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521NEWREF(op)
522 object *op;
523{
524 ref_total++;
525 op->ob_refcnt = 1;
526 op->_ob_next = refchain._ob_next;
527 op->_ob_prev = &refchain;
528 refchain._ob_next->_ob_prev = op;
529 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000530#ifdef COUNT_ALLOCS
531 inc_count(op->ob_type);
532#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533}
534
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000535void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536UNREF(op)
537 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000539 register object *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000540 if (op->ob_refcnt < 0)
541 fatal("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000542 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000543 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
544 fatal("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000545#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000546 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
547 if (p == op)
548 break;
549 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000550 if (p == &refchain) /* Not found */
551 fatal("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000552#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 op->_ob_next->_ob_prev = op->_ob_prev;
554 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000555 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000556#ifdef COUNT_ALLOCS
557 op->ob_type->tp_free++;
558#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000559}
560
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000561void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000562DELREF(op)
563 object *op;
564{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000565 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000566 UNREF(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000567 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000568 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569}
570
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000571void
Guido van Rossumded690f1996-05-24 20:48:31 +0000572_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573 FILE *fp;
574{
575 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000576 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000578 if (op->ob_refcnt == 1 && is_stringobject(op))
579 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000581 if (printobject(op, fp, 0) != 0)
582 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583 putc('\n', fp);
584 }
585}
586
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000587PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000588_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000589 PyObject *self;
590 PyObject *args;
591{
592 int i, n;
593 PyObject *t = NULL;
594 PyObject *res, *op;
595
596 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
597 return NULL;
598 op = refchain._ob_next;
599 res = PyList_New(0);
600 if (res == NULL)
601 return NULL;
602 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
603 while (op == self || op == args || op == res || op == t ||
604 t != NULL && op->ob_type != (PyTypeObject *) t) {
605 op = op->_ob_next;
606 if (op == &refchain)
607 return res;
608 }
609 if (PyList_Append(res, op) < 0) {
610 Py_DECREF(res);
611 return NULL;
612 }
613 op = op->_ob_next;
614 }
615 return res;
616}
617
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000619
620
621/* Hack to force loading of cobject.o */
622static PyTypeObject *cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000623
624
625/* Hack to force loading of abstract.o */
626static int (*abstract_hack) FPROTO((PyObject *)) = &PyObject_Length;