cdef public class and vtab export

111 views
Skip to first unread message

PatN

unread,
Feb 15, 2012, 2:23:55 PM2/15/12
to cython-users
I am pretty new to Cython but have wrapped C/C++ using other methods
(C api, PyCXX, Boost::Python, and ctypes.) I'm really enjoying Cython
so far, but have the following question:

I have a c++ class which needs to invoke a bound Python method. The
cleanest way I see is to have the usual Cython extension class
(declared 'cdef public class..) with a thisptr to the c++ object, and
have the c++ object have a void *py_obj which holds a pointer to the
extension class struct. So far, so good - except that when the c++
wants to call a bound method in the Cython extension class, it needs
the virtual function table which Cython emits in the .cpp source, but
not in the .h file (generated because I declared the class public.) I
find it odd that the vtab struct is not exported into the .h file,
because the extension class object structure which gets exported
includes a member called __pyx_vtab, but the actual structure of the
vtab isn't in the .h file; so I can *almost* get everything I need! Am
I missing something? A keyword perhaps, which causes the vtab to be
exported?

===== cython code
cdef public class MyTestClass(object)[object MyTestClassObject, type
MyTestType] :
cdef TPyBathControl *ths # hold a C++ instance which we're
wrapping
# This is the class whose c++ method wants to call this
object's cb method.

def __cinit__(self):
self.ths = new TPyBathControl()
self.ths.py_obj = <void *>self # put reference to this
object in the cpp object

def __dealloc__(self):
del self.ths

cdef void cb(MyTestClass self):
print 'called python'

===== generated .h file snippet
struct MyTestClassObject {
PyObject_HEAD
struct __pyx_vtabstruct_11basf_ext_cy_MyTestClass *__pyx_vtab;
TPyBathControl *ths;
};
===== What's missing from .h file?
struct __pyx_vtabstruct_11basf_ext_cy_MyTestClass ...

Thanks in advance for any advice.
Reply all
Reply to author
Forward
0 new messages