如何确定Python是用UCS-2还是UCS-4编译的?

发布于 2024-08-04 17:40:15 字数 465 浏览 3 评论 0原文

正如标题所说。

$ ./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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

↙温凉少女 2024-08-11 17:40:16

另一种方法是创建一个 Unicode 数组并查看项目大小:

import array
bytes_per_char = array.array('u').itemsize

引用 array 文档

'u' 类型代码对应于 Python 的 unicode 字符。在窄 Unicode 版本上,这是 2 字节,在宽版本上,这是 4 字节。

请注意,从 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:

import array
bytes_per_char = array.array('u').itemsize

Quote from the array docs:

The 'u' typecode corresponds to Python’s unicode character. On narrow Unicode builds this is 2-bytes, on wide builds this is 4-bytes.

Note that the distinction between narrow and wide Unicode builds is dropped from Python 3.3 onward, see PEP393. The 'u' typecode for array is deprecated since 3.3 and scheduled for removal in Python 4.0.

山色无中 2024-08-11 17:40:15

当使用 --enable-unicode=ucs4 构建时:

>>> import sys
>>> print sys.maxunicode
1114111

当使用 --enable-unicode=ucs2 构建时:

>>> import sys
>>> print sys.maxunicode
65535

When built with --enable-unicode=ucs4:

>>> import sys
>>> print sys.maxunicode
1114111

When built with --enable-unicode=ucs2:

>>> import sys
>>> print sys.maxunicode
65535
沉溺在你眼里的海 2024-08-11 17:40:15

对于 UCS-2,它是 0xFFFF(或 65535);对于 UCS-4,它是 0x10FFFF(或 1114111):

Py_UNICODE
PyUnicode_GetMax(void)
{
#ifdef Py_UNICODE_WIDE
    return 0x10FFFF;
#else
    /* This is actually an illegal character, so it should
       not be passed to unichr. */
    return 0xFFFF;
#endif
}

UCS-4 模式中的最大字符由 UTF-16 中可表示的最大值定义。

It's 0xFFFF (or 65535) for UCS-2, and 0x10FFFF (or 1114111) for UCS-4:

Py_UNICODE
PyUnicode_GetMax(void)
{
#ifdef Py_UNICODE_WIDE
    return 0x10FFFF;
#else
    /* This is actually an illegal character, so it should
       not be passed to unichr. */
    return 0xFFFF;
#endif
}

The maximum character in UCS-4 mode is defined by the maxmimum value representable in UTF-16.

海的爱人是光 2024-08-11 17:40:15

我曾经遇到过同样的问题。我在我的 wiki 上为自己记录了它

http ://arcoleo.org/dsawiki/Wiki.jsp?page=Python%20UTF%20-%20UCS2%20or%20UCS4

我写了 -

import sys
sys.maxunicode > 65536 and 'UCS4' or 'UCS2'

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 -

import sys
sys.maxunicode > 65536 and 'UCS4' or 'UCS2'
兮子 2024-08-11 17:40:15

sysconfig 将从 python 的配置变量中得知 unicode 大小。

可以像这样查询 buildflags。

Python 2.7:

import sysconfig
sysconfig.get_config_var('Py_UNICODE_SIZE')

Python 2.6:

import distutils
distutils.sysconfig.get_config_var('Py_UNICODE_SIZE')

sysconfig will tell the unicode size from the configuration variables of python.

The buildflags can be queried like this.

Python 2.7:

import sysconfig
sysconfig.get_config_var('Py_UNICODE_SIZE')

Python 2.6:

import distutils
distutils.sysconfig.get_config_var('Py_UNICODE_SIZE')
执笏见 2024-08-11 17:40:15

我遇到了同样的问题,并发现了一段半官方的代码,它完全可以做到这一点,并且对于有同样问题的人来说可能会很有趣: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文