I've got a C library that I'm trying to wrap in Cython. One of the classes I'm creating contains a pointer to a C structure. I'd like to write a copy constructor that would create a second Python object pointing to the same C structure, but I'm having trouble, as the pointer cannot be converted into a python object.
Here's a sketch of what I'd like to have:
cdef class StructName:
cdef c_libname.StructName* __structname
def __cinit__(self, other = None):
if not other:
self.__structname = c_libname.constructStructName()
elif type(other) is StructName:
self.__structname = other.__structname
The real problem is that last line - it seems Cython can't access cdef fields from within a python method. I've tried writing an accessor method, with the same result. How can I create a copy constructor in this situation?
When playing with
cdef
classes, attribute access are compiled to C struct member access. As a consequence, to access to acdef
member of an objectA
you have to be sure of the type ofA
. In__cinit__
you didn't tell Cython that other is an instance ofStructName
. Therefore Cython refuses to compileother.__structname
. To fix the problem, just writeNote:
None
is equivalent toNULL
and therefore is accepted as aStructName
.If you want more polymorphism then you have to rely on type casts: