Message315272
Nick and Raymond, I added both of you as nosy because, among other reasons, you commented on the latest version of PEP575.
I agree that there is a bug in current CPython, in that the is* functions should return, not raise, but I am not sure where is it, and hence if the PR is the right fix. I am wondering if, instead, the bug is in m, the object returned by MethodType, or in attribute lookup thereupon.
MethodType is the type of bound user-defined (Python-coded) functions and ismethod is true for instances thereof. Consider 4 other bound methods, two 'normal'(callable is method of instance class) and two, like Jeroen's, 'odd' (callable not related to int).
>>> cm = Callable().__call__
>>> cm.__code__
<code object __call__ at 0x00000202C1C53780, file "<pyshell#5>", line 2>
>>> cm.__name__
'__call__'
>>> cm2 = MethodType(Callable.__call__, Callable())
>>> cm2.__code__
<code object __call__ at 0x00000202C1C53780, file "<pyshell#5>", line 2>
>>> cm2.__name__
'__call__'
>>> m2 = MethodType(Callable.__call__, 42)
>>> m2.__code__
<code object __call__ at 0x00000202C1C53780, file "<pyshell#5>", line 2>
>>> m2.__name__
'__call__'
>>> m2()
()
>>> m3 = MethodType(Callable().__call__, 42)
>>> m3.__code__
<code object __call__ at 0x00000202C1C53780, file "<pyshell#5>", line 2>
>>> m3.__name__
'__call__'
>>> m3()
(42,)
>>> m = MethodType(Callable(), 42)
>>> m.__code__
... AttributeError: 'Callable' object has no attribute '__code__'
>>> m.__name__
... AttributeError: 'Callable' object has no attribute '__name__'
>>> m()
(42,)
They all have the same attributes exposed by dir(), which omits both'__name__', promised in the docstring for ismethod*, and '__code__', assumed by the is--- functions under discussion.
>>> dir(cm) == dir(cm2) == dir(m) == dir(m2) == dir(m3)
True
>>> ('__code__' in dir(cm)) or ('__name__' in dir(cm))
False
However, accessing those names anyway, as (hidden) attributes, works for all but m. Should doing so also work for m? (If not, the ismethod docstring should not 'guarantee' .__name__.)
* " Instance method objects provide these attributes:
__doc__ documentation string
__name__ name with which this method was defined
..." |
|
Date |
User |
Action |
Args |
2018-04-13 22:46:48 | terry.reedy | set | recipients:
+ terry.reedy, rhettinger, ncoghlan, jdemeyer |
2018-04-13 22:46:48 | terry.reedy | set | messageid: <1523659608.19.0.682650639539.issue33261@psf.upfronthosting.co.za> |
2018-04-13 22:46:48 | terry.reedy | link | issue33261 messages |
2018-04-13 22:46:48 | terry.reedy | create | |
|