[英]Exposing C++ functions, that return pointer using Boost.Python
I want to expose the following C++ function to Python using Boost.Python: 我想使用Boost.Python将以下C ++函数公开给Python:
int* test1() {
return new int(42);
}
// Now exposing the function with Boost.Python
BOOST_PYTHON_MODULE(libtest1)
{
using namespace boost::python;
def("test1", test1);
}
When I try to compile this library the error occurs due to (it's my guess) Boost.Python don't know, how to convert int* to PyObject. 当我尝试编译这个库时,由于(我的猜测)Boost.Python不知道如何将int *转换为PyObject而发生错误。
I think what needs to be done is to define conversion structure, something like this: 我认为需要做的是定义转换结构,如下所示:
template<class T>
struct int_ptr_to_python
{
static PyObject* convert(int* i_ptr)
{
return i_ptr;
}
};
And pass it to the BOOST_PYTHON_MODULE declaration: 并将其传递给BOOST_PYTHON_MODULE声明:
BOOST_PYTHON_MODULE(libtest1)
{
using namespace boost::python;
def("test1", test1);
to_python_converter<int*, int_ptr_to_python<int*> >();
}
But it also doesn't work. 但它也行不通。 And I can't find any information about how the functions, that return pointers should be handled.
而且我找不到有关如何处理返回指针的函数的任何信息。
Does anyone can help? 有人可以帮忙吗?
In short, one cannot directly expose a function returning int*
with Boost.Python, as there is no meaningful corresponding type in Python given integers are immutable. 简而言之,一个人不能直接暴露一个函数,用Boost.Python返回
int*
,因为在Python中没有有意义的相应类型,因为整数是不可变的。
int
by value. int
值返回int
。 This may require using an auxiliary function to adapt legacy APIs. Rather than present the final solution immediately, I would like to take the time to step through the compiler errors. 我不想立即提出最终解决方案,而是花时间逐步解决编译器错误。 With Boost.Python, sometimes pre-C++11 static assertions are used to provide instructions in the compiler messages.
使用Boost.Python,有时使用预C ++ 11静态断言来提供编译器消息中的指令。 Unfortunately, it can be a bit difficult to find them amongst the heavy templates.
不幸的是,在繁重的模板中找到它们可能有点困难。
The following code: 以下代码:
#include <boost/python.hpp>
int* make_int() { return new int(42); }
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::def("make_int", &make_int);
}
produces the following relevant output in clang, with the important details accentuated in bold: 在clang中产生以下相关输出,重要细节以粗体突出显示:
.../boost/python/detail/caller.hpp:102:98: error: no member named 'get_pytype' in 'boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<int*>' ...create_result_converter((PyObject*)0, (ResultConverter *)0, (ResultConverter *)0).g...
Boost.Python is informing us that a boost::python::return_value_policy
needs to be specified for functions returning int*
. Boost.Python告诉我们需要为返回
int*
函数指定boost::python::return_value_policy
。 There are various models of ResultConverterGenerators . ResultConverterGenerators有各种模型。 Often times the policies are used to control the ownership or lifetime semantics of the returned object.
通常,策略用于控制返回对象的所有权或生命周期语义。 As the function in the original code is returning a new pointer directly to Python, the
boost::python::manage_new_object
is appropriate if the caller is expected to take responsibility for deleting the object. 由于原始代码中的函数直接向Python返回一个新指针,因此如果调用者需要负责删除该对象,则
boost::python::manage_new_object
是合适的。
Specifying a policy for object management still fails: 指定对象管理策略仍然失败:
#include <boost/python.hpp>
int* make_int() { return new int(42); }
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::def("make_int", &make_int,
python::return_value_policy<python::manage_new_object>());
}
produces the following relevant output: 产生以下相关输出:
.../boost/python/object/make_instance.hpp:27:9: error: no matching function for call to 'assertion_failed'BOOST_MPL_ASSERT((mpl::or_<is_class<T>, is_union<T> >));
In this case, Boost.Python is informing us that the object returned from a function with a managed_new_object
ResultConverterGenerator must be either a class
or union
. 在这种情况下,Boost.Python通知我们从具有
managed_new_object
ResultConverterGenerator的函数返回的对象必须是class
或union
。 For an int*
, the most appropriate solution is to return the int
by value when passing through the Boost.Python layer. 对于
int*
,最合适的解决方案是在通过Boost.Python层时返回int
值。 However, for completeness, below demonstrates: 但是,为了完整性,以下说明:
#include <boost/python.hpp>
/// Legacy API.
int* make_int() { return new int(42); }
/// Auxiliary function that adapts the legacy API to Python.
int py_make_int()
{
std::auto_ptr<int> ptr(make_int());
return *ptr;
}
/// Auxiliary class that adapts the legacy API to Python.
class holder
: private boost::noncopyable
{
public:
holder()
: value_(make_int())
{}
int get_value() const { return *value_; }
void set_value(int value) { *value_ = value; }
private:
std::auto_ptr<int> value_;
};
/// Factory function for the holder class.
holder* make_holder()
{
return new holder();
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::def("make_int", &py_make_int);
python::class_<holder, boost::noncopyable>("Holder", python::no_init)
.add_property("value",
python::make_function(&holder::get_value),
python::make_function(&holder::set_value))
;
python::def("make_holder", &make_holder,
python::return_value_policy<python::manage_new_object>());
}
Interactive usage: 互动用法:
>>> import example
>>> assert(42 == example.make_int())
>>> holder = example.Holder()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: This class cannot be instantiated from Python
>>> holder = example.make_holder()
>>> assert(42 == holder.value)
>>> holder.value *= 2
>>> assert(84 == holder.value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.