将 C++ 创建的对象附加到 python 列表并使其由 python 管理
Append C++ created object to python list and make it managed by python
好吧,我已经检查了一段时间了,找不到答案。
我想附加一个暴露给 python 的对象,比如 Foo:
struct Foo {
Foo(){ std::cout << "Creating a Foo object" << std::endl;}
virtual ~Foo(){ std::cout << "Destroying a Foo object" << std::endl;}
};
我使用 Foo 继承的对象,有时我想将它们附加到 python 列表中。为此,我创建了一个 FooWrapper,它继承自 Foo 并使用复制构造函数
struct FooWrapper : public Foo {
FooWrapper(const Foo& foo):Foo(foo){ std::cout << "Creating a copy from foo using FooWrapper" << std::endl;}
virtual ~FooWrapper(){ std::cout << "Destroying a FooWrapper object" << std::endl;}
};
这暴露于python:
BOOST_PYTHON_MODULE(foo_module)
{
using namespace py = boost::python;
py::class_<FooWrapper>("FooWrapper", py::no_init)…
}
我有一个方法可以将最终的 Foo 对象作为 FooWrapper 添加到 python 列表中,例如:
void appendFoosToList(py::list &list)
{
for ( const Foo* foo : m_foos )
{
list.append( FooWrapper( *foo ) );
}
}
我该怎么做才能不创建临时对象然后复制到列表,而是将这个对象附加到列表,而不必复制临时对象?
我阅读了很多文档(boost_faq, boost_python_wiki), many times I got this运行时错误:
TypeError: No to_python (by-value) converter found for C++ type:
BPL was unable to get C++ value from Python object.
For example, when calling extract(.attr("len")())
to get object length you omitted "()".
并且未能找到解决方案。
我找不到关于这个的明确文档,所以我来这里作为最后的手段。
简而言之,在免费存储上分配包装器并使用 manage_new_object
结果转换将所有权转移到 Python 对象。这将导致 Boost.Python 在构造 Python 对象时复制指针,而不是复制指针对象。
C++ 对象嵌入到 Python 对象中。这是通过 class_
公开 class 时提供的 HeldType
,默认为公开的 C++ 类型。通常,在公开函数时,可以使用 CallPolicy 实例扩充返回并嵌入到 Python 对象中的 C++ 类型。特别是,使用 return_value_policy
CallPolicy 的实例和 manage_new_object
ResultConverterGenerator 允许嵌入类型是指针,Python 对象将管理所有权。
以下函数可用于将对象的所有权转移给 Python 而无需复制指针对象:
/// @brief Transfer ownership to a Python object. If the transfer fails,
/// then object will be destroyed and an exception is thrown.
template <typename T>
boost::python::object transfer_to_python(T* t)
{
// Transfer ownership to a smart pointer, allowing for proper cleanup
// incase Boost.Python throws.
std::unique_ptr<T> ptr(t);
// Use the manage_new_object generator to transfer ownership to Python.
namespace python = boost::python;
typename python::manage_new_object::apply<T*>::type converter;
// Transfer ownership to the Python handler and release ownership
// from C++.
python::handle<> handle(converter(*ptr));
ptr.release();
return python::object(handle);
}
用法示例:
void appendFoosToList(boost::python::list& list)
{
for (const Foo* foo : m_foos)
{
list.append(transfer_to_python(new FooWrapper(*foo)));
}
}
这里有一个完整的例子demonstrating这种方法:
#include <iostream>
#include <boost/python.hpp>
// Mocks...
class spam
{
public:
spam() { std::cout << "spam(): " << this << std::endl; }
spam(const spam&)
{
std::cout << "spam(const spam&): " << this << std::endl;
}
~spam() { std::cout << "~spam(): " << this << std::endl; }
};
/// @brief Transfer ownership to a Python object. If the transfer fails,
/// then object will be destroyed and an exception is thrown.
template <typename T>
boost::python::object transfer_to_python(T* t)
{
// Transfer ownership to a smart pointer, allowing for proper cleanup
// incase Boost.Python throws.
std::unique_ptr<T> ptr(t);
// Use the manage_new_object generator to transfer ownership to Python.
namespace python = boost::python;
typename python::manage_new_object::apply<T*>::type converter;
// Transfer ownership to the Python handler and release ownership
// from C++.
python::handle<> handle(converter(*ptr));
ptr.release();
return python::object(handle);
}
void append_to_list(boost::python::list& list)
{
list.append(transfer_to_python(new spam()));
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<spam>("Spam", python::no_init);
python::def("append_to_list", &append_to_list);
}
交互使用:
>>> import example
>>> spams = []
>>> example.append_to_list(spams)
spam(): 0x25cbd90
>>> assert(type(spams[0]) is example.Spam)
>>> del spams
~spam(): 0x25cbd90
好吧,我已经检查了一段时间了,找不到答案。
我想附加一个暴露给 python 的对象,比如 Foo:
struct Foo {
Foo(){ std::cout << "Creating a Foo object" << std::endl;}
virtual ~Foo(){ std::cout << "Destroying a Foo object" << std::endl;}
};
我使用 Foo 继承的对象,有时我想将它们附加到 python 列表中。为此,我创建了一个 FooWrapper,它继承自 Foo 并使用复制构造函数
struct FooWrapper : public Foo {
FooWrapper(const Foo& foo):Foo(foo){ std::cout << "Creating a copy from foo using FooWrapper" << std::endl;}
virtual ~FooWrapper(){ std::cout << "Destroying a FooWrapper object" << std::endl;}
};
这暴露于python:
BOOST_PYTHON_MODULE(foo_module)
{
using namespace py = boost::python;
py::class_<FooWrapper>("FooWrapper", py::no_init)…
}
我有一个方法可以将最终的 Foo 对象作为 FooWrapper 添加到 python 列表中,例如:
void appendFoosToList(py::list &list)
{
for ( const Foo* foo : m_foos )
{
list.append( FooWrapper( *foo ) );
}
}
我该怎么做才能不创建临时对象然后复制到列表,而是将这个对象附加到列表,而不必复制临时对象?
我阅读了很多文档(boost_faq, boost_python_wiki), many times I got this运行时错误:
TypeError: No to_python (by-value) converter found for C++ type:
BPL was unable to get C++ value from Python object.
For example, when calling extract(.attr("len")()) to get object length you omitted "()".
并且未能找到解决方案。
我找不到关于这个的明确文档,所以我来这里作为最后的手段。
简而言之,在免费存储上分配包装器并使用 manage_new_object
结果转换将所有权转移到 Python 对象。这将导致 Boost.Python 在构造 Python 对象时复制指针,而不是复制指针对象。
C++ 对象嵌入到 Python 对象中。这是通过 class_
公开 class 时提供的 HeldType
,默认为公开的 C++ 类型。通常,在公开函数时,可以使用 CallPolicy 实例扩充返回并嵌入到 Python 对象中的 C++ 类型。特别是,使用 return_value_policy
CallPolicy 的实例和 manage_new_object
ResultConverterGenerator 允许嵌入类型是指针,Python 对象将管理所有权。
以下函数可用于将对象的所有权转移给 Python 而无需复制指针对象:
/// @brief Transfer ownership to a Python object. If the transfer fails,
/// then object will be destroyed and an exception is thrown.
template <typename T>
boost::python::object transfer_to_python(T* t)
{
// Transfer ownership to a smart pointer, allowing for proper cleanup
// incase Boost.Python throws.
std::unique_ptr<T> ptr(t);
// Use the manage_new_object generator to transfer ownership to Python.
namespace python = boost::python;
typename python::manage_new_object::apply<T*>::type converter;
// Transfer ownership to the Python handler and release ownership
// from C++.
python::handle<> handle(converter(*ptr));
ptr.release();
return python::object(handle);
}
用法示例:
void appendFoosToList(boost::python::list& list)
{
for (const Foo* foo : m_foos)
{
list.append(transfer_to_python(new FooWrapper(*foo)));
}
}
这里有一个完整的例子demonstrating这种方法:
#include <iostream>
#include <boost/python.hpp>
// Mocks...
class spam
{
public:
spam() { std::cout << "spam(): " << this << std::endl; }
spam(const spam&)
{
std::cout << "spam(const spam&): " << this << std::endl;
}
~spam() { std::cout << "~spam(): " << this << std::endl; }
};
/// @brief Transfer ownership to a Python object. If the transfer fails,
/// then object will be destroyed and an exception is thrown.
template <typename T>
boost::python::object transfer_to_python(T* t)
{
// Transfer ownership to a smart pointer, allowing for proper cleanup
// incase Boost.Python throws.
std::unique_ptr<T> ptr(t);
// Use the manage_new_object generator to transfer ownership to Python.
namespace python = boost::python;
typename python::manage_new_object::apply<T*>::type converter;
// Transfer ownership to the Python handler and release ownership
// from C++.
python::handle<> handle(converter(*ptr));
ptr.release();
return python::object(handle);
}
void append_to_list(boost::python::list& list)
{
list.append(transfer_to_python(new spam()));
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<spam>("Spam", python::no_init);
python::def("append_to_list", &append_to_list);
}
交互使用:
>>> import example
>>> spams = []
>>> example.append_to_list(spams)
spam(): 0x25cbd90
>>> assert(type(spams[0]) is example.Spam)
>>> del spams
~spam(): 0x25cbd90