From: Guido van Rossum Date: Wed, 16 Apr 2003 21:13:23 +0000 (+0000) Subject: - The repr() of a weakref object now shows the __name__ attribute of X-Git-Tag: v2.3c1~1158 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c1f6e8cbc1d60c5fe00121c67bfc581301e7ce50;p=python - The repr() of a weakref object now shows the __name__ attribute of the referenced object, if it has one. Also use %p to format pointers consistently, and use in proxy_repr(), to match the type name. --- diff --git a/Misc/NEWS b/Misc/NEWS index 1e3800b3c2..5cc7ce8972 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 2.3 beta 1? Core and builtins ----------------- +- The repr() of a weakref object now shows the __name__ attribute of + the referenced object, if it has one. + - super() no longer ignores data descriptors, except __class__. See the thread started at http://mail.python.org/pipermail/python-dev/2003-April/034338.html diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index b059080ab4..e26cb65b13 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -124,15 +124,24 @@ weakref_repr(PyWeakReference *self) { char buffer[256]; if (PyWeakref_GET_OBJECT(self) == Py_None) { - PyOS_snprintf(buffer, sizeof(buffer), "", - (long)(self)); + PyOS_snprintf(buffer, sizeof(buffer), "", self); } else { + char *name = NULL; + PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self), + "__name__"); + if (nameobj == NULL) + PyErr_Clear(); + else if (PyString_Check(nameobj)) + name = PyString_AS_STRING(nameobj); PyOS_snprintf(buffer, sizeof(buffer), - "", - (long)(self), + name ? "" + : "", + self, PyWeakref_GET_OBJECT(self)->ob_type->tp_name, - (long)(PyWeakref_GET_OBJECT(self))); + PyWeakref_GET_OBJECT(self), + name); + Py_XDECREF(nameobj); } return PyString_FromString(buffer); } @@ -268,7 +277,7 @@ proxy_repr(PyWeakReference *proxy) { char buf[160]; PyOS_snprintf(buf, sizeof(buf), - "", proxy, + "", proxy, PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name, PyWeakref_GET_OBJECT(proxy)); return PyString_FromString(buf);