[C++-sig] Pass C++ object to python issue

Martin Dai blast2001 at 21cn.com
Wed Apr 27 05:44:25 CEST 2005


Hi  everyone,

    Now  I  have solved the pass variable when the variable is simple type,
but when I bind the C++ class object into the dictionary,  it compile
successfully,  it will abort once execute it.
The below is the c++ source code:

    if (PyImport_AppendInittab("embedded_hello", initembedded_hello) == -1)
        throw std::runtime_error("Failed to add embedded_hello to the
interpreter's "
                                 "builtin modules");
    Py_Initialize();
    python::object main_module((

python::handle<>(python::borrowed(PyImport_AddModule("__main__")))));

    python::object main_namespace((main_module.attr("__dict__")));
    Base * pBase = new Base(2000);
    main_namespace["retValue"] = 5;
    main_namespace["AppBase"] = python::object(python::ptr(pBase));
//abort here

    python::handle<> result(
        PyRun_String(
            "from embedded_hello import *        \n"
            "print AppBase.i \n"
        Py_file_input, main_namespace.ptr(), main_namespace.ptr())
        );


Notes: Base is the class name I defined and main_namespace is the dict which
is passed to PyRun_String.


Could someone figure out the cause ?
Thanks very much !


Best Regards,
Blade






More information about the Cplusplus-sig mailing list