- 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.
This commit is contained in:
Guido van Rossum 2003-04-16 21:13:23 +00:00
parent 6f29ff319b
commit c1f6e8cbc1
2 changed files with 18 additions and 6 deletions

View File

@ -12,6 +12,9 @@ What's New in Python 2.3 beta 1?
Core and builtins 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 - super() no longer ignores data descriptors, except __class__. See
the thread started at the thread started at
http://mail.python.org/pipermail/python-dev/2003-April/034338.html http://mail.python.org/pipermail/python-dev/2003-April/034338.html

View File

@ -124,15 +124,24 @@ weakref_repr(PyWeakReference *self)
{ {
char buffer[256]; char buffer[256];
if (PyWeakref_GET_OBJECT(self) == Py_None) { if (PyWeakref_GET_OBJECT(self) == Py_None) {
PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %lx; dead>", PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self);
(long)(self));
} }
else { 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), PyOS_snprintf(buffer, sizeof(buffer),
"<weakref at %#lx; to '%.50s' at %#lx>", name ? "<weakref at %p; to '%.50s' at %p (%s)>"
(long)(self), : "<weakref at %p; to '%.50s' at %p>",
self,
PyWeakref_GET_OBJECT(self)->ob_type->tp_name, 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); return PyString_FromString(buffer);
} }
@ -268,7 +277,7 @@ proxy_repr(PyWeakReference *proxy)
{ {
char buf[160]; char buf[160];
PyOS_snprintf(buf, sizeof(buf), 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)->ob_type->tp_name,
PyWeakref_GET_OBJECT(proxy)); PyWeakref_GET_OBJECT(proxy));
return PyString_FromString(buf); return PyString_FromString(buf);