blob: be40c40ed34d7a24072b31dc6cecb32089c9d990 [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{
329 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossum016564a1995-01-07 11:54:44 +0000330 err_setstr(AttributeError, "attribute-less object");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331 return NULL;
332 }
333 else {
334 return (*v->ob_type->tp_getattr)(v, name);
335 }
336}
337
338int
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000339hasattr(v, name)
340 object *v;
341 char *name;
342{
343 object *res = getattr(v, name);
344 if (res != NULL) {
345 DECREF(res);
346 return 1;
347 }
348 err_clear();
349 return 0;
350}
351
352int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000353setattr(v, name, w)
354 object *v;
355 char *name;
356 object *w;
357{
358 if (v->ob_type->tp_setattr == NULL) {
359 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000360 err_setstr(TypeError,
361 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000362 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000363 err_setstr(TypeError,
364 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000365 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000366 }
367 else {
368 return (*v->ob_type->tp_setattr)(v, name, w);
369 }
370}
371
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000372/* Test a value used as condition, e.g., in a for or if statement.
373 Return -1 if an error occurred */
374
375int
376testbool(v)
377 object *v;
378{
379 int res;
380 if (v == None)
381 res = 0;
382 else if (v->ob_type->tp_as_number != NULL)
383 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
384 else if (v->ob_type->tp_as_mapping != NULL)
385 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
386 else if (v->ob_type->tp_as_sequence != NULL)
387 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
388 else
389 res = 1;
390 if (res > 0)
391 res = 1;
392 return res;
393}
394
Guido van Rossum5524a591995-01-10 15:26:20 +0000395/* Coerce two numeric types to the "larger" one.
396 Increment the reference count on each argument.
397 Return -1 and raise an exception if no coercion is possible
398 (and then no reference count is incremented).
399*/
400
401int
402coerce(pv, pw)
403 object **pv, **pw;
404{
405 register object *v = *pv;
406 register object *w = *pw;
407 int res;
408
409 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
410 INCREF(v);
411 INCREF(w);
412 return 0;
413 }
414 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
415 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
416 if (res <= 0)
417 return res;
418 }
419 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
420 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
421 if (res <= 0)
422 return res;
423 }
424 err_setstr(TypeError, "number coercion failed");
425 return -1;
426}
427
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000429/* Test whether an object can be called */
430
431int
432callable(x)
433 object *x;
434{
435 if (x == NULL)
436 return 0;
437 if (x->ob_type->tp_call != NULL ||
438 is_funcobject(x) ||
439 is_instancemethodobject(x) ||
440 is_methodobject(x) ||
441 is_classobject(x))
442 return 1;
443 if (is_instanceobject(x)) {
444 object *call = getattr(x, "__call__");
445 if (call == NULL) {
446 err_clear();
447 return 0;
448 }
449 /* Could test recursively but don't, for fear of endless
450 recursion if some joker sets self.__call__ = self */
451 DECREF(call);
452 return 1;
453 }
454 return 0;
455}
456
457
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458/*
459NoObject is usable as a non-NULL undefined value, used by the macro None.
460There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000461so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462*/
463
Guido van Rossum0c182a11992-03-27 17:26:13 +0000464/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000465static object *
466none_repr(op)
467 object *op;
468{
469 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470}
471
472static typeobject Notype = {
473 OB_HEAD_INIT(&Typetype)
474 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000475 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476 0,
477 0,
478 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000479 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000480 0, /*tp_getattr*/
481 0, /*tp_setattr*/
482 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000483 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000484 0, /*tp_as_number*/
485 0, /*tp_as_sequence*/
486 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000487 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488};
489
490object NoObject = {
491 OB_HEAD_INIT(&Notype)
492};
493
494
Guido van Rossum84a90321996-05-22 16:34:47 +0000495#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496
497static object refchain = {&refchain, &refchain};
498
499NEWREF(op)
500 object *op;
501{
502 ref_total++;
503 op->ob_refcnt = 1;
504 op->_ob_next = refchain._ob_next;
505 op->_ob_prev = &refchain;
506 refchain._ob_next->_ob_prev = op;
507 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000508#ifdef COUNT_ALLOCS
509 inc_count(op->ob_type);
510#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000511}
512
Guido van Rossum3f5da241990-12-20 15:06:42 +0000513UNREF(op)
514 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516 register object *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000517 if (op->ob_refcnt < 0)
518 fatal("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000519 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000520 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
521 fatal("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000522#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000523 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
524 if (p == op)
525 break;
526 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000527 if (p == &refchain) /* Not found */
528 fatal("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000529#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530 op->_ob_next->_ob_prev = op->_ob_prev;
531 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000532 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000533#ifdef COUNT_ALLOCS
534 op->ob_type->tp_free++;
535#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536}
537
538DELREF(op)
539 object *op;
540{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000541 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000542 UNREF(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000543 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000544 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545}
546
Guido van Rossumded690f1996-05-24 20:48:31 +0000547_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548 FILE *fp;
549{
550 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000551 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000553 if (op->ob_refcnt == 1 && is_stringobject(op))
554 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000556 if (printobject(op, fp, 0) != 0)
557 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558 putc('\n', fp);
559 }
560}
561
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000562PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000563_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000564 PyObject *self;
565 PyObject *args;
566{
567 int i, n;
568 PyObject *t = NULL;
569 PyObject *res, *op;
570
571 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
572 return NULL;
573 op = refchain._ob_next;
574 res = PyList_New(0);
575 if (res == NULL)
576 return NULL;
577 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
578 while (op == self || op == args || op == res || op == t ||
579 t != NULL && op->ob_type != (PyTypeObject *) t) {
580 op = op->_ob_next;
581 if (op == &refchain)
582 return res;
583 }
584 if (PyList_Append(res, op) < 0) {
585 Py_DECREF(res);
586 return NULL;
587 }
588 op = op->_ob_next;
589 }
590 return res;
591}
592
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000594
595
596/* Hack to force loading of cobject.o */
597static PyTypeObject *cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000598
599
600/* Hack to force loading of abstract.o */
601static int (*abstract_hack) FPROTO((PyObject *)) = &PyObject_Length;