摘要:sys.getfilesystemencoding()的行为如文档所述.造成混淆的原因是setlocale(LC_CTYPE,“”)(用户的偏好)和默认C语言环境之间的差异.
该脚本始终以默认的C语言环境开头:
>>> import locale
>>> locale.nl_langinfo(locale.CODESET)
'ANSI_X3.4-1968'
但是getfilesystemencoding()使用用户的语言环境:
>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'
>>> locale.setlocale(locale.LC_CTYPE, '')
'en_US.UTF-8'
>>> locale.nl_langinfo(locale.CODESET)
'UTF-8'
$LC_CTYPE=C python -c 'import sys; print(sys.getfilesystemencoding())'
ANSI_X3.4-1968
$LC_CTYPE=C.UTF-8 python -c 'import sys; print(sys.getfilesystemencoding())'
UTF-8
where can i find the source code about setting Py_FileSystemDefaultEncoding.
Python 2.7的源代码中有两个地方:
> bltinmodule.c在Windows和OS X上指定Py_FileSystemDefaultEncoding
> Py_InitializeEx() sets it on other Unix systems-注意:setlocale(LC_CTYPE,“”)在nl_langinfo(CODESET)之前被调用,并在之后恢复回setlocale(LC_CTYPE,saved_locale).
Can you give me some advice how to search some keywords in python source code
要找到这些地方:
$hg clone https://hg.python.org/cpython && cd cpython
$hg update 2.7
>在编辑器中搜索Py_FileSystemDefaultEncoding * =正则表达式,例如:
$make TAGS # to create tags table
在Emacs中:M-x标签-搜索RET Py_FileSystemDefaultEncoding * = RET和M-,以继续搜索.