1

module.cpp:

#include <boost/python.hpp>

namespace py = boost::python;

py::object* foo()
{
    return new py::object("Test");
}

BOOST_PYTHON_MODULE(module)
{
    py::def("foo", &foo, py::return_value_policy<py::manage_new_object>());
}

python.py:

from module import foo
x = foo()

The python code throws an exception:
TypeError: No Python class registered for C++ class class boost::python::api::object

The main reason is returning a newly created object by pointer instead of returning by value which causes copying. Am I wrong?

Actually, I want to return a lot of strings, but I don't know how to make that the cheapest way.

Symon
  • 1,626
  • 1
  • 23
  • 31

0 Answers0