Reputation: 1
I have this C library that I want to create a Python binding for. I chose CFFI for this task.
In this lib of tens of functions, some takes void*
as an input. As the lib imply, you can actually pass anything to it, as long as you define the size of an individual object and the number of it. It is made to take an array of arbitrary data.
My question is: How can I expose a Python wrapper/binding function that can take anything as an input, convert/map it to C side using CFFI, and call the function using CFFI ?
I've successfully called a function with some "pre-defined" data, like an array of integers or floats, but it looks like CFFI doesn't support custom data structure when using ffi.new()
.
Here is an example where I construct an array of ints and then call the C lib function on it.
from _myPythonBindingLibModule import ffi, lib
# array of int
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
data_c = ffi.new("int[]", data)
result = lib.doSomethingInC(data_c, len(data), ffi.sizeof(data_c[0]))
Well, I can do that because I know the type of data I'm passing to the C function, but how can I write a wrapper around that function that can take anything ? The limiting factor for me right now is the CFFI lib that takes either array of PODs (int, float, char...) or bytes via the ffi.new()
and ffi.from_buffer()
functions.
Some thoughts on which I need advice:
ffi.from_buffer()
, but I need to pass a bytes
to it, so it would need some boilerplate to do the conversion. Is it even possible ?Upvotes: 0
Views: 68