如何确定Python是用UCS-2还是UCS-4编译的?
正如标题所说。
$ ./configure --help | grep -i ucs
--enable-unicode[=ucs[24]]
搜索了官方文档,发现了这样的内容:
sys.maxunicode :一个整数,给出 最大支持的代码点 统一码字符。这个的价值 取决于配置选项 指定是否使用 Unicode 字符存储为 UCS-2 或 UCS-4。
这里不清楚的是 - 哪个值对应于 UCS-2 和 UCS-4。
该代码预计可在 Python 2.6+ 上运行。
Just what the title says.
$ ./configure --help | grep -i ucs
--enable-unicode[=ucs[24]]
Searching the official documentation, I found this:
sys.maxunicode: An integer giving the
largest supported code point for a
Unicode character. The value of this
depends on the configuration option
that specifies whether Unicode
characters are stored as UCS-2 or
UCS-4.
What is not clear here is - which value(s) correspond to UCS-2 and UCS-4.
The code is expected to work on Python 2.6+.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另一种方法是创建一个 Unicode 数组并查看项目大小:
引用
array
文档:请注意,从 Python 3.3 开始,窄 Unicode 版本和宽 Unicode 版本之间的区别已被删除,请参阅 PEP393< /a>.
array
的'u'
类型代码自 3.3 起已弃用,并计划在 Python 4.0 中删除。Another way is to create an Unicode array and look at the itemsize:
Quote from the
array
docs:Note that the distinction between narrow and wide Unicode builds is dropped from Python 3.3 onward, see PEP393. The
'u'
typecode forarray
is deprecated since 3.3 and scheduled for removal in Python 4.0.65535 是 UCS-2:
65535 is UCS-2:
当使用 --enable-unicode=ucs4 构建时:
当使用 --enable-unicode=ucs2 构建时:
When built with --enable-unicode=ucs4:
When built with --enable-unicode=ucs2:
对于 UCS-2,它是 0xFFFF(或 65535);对于 UCS-4,它是 0x10FFFF(或 1114111):
UCS-4 模式中的最大字符由 UTF-16 中可表示的最大值定义。
It's 0xFFFF (or 65535) for UCS-2, and 0x10FFFF (or 1114111) for UCS-4:
The maximum character in UCS-4 mode is defined by the maxmimum value representable in UTF-16.
我曾经遇到过同样的问题。我在我的 wiki 上为自己记录了它
http ://arcoleo.org/dsawiki/Wiki.jsp?page=Python%20UTF%20-%20UCS2%20or%20UCS4
我写了 -
I had this same issue once. I documented it for myself on my wiki at
http://arcoleo.org/dsawiki/Wiki.jsp?page=Python%20UTF%20-%20UCS2%20or%20UCS4
I wrote -
sysconfig 将从 python 的配置变量中得知 unicode 大小。
可以像这样查询 buildflags。
Python 2.7:
Python 2.6:
sysconfig will tell the unicode size from the configuration variables of python.
The buildflags can be queried like this.
Python 2.7:
Python 2.6:
我遇到了同样的问题,并发现了一段半官方的代码,它完全可以做到这一点,并且对于有同样问题的人来说可能会很有趣: https://bitbucket.org/pypa/wheel/src/cf4e2d98ecb1f168c50a6de496959b4a10c6b122/wheel/ pep425tags.py?at=default&fileviewer=file-view-default#pep425tags.py-83:89。
它来自wheel项目,需要检查python是否是用ucs-2或ucs-4编译的,因为它会更改生成的二进制文件的名称。
I had the same issue and found a semi-official piece of code that does exactly that and may be interesting for people with the same issue: https://bitbucket.org/pypa/wheel/src/cf4e2d98ecb1f168c50a6de496959b4a10c6b122/wheel/pep425tags.py?at=default&fileviewer=file-view-default#pep425tags.py-83:89.
It comes from the wheel project which needs to check if the python is compiled with ucs-2 or ucs-4 because it will change the name of the binary file generated.