我已经用Py ++包装了一个C++类,并且所有东西都在Python中工作得很好。我可以实例化C++类,调用方法等。如何从boost :: python中从Python类型中提取包装的C++类型?
我正在尝试将一些Python嵌入到C++应用程序中。这也适用于大多数情况。我可以调用一个Python模块的功能,得到的返回值等
的Python代码我打电话的回报,我包裹的一类:
import _myextension as myext
def run_script(arg):
my_cpp_class = myext.MyClass()
return my_cpp_class
我在C中调用这个函数++像这样:
// ... excluding error checking, ref counting, etc. for brevity ...
PyObject *pModule, *pFunc, *pArgs, *pReturnValue;
Py_Initialize();
pModule = PyImport_Import(PyString_FromString("cpp_interface"));
pFunc = PyObject_GetAttrString(pModule, "run_script");
pArgs = PyTuple_New(1); PyTuple_SetItem(pArgs, 0, PyString_FromString("an arg"));
pReturnValue = PyObject_CallObject(pFunc, pArgs);
bp::extract< MyClass& > extractor(pReturnValue); // PROBLEM IS HERE
if (extractor.check()) { // This check is always false
MyClass& cls = extractor();
}
问题是提取从未真正抽取/转换的PyObject *到MyClass的(即extractor.check()始终是假的)。
根据the docs这是提取包装的C++类的正确方法。
我试过从Python函数返回基本数据类型(int/float/dicts),并且它们都被正确提取。
有什么我失踪了吗?是否有另一种获取数据并投射到MyClass的方法?