How To Access Arrays Passed To Ctypes Callbacks As Numpy Arrays?
I'm trying to integrate some numerical code written in C into a Python library using numpy and ctypes. I've already got the actual computations working but would now like to report
Solution 1:
I've found a solution using ctypes.POINTER(ctypes.c_double)
and numpy.ctypeslib.as_array - according to the numpy.ctypeslib documentation, this will share the memory with the array:
callback_func = ctypes.CFUNCTYPE(
None, # return
ctypes.POINTER(ctypes.c_double), # x
ctypes.c_int # n
)
[...]
@callback_funcdefcallback(x, n):
x = npct.as_array(x, (n,))
print("x: {0}, n: {1}".format(x, n))
Anyone with a more elegant solution, perhaps using the ndpointer
objects?
Post a Comment for "How To Access Arrays Passed To Ctypes Callbacks As Numpy Arrays?"