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

2024-04-24 09:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

正如标题所说。

$ ./configure --help | grep -i ucs
  --enable-unicode[=ucs[24]]

在搜索官方文档时,我发现:

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.

这里不清楚的是-哪个值对应于UCS-2和UCS-4。

这段代码有望在Python2.6+上运行。


Tags: the文档an标题官方enableconfiguresys
3条回答

我也有过同样的问题。我把它记录在我的维基上

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

我写了-

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

对于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中表示的最大值定义。

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

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

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

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

相关问题 更多 >