I am trying to wrap an existing C library using cython. The library uses callbacks which I would like to redirect to execute python code. Lets say that the corresponding line in the header is the following:
typedef RETCODE (*FUNC_EVAL)(int a, int b, void* func_data);
where the return code is used to signal an error. The API to create a corresponding C struct is as follows:
RETCODE func_create(Func** fstar,
FUNC_EVAL func_eval,
void* func_data);
I added a cython header / implementation files. The header contains the typedef:
ctypedef RETCODE (*FUNC_EVAL)(int a,
int b,
void* func_data)
The implementation contains a wrapper function:
cdef RETCODE func_eval(int a,
int b,
void* func_data):
(<object> func_data).func_eval(a, b)
return OKAY;
I can pass this function to the func_create cython wrapper just fine.
However, I want to make sure that exceptions in the python code are
reported back to the C library by returning an ERROR value as
a return code. So I added the following:
cdef RETCODE func_eval(int a,
int b,
void* func_data) except ERROR:
(<object> func_data).func_eval(a, b)
return OKAY;
However, now cython terminates with the following error message:
Cannot assign type 'RETCODE (*)(int, int, void *) except ERROR' to 'FUNC_EVAL'
Am I using the except ... statement wrong?