I am trying to link a common C library with some python code. The library interface is similar to
typedef struct{ int v1; double* v2} input;
There are two other types: for configuration and output type.
I installed these structures in python using ctypes Structure as follows:
class input(Structure): _fields_ = [("v1",c_int),("v2",POINTER(c_double)]
C code has some functions that get a pointer to this structure, and argtypes types are defined as:
fun.argtypes = [constraints,input,POINTER(input)]
constraints is another structure with some int fields for configuration.
First I update the v2 field in the input structure
input.v2 = generated_array.ctypes.data_as(POINTER(c_double))
Then I call it:
fun(constraints,input,byref(output))
The function prototype asks for struct and * for struct (it is assumed that the type of output struct is the same as the type of the input structure).
Then I want to access the fun results stored in the v2 output field. But I get unexpected results. Is there a better / right way to do this?
I searched a lot here and read the documentation, but I canβt find what is wrong. I have no error messages, but the warnings I get from the shared library seem to indicate that there are errors on these interfaces.
I think I found the problem:
When I call the method, the numpy complex array is called. Then I create 4 vectors:
out_real = ascontiguousarray(zeros(din.size,dtype=c_double)) out_imag = ascontiguousarray(zeros(din.size,dtype=c_double)) in_real = ascontiguousarray(din.real,dtype = c_double) in_imag = ascontiguousarray(din.imag,dtype = c_double)
where din is the input vector. I tested the method as follows:
print in_real.ctypes.data_as(POINTER(c_double)) print in_imag.ctypes.data_as(POINTER(c_double)) print out_real.ctypes.data_as(POINTER(c_double)) print out_imag.ctypes.data_as(POINTER(c_double))
and the result:
<model.LP_c_double object at 0x1d81f80> <model.LP_c_double object at 0x1d81f80> <model.LP_c_double object at 0x1d81f80> <model.LP_c_double object at 0x1d81f80>
It seems that they all point to the same place.
After some changes, it works as expected ...
After several tests, I found that the code was almost correct the first time. I created an instance of Structure once and updated its fields. I changed the creation of a new instance with every call to fun . I also changed all array types as equivalent ctypes; this apparently made the function work properly.
The printing behavior is still preserved, as in the above test, but the function seems to work even with this strange behavior. This is correct, as pointed out by @ericsun's comment below.