Reputation: 22492
I am trying to wrap a header file which has lots of functions like this
test.h
void test(int N, int* data_in, int* data_out);
so that I can use those from numpy.
Right now I have the following cython code:
test.pyx
import numpy as np
cimport numpy as np
ctypedef np.int_t itype_t
cdef extern from 'VolumeForm.h':
void _test 'test' (int, int*, int*)
def wrap_test(np.ndarray[itype_t, ndim=2] data):
cdef np.ndarray[dtype_t, ndim=1] out
out = np.zeros((data.shape[0],1), dtype=np.double)
_test(
data.shape[0],
<itype_t*> data.data,
<itype_t*> out.data
)
return out
However, when I try to compile it I get the error:
Error converting Pyrex file to C:
(...)
Cannot assign type 'test.itype_t *' to 'int *'
How can I fix this?
Upvotes: 4
Views: 1526
Reputation: 1868
Another workaround that doesn't require you to change things from int
s to long
s: change the function signature in the cdef extern from '...'
block. Cython uses the declarations in a cdef extern
block only to check types while generating the .c
file, but the generated C code merely does an #include "VolumeForm.h"
, so you can get away with it.
import numpy as np
cimport numpy as np
ctypedef np.int_t itype_t
cdef extern from 'VolumeForm.h':
# NOTE: We changed the int* declarations to itype_t*
void _test 'test' (int, itype_t*, itype_t*)
def wrap_test(np.ndarray[itype_t, ndim=2] data):
cdef np.ndarray[dtype_t, ndim=1] out
out = np.zeros((data.shape[0],1), dtype=np.double)
_test(
data.shape[0],
<itype_t*> data.data,
<itype_t*> out.data
)
return out
Cython does not complain about the above.
Upvotes: 2
Reputation: 41
This issue is currently being discussed on the Cython mailing list; apparently it stems from a small bug in one of the Cython libraries:
http://codespeak.net/mailman/listinfo/cython-dev
For the moment, one potential workaround is to use NumPy arrays of dtype np.long, and then to write 'ctypedef np.long_t itype_t' instead. Then you just have to make the C code happy with long ints instead of ints.
Upvotes: 4