PosithPython:在传递C++输出对象时不匹配C++签名

2024-09-29 17:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Python扩展我的C++代码,然后使用BooSt.python从C++导出结构数据,但是在传递参数C++或Python时捕获异常“Error MyRealyySyt”。p>

C++导出代码:


    class TestData
    {
    public:
        int val = 0;
    };

    class World
    {
    public:
        void greet(const TestData& td) {
            cout << "value: " << td.val << endl;
        }
    };

    void DoIt(World& w)
    {
        // do something
    }

    BOOST_PYTHON_MODULE(PyGameExport)
    {
        boost::python::class_<World>("World")
            .def("greet", &World::greet)
            ;

        boost::python::class_<TestData>("testData", boost::python::init<>())
            .def_readwrite("val", &TestData::val);

        boost::python::def("doIt", &DoIt);
    }

Person.py


    import PyGameExport as py

    def some_func(world):
        py.doIt(world)

    class Person:
        def greetReset(self, instance):
            d = py.testData()
            d.aa=777
            instance.greet(d)

问题1:Python函数不能将对象参数传递给C++ +/P>


    void test_function2()
    {
        Py_Initialize();
        PyInit_PyGameExport();

        using namespace boost::python;
        try {

            PyObject *pModule = PyImport_ImportModule("Person");
            if (!pModule) {
                std::cout << PythonException::parse_python_exception() << std::endl;
                return;
            }

            World world;
            boost::python::call_method<void>(pModule, "some_func", boost::ref(world));
        }
        catch (error_already_set) {
            PyErr_Print();
        }
    }

QurTrime2:Python类成员Funcon不能构造C++的导出对象并将其传递给C++ ++


    void test_function1()
    {
        Py_Initialize();
        PyInit_PyGameExport();

        using namespace boost::python;
        try {

            PyObject *pModule = PyImport_ImportModule("Person");
            if (!pModule) {
                std::cout << PythonException::parse_python_exception() << std::endl;
                return;
            }

            boost::python::handle<> module(pModule);
            boost::python::object main_module(module);
            boost::python::object main_namespace = main_module.attr("__dict__");


            PyObject* py_obj = NULL;
            py_obj = PyRun_String("Person()", Py_eval_input, main_namespace.ptr(), main_namespace.ptr());
            if (!py_obj)
                return;

            World world;
            boost::python::call_method<void>(py_obj, "greetReset", boost::ref(world));
        }
        catch (error_already_set) {
            PyErr_Print();
        }
    }

例外情况:

``

Traceback (most recent call last):
  File "D:\git_dev_home\proj_server\product\Debug\Person.py", line 12, in greetR
eset
    instance.greet(d)
Boost.Python.ArgumentError: Python argument types in
    World.greet(World, testData)
did not match C++ signature:
    greet(class World {lvalue}, class TestData)


Thanks for your answer.

Tags: pyworldmaindefvalnamespaceclassperson

热门问题