开发者

how to extract a unicode string with boost.python

开发者 https://www.devze.com 2023-03-19 00:49 出处:网络
It seems that the code开发者_运维技巧 will crash when I do extract<const char*>(\"a unicode string\")

It seems that the code开发者_运维技巧 will crash when I do extract<const char*>("a unicode string")

Anyone know how to solve this?


This compiles and works for me, with your example string and using Python 2.x:

void process_unicode(boost::python::object u) {
  using namespace boost::python;
  const char* value = extract<const char*>(str(u).encode("utf-8"));
  std::cout << "The string value is '"<< value << "'" << std::endl;
}

You can write a specific from-python converter, if you wish to auto-convert PyUnicode (@Python2.x) to const 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 as PyUnicode 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 into boost::python::str objects. I have not verified how those are handled w.r.t. unicode translation in this case.


Have you tried

extract<std::string>("a unicode string").c_str() 

or

extract<wchar_t*>(...)
0

精彩评论

暂无评论...
验证码 换一张
取 消