Program Club

Python이 UCS-2 또는 UCS-4로 컴파일되었는지 확인하는 방법은 무엇입니까?

proclub 2020. 11. 29. 12:32
반응형

Python이 UCS-2 또는 UCS-4로 컴파일되었는지 확인하는 방법은 무엇입니까?


제목이 말하는 그대로.

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

공식 문서를 검색하다가 다음을 발견했습니다.

sys.maxunicode : 유니 코드 문자에 대해 지원되는 가장 큰 코드 포인트를 제공하는 정수입니다. 은 유니 코드 문자가 UCS-2 또는 UCS-4로 저장되는지 여부를 지정하는 구성 옵션에 따라 다릅니다.

여기서 명확하지 않은 것은 UCS-2 및 UCS-4에 해당하는 값입니다.

코드는 Python 2.6 이상에서 작동 할 것으로 예상됩니다.


--enable-unicode = ucs4로 빌드 된 경우 :

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

--enable-unicode = ucs2로 빌드 된 경우 :

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

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으로 표현 가능한 최대 값으로 정의됩니다.


나는 한 번 같은 문제가 있었다. 내 위키에 문서화했습니다.

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

나는 썼다-

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

sysconfig 는 파이썬의 구성 변수에서 유니 코드 크기를 알려줍니다.

빌드 플래그는 다음과 같이 쿼리 할 수 ​​있습니다.

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')

나는 같은 문제가 있었고 정확히 그것을 수행하고 같은 문제를 가진 사람들에게 흥미로울 수있는 반 공식 코드를 발견했습니다 : https://bitbucket.org/pypa/wheel/src/cf4e2d98ecb1f168c50a6de496959b4a10c6b122/wheel/pep425tags.py ? = 기본 및 파일 뷰어에서 = 파일보기 기본 # pep425tags.py-83 : 89 .

생성 된 바이너리 파일의 이름을 변경하기 때문에 파이썬이 ucs-2 또는 ucs-4로 컴파일되었는지 확인해야하는 휠 프로젝트에서 나옵니다.


또 다른 방법은 유니 코드 배열을 만들고 항목 화를 보는 것입니다.

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.


65535 is UCS-2:

Thus code point U+0000 is encoded as the number 0, and U+FFFF is encoded as 65535 (which is FFFF16 in hexadecimal).

참고URL : https://stackoverflow.com/questions/1446347/how-to-find-out-if-python-is-compiled-with-ucs-2-or-ucs-4

반응형