[Cython] wrapping c code, classmethod question
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Mon Oct 6 18:16:04 CEST 2008
Uwe Schmitt wrote:
> Hi,
>
> I'm trying to wrap an existing C entension as follows:
>
> cdef struct A:
> double * values
> int n
>
>
> cdef class Test:
> cdef A a
>
> @classmethod
> def buildFromData(cls, filename):
>
> cdef A a # = foofoo(filename)
>
> obj = cls()
> obj.a = a
> return obj
>
> Running Cython the "obj.a=a" results in """Cannot convert 'A' to Python
> object""".
Because "obj.a" is an attribute of the Python object "obj", while a is a C
struct...
I.e. you need to type "cdef Test obj = cls()" instead, so that it is known
compile-time that "obj" will be an instance of Test and thus have a typed
"a" field.
(I don't know myself whether classmethod works as intended for cdef
classes either, but this should be one obstacle less...)
Dag Sverre
More information about the Cython-dev
mailing list