问题
I want to export an abstract class from C++(Base) to create an inherited class in Python(Derived) and finally extract that class for create a C++ pointer object(Base*). I find this solution. but it didn't work for me although it compiles, the execution halt with an exception. My code is this:
#if PY_MAJOR_VERSION >= 3
# define INIT_MODULE PyInit_module
extern "C" PyObject* INIT_MODULE();
#else
# define INIT_MODULE initmodule
extern "C" void INIT_MODULE();
#endif
int main()
{
PyImport_AppendInittab((char*)"module", INIT_MODULE);
Py_Initialize();
object main = import("__main__");
object global = main.attr("__dict__");
PySys_SetPath(L".");
global["derivedmodule"] = import("derivedmodule");
object obj = eval("derivedmodule.Derived()", global);
extract<Base*> ex(obj); // Here is the problem, the extraction didn't work
// { <boost::python::converter::extract_pointer<module::Base*>> =
// {m_source = 0x7ffff6f94030,
// m_result = 0x0}, <No data fields>}
if(ex.check()){
Base * const b = ex();
b->foo();
std::cout << "SUCCESS\n";
return 0;
} else {
std::cout << "FAIL\n"; // Always jumps here.
return 1;
}
}
"module" and "derivedmodule" work on python interpreter.
回答1:
I finally found a solution. In the "derivedmodule" the class must initialize the base class like this:
class Derived(derivedmodule.Base):
def __init__(self):
derivedmodule.Base.__init__(self)
And the imported Base class must have the init function like this:
class_<BaseWrap, /*Holder*/, boost::noncopyable>("Base", init<>())
// The holder could be empty or a class like shared_ptr<Base>, but the essential is the init<>() function.
来源:https://stackoverflow.com/questions/61769980/extract-in-c-a-python-object-inherited-from-c-class