How to introspect a function defined in a Cython C extension module
python的
MWE:
1 2 3 | # mwe.pyx def example(a, b=None): pass |
和
1 2 3 4 5 | import pyximport; pyximport.install() import mwe import inspect inspect.signature(mwe.example) |
产量:
1 2 3 4 5 6 7 8 9 10 | Traceback (most recent call last): File"mwe_py.py", line 5, in <module> inspect.signature(mwe.example) File"/nix/store/134l79vxb91w8mhxxkj6kb5llf7dmwpm-python3-3.4.5/lib/python3.4/inspect.py", line 2063, in signature return _signature_internal(obj) File"/nix/store/134l79vxb91w8mhxxkj6kb5llf7dmwpm-python3-3.4.5/lib/python3.4/inspect.py", line 1965, in _signature_internal skip_bound_arg=skip_bound_arg) File"/nix/store/134l79vxb91w8mhxxkj6kb5llf7dmwpm-python3-3.4.5/lib/python3.4/inspect.py", line 1890, in _signature_from_builtin raise ValueError("no signature found for builtin {!r}".format(func)) ValueError: no signature found for builtin <built-in function example> |
在python 3.4.5和cython 0.24.1中
我已经撤回了我的重复建议(说这是不可能的…)进一步调查。它似乎可以与Cython(v0.23.4)和python 3.4.4的最新版本配合使用。
1 2 3 4 | import cython import inspect scope = cython.inline("""def f(a,*args,b=False): pass""") print(inspect.getfullargspec(scope['f'])) |
给出输出
FullArgSpec(args=['a'], varargs='args', varkw=None, defaults=None, kwonlyargs=['b'], kwonlydefaults={'b': False}, annotations={})
文档中还提到了编译选项"
我有一种感觉,这可能取决于最近对
编辑:如果使用
1 2 3 4 | import cython @cython.binding(True) def example(a, b=None): pass |
我怀疑