blob: 747657cd22c254f4dce9ca89397a789a77f948ff [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 Rossum85a5fbb1990-10-14 12:07:46 +0000103object *
104newobject(tp)
105 typeobject *tp;
106{
107 object *op = (object *) malloc(tp->tp_basicsize);
108 if (op == NULL)
109 return err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 op->ob_type = tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000111 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 return op;
113}
114
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115varobject *
116newvarobject(tp, size)
117 typeobject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000118 int size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119{
120 varobject *op = (varobject *)
121 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
122 if (op == NULL)
Guido van Rossum05ab1111991-05-05 20:10:41 +0000123 return (varobject *)err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 op->ob_type = tp;
125 op->ob_size = size;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000126 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 return op;
128}
129
Guido van Rossum90933611991-06-07 16:10:43 +0000130int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131printobject(op, fp, flags)
132 object *op;
133 FILE *fp;
134 int flags;
135{
Guido van Rossum278ef591991-07-27 21:40:24 +0000136 int ret = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000137 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000138 return -1;
Guido van Rossum90933611991-06-07 16:10:43 +0000139 if (op == NULL) {
140 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141 }
Guido van Rossum90933611991-06-07 16:10:43 +0000142 else {
143 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000144 fprintf(fp, "<refcnt %u at %lx>",
145 op->ob_refcnt, (long)op);
146 else if (op->ob_type->tp_print == NULL) {
147 if (op->ob_type->tp_repr == NULL) {
148 fprintf(fp, "<%s object at %lx>",
149 op->ob_type->tp_name, (long)op);
150 }
151 else {
Guido van Rossumc6004111993-11-05 10:22:19 +0000152 object *s;
153 if (flags & PRINT_RAW)
154 s = strobject(op);
155 else
156 s = reprobject(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000157 if (s == NULL)
158 ret = -1;
159 else if (!is_stringobject(s)) {
160 err_setstr(TypeError,
161 "repr not string");
162 ret = -1;
163 }
164 else {
165 fprintf(fp, "%s", getstringvalue(s));
166 }
167 XDECREF(s);
168 }
169 }
Guido van Rossum90933611991-06-07 16:10:43 +0000170 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000171 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000172 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000173 if (ret == 0) {
174 if (ferror(fp)) {
Guido van Rossum2912f221991-12-10 13:59:09 +0000175 err_errno(IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000176 clearerr(fp);
177 ret = -1;
178 }
179 }
180 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181}
182
183object *
184reprobject(v)
185 object *v;
186{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000187 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000188 return NULL;
Guido van Rossum90933611991-06-07 16:10:43 +0000189 if (v == NULL)
190 return newstringobject("<NULL>");
191 else if (v->ob_type->tp_repr == NULL) {
192 char buf[120];
193 sprintf(buf, "<%.80s object at %lx>",
194 v->ob_type->tp_name, (long)v);
195 return newstringobject(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196 }
Guido van Rossum90933611991-06-07 16:10:43 +0000197 else
198 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199}
200
Guido van Rossumc6004111993-11-05 10:22:19 +0000201object *
202strobject(v)
203 object *v;
204{
205 if (v == NULL)
206 return newstringobject("<NULL>");
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000207 else if (is_stringobject(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000208 INCREF(v);
209 return v;
210 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000211 else if (v->ob_type->tp_str != NULL)
212 return (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000213 else {
Guido van Rossum32b582b1995-01-17 16:35:13 +0000214 object *func;
Guido van Rossumc6004111993-11-05 10:22:19 +0000215 object *res;
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000216 if (!is_instanceobject(v) ||
217 (func = getattr(v, "__str__")) == NULL) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000218 err_clear();
219 return reprobject(v);
220 }
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000221 res = call_object(func, (object *)NULL);
Guido van Rossumc6004111993-11-05 10:22:19 +0000222 DECREF(func);
223 return res;
224 }
225}
226
Guido van Rossum20566841995-01-12 11:26:10 +0000227static object *
228do_cmp(v, w)
229 object *v, *w;
230{
231 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
232 because the check in cmpobject() reverses the objects first.
233 This is intentional -- it makes no sense to define cmp(x,y) different
234 than -cmp(y,x). */
235 if (is_instanceobject(v) || is_instanceobject(w))
236 return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp);
237 return newintobject((long)cmpobject(v, w));
238}
239
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240int
241cmpobject(v, w)
242 object *v, *w;
243{
244 typeobject *tp;
245 if (v == w)
246 return 0;
247 if (v == NULL)
248 return -1;
249 if (w == NULL)
250 return 1;
Guido van Rossum20566841995-01-12 11:26:10 +0000251 if (is_instanceobject(v) || is_instanceobject(w)) {
252 object *res;
253 int c;
254 if (!is_instanceobject(v))
255 return -cmpobject(w, v);
256 res = do_cmp(v, w);
257 if (res == NULL) {
258 err_clear();
259 return (v < w) ? -1 : 1;
260 }
261 if (!is_intobject(res)) {
262 DECREF(res);
263 return (v < w) ? -1 : 1;
264 }
265 c = getintvalue(res);
266 DECREF(res);
267 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
268 }
Guido van Rossum9fb03681991-07-01 18:48:04 +0000269 if ((tp = v->ob_type) != w->ob_type) {
270 if (tp->tp_as_number != NULL &&
271 w->ob_type->tp_as_number != NULL) {
272 if (coerce(&v, &w) != 0) {
273 err_clear();
274 /* XXX Should report the error,
275 XXX but the interface isn't there... */
276 }
277 else {
278 int cmp = (*v->ob_type->tp_compare)(v, w);
279 DECREF(v);
280 DECREF(w);
281 return cmp;
282 }
283 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000285 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 if (tp->tp_compare == NULL)
287 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000288 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289}
290
Guido van Rossum9bfef441993-03-29 10:43:31 +0000291long
292hashobject(v)
293 object *v;
294{
295 typeobject *tp = v->ob_type;
296 if (tp->tp_hash != NULL)
297 return (*tp->tp_hash)(v);
298 if (tp->tp_compare == NULL)
299 return (long) v; /* Use address as hash value */
300 /* If there's a cmp but no hash defined, the object can't be hashed */
301 err_setstr(TypeError, "unhashable type");
302 return -1;
303}
304
Guido van Rossum3f5da241990-12-20 15:06:42 +0000305object *
306getattr(v, name)
307 object *v;
308 char *name;
309{
310 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossum016564a1995-01-07 11:54:44 +0000311 err_setstr(AttributeError, "attribute-less object");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000312 return NULL;
313 }
314 else {
315 return (*v->ob_type->tp_getattr)(v, name);
316 }
317}
318
319int
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000320hasattr(v, name)
321 object *v;
322 char *name;
323{
324 object *res = getattr(v, name);
325 if (res != NULL) {
326 DECREF(res);
327 return 1;
328 }
329 err_clear();
330 return 0;
331}
332
333int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334setattr(v, name, w)
335 object *v;
336 char *name;
337 object *w;
338{
339 if (v->ob_type->tp_setattr == NULL) {
340 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000341 err_setstr(TypeError,
342 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000343 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000344 err_setstr(TypeError,
345 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000346 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000347 }
348 else {
349 return (*v->ob_type->tp_setattr)(v, name, w);
350 }
351}
352
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000353/* Test a value used as condition, e.g., in a for or if statement.
354 Return -1 if an error occurred */
355
356int
357testbool(v)
358 object *v;
359{
360 int res;
361 if (v == None)
362 res = 0;
363 else if (v->ob_type->tp_as_number != NULL)
364 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
365 else if (v->ob_type->tp_as_mapping != NULL)
366 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
367 else if (v->ob_type->tp_as_sequence != NULL)
368 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
369 else
370 res = 1;
371 if (res > 0)
372 res = 1;
373 return res;
374}
375
Guido van Rossum5524a591995-01-10 15:26:20 +0000376/* Coerce two numeric types to the "larger" one.
377 Increment the reference count on each argument.
378 Return -1 and raise an exception if no coercion is possible
379 (and then no reference count is incremented).
380*/
381
382int
383coerce(pv, pw)
384 object **pv, **pw;
385{
386 register object *v = *pv;
387 register object *w = *pw;
388 int res;
389
390 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
391 INCREF(v);
392 INCREF(w);
393 return 0;
394 }
395 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
396 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
397 if (res <= 0)
398 return res;
399 }
400 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
401 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
402 if (res <= 0)
403 return res;
404 }
405 err_setstr(TypeError, "number coercion failed");
406 return -1;
407}
408
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000410/* Test whether an object can be called */
411
412int
413callable(x)
414 object *x;
415{
416 if (x == NULL)
417 return 0;
418 if (x->ob_type->tp_call != NULL ||
419 is_funcobject(x) ||
420 is_instancemethodobject(x) ||
421 is_methodobject(x) ||
422 is_classobject(x))
423 return 1;
424 if (is_instanceobject(x)) {
425 object *call = getattr(x, "__call__");
426 if (call == NULL) {
427 err_clear();
428 return 0;
429 }
430 /* Could test recursively but don't, for fear of endless
431 recursion if some joker sets self.__call__ = self */
432 DECREF(call);
433 return 1;
434 }
435 return 0;
436}
437
438
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439/*
440NoObject is usable as a non-NULL undefined value, used by the macro None.
441There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000442so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443*/
444
Guido van Rossum0c182a11992-03-27 17:26:13 +0000445/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000446static object *
447none_repr(op)
448 object *op;
449{
450 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451}
452
453static typeobject Notype = {
454 OB_HEAD_INIT(&Typetype)
455 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000456 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457 0,
458 0,
459 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000460 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000461 0, /*tp_getattr*/
462 0, /*tp_setattr*/
463 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000464 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000465 0, /*tp_as_number*/
466 0, /*tp_as_sequence*/
467 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000468 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469};
470
471object NoObject = {
472 OB_HEAD_INIT(&Notype)
473};
474
475
Guido van Rossum84a90321996-05-22 16:34:47 +0000476#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477
478static object refchain = {&refchain, &refchain};
479
480NEWREF(op)
481 object *op;
482{
483 ref_total++;
484 op->ob_refcnt = 1;
485 op->_ob_next = refchain._ob_next;
486 op->_ob_prev = &refchain;
487 refchain._ob_next->_ob_prev = op;
488 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000489#ifdef COUNT_ALLOCS
490 inc_count(op->ob_type);
491#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000492}
493
Guido van Rossum3f5da241990-12-20 15:06:42 +0000494UNREF(op)
495 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000497 register object *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000498 if (op->ob_refcnt < 0)
499 fatal("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000500 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000501 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
502 fatal("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000503#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000504 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
505 if (p == op)
506 break;
507 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000508 if (p == &refchain) /* Not found */
509 fatal("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000510#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000511 op->_ob_next->_ob_prev = op->_ob_prev;
512 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000513 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000514#ifdef COUNT_ALLOCS
515 op->ob_type->tp_free++;
516#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000517}
518
519DELREF(op)
520 object *op;
521{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000522 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000523 UNREF(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000524 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000525 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526}
527
528printrefs(fp)
529 FILE *fp;
530{
531 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000532 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000534 if (op->ob_refcnt == 1 && is_stringobject(op))
535 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000536 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000537 if (printobject(op, fp, 0) != 0)
538 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539 putc('\n', fp);
540 }
541}
542
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000543PyObject *
544getobjects(self, args)
545 PyObject *self;
546 PyObject *args;
547{
548 int i, n;
549 PyObject *t = NULL;
550 PyObject *res, *op;
551
552 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
553 return NULL;
554 op = refchain._ob_next;
555 res = PyList_New(0);
556 if (res == NULL)
557 return NULL;
558 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
559 while (op == self || op == args || op == res || op == t ||
560 t != NULL && op->ob_type != (PyTypeObject *) t) {
561 op = op->_ob_next;
562 if (op == &refchain)
563 return res;
564 }
565 if (PyList_Append(res, op) < 0) {
566 Py_DECREF(res);
567 return NULL;
568 }
569 op = op->_ob_next;
570 }
571 return res;
572}
573
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000575
576
577/* Hack to force loading of cobject.o */
578static PyTypeObject *cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000579
580
581/* Hack to force loading of abstract.o */
582static int (*abstract_hack) FPROTO((PyObject *)) = &PyObject_Length;