I am trying to write a wrapper around a C program so that I can call it from Python. I am using Cython for this. The C function takes a callback function as an argument, but this callback function will only be known at runtime of the python program. I was looking for how to do this and it seems like there is no easy solution, but the following seems to work:
libc = cdll.LoadLibrary("myfunc.so")
....
c_wrapper(libc.fun, ...)
.
cdef extern void mainfunction(void *F, ...)
ctypedef void (*myfuncptr) ()
def c_wrapper(f, ...)
cdef myfuncptr thisfunc
thisfunc = (<myfuncptr*><size_t>addressof(f))[0]
mainfunction(thisfunc, ...)
This method works for C and FORTRAN functions (assuming it will work for most compiled languges) and Python functions (using C types), but this seems a bit inconvenient. Is there an even easier way to do this in Cython?
thank
EDIT: I can't change the C library I'm trying to wrap
source
share