From 2e8f6140ff8dfc5d5d81825ac7ec8826f750fede Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 3 Sep 1992 20:32:55 +0000 Subject: If a type has a repr function but no print function, printing it now calls the repr function. When the refcount is bad, don't print the object at all (chances of crashes). Changes to checking and printing of references: the consistency check is somewhat faster; don't print strings referenced once (most occur in function's name lists). --- Objects/object.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/Objects/object.c b/Objects/object.c index 5f54b36..16053a9 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -77,10 +77,28 @@ printobject(op, fp, flags) } else { if (op->ob_refcnt <= 0) - fprintf(fp, "(refcnt %u):", op->ob_refcnt); - if (op->ob_type->tp_print == NULL) - fprintf(fp, "<%s object at %lx>", - op->ob_type->tp_name, (long)op); + fprintf(fp, "", + op->ob_refcnt, (long)op); + else if (op->ob_type->tp_print == NULL) { + if (op->ob_type->tp_repr == NULL) { + fprintf(fp, "<%s object at %lx>", + op->ob_type->tp_name, (long)op); + } + else { + object *s = reprobject(op); + if (s == NULL) + ret = -1; + else if (!is_stringobject(s)) { + err_setstr(TypeError, + "repr not string"); + ret = -1; + } + else { + fprintf(fp, "%s", getstringvalue(s)); + } + XDECREF(s); + } + } else ret = (*op->ob_type->tp_print)(op, fp, flags); } @@ -252,6 +270,12 @@ UNREF(op) fprintf(stderr, "UNREF negative refcnt\n"); abort(); } + if (op == &refchain || + op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { + fprintf(stderr, "UNREF invalid object\n"); + abort(); + } +#ifdef SLOW_UNREF_CHECK for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { if (p == op) break; @@ -260,8 +284,10 @@ UNREF(op) fprintf(stderr, "UNREF unknown object\n"); abort(); } +#endif op->_ob_next->_ob_prev = op->_ob_prev; op->_ob_prev->_ob_next = op->_ob_next; + op->_ob_next = op->_ob_prev = NULL; } DELREF(op) @@ -269,14 +295,17 @@ DELREF(op) { UNREF(op); (*(op)->ob_type->tp_dealloc)(op); + op->ob_type = NULL; } printrefs(fp) FILE *fp; { object *op; - fprintf(fp, "Remaining objects:\n"); + fprintf(fp, "Remaining objects (except strings referenced once):\n"); for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { + if (op->ob_refcnt == 1 && is_stringobject(op)) + continue; /* Will be printed elsewhere */ fprintf(fp, "[%d] ", op->ob_refcnt); if (printobject(op, fp, 0) != 0) err_clear(); -- cgit v0.12