Wrapping C function in Cython and NumPy
c, cython, numpy, python, word-wrap
Solution
You should add `cdef np.ndarray` before the `out_array` assignement:
def pyfunc(np.ndarray[np.int32_t, ndim=1] in_array):
cdef np.ndarray out_array = np.zeros((512,), dtype = np.int32)
n = len(in_array)
mymodule.c_func(<int *> in_array.data, n, <int *> out_array.data)
return out_array
Problem
I'd like to call my C function from Python, in order to manipulate some NumPy arrays. The function is like this: ``` void c_func(int *in_array, int n, int *out_array); ``` where the results are supplied in out_array, whose size I know in advance (not my function, actually). I try to do in the corresponding .pyx file the following, in order to able to pass the input to the function from a NumPy array, and store the result in a NumPy array: ``` def pyfunc(np.ndarray[np.int32_t, ndim=1] in_array): n = len(in_array) out_array = np.zeros((512,), dtype = np.int32) mymodule.c_func(<int *> in_array.data, n, <int *> out_array.data) return out_array ``` But I get `"Python objects cannot be cast to pointers of primitive types"` error for the output assignment. How do I accomplish this? (If I require that the Python caller allocates the proper output array, then I can do ``` def pyfunc(np.ndarray[np.int32_t, ndim=1] in_array, np.ndarray[np.int32_t, ndim=1] out_array): n = len(in_array) mymodule.cfunc(<int *> in_array.data, n, <int*> out_array.data) ``` But can I do this in a way that the caller doesn't have to pre-allocate the appropriately sized output array?