Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

Boost-python How to pass a c++ class instance to a python class

I am new to boost python. I have to first init a cpp class instance in cpp code, and then pass this cpp instance to python code, use a python class instance to invoke it(the cpp instance). I have tried the Python/C API way, but failed, so I wonder how to pass a c++ class instance to a python class.

The following is my code, changed from the boost python demo.

in main.cpp

#include <python2.6/Python.h>
#include <boost/python.hpp>
#include <iostream>

using namespace boost::python;
using namespace std;

class World
{
private:
    string name;
public:
    void set(string name)
    {
        this->name = name;
    }
    void greet()
    {
        cout << "hello, I am " << name << endl;
    }
};

typedef boost::shared_ptr< World > world_ptr;

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
    .def("greet", &World::greet)
    .def("set", &World::set)
    ;

    register_ptr_to_python<world_ptr>();
};

int main()
{
    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");

    world_ptr worldObjectPtr (new World);
    worldObjectPtr->set("C++!");

    try
    {
        inithello();
        PyObject* pModule =PyImport_ImportModule("python");
        PyObject* pDict = PyModule_GetDict(pModule);
        PyObject* pClassHelloPython = PyDict_GetItemString(pDict, "Person");
        PyObject* pInstanceHelloPython = PyInstance_New(pClassHelloPython, NULL, NULL);

        PyObject_CallMethod(pInstanceHelloPython, "sayHi", NULL);
        worldObjectPtr->greet();
        PyObject_CallMethod(pInstanceHelloPython, "greetReset", "O", worldObjectPtr);
        worldObjectPtr->greet();
    }
    catch (error_already_set)
    {
        PyErr_Print();
    }

    Py_Finalize();

    return 0;
}

in python.py

class Person:
    def sayHi(self):
        print 'hello from python'

    def greetReset(self, instance):
        instance.set('Python')

In the above code, I want to pass the worldObjectPtr to the pInstanceHelloPython, thus, pInstanceHelloPython can set the worldObjectPtr->name to Python. But I just don't know how to do it. Thank you for your patience in advance!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pass the object pointer via boost::python::ptr to python. This will prevent the python interpreter from makeing a copy:

#include <boost/python.hpp>
#include <string>
#include <iostream>

using namespace boost::python;
using namespace std;

class World
{
private:
    string name;
public:
    void set(string name) {
        this->name = name;
    }
    void greet() {
        cout << "hello, I am " << name << endl;
    }
};

typedef boost::shared_ptr< World > world_ptr;

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
};

int main(int argc, char **argv)
{
    Py_Initialize();
    try {
        PyRun_SimpleString(
            "class Person:
"
            "    def sayHi(self):
"
            "        print 'hello from python'
"
            "    def greetReset(self, instance):
"
            "        instance.set('Python')
"
          );

        world_ptr worldObjectPtr (new World);
        worldObjectPtr->set("C++!");

        inithello();
        object o_main 
            = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
        object o_person_type = o_main.attr("Person");
        object o_person = o_person_type();
        object o_func1 = o_person.attr("sayHi");
        o_func1();
        object o_func2 = o_person.attr("greetReset");
        o_func2(boost::python::ptr(worldObjectPtr.get()));
        worldObjectPtr->greet();
    }
    catch (error_already_set) {
        PyErr_Print();
    }

    Py_Finalize();

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...