diff options
author | Guido van Rossum <guido@python.org> | 2003-04-16 21:13:23 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2003-04-16 21:13:23 (GMT) |
commit | c1f6e8cbc1d60c5fe00121c67bfc581301e7ce50 (patch) | |
tree | e508b3b6a8621b3330035178cd825a84e7f753c7 /Objects | |
parent | 6f29ff319b83c4e5cec1ddb06075d7a9da377a19 (diff) | |
download | cpython-c1f6e8cbc1d60c5fe00121c67bfc581301e7ce50.zip cpython-c1f6e8cbc1d60c5fe00121c67bfc581301e7ce50.tar.gz cpython-c1f6e8cbc1d60c5fe00121c67bfc581301e7ce50.tar.bz2 |
- 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 <weakproxy ...>
in proxy_repr(), to match the type name.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/weakrefobject.c | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index b059080..e26cb65 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), "<weakref at %lx; dead>", - (long)(self)); + PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", 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), - "<weakref at %#lx; to '%.50s' at %#lx>", - (long)(self), + name ? "<weakref at %p; to '%.50s' at %p (%s)>" + : "<weakref at %p; to '%.50s' at %p>", + 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), - "<weakref at %p to %.100s at %p>", proxy, + "<weakproxy at %p to %.100s at %p>", proxy, PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name, PyWeakref_GET_OBJECT(proxy)); return PyString_FromString(buf); |