Boost Python 找不到 std::string 的 to_python 转换器
所以,我试图创建一个 to_python 转换器,它允许我从公开的函数返回 boost::Optional ,如果设置了可选值,则将其视为 T ,如果没有设置,则将其视为 None 。基于我在 C++Sig 上找到的帖子,我写了下面的代码。
template<typename T>
struct optional_ : private boost::noncopyable {
struct conversion {
static PyObject* convert(boost::optional<T> const& value) {
if (value) {
return boost::python::to_python_value<T>()(*value);
}
Py_INCREF(Py_None);
return Py_None;
}
};
explicit optional_() {
boost::python::to_python_converter<boost::optional<T>, conversion>();
}
};
据我所知,它适用于转换可选值,但 python 抛出以下异常“TypeError:找不到 C++ 类型的 to_python (按值)转换器:std::string”。我知道 C++ 能够将字符串转换为 python,因为我公开的大多数函数都返回字符串。为什么 boost::python::to_python_value 无法识别它,我如何利用它具有的任何转换器?
通过更改为以下内容来修复(基于 此文章):
template<typename T>
struct optional_ : private boost::noncopyable {
struct conversion {
static PyObject* convert(boost::optional<T> const& value) {
using namespace boost::python;
return incref((value ? object(*value) : object()).ptr());
}
};
explicit optional_() {
boost::python::to_python_converter<boost::optional<T>, conversion>();
}
};
现在只做另一个版本,以便它更干净并且工作得更好。
So, I am trying to create a to_python converter that will allow me to return a boost::optional from an exposed function and have it treated as T if the optional is set and None if not. Based on a post I found on C++Sig, I wrote the following code.
template<typename T>
struct optional_ : private boost::noncopyable {
struct conversion {
static PyObject* convert(boost::optional<T> const& value) {
if (value) {
return boost::python::to_python_value<T>()(*value);
}
Py_INCREF(Py_None);
return Py_None;
}
};
explicit optional_() {
boost::python::to_python_converter<boost::optional<T>, conversion>();
}
};
As far as I can tell, it works for converting the optionals, but python throws the following exception "TypeError: No to_python (by-value) converter found for C++ type: std::string". I know that C++ is able to convert strings to python since most of my exposed functions return strings. Why doesn't boost::python::to_python_value recognize it, and how can I utilize whatever converter it has?
Fixed by changing to the following (based on this article):
template<typename T>
struct optional_ : private boost::noncopyable {
struct conversion {
static PyObject* convert(boost::optional<T> const& value) {
using namespace boost::python;
return incref((value ? object(*value) : object()).ptr());
}
};
explicit optional_() {
boost::python::to_python_converter<boost::optional<T>, conversion>();
}
};
Now to just do the other version so that it is cleaner and works better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这里是基于原始 C++ sig 帖子的整个可选转换器,但重写为使用高级 boost.python API(对奇怪的间距感到抱歉)。
Ok here is the entire to and from optional converter based on the original C++ sig post but rewritten to use the high level boost.python API (sorry about the weird spacing).
上面的代码有一些拼写错误 - 这是更正的版本:
There are a few typos in the above code - here is the corrected version: