python - 使用 UCS-4 的 Python Swig 绑定(bind)

标签 python unicode swig

有谁知道是否有办法让 SWIG 将字符串编码为 Python 的 UCS-4? SWIG 文档指出,使用类型映射可以实现这一点,但未提供任何其他详细信息或示例。

就上下文而言,我正在使用一组 Python 脚本扩展 Blender 3D 软件。我们需要将这些脚本与各种机器人软件连接起来,我们使用 SWIG 来编译 Python 库。 Blender 使用自己的 Python 3.2 预编译 --with-wide-unicode 选项,因此它使用 UCS-4 unicode 字符串。 但是,默认 SWIG 将字符串编码为 UCS-2,因此在与 Blender 交互时,我总是会收到如下错误:“ undefined symbol :PyUnicodeUCS2_*”。

最佳答案

这是来自 SWIG 文档,也许你已经看到了:

At this time, SWIG provides limited support for Unicode and wide-character strings (the C wchar_t type). Some languages provide typemaps for wchar_t, but bear in mind these might not be portable across different operating systems. This is a delicate topic that is poorly understood by many programmers and not implemented in a consistent manner across languages. For those scripting languages that provide Unicode support, Unicode strings are often available in an 8-bit representation such as UTF-8 that can be mapped to the char * type (in which case the SWIG interface will probably work). If the program you are wrapping uses Unicode, there is no guarantee that Unicode characters in the target language will use the same internal representation (e.g., UCS-2 vs. UCS-4). You may need to write some special conversion functions.

所以听起来你应该将它映射到 char* ,然后在必要时弄清楚如何手动转换它。听起来一开始就很乱。

关于python - 使用 UCS-4 的 Python Swig 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7090957/

相关文章:

python - 列表的聚合列

c++ - 使用 ATL 将 LPTSTR 转换为 LPWSTR

python - SQLite、python、unicode 和非 utf 数据

python - SWIG 编译的 C++/Python 代码的测试覆盖率

python - Swig Python 模块中的 C++ 内存泄漏

python - typedef 不适用于 SWIG(python 包装 C++ 代码)

python - 如何创建 XML 模板?

python - Keras:如何在训练期间在自定义生成器中获得模型预测(或最后一层输出)?

php - 在PHP中计算带有半角假名的日语多字节字符串的长度

python - 如何找到信号周期(自相关 vs 快速傅立叶变换 vs 功率谱密度)?