Message291728
inspect.getargspec is deprecated in favor of .getfullargspec and .signature and is implemented in with .getfullargspec. This, in turn, calls ._signature_from_callable which ultimately looks for (perhaps after recursive unwrap calls) obj.__signature__. So I expect that the case you 'care most about' already works. True?
It appears that .signature is intended to work for cython functions via the following helper function. Its code is somewhat awkward and tests that the object has needed attributes with needed types.
def _signature_is_functionlike(obj):
"""Private helper to test if `obj` is a duck type of FunctionType.
A good example of such objects are functions compiled with
Cython, which have all attributes that a pure Python function
would have, but have their code statically compiled.
"""
That does leave cases like the inspect.getfile code you quoted. It could be fixed with some fiddly code, but there would still be .getclosurevariables and a couple of other uses of isfunction to review.
I reviewed the function and code attributes listed in
https://docs.python.org/3/library/inspect.html#types-and-members
and I think the necessary differences a function compiled by CPython and anything else are limited to the code object.
Proposal: for a cleaner solution, define a 'mincode' base class that lacks, for instance, co_code, co_consts, co_flags, co_lnotab, and co_stacksize. Make code a subclass of this. Define 'minfunction' as a function whose __code__ is a mincode. Make function a subclass of this. Define 'isminfunction' and replace 'isfunction' where a mincode is sufficient. This might allow, for instance, _signature_is_functionlike to be removed.
Details should perhaps be specified in a relatively short PEP. Discussion could maybe continue on python-ideas. |
|
Date |
User |
Action |
Args |
2017-04-15 20:04:44 | terry.reedy | set | recipients:
+ terry.reedy, scoder, steven.daprano, r.david.murray, serhiy.storchaka, jdemeyer |
2017-04-15 20:04:44 | terry.reedy | set | messageid: <1492286684.13.0.44465761658.issue30071@psf.upfronthosting.co.za> |
2017-04-15 20:04:44 | terry.reedy | link | issue30071 messages |
2017-04-15 20:04:43 | terry.reedy | create | |
|