Terry J. Reedy added the comment:

inspect.isfunction(object) is documented to
    Return true if the object is a Python function, which includes functions 
created by a lambda expression.

This is currently implemented as "isinstance(object, types.FunctionType)".

The docs usually regard a 'Python function' as the result of a def statement or 
lambda expression.  The inspect doc says that a function includes a particular 
set of attributes.  One of them is a code object with its own fairly extensive 
set of attributes.  Some of them are derived from the Python code.  But others, 
in particular co_code, are specific to the current CPython bytecode version.  
(And co_code is intentionally writable.)

To me, the main purpose of checking that something is a function, as opposed to 
just being callable, is to know whether one can dependably access the 
attributes.  Given that some are inherently CPython specific, including objects 
compiled by third-party software seems dubious.  (There is also the issue of 
not being able to test with 3rd party objects.)

The referenced cython doc says
"""While it is quite possible to emulate the interface of functions in Cython’s 
own function type, and recent Cython releases have seen several improvements 
here,"""

To me, this implies to me that Cython function (compiled from Cython's extended 
def statements) do not yet perfectly emulate (fulfill) 'Python functions'.  As 
indicated above, perfect emulation seems impossible for Cython or any other 
external compiler that does not use the same bytecode.

"""the “inspect” module does not consider a Cython implemented function a 
“function”, because it tests the object type explicitly instead of comparing an 
abstract interface or an abstract base class. This has a negative impact on 
code that uses inspect to inspect function objects, but would require a change 
to Python itself."""

Where the current situation would be annoying is if working code uses 
isfunction and then Cython is used to speed up the code.  But Cython could 
supply, if it does not now, expanded functions along with the list of 
cyfunction attributes and an indication of which are compatible with CPython 
function attributes.

Cython is not the only 3rd party compiler, and not the only one that might ever 
be linkable to CPython.  So any change to CPython should not be limited to 
Cython.

If it were possible for Cython to makes its CythonFunction class a subclass of 
FunctionType, the issue would be 'solved', though the incompatibilities would 
remain.

----------
nosy: +terry.reedy
stage:  -> test needed

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

Reply via email to