Boost::Python::object throwing Python TypeE的Boost::Python Forward声明

2024-06-16 11:02:08 发布

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

我试图重构我的项目的一些部分,特别是Python/C++接口。 标准的boost::python python初始化之前正在运行:

boost::python::object main_module = boost::python::import("__main__");
boost::python::object globals(main_module.attr("__dict__"));

//。。。在

但是,在把它分解成一个类之后,我得到

^{pr2}$

实例化PyInterface对象时,如下所示:

namespace py = boost::python;
class PyInterface
{
private:
    py::object
        main_module,
        global,
        tmp;
    //...
public:
    PyInterface();
    //...
};

PyInterface::PyInterface()
{
    std::cout << "Initializing..." << std::endl;
    Py_Initialize();
    std::cout << "Accessing main module..." << std::endl;
    main_module = py::import("__main__");
    std::cout << "Retrieve global namespace..." << std::endl;
    global(main_module.attr("__dict__"));
    //...
}

//in test.cpp
int main()
{
    PyInterface python;
    //...
}

Running gives the following output:
Initializing...
Accessing main module...
Retrieving global namespace...

TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::proxy<boost::python::api::attribute_policies>

我唯一能想到的是,它与在使用它之前声明“globals”有关。在这种情况下,有没有其他方法可以让我这样做?在


Tags: pyimportobjectmainnamespaceglobaldictattr
1条回答
网友
1楼 · 发布于 2024-06-16 11:02:08

啊!修好了。在

将构造函数中对全局参数的调用从

globals(main_method.attr("__dict__"));

要改用赋值运算符:

^{pr2}$

回首往事,这似乎非常明显,但至少我知道我不是唯一一个因为没有人对我下手而受挫的人。在

相关问题 更多 >