不能在 Boost Python 中 return class 的实例

Can't return instance of class in Boost Python

我正在尝试 return 一个 C++ 实例 class 和 return 使用 Boost.Python 将其 Python。 我想做的是使用如下代码:

class foo {
    std::string name;
    public:
    void set_name(std::string name) {this->name = name;}
    std::string get_name() {return this->name;}
};
class bar {
    public:
    foo get_foo(std::string name) {
        foo my_foo;
        my_foo.set_name(name);
        return my_foo;
    }
};

我用:

包装它
BOOST_PYTHON_MODULE(foobar)
{
    class_<foo, boost::noncopyable>("foo")
    .def("set_name", &foo::set_name)
    .def("get_name", &foo::get_name)
;

    class_<bar, boost::noncopyable>("bar")
    .def("get_foo", &bar::get_foo);
 }

你可能也注意到了,我需要 foo 和 bar 都是不可复制的,因为在实际项目中,里面有套接字。 尝试编译以上内容时,我得到一个 Python TypeError:

TypeError: No to_python (by-value) converter found for C++ type

我这样用 Python 调用它:

Python 3.6.1 (default, Mar 22 2017, 06:17:05) 
[GCC 6.3.0 20170321] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foobar
>>> a=foobar.bar()
>>> b=a.get_foo("myname")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: foo
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: foo
>>> 

我找到了使代码正常工作的方法。看来我应该使用指针 return 类型和 boost::python::return_value_policy<boost::python::manage_new_object>().

#include <boost/python.hpp>
class foo {
    std::string name;
    public:
    void set_name(std::string name) {this->name = name;}
    std::string get_name() {return this->name;}
};
class bar {
    public:
    foo* get_foo(std::string name) {
        foo* my_foo = new foo();
        my_foo->set_name(name);
        return my_foo;
    }
};
BOOST_PYTHON_MODULE(foobar)
{
    boost::python::class_<foo, boost::noncopyable>("foo")
    .def("set_name", &foo::set_name)
    .def("get_name", &foo::get_name)
;

    boost::python::class_<bar, boost::noncopyable>("bar")
    .def("get_foo", &bar::get_foo, boost::python::return_value_policy<boost::python::manage_new_object>());

 }