alf
alf

Reputation: 35

cython pass pointer to constructor of extension types

I defined a extension types with an array and a float number. But it goes wrong when I try to compile it: Cannot convert Python object argument to type 'float *'. It seems that I couldn't pass a pointer to constructor. Is there any solution to avoid this problem?

cdef class Particle(object):
    cdef float pos[3]
    cdef float D

    def __init__(self,float pos[3],float D):
        self.pos=pos
        self.D=D

Upvotes: 3

Views: 268

Answers (1)

J.J. Hakala
J.J. Hakala

Reputation: 6214

I think a C array is not allowed there, tuple or list might work.

cdef class Particle(object):
    cdef float pos[3]
    cdef float D

    def __init__(self, tuple pos, float D):
        self.pos = pos
        self.D = D

The code produced by cython seems sensible,

if (unlikely(__Pyx_carray_from_py_float(__pyx_v_pos, __pyx_t_1, 3) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  memcpy(&(__pyx_v_self->pos[0]), __pyx_t_1, sizeof(__pyx_v_self->pos[0]) * (3));

Upvotes: 2

Related Questions