1

プロジェクトの一部、特にPython /C++インターフェイスをリファクタリングしようとしています。標準のboost::python python初期化は以前は機能していました:

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

//..。

しかし、それを独自のクラスに分解した後、私は

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

以下のように、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>

私が考えることができる唯一のことは、それを使用する前に「グローバル」を宣言することと関係があるということです。その場合、私がこれを行うことができる別の方法はありますか?

4

1 に答える 1

0

ああ!修正しました。

コンストラクターのグローバルへの呼び出しをから変更する

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

代わりに代入演算子を使用するには:

globals = main_method.attr("__dict__");

振り返ってみると、それは完全に明白に思えますが、少なくとも、私をだましている人がいないことから判断して、私だけが困惑しているわけではないことを知っています。

于 2012-04-06T12:56:17.597 に答える