I have the following question. We have to pass callback functions to the C code. If the function is a Cython function in the same module, the situation is quite simple
In this example, extracted from a Python wrapper to the Cubature integration C library, a Python function is passed to a C function that has the prototype given by cfunction
. You can create a function with the same prototype, called cfunction_cb
(callback), and returning the same type, int
in this example):
cdef object f
ctypedef int (*cfunction) (double a, double b, double c, void *args)
cdef int cfunction_cb(double a, double b, double c, void *args):
global f
result_from_function = (
When calling the C function, you can cast your callback wrapper using the C prototype:
def main(pythonf, double a, double b, double c, args):
global f
f = pythonf
c_function( cfunction_cb,
double a,
double b,
double c,
args )
In this example it is also shown how to pass arguments to your Python function, through C.