Title: Make inspect agnostic about whether functions are implemented in Python or C
Type: behavior Stage: test needed
Components: Versions: Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: larry Nosy List: Arfrever, larry, ncoghlan, pitrou, terry.reedy, yselivanov
Priority: normal Keywords:

Created on 2014-02-21 01:19 by larry, last changed 2014-02-23 09:56 by ncoghlan.

Messages (5)
msg211783 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-02-21 01:19
Some of the methods on the inspect module behave differently depending on whether the callable passed in was implemented in C or in Python.  For example, ismethod() only returns True for bound methods implemented in Python.

I assert that the interface presented by inspect should be agnostic about the underlying implementation.  So for example ismethod() should return True for bound methods on builtin classes.
msg211864 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-02-21 14:54
The docs are quite clear about that:


    Return true if the object is a bound method written in Python.

In other words, this is not a bug, and there's probably code in the wild relying on it.
msg211865 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-02-21 15:08
Right, the lower level APIs in the inspect module *aren't* designed to hide the implementation details from introspection code, they're there as helpers to let introspection code decide what assumptions are valid. For true Python code, you can do many more interesting things (like bytecode hacks) that simply aren't possible with native implementations in C, Java, C#, etc.

A lot of the names aren't great, but I think that's just because so many parts of the API have been around for so long.
msg211896 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-02-21 23:54
The premise of the builtins signature project is that we should work toward eliminating unnecessary differences between functions coded in Python and those coded otherwise. Part of inspect.signature is a clause to remove the first parameter of the underlying function for bound methods#. One would like to write the equivalent of

if ismethod(f) and f.params[0] is not *args, remove f.params[0].

I understand Larry as proposing that the code in .signature for ismethod(C_func) properly belongs in .ismethod itself, for anyone to use, and I agree. However, use of this code has to be an option turned off by default. My revised proposal is to add 'other_lang=False' and "If *other_lang* is true, also return True if the object is a bound method written in another language." Another name might be 'non_py'. I don't think the name matters too much as long as the default is False for Python-only.

# Another unnecessary difference is that [].append, for instance, does not have a .__func__ attribute linking to list.append, even though it must have one internally. Perhaps bound C methods should also have .__func__.
msg211987 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-02-23 09:56
The problem is that "ismethod()" is not a particularly well-defined concept, except insofar as it means "behaves the same way as the callable returned when a Python function is retrieved through a class instance".

"isboundmethod()" could be well-defined, especially if it was introduced in parallel with a types.BoundMethod ABC that standardised the __func__ property.
Date User Action Args
2014-02-23 09:56:52ncoghlansetmessages: + msg211987
2014-02-23 08:12:31Arfreversetnosy: + Arfrever
2014-02-21 23:54:40terry.reedysetnosy: + terry.reedy

messages: + msg211896
stage: needs patch -> test needed
2014-02-21 15:08:34ncoghlansetmessages: + msg211865
2014-02-21 14:54:33pitrousetnosy: + ncoghlan, pitrou
messages: + msg211864
2014-02-21 01:19:46larrycreate