diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-08-20 23:41:57 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-08-20 23:41:57 (GMT) |
commit | 48ad7c0b01de1be182c3894e1691861ccffb82ea (patch) | |
tree | 85fc3421bdd9d61bd0f1c9c9ce19971052c81a4d /Lib/test/test_descr.py | |
parent | 2b7ccbda900f9fc697aac6fdb72841c83ca640eb (diff) | |
download | cpython-48ad7c0b01de1be182c3894e1691861ccffb82ea.zip cpython-48ad7c0b01de1be182c3894e1691861ccffb82ea.tar.gz cpython-48ad7c0b01de1be182c3894e1691861ccffb82ea.tar.bz2 |
use __qualname__ to compute bound method repr (closes #21389)
Patch from Steven Barker.
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r-- | Lib/test/test_descr.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 634ba7e..39782a4 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4423,6 +4423,61 @@ order (MRO) for bases """ 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"<bound method .*Foo\.method of <.*Foo object at .*>>") + + + 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"<bound method .*Base\.method of <.*Base object at .*>>") + self.assertRegex(repr(derived1.method), + r"<bound method .*Base\.method of <.*Derived1 object at .*>>") + self.assertRegex(repr(derived2.method), + r"<bound method .*Derived2\.method of <.*Derived2 object at .*>>") + self.assertRegex(repr(super_d2.method), + r"<bound method .*Base\.method of <.*Derived2 object at .*>>") + + class Foo: + @classmethod + def method(cls): + pass + foo = Foo() + self.assertRegex(repr(foo.method), # access via instance + r"<bound method .*Foo\.method of <class '.*Foo'>>") + self.assertRegex(repr(Foo.method), # access via the class + r"<bound method .*Foo\.method of <class '.*Foo'>>") + + + 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"<bound method \? of <object object at .*>>") + func.__name__ = "name" + self.assertRegex(repr(method), + r"<bound method name of <object object at .*>>") + func.__qualname__ = "qualname" + self.assertRegex(repr(method), + r"<bound method qualname of <object object at .*>>") + class DictProxyTests(unittest.TestCase): def setUp(self): |