python - A way to pass c++ object to another object's method in cython -


i have 2 classes (let's assume simple ones, implementation not important). defs.pxd file (with cython defs) looks this:

cdef extern "a.hpp":   cdef cppclass a:     a() except +  cdef extern "b.hpp":   cdef cppclass b:     b() except +     int func (a) 

my pyx file (with python defs) looks this:

from cython.operator cimport dereference deref libcpp.memory cimport shared_ptr  cimport defs  cdef class a:     cdef shared_ptr[cquacker_defs.a] _this      @staticmethod     cdef inline _from_this(shared_ptr[cquacker_defs.a] _this):         cdef result = a.__new__(a)         result._this = _this         return result      def __init__(self):         self._this.reset(new cquacker_defs.a())  cdef class b:     cdef shared_ptr[cquacker_defs.b] _this      @staticmethod     cdef inline b _from_this(shared_ptr[cquacker_defs.b] _this):         cdef b result = b.__new__(b)         result._this = _this         return result      def __init__(self):         self._this.reset(new cquacker_defs.b())      def func(self, a):       return deref(self._this).func(deref(a._this)) 

the thing deref(self._this) works right, deref(a._this) doesn't error:

invalid operand type '*' (python object) 

how can pass 1 python object's internal c++ object one's method in python?

def func(self, a):     return # ... before 

you need tell cython a of type a (the type checked when call it). way knows a._this , doesn't treat python attribute lookup. can access cdefed attributes if cython knows type @ comple time.


Comments