[英]Boost::Python Forward Declaration of boost::python::object throwing python TypeError
我正在嘗試重構項目的某些部分,尤其是 Python/C++ 接口。 標准 boost::python python 初始化工作之前:
boost::python::object main_module = boost::python::import("__main__");
boost::python::object globals(main_module.attr("__dict__"));
//...
但是,在將其分解為 class 之后,我得到了
TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::proxy<boost::python::api::attribute_policies>
實例化 PyInterface object 時,如下所示:
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”有關。 在這種情況下,還有另一種方法可以做到這一點嗎?
啊。 固定它。
將構造函數中對全局變量的調用從
globals(main_method.attr("__dict__"));
改為使用賦值運算符:
globals = main_method.attr("__dict__");
回想起來,這似乎是顯而易見的,但至少我知道我不是唯一一個因為沒有人對我感興趣而感到難過的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.