如何使用 boost.python 提取 unicode 字符串
当我执行 extract
时,代码似乎会崩溃
有人知道如何解决这个问题吗?
It seems that the code will crash when I do extract<const char*>("a unicode string")
Anyone know how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可以编译并为我工作,使用您的示例字符串并使用Python 2.x:
您可以编写特定的 from-python 转换器,如果您希望自动转换
PyUnicode
(@Python2.x)到const wchar_t*
或来自 ICU 的类型(这似乎是在 C++ 上处理 Unicode 的常见建议)。如果您希望完全支持 ASCII 范围之外的 unicode 字符(例如,
á
、ç
或ï
等重音字符,您需要编写 from-python 转换器,请注意,如果您希望同时支持 Python 2.x 和 3.x,则必须分别完成。 <一href="http://docs.python.org/3/howto/cporting.html" rel="nofollow">PyUnicode 类型已弃用,现在字符串类型作为PyUnicode
用于 Python 2.x没有任何。#if PY_VERSION_HEX >= 0x03000000
无法处理的[edit]
上面的注释是错误的,请注意,由于 Python 3.x 将 unicode 字符串视为普通字符串,因此
boost::python
会将其包装到boost 中。 ::python::str
对象。在这种情况下,我还没有验证如何处理这些对象。This compiles and works for me, with your example string and using Python 2.x:
You can write a specific from-python converter, if you wish to auto-convert
PyUnicode
(@Python2.x) toconst wchar_t*
or to a type from ICU (that seems to be the common recommendation for dealing with Unicode on C++).If you want full support to unicode characters which are not in the ASCII range (for example, accented characters such as
á
,ç
orï
, you will need to write the from-python converter. Note this will have to be done separately for Python 2.x and 3.x, if you wish to support both. For Python 3.x, the PyUnicode type was deprecated and now the string type works asPyUnicode
used to for Python 2.x.Nothing that a couple of.#if PY_VERSION_HEX >= 0x03000000
cannot handle[edit]
The above comment was wrong. Note that, since Python 3.x treats unicode strings as normal strings,
boost::python
will wrap that intoboost::python::str
objects. I have not verified how those are handled w.r.t. unicode translation in this case.你有没有尝试过
或者
Have you tried
or