I have C ++ code that defines some classes that I need to use, but I need to be able to send these classes to Python code. In particular, I need to instantiate the class in C ++, create Python objects to serve as a wrapper for these C ++ objects, and then pass these Python objects to Python code for processing. This is just one part of a larger C ++ program, so it needs to be executed eventually in C ++ using the C / Python API.
To make my life easier, I used Cython to define extension classes (cdef classes) that serve as Python wrappers for my C ++ objects. I use a typical format in which the cdef class contains a pointer to the C ++ class, which is then initialized when the cdef class is instantiated. Since I also want to be able to replace the pointer, if I have an existing C ++ object for wrapping, I have added methods to my cdef classes in the accept() C ++ object and take its pointer. My other cdef classes successfully use the accept() method in Cython, for example, when one object belongs to another.
Here is an example of my Cython code:
MyCPlus.pxd
cdef extern from "MyCPlus.h" namespace "mynamespace": cdef cppclass MyCPlus_Class: MyCPlus_Class() except +
PyModule.pyx
cimport MyCPlus from libcpp cimport bool cdef class Py_Class [object Py_Class, type PyType_Class]: cdef MyCPlus.MyCPlus_Class* thisptr cdef bool owned cdef void accept(self, MyCPlus.MyCPlus_Class &indata): if self.owned: del self.thisptr self.thisptr = &indata self.owned = False def __cinit__(self): self.thisptr = new MyCPlus.MyCPlus_Class() self.owned = True def __dealloc__(self): if self.owned: del self.thisptr
The problem occurs when I try to access the accept() method from C ++. I tried using the public and api keywords in my cdef class and the accept() method, but I can't figure out how to expose this method in a C structure in automatically generating an .h file in Cython. No matter what I try, the C structure looks like this:
PyModule.h (Cython Auto Generation)
struct Py_Class { PyObject_HEAD struct __pyx_vtabstruct_11PyModule_Py_Class *__pyx_vtab; mynamespace::MyCPlus_Class *thisptr; bool owned; };
I also tried typing self as Py_Class , and I even tried forward-decloring Py_Class with the keywords public and api . I also experimented with creating a static accept() method. Nothing I tried works to expose the accept() method so that I can use it from C ++. I tried to access it via __pyx_vtab , but I got a compiler error, "invalid use of incomplete type". I searched quite a bit, but did not see a solution for this. Can someone help me? Please thanks!