I am writing a a high level interface for a C library for Python using Cython.
I have an extension Type A
that initializes the library with a pointer to a more complex C context structure c_context
. The pointer is saved in A
.
A
also has a def
function which in turn creates another extension Type B
initializing another C structure with a library function call. This structure is needed for the subsequent library calls made in B
.
B
needs the c_context
pointer from A
which is wrapped by me within the extension type py_context
in order to pass it to __cinit__
from B
:
#lib.pxd (C library definitions)
cdef extern from "lib.h":
ctypedef struct c_context:
pass
#file py_context.pxd
from lib cimport c_context
cdef class py_context:
cdef c_context *context
cdef create(cls, c_context *context)
cdef c_context* get(self)
#file py_context.pyx
def class py_context:
@staticmethod
cdef create(cls, c_context *c):
cls = py_nfc_context()
cls.context = c
return cls
cdef c_context* get(self):
return self.context
Passing the wrapper with the correct C context works perfectly.
Now I need to get the C struct out of py_context
again and save it in B
. I added cdef c_context get(self)
to py_context.pxd/pyx
.
Calling py_context.get()
from Bs __cinit__
results in: AttributeError: py_context object has no attribute get.
It seems like I do not get my head around when to call cdef
functions in Cython.
So my question is: What is the best way to extract the C struct from my wrapper class again?