如何找出Python是用UCS-2还是UCS-4编译的?

Rei*_*ica 62 python unicode ucs2

正如标题所说的那样.

$ ./configure --help | grep -i ucs
  --enable-unicode[=ucs[24]]
Run Code Online (Sandbox Code Playgroud)

搜索官方文档,我发现了这个:

sys.maxunicode:一个整数,给出Unicode字符支持的最大代码点.其取决于配置选项,该选项指定Unicode字符是否存储为UCS-2或UCS-4.

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

该代码预计适用于Python 2.6+.

Ste*_*tef 121

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

>>> import sys
>>> print sys.maxunicode
1114111
Run Code Online (Sandbox Code Playgroud)

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

>>> import sys
>>> print sys.maxunicode
65535
Run Code Online (Sandbox Code Playgroud)

  • 对于Python 3,这不再普遍正确.请参阅https://docs.python.org/3.4/c-api/unicode.html:`自从Python 3.3中实现PEP 393以来,Unicode对象在内部使用各种表示形式`.https://www.python.org/dev/peps/pep-0393/ (2认同)
  • @ Jan-PhilipGehrcke:`deficient_unicode_build =(sys.maxunicode <0x10ffff)`适用于任何Python版本(即使在`sys.maxunicode == 0x10ffff`中使用灵活的内部表示).灵活的表示允许获得正确的结果,如ucs4在以前的版本上所做的,而在某些情况下使用的内存比ucs4少. (2认同)

Mar*_*wis 19

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
}
Run Code Online (Sandbox Code Playgroud)

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


小智 11

我有过同样的问题一次.我在我的wiki上为自己记录了这个

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

我写 -

import sys
sys.maxunicode > 65536 and 'UCS4' or 'UCS2'
Run Code Online (Sandbox Code Playgroud)

  • 对于任何想知道这是做什么的人:如果sys.maxunicode> 65536,那么它是一种旧的(<Python 2.5)做'UCS4'的方式'UCS2'. (4认同)

小智 8

sysconfig将从python的配置变量中告诉unicode大小.

可以像这样查询构建标志.

Python 2.7:

import sysconfig
sysconfig.get_config_var('Py_UNICODE_SIZE')
Run Code Online (Sandbox Code Playgroud)

Python 2.6:

import distutils
distutils.sysconfig.get_config_var('Py_UNICODE_SIZE')
Run Code Online (Sandbox Code Playgroud)