其中Py_FileSystemDefaultEncoding是在python源代码中设置的

2024-05-02 16:50:42 发布

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

我很好奇python源代码如何设置Py_FileSystemDefaultEncoding的值。我收到了一件奇怪的事。在

因为python doc关于sys.getfilesystemcoding()说:

On Unix, the encoding is the user’s preference according to the result of nl_langinfo(CODESET), or None if the nl_langinfo(CODESET) failed.

我使用python2.7.6

```

>>>import sys
>>>sys.getfilesystemencoding()
>>>'UTF-8'
>>>import locale
>>>locale.nl_langinfo(locale.CODESET)
>>>'ANSI_X3.4-1968'

```
这里有一个问题:为什么getFileSystemCoding()的值与locale.nl\u landinfo()因为doc说getfilesystemencoding()是从locale.nl\u landinfo(). 在

以下是我的终端中的locale命令输出:

^{pr2}$

Tags: thepyimportdoc源代码onnlsys
1条回答
网友
1楼 · 发布于 2024-05-02 16:50:42

摘要:sys.getfilesystemencoding()的行为与文档中的相同。这种混淆是由于setlocale(LC_CTYPE, "")(用户的首选项)和默认的C语言环境之间的差异造成的。在


脚本始终以默认的C语言环境开头:

>>> import locale
>>> locale.nl_langinfo(locale.CODESET)
'ANSI_X3.4-1968'

但是getfilesystemencoding()使用用户的区域设置:

^{pr2}$

空字符串作为区域设置名称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

where can i find the source code about setting Py_FileSystemDefaultEncoding.

Python 2.7的源代码中有两个位置:

  • ^{}在Windows和OS X上指定Py_FileSystemDefaultEncoding
  • ^{} 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

要找到这些地方:

  • clone Python 2.7 source code

    $ hg clone https://hg.python.org/cpython && cd cpython
    $ hg update 2.7
    
  • 在编辑器中搜索Py_FileSystemDefaultEncoding *=regex,例如:

    $ make TAGS # to create tags table
    

    在Emacs中:M-x tags-search RET Py_FileSystemDefaultEncoding *= RET和{}继续搜索。

相关问题 更多 >