I'm new to Cython so bear with me. I'm trying to cast some data to a pointer to an extension class. The class is essentially a glorified struct. It is declared in my .pxd file as:
cdef class Field:
cdef:
np.float64_t u # x velocity
np.float64_t v # y velocity
np.float64_t w # z velocity
cdef update(self)
And of course this is implemented in a .pyx file. In my driver code, I have a 4-dimensional array of np.float64_t
's. The first three dimensions represent x, y, and z. The fourth dimension is supposed to represent these three values u, v, w
. I allocate the grid in a pure Python driver program which then passes the grid to a Cython file. In the Cython file, I cast:
curr_grid_element = (<Field *> &grid[xx, yy, zz, 0])
curr_grid_element.update()
But when I do so, I get the error: Pointer base type cannot be a Python object
.
Which has me rather confused, as I thought Field
was pure C.
Cython extension types do not translate into c structures, they translate to python built in types. At a high level this means Field
is a python class which happens to be implemented in C, like list
or ndarray
for example. At a lower level, cython does use c structures to implement extension types but these structures have fields you are not accounting for. Namely a field for tracking its type and anther for reference counting (maybe some others too).
I think what you want is struct, not an extension type. Try this:
cdef struct Field:
np.float64_t u # x velocity
np.float64_t v # y velocity
np.float64_t w # z velocity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With