如何为 boost python 中作为元组的一部分返回的对象指定返回策略
我有一个创建 C++ 对象的函数。在重构之前,我从 C++ 函数返回一个指针,并且在 boost python 包装器声明中,我将使用 boost::python::return_value_policy
MyObject* create_object_from_description(std::string& description)
{
...
return myObject;
}
BOOST_PYTHON_MODULE(pymol) {
boost::python::def("create_obj_from_desc", create_object_from_description,
(boost::python::arg("description")),
"",
boost::python::return_value_policy<boost::python::manage_new_object>()
);
现在我需要返回的不仅仅是对象,还需要一些错误消息,重构基本上改变了 C++ 函数以让它返回一个元组:
boost::python::tuple create_object_from_description(std::string& description)
{
...
return boost::python::make_tuple(myObject, errmsg);
}
我应该如何通过此更改指定返回策略?
I have a function that creates a C++ object. Before the refactor, I was returning a pointer from the C++ function, and in the boost python wrapper declaration, I would use boost::python::return_value_policy<boost::python::manage_new_object>
. Something like this:
MyObject* create_object_from_description(std::string& description)
{
...
return myObject;
}
BOOST_PYTHON_MODULE(pymol) {
boost::python::def("create_obj_from_desc", create_object_from_description,
(boost::python::arg("description")),
"",
boost::python::return_value_policy<boost::python::manage_new_object>()
);
Now I need to return more than just the object, but also some error messages, and the refactor basically changes the C++ function to let it return a tuple:
boost::python::tuple create_object_from_description(std::string& description)
{
...
return boost::python::make_tuple(myObject, errmsg);
}
How should I specify the return policy with this change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不可能的。您可能需要做的是更改函数签名并在 python 中重新包装该函数。像这样:
然后在 python 中有一个像这样的函数:
当然,你真正应该做的是抛出异常。 Boost::python 在将 c++ 异常转换为 python 异常方面做得非常好,反之亦然
I don't think that this is possible. What you probably need to do is change the function signature and re-wrap the function in python. Like this:
Then in python have a function like this:
Of course, what you should REALLY do is throw an exception. Boost::python does a very nice job of turning c++ exceptions in to python exception and vice-versa