changeset: 92170:92dcee426014 parent: 92162:10d0a692b1b6 user: Benjamin Peterson date: Wed Aug 20 18:41:57 2014 -0500 files: Lib/test/test_defaultdict.py Lib/test/test_descr.py Misc/NEWS Objects/classobject.c description: use __qualname__ to compute bound method repr (closes #21389) Patch from Steven Barker. diff -r 10d0a692b1b6 -r 92dcee426014 Lib/test/test_defaultdict.py --- a/Lib/test/test_defaultdict.py Wed Aug 20 07:55:53 2014 +0530 +++ b/Lib/test/test_defaultdict.py Wed Aug 20 18:41:57 2014 -0500 @@ -157,8 +157,9 @@ def _factory(self): return [] d = sub() - self.assertTrue(repr(d).startswith( - "defaultdict(, \{\}\)") # NOTE: printing a subclass of a builtin type does not call its # tp_print slot. So this part is essentially the same test as above. diff -r 10d0a692b1b6 -r 92dcee426014 Lib/test/test_descr.py --- a/Lib/test/test_descr.py Wed Aug 20 07:55:53 2014 +0530 +++ b/Lib/test/test_descr.py Wed Aug 20 18:41:57 2014 -0500 @@ -4423,6 +4423,61 @@ self.assertIn("__dict__", Base.__dict__) self.assertNotIn("__dict__", Sub.__dict__) + def test_bound_method_repr(self): + class Foo: + def method(self): + pass + self.assertRegex(repr(Foo().method), + r">") + + + class Base: + def method(self): + pass + class Derived1(Base): + pass + class Derived2(Base): + def method(self): + pass + base = Base() + derived1 = Derived1() + derived2 = Derived2() + super_d2 = super(Derived2, derived2) + self.assertRegex(repr(base.method), + r">") + self.assertRegex(repr(derived1.method), + r">") + self.assertRegex(repr(derived2.method), + r">") + self.assertRegex(repr(super_d2.method), + r">") + + class Foo: + @classmethod + def method(cls): + pass + foo = Foo() + self.assertRegex(repr(foo.method), # access via instance + r">") + self.assertRegex(repr(Foo.method), # access via the class + r">") + + + class MyCallable: + def __call__(self, arg): + pass + func = MyCallable() # func has no __name__ or __qualname__ attributes + instance = object() + method = types.MethodType(func, instance) + self.assertRegex(repr(method), + r">") + func.__name__ = "name" + self.assertRegex(repr(method), + r">") + func.__qualname__ = "qualname" + self.assertRegex(repr(method), + r">") + class DictProxyTests(unittest.TestCase): def setUp(self): diff -r 10d0a692b1b6 -r 92dcee426014 Misc/NEWS --- a/Misc/NEWS Wed Aug 20 07:55:53 2014 +0530 +++ b/Misc/NEWS Wed Aug 20 18:41:57 2014 -0500 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #21389: Displaying the __qualname__ of the underlying function in the + repr of a bound method. + - Issue #22206: Using pthread, PyThread_create_key() now sets errno to ENOMEM and returns -1 (error) on integer overflow. diff -r 10d0a692b1b6 -r 92dcee426014 Objects/classobject.c --- a/Objects/classobject.c Wed Aug 20 07:55:53 2014 +0530 +++ b/Objects/classobject.c Wed Aug 20 18:41:57 2014 -0500 @@ -15,6 +15,7 @@ #endif _Py_IDENTIFIER(__name__); +_Py_IDENTIFIER(__qualname__); PyObject * PyMethod_Function(PyObject *im) @@ -243,51 +244,33 @@ { PyObject *self = a->im_self; PyObject *func = a->im_func; - PyObject *klass; - PyObject *funcname = NULL ,*klassname = NULL, *result = NULL; - char *defname = "?"; + PyObject *funcname = NULL, *result = NULL; + const char *defname = "?"; - if (self == NULL) { - PyErr_BadInternalCall(); - return NULL; - } - klass = (PyObject*)Py_TYPE(self); - - funcname = _PyObject_GetAttrId(func, &PyId___name__); + funcname = _PyObject_GetAttrId(func, &PyId___qualname__); if (funcname == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); + + funcname = _PyObject_GetAttrId(func, &PyId___name__); + if (funcname == NULL) { + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) + return NULL; + PyErr_Clear(); + } } - else if (!PyUnicode_Check(funcname)) { + + if (funcname != NULL && !PyUnicode_Check(funcname)) { Py_DECREF(funcname); funcname = NULL; } - if (klass == NULL) - klassname = NULL; - else { - klassname = _PyObject_GetAttrId(klass, &PyId___name__); - if (klassname == NULL) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { - Py_XDECREF(funcname); - return NULL; - } - PyErr_Clear(); - } - else if (!PyUnicode_Check(klassname)) { - Py_DECREF(klassname); - klassname = NULL; - } - } - /* XXX Shouldn't use repr()/%R here! */ - result = PyUnicode_FromFormat("", - klassname, defname, + result = PyUnicode_FromFormat("", funcname, defname, self); Py_XDECREF(funcname); - Py_XDECREF(klassname); return result; }