This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Steven.Barker
Recipients Steven.Barker
Date 2014-05-09.20:15:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1399666529.37.0.607805115427.issue21389@psf.upfronthosting.co.za>
In-reply-to
Content
Here's a patch that changes the behavior of method_repr in Objects/classobject.c . It first tries to use __func__.__qualname__, then tries __func__.__name__ as a fallback and finally uses "?" if neither of those attributes are available.

I'm not sure if the __name__ fallback is tested (as it seems that pretty much all callables have __qualname__ these days). The last "?" case actually does get tested by Lib/test/test_descr.py which creates a messed up method with classmethod(1).__get__(1) (which oddly does not raise an error immediately upon creation, but rather only when it is called).

I've not written C in several years, so please let me know if you see I've done something obviously wrong, or any places the patch could be improved. It is mostly a copy-and-paste of existing code with a few modifications and deletions, so hopefully I can't have messed up anything too badly!

I'm currently ignoring a comment in the code that says we "shouldn't use repr()/%R" to format __self__. I don't really understand that, so I've stick with the existing behavior on that front. If that is something that should change, I'd be happy to try reworking it in some other way, just let me know what the concern is.

Here are some examples of the new repr output in a build with the patch:

>>> class A():
...   def foo(self):
...     pass
...
>>> class B(A):
...   def foo(self):
...     pass
...
>>> class C(A):
...   pass
...
>>> class D():
...   @classmethod
...   def bar():
...     pass
...
>>> A().foo
<bound method A.foo of <__main__.A object at 0x02267508>>
>>> B().foo
<bound method B.foo of <__main__.B object at 0x02267578>>
>>> C().foo
<bound method A.foo of <__main__.C object at 0x02267658>>
>>> super(B, B()).foo
<bound method A.foo of <__main__.B object at 0x022676C8>>
>>> D.bar
<bound method D.bar of <class '__main__.D'>>
History
Date User Action Args
2014-05-09 20:15:29Steven.Barkersetrecipients: + Steven.Barker
2014-05-09 20:15:29Steven.Barkersetmessageid: <1399666529.37.0.607805115427.issue21389@psf.upfronthosting.co.za>
2014-05-09 20:15:29Steven.Barkerlinkissue21389 messages
2014-05-09 20:15:28Steven.Barkercreate