Steven Barker added the comment:

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'>>

----------
keywords: +patch
versions: +Python 3.5
Added file: http://bugs.python.org/file35202/method_repr.diff

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue21389>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to