问题描述
我对python源代码如何设置Py_FileSystemDefaultEncoding的值感到好奇.而且我收到了一件奇怪的事情.
i am curious about how python source code set the value of Py_FileSystemDefaultEncoding. And i have receive a strange thing.
因为有关sys.getfilesystemencoding()的python doc
Since python doc about sys.getfilesystemencoding() said that:
我使用python 2.7.6
i use python 2.7.6
```
>>>import sys
>>>sys.getfilesystemencoding()
>>>'UTF-8'
>>>import locale
>>>locale.nl_langinfo(locale.CODESET)
>>>'ANSI_X3.4-1968'
```
这里的问题是:为什么getfilesystemencoding()的值与locale.nl_landinfo()的值不同,因为文档说getfilesystemencoding()是从locale.nl_landinfo()派生的.
```
Here is the question: why the value of getfilesystemencoding() is different from the value of locale.nl_landinfo() since the doc says that getfilesystemencoding() is derived from locale.nl_landinfo().
这是我的终端中的语言环境命令输出:
Here is the locale command output in my terminal:
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=
推荐答案
摘要:sys.getfilesystemencoding()
的行为与所记录的一样.造成混淆的原因是setlocale(LC_CTYPE, "")
(用户的偏好)和默认C语言环境之间的差异.
Summary: sys.getfilesystemencoding()
behaves as documented. The confusion is due to the difference between setlocale(LC_CTYPE, "")
(user's preference) and the default C locale.
脚本始终以默认的C语言环境开头:
The script always starts with the default C locale:
>>> import locale
>>> locale.nl_langinfo(locale.CODESET)
'ANSI_X3.4-1968'
但是getfilesystemencoding()
使用用户的语言环境:
But getfilesystemencoding()
uses user's locale:
>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'
>>> locale.setlocale(locale.LC_CTYPE, '')
'en_US.UTF-8'
>>> locale.nl_langinfo(locale.CODESET)
'UTF-8'
空字符串作为区域设置名称选择基于区域设置的名称用户选择合适的环境变量.
Empty string as a locale name selects a locale based on the user choice of the appropriate environment variables.
$ 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
Python 2.7的源代码中有两个地方:
There are two places in the source code for Python 2.7:
-
bltinmodule.c
在Windows和OS X -
Py_InitializeEx()
在其他Unix系统上进行设置-注意:setlocale(LC_CTYPE, "")
在nl_langinfo(CODESET)
之前被调用,并在setlocale(LC_CTYPE, saved_locale)
之后被恢复.
bltinmodule.c
specifiesPy_FileSystemDefaultEncoding
on Windows and OS XPy_InitializeEx()
sets it on other Unix systems -- notice:setlocale(LC_CTYPE, "")
is called beforenl_langinfo(CODESET)
and it is restored backsetlocale(LC_CTYPE, saved_locale)
after.
要找到这些地方:
$ hg clone https://hg.python.org/cpython && cd cpython
$ hg update 2.7
在编辑器中搜索Py_FileSystemDefaultEncoding *=
正则表达式,例如:
search for Py_FileSystemDefaultEncoding *=
regex in your editor e.g.:
$ make TAGS # to create tags table
在Emacs中:M-x tags-search RET Py_FileSystemDefaultEncoding *= RET
和M-,
继续搜索.
in Emacs: M-x tags-search RET Py_FileSystemDefaultEncoding *= RET
and M-,
to continue the search.
这篇关于在python源代码中设置了Py_FileSystemDefaultEncoding的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!