Let's say I have the code snippet:
ctypedef double (*scalar_func)(double)
cdef double c_func(scalar_func f):
return f(1.0)
def call_from_py(py_f):
cdef scalar_func f = py_f
return c_func(f)
Here, I want to pass the Python function py_f
to c_func
. However, this yields
the error
Error compiling Cython file:
------------------------------------------------------------
...
cdef double c_func(scalar_func f):
return f(1.0)
def call_from_py(py_f):
cdef scalar_func f = py_f
^
------------------------------------------------------------
/Users/david/.ipython/cython/_cython_magic_39903ba379ce438d042b67efa2d500fe.pyx:9:22: Cannot convert Python object to 'scalar_func'
How can I pass a Python function to c_func
, i.e. how can I convert
a Python function to a C function pointer?