简体   繁体   中英

Extract in C++ a Python object inherited from C++ Class

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM