The problem I'm currently facing is to assign a python function to a C structure member variable that is a function pointer.
I get the error from the setter function generated by Swig when we call the SWIG_ConvertFunctionPtr for the assigned value stating that the expected type is not correct. For the sake of simplicity lets consider (void*) function pointer.
C code:
typedef void (*MyFunctionPtr)();
struct MyStruct {
MyFunctionPtr func;
};
Python Code:
import example
def my_python_function():
print("Hello from Python!")
my_struct = example.MyStruct()
my_struct.func = my_python_function
the assignment would throw a run time error.
I did not add anything in my swig code to handle this except for including the header file. I wasn't able to find any tricks from the documentation or other forums. I won't be able to modify the C code, hence looking for solutions via python/swig.
Do you happen to have any ideas regarding how I can workaround this? Any pointers would be really helpful.
Edit (experiments):
Expt 1: typemap to typecast the input to void pointer (but still got the same error for mismatched type)
%typemap(in) void* MyFunctionPtr %{
PyObject *func;
if (PyCallable_Check($input)) {
func = $input;
} else {
PyErr_SetString(PyExc_TypeError, "funcptr must be a callable object");
return NULL;
}
$1 = (void*)func;
%}
Expt 2: I came across the following trick for C callback in swig. But in this case they are passing the python function as a PyObject and then type casting it to void in the function arguments.
http://www.fifi.org/doc/swig-examples/python/callback/widget.i
Still won't be able to achieve the structure assignment in this case.
Partial Solution:
- created a global PyObject* to store the function. (Bad design according to swig)
- override the constructor of the structure to accept the function as an input and assign it to the global variable.