ctypeslib
ctypeslib copied to clipboard
C++ interface
The current implementation of ctypeslib does not support C++ interface (I understand this could be a very complicated issue). If I have a c++ interface like this:
class TestClassInterface
{
public:
virtual void test() = 0;
virtual void test1() = 0;
};
TestClassInterface* createTestClass();
the output of clang2py is like this:
class class_TestClassInterface(Structure):
pass
class_TestClassInterface._pack_ = 1 # source:False
class_TestClassInterface._fields_ = [
('PADDING_0', ctypes.c_ubyte * 8),
]
createTestClass = _libraries['FIXME_STUB'].createTestClass
createTestClass.restype = ctypes.POINTER(class_TestClassInterface)
createTestClass.argtypes = []
Which basically generates a dummy class.
I would expect something like this:
class TestClassInterface(ctypes.Structure):
class VTable(ctypes.Structure):
_pack_ = 1
_fields_ = [
('test1', ctypes.CFUNCTYPE(None, ctypes.POINTER(None))),
('test2', ctypes.CFUNCTYPE(None, ctypes.POINTER(None))),
]
_pack_= 1
_fields_ = [
('vtable', ctypes.POINTER(VTable)),
]
python_dll.getTestClass.restype = ctypes.POINTER(TestClassInterface)
And I can call the function from python like:
test_wrapper = python_dll.getTestClass()
test_wrapper.contents.vtable.contents.test1(test_wrapper)
test_wrapper.contents.vtable.contents.test2(test_wrapper)
To my understanding, you can't use C++ with ctypes, because it does not result in a stable, FFI-bindable ABI (as opposed to C). You'll want something like pybind11 instead.
See also https://stackoverflow.com/a/1616143/15547292.