[英]Boost Python: Fails to call a C++ virtual function, which is overridden in a Python derived class
I have a Python class, which is derived from a C++ class and overrides part of the functions. 我有一个Python类,它是从C ++类派生的,并且覆盖了部分函数。 A C++ function receives an object of the Python class and stores it in a vector, so the Python class should not be collected by the GC. C ++函数接收Python类的对象并将其存储在向量中,因此GC不应收集Python类。 I tried two different approaches, but first one doesn't compile and second doesn't see the overrides at run time (the functions of C++ are called instead of the Python class functions). 我尝试了两种不同的方法,但是第一种方法不编译,第二种方法在运行时看不到重写(调用C ++函数而不是Python类函数)。 Please tell me what is the right way to do this. 请告诉我什么是正确的方法。
The C++ code: C ++代码:
class MyClass
{
public:
virtual void PureVirtual(int i) = 0;
virtual const char* VirtualWithDefaultImpl()
{
return "MyClass";
}
};
void MyFnc(boost::shared_ptr<MyClass> obj)
{
myVector.push_back(obj);
std::cout << obj->VirtualWithDefaultImpl() << std::endl;
obj->PureVirtual(0);
}
The python wrapper: python包装器:
class MyClassWrap : public MyClass, wrapper<MyClass>
{
public:
MyClassWrap(PyObject* self) : m_self(self)
{
Py_INCREF(self);
}
MyClassWrap(PyObject* self, const MyClass& other) : MyClass(other), m_self(self)
{
Py_INCREF(self);
}
~MyClassWrap()
{
Py_DECREF(m_self);
}
virtual void PureVirtual(int i)
{
this->get_override("PureVirtual")(i);
}
virtual const char* VirtualWithDefaultImpl()
{
if (override f = this->get_override("VirtualWithDefaultImpl"))
return f();
return MyClass::VirtualWithDefaultImpl();
}
const char* DefaultVirtualWithDefaultImpl()
{
return this->MyClass::VirtualWithDefaultImpl();
}
private:
PyObject* m_self;
};
BOOST_PYTHON_MODULE(MyModule)
{
// First approach:
// Fails in compilation with error C2243: 'type cast' : conversion from
// 'MyClassWrap *' to 'boost::python::wrapper<T> *' exists, but is inaccessible
//class_<MyClassWrap, boost::shared_ptr<MyClassWrap>, boost::noncopyable>("MyClass")
// Second approach:
// Doesn't see the overrides at runtime
class_<MyClass, boost::shared_ptr<MyClassWrap>, boost::noncopyable>("MyClass")
.def("PureVirtual", pure_virtual(&MyClass::PureVirtual), args("i"))
.def("VirtualWithDefaultImpl", &MyClass::VirtualWithDefaultImpl,
&MyClassWrap::DefaultVirtualWithDefaultImpl);
def("MyFnc", &MyFnc, args("obj"));
}
The Python code: Python代码:
class PythonDerived(MyModule.MyClass):
def PureVirtual(self, i):
print i
def VirtualWithDefaultImpl(self):
return 'PythonDerived'
MyModule.MyFnc(PythonDerived())
Output of the second approach run. 运行第二种方法的输出。 As you can see the functions of MyClass are called instead of the PythonDerived functions: 如您所见,调用了MyClass的函数而不是PythonDerived函数:
MyClass
File "z:\tmp\tmp.py", line 11, in <module>
MyModule.MyFnc(PythonDerived())
TypeError: 'NoneType' object is not callable
I have modified your wrapper class and how it is exposed to python and now that is working as expected: 我已经修改了您的包装器类,以及如何将其暴露给python,现在可以按预期运行:
class MyClassWrap : public MyClass, public python::wrapper<MyClass>
{
public:
MyClassWrap() : MyClass()
{
}
virtual void PureVirtual(int i)
{
this->get_override("PureVirtual")(i);
}
virtual const char* VirtualWithDefaultImpl()
{
if (python::override f = this->get_override("VirtualWithDefaultImpl"))
return f();
return MyClass::VirtualWithDefaultImpl();
}
const char* DefaultVirtualWithDefaultImpl()
{
return this->MyClass::VirtualWithDefaultImpl();
}
};
BOOST_PYTHON_MODULE(MyModule)
{
python::class_<MyClassWrap, boost::shared_ptr<MyClassWrap>, boost::noncopyable>("MyClass")
.def("PureVirtual", python::pure_virtual(&MyClassWrap::PureVirtual), python::args("i"))
.def("VirtualWithDefaultImpl", &MyClassWrap::VirtualWithDefaultImpl,
&MyClassWrap::DefaultVirtualWithDefaultImpl);
python::def("MyFnc", &MyFnc, python::args("obj"));
}
Output: 输出:
PythonDerived
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.