blob: 0af31babaaa3f6d0cf7b9af055ab1db29eaee2a7 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum6f9e4331995-03-29 16:57:48 +000036#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossum3f5da241990-12-20 15:06:42 +000037long ref_total;
38#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
Guido van Rossum3f5da241990-12-20 15:06:42 +000040/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
41 These are used by the individual routines for object creation.
42 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000044#ifdef COUNT_ALLOCS
45static typeobject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000046extern int tuple_zero_allocs, fast_tuple_allocs;
47extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000048extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000049void
50dump_counts()
51{
52 typeobject *tp;
53
54 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000055 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
56 tp->tp_name, tp->tp_alloc, tp->tp_free,
57 tp->tp_maxalloc);
58 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
59 fast_tuple_allocs, tuple_zero_allocs);
60 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
61 quick_int_allocs, quick_neg_int_allocs);
62 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
63 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000064}
65
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000066PyObject *
67get_counts()
68{
69 PyTypeObject *tp;
70 PyObject *result;
71 PyObject *v;
72
73 result = PyList_New(0);
74 if (result == NULL)
75 return NULL;
76 for (tp = type_list; tp; tp = tp->tp_next) {
77 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
78 tp->tp_free, tp->tp_maxalloc);
79 if (v == NULL) {
80 Py_DECREF(result);
81 return NULL;
82 }
83 if (PyList_Append(result, v) < 0) {
84 Py_DECREF(v);
85 Py_DECREF(result);
86 return NULL;
87 }
88 Py_DECREF(v);
89 }
90 return result;
91}
92
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000093void
94inc_count(tp)
95 typeobject *tp;
96{
97 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000098 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000099 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000100 fatal("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000101 tp->tp_next = type_list;
102 type_list = tp;
103 }
104 tp->tp_alloc++;
105 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
106 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
107}
108#endif
109
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000110#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111object *
112newobject(tp)
113 typeobject *tp;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000114#else
115object *
116newobject(tp,op)
117 typeobject *tp;
118 PyObject *op;
119#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120{
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000121#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122 object *op = (object *) malloc(tp->tp_basicsize);
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000123#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 if (op == NULL)
125 return err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 op->ob_type = tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000127 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return op;
129}
130
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000131#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132varobject *
133newvarobject(tp, size)
134 typeobject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000135 int size;
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000136#else
137varobject *
138newvarobject(tp, size, op)
139 typeobject *tp;
140 int size;
141 varobject *op;
142#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143{
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000144#ifndef MS_COREDLL
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 varobject *op = (varobject *)
146 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000147#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 if (op == NULL)
Guido van Rossum05ab1111991-05-05 20:10:41 +0000149 return (varobject *)err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 op->ob_type = tp;
151 op->ob_size = size;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000152 NEWREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 return op;
154}
155
Guido van Rossum90933611991-06-07 16:10:43 +0000156int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157printobject(op, fp, flags)
158 object *op;
159 FILE *fp;
160 int flags;
161{
Guido van Rossum278ef591991-07-27 21:40:24 +0000162 int ret = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000163 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000164 return -1;
Guido van Rossum90933611991-06-07 16:10:43 +0000165 if (op == NULL) {
166 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 }
Guido van Rossum90933611991-06-07 16:10:43 +0000168 else {
169 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000170 fprintf(fp, "<refcnt %u at %lx>",
171 op->ob_refcnt, (long)op);
172 else if (op->ob_type->tp_print == NULL) {
173 if (op->ob_type->tp_repr == NULL) {
174 fprintf(fp, "<%s object at %lx>",
175 op->ob_type->tp_name, (long)op);
176 }
177 else {
Guido van Rossumc6004111993-11-05 10:22:19 +0000178 object *s;
179 if (flags & PRINT_RAW)
180 s = strobject(op);
181 else
182 s = reprobject(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000183 if (s == NULL)
184 ret = -1;
185 else if (!is_stringobject(s)) {
186 err_setstr(TypeError,
187 "repr not string");
188 ret = -1;
189 }
190 else {
191 fprintf(fp, "%s", getstringvalue(s));
192 }
193 XDECREF(s);
194 }
195 }
Guido van Rossum90933611991-06-07 16:10:43 +0000196 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000197 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000198 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000199 if (ret == 0) {
200 if (ferror(fp)) {
Guido van Rossum2912f221991-12-10 13:59:09 +0000201 err_errno(IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000202 clearerr(fp);
203 ret = -1;
204 }
205 }
206 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207}
208
209object *
210reprobject(v)
211 object *v;
212{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000213 if (sigcheck())
Guido van Rossum90933611991-06-07 16:10:43 +0000214 return NULL;
Guido van Rossum90933611991-06-07 16:10:43 +0000215 if (v == NULL)
216 return newstringobject("<NULL>");
217 else if (v->ob_type->tp_repr == NULL) {
218 char buf[120];
219 sprintf(buf, "<%.80s object at %lx>",
220 v->ob_type->tp_name, (long)v);
221 return newstringobject(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222 }
Guido van Rossum90933611991-06-07 16:10:43 +0000223 else
224 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225}
226
Guido van Rossumc6004111993-11-05 10:22:19 +0000227object *
228strobject(v)
229 object *v;
230{
231 if (v == NULL)
232 return newstringobject("<NULL>");
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000233 else if (is_stringobject(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000234 INCREF(v);
235 return v;
236 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000237 else if (v->ob_type->tp_str != NULL)
238 return (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000239 else {
Guido van Rossum32b582b1995-01-17 16:35:13 +0000240 object *func;
Guido van Rossumc6004111993-11-05 10:22:19 +0000241 object *res;
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000242 if (!is_instanceobject(v) ||
243 (func = getattr(v, "__str__")) == NULL) {
Guido van Rossumc6004111993-11-05 10:22:19 +0000244 err_clear();
245 return reprobject(v);
246 }
Guido van Rossum1311e3c1995-07-12 02:22:06 +0000247 res = call_object(func, (object *)NULL);
Guido van Rossumc6004111993-11-05 10:22:19 +0000248 DECREF(func);
249 return res;
250 }
251}
252
Guido van Rossum20566841995-01-12 11:26:10 +0000253static object *
254do_cmp(v, w)
255 object *v, *w;
256{
257 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
258 because the check in cmpobject() reverses the objects first.
259 This is intentional -- it makes no sense to define cmp(x,y) different
260 than -cmp(y,x). */
261 if (is_instanceobject(v) || is_instanceobject(w))
262 return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp);
263 return newintobject((long)cmpobject(v, w));
264}
265
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266int
267cmpobject(v, w)
268 object *v, *w;
269{
270 typeobject *tp;
271 if (v == w)
272 return 0;
273 if (v == NULL)
274 return -1;
275 if (w == NULL)
276 return 1;
Guido van Rossum20566841995-01-12 11:26:10 +0000277 if (is_instanceobject(v) || is_instanceobject(w)) {
278 object *res;
279 int c;
280 if (!is_instanceobject(v))
281 return -cmpobject(w, v);
282 res = do_cmp(v, w);
283 if (res == NULL) {
284 err_clear();
285 return (v < w) ? -1 : 1;
286 }
287 if (!is_intobject(res)) {
288 DECREF(res);
289 return (v < w) ? -1 : 1;
290 }
291 c = getintvalue(res);
292 DECREF(res);
293 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
294 }
Guido van Rossum9fb03681991-07-01 18:48:04 +0000295 if ((tp = v->ob_type) != w->ob_type) {
296 if (tp->tp_as_number != NULL &&
297 w->ob_type->tp_as_number != NULL) {
298 if (coerce(&v, &w) != 0) {
299 err_clear();
300 /* XXX Should report the error,
301 XXX but the interface isn't there... */
302 }
303 else {
304 int cmp = (*v->ob_type->tp_compare)(v, w);
305 DECREF(v);
306 DECREF(w);
307 return cmp;
308 }
309 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000311 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312 if (tp->tp_compare == NULL)
313 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000314 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315}
316
Guido van Rossum9bfef441993-03-29 10:43:31 +0000317long
318hashobject(v)
319 object *v;
320{
321 typeobject *tp = v->ob_type;
322 if (tp->tp_hash != NULL)
323 return (*tp->tp_hash)(v);
324 if (tp->tp_compare == NULL)
325 return (long) v; /* Use address as hash value */
326 /* If there's a cmp but no hash defined, the object can't be hashed */
327 err_setstr(TypeError, "unhashable type");
328 return -1;
329}
330
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331object *
332getattr(v, name)
333 object *v;
334 char *name;
335{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000336 if (v->ob_type->tp_getattro != NULL) {
337 object *w, *res;
338 w = newstringobject(name);
339 if (w == NULL)
340 return NULL;
341 res = (*v->ob_type->tp_getattro)(v, w);
342 XDECREF(w);
343 return res;
344 }
345
Guido van Rossum3f5da241990-12-20 15:06:42 +0000346 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossum016564a1995-01-07 11:54:44 +0000347 err_setstr(AttributeError, "attribute-less object");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000348 return NULL;
349 }
350 else {
351 return (*v->ob_type->tp_getattr)(v, name);
352 }
353}
354
355int
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000356hasattr(v, name)
357 object *v;
358 char *name;
359{
360 object *res = getattr(v, name);
361 if (res != NULL) {
362 DECREF(res);
363 return 1;
364 }
365 err_clear();
366 return 0;
367}
368
369int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000370setattr(v, name, w)
371 object *v;
372 char *name;
373 object *w;
374{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000375 if (v->ob_type->tp_setattro != NULL) {
376 object *s;
377 int res;
378 s = newstringobject(name);
379 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000380 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000381 res = (*v->ob_type->tp_setattro)(v, s, w);
382 XDECREF(s);
383 return res;
384 }
385
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386 if (v->ob_type->tp_setattr == NULL) {
387 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000388 err_setstr(TypeError,
389 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000390 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000391 err_setstr(TypeError,
392 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000393 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000394 }
395 else {
396 return (*v->ob_type->tp_setattr)(v, name, w);
397 }
398}
399
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000400/* Test a value used as condition, e.g., in a for or if statement.
401 Return -1 if an error occurred */
402
403int
404testbool(v)
405 object *v;
406{
407 int res;
408 if (v == None)
409 res = 0;
410 else if (v->ob_type->tp_as_number != NULL)
411 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
412 else if (v->ob_type->tp_as_mapping != NULL)
413 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
414 else if (v->ob_type->tp_as_sequence != NULL)
415 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
416 else
417 res = 1;
418 if (res > 0)
419 res = 1;
420 return res;
421}
422
Guido van Rossum5524a591995-01-10 15:26:20 +0000423/* Coerce two numeric types to the "larger" one.
424 Increment the reference count on each argument.
425 Return -1 and raise an exception if no coercion is possible
426 (and then no reference count is incremented).
427*/
428
429int
430coerce(pv, pw)
431 object **pv, **pw;
432{
433 register object *v = *pv;
434 register object *w = *pw;
435 int res;
436
437 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
438 INCREF(v);
439 INCREF(w);
440 return 0;
441 }
442 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
443 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
444 if (res <= 0)
445 return res;
446 }
447 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
448 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
449 if (res <= 0)
450 return res;
451 }
452 err_setstr(TypeError, "number coercion failed");
453 return -1;
454}
455
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000457/* Test whether an object can be called */
458
459int
460callable(x)
461 object *x;
462{
463 if (x == NULL)
464 return 0;
465 if (x->ob_type->tp_call != NULL ||
466 is_funcobject(x) ||
467 is_instancemethodobject(x) ||
468 is_methodobject(x) ||
469 is_classobject(x))
470 return 1;
471 if (is_instanceobject(x)) {
472 object *call = getattr(x, "__call__");
473 if (call == NULL) {
474 err_clear();
475 return 0;
476 }
477 /* Could test recursively but don't, for fear of endless
478 recursion if some joker sets self.__call__ = self */
479 DECREF(call);
480 return 1;
481 }
482 return 0;
483}
484
485
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486/*
487NoObject is usable as a non-NULL undefined value, used by the macro None.
488There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000489so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490*/
491
Guido van Rossum0c182a11992-03-27 17:26:13 +0000492/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000493static object *
494none_repr(op)
495 object *op;
496{
497 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498}
499
500static typeobject Notype = {
501 OB_HEAD_INIT(&Typetype)
502 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000503 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000504 0,
505 0,
506 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000507 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000508 0, /*tp_getattr*/
509 0, /*tp_setattr*/
510 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000511 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000512 0, /*tp_as_number*/
513 0, /*tp_as_sequence*/
514 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000515 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516};
517
518object NoObject = {
519 OB_HEAD_INIT(&Notype)
520};
521
522
Guido van Rossum84a90321996-05-22 16:34:47 +0000523#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524
525static object refchain = {&refchain, &refchain};
526
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000527void
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528NEWREF(op)
529 object *op;
530{
531 ref_total++;
532 op->ob_refcnt = 1;
533 op->_ob_next = refchain._ob_next;
534 op->_ob_prev = &refchain;
535 refchain._ob_next->_ob_prev = op;
536 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000537#ifdef COUNT_ALLOCS
538 inc_count(op->ob_type);
539#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540}
541
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000542void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000543UNREF(op)
544 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000546 register object *p;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000547 if (op->ob_refcnt < 0)
548 fatal("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000549 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000550 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
551 fatal("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000552#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000553 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
554 if (p == op)
555 break;
556 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000557 if (p == &refchain) /* Not found */
558 fatal("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000559#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 op->_ob_next->_ob_prev = op->_ob_prev;
561 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000562 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000563#ifdef COUNT_ALLOCS
564 op->ob_type->tp_free++;
565#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000566}
567
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000568void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000569DELREF(op)
570 object *op;
571{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000572 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000573 UNREF(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000574 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000575 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576}
577
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000578void
Guido van Rossumded690f1996-05-24 20:48:31 +0000579_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580 FILE *fp;
581{
582 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000583 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000585 if (op->ob_refcnt == 1 && is_stringobject(op))
586 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000588 if (printobject(op, fp, 0) != 0)
589 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590 putc('\n', fp);
591 }
592}
593
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000594PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000595_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000596 PyObject *self;
597 PyObject *args;
598{
599 int i, n;
600 PyObject *t = NULL;
601 PyObject *res, *op;
602
603 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
604 return NULL;
605 op = refchain._ob_next;
606 res = PyList_New(0);
607 if (res == NULL)
608 return NULL;
609 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
610 while (op == self || op == args || op == res || op == t ||
611 t != NULL && op->ob_type != (PyTypeObject *) t) {
612 op = op->_ob_next;
613 if (op == &refchain)
614 return res;
615 }
616 if (PyList_Append(res, op) < 0) {
617 Py_DECREF(res);
618 return NULL;
619 }
620 op = op->_ob_next;
621 }
622 return res;
623}
624
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000626
627
628/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000629PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000630
631
632/* Hack to force loading of abstract.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000633int (*_Py_abstract_hack) FPROTO((PyObject *)) = &PyObject_Length;