示例无法编译,因为:
- 第一个示例尝试将未指定的类型(
override::operator() 的返回类型)转换为不兼容的类型。特别是,Boost.Python 目前不支持std::unique_ptr,因此不会转换为它。
- 当调用函数声明返回
void时,第二个示例尝试返回上述未指定类型。
从 Python 的角度来看,字符串是不可变的,尝试将字符串的所有权从 Python 转移到 C++ 违反了语义。但是,可以在 C++ 中创建字符串的副本,并将复制的字符串的所有权传递给 C++。例如:
std::unique_ptr<std::string> BaseWrap::f()
{
// This could throw if the Python method throws or the Python
// method returns a value that is not convertible to std::string.
std::string result = this->get_override("f")();
// Adapt the result to the return type.
return std::unique_ptr<std::string>(new std::string(result));
}
this->get_override("f")() 返回的对象具有未指定的类型,但可用于转换为 C++ 类型。如果 Python 抛出,覆盖的调用将抛出,如果从 Python 返回的对象不能转换为 C++ 类型,则转换为 C++ 类型将抛出。
这里有一个完整的例子demonstrating 两种方法将返回的 Python 对象适配为 C++ 对象。如上所述,可以使用override 转换。或者,可以使用boost::python::extract<>,允许在执行转换之前检查转换是否会失败:
#include <memory> // std::unique_ptr
#include <boost/algorithm/string.hpp> // boost::to_upper_copy
#include <boost/python.hpp>
struct base
{
virtual ~base() {}
virtual std::unique_ptr<std::string> perform() = 0;
};
struct base_wrap : base, boost::python::wrapper<base>
{
std::unique_ptr<std::string> perform()
{
namespace python = boost::python;
// This could throw if the Python method throws or the Python
// method returns a value that is not convertible to std::string.
std::string result = this->get_override("perform")();
// Alternatively, an extract could be used to defer extracting the
// result.
python::object method(this->get_override("perform"));
python::extract<std::string> extractor(method());
// Check that extractor contains a std::string without throwing.
assert(extractor.check());
// extractor() would throw if it did not contain a std::string.
assert(result == extractor());
// Adapt the result to the return type.
return std::unique_ptr<std::string>(new std::string(result));
}
};
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<base_wrap, boost::noncopyable>("Base", python::init<>())
.def("perform", python::pure_virtual(&base::perform))
;
python::def("make_upper", +[](base* object) {
auto result = object->perform(); // Force dispatch through base_wrap.
assert(result);
return boost::to_upper_copy(*result);
});
}
互动使用:
>>> import example
>>> class Derived(example.Base):
... def perform(self):
... return "abc"
...
>>> derived = Derived()
>>> assert("ABC" == example.make_upper(derived))