在python源代码中设置了Py_FileSystemDefaultEncoding的位置 [英] where Py_FileSystemDefaultEncoding is set in python source code

查看:146
本文介绍了在python源代码中设置了Py_FileSystemDefaultEncoding的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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:

在Unix上,根据nl_langinfo(CODESET)的结果,编码是用户的首选项;如果nl_langinfo(CODESET)失败,则编码为None.

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.

我使用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


在哪里可以找到有关设置Py_FileSystemDefaultEncoding的源代码.

where can i find the source code about setting Py_FileSystemDefaultEncoding.

Python 2.7的源代码中有两个地方:

There are two places in the source code for Python 2.7:

  • bltinmodule.c specifies Py_FileSystemDefaultEncoding on Windows and OS X
  • Py_InitializeEx() sets it on other Unix systems -- notice: setlocale(LC_CTYPE, "") is called before nl_langinfo(CODESET) and it is restored back setlocale(LC_CTYPE, saved_locale) after.

能给我一些建议,如何在python源代码中搜索一些关键字

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 *=正则表达式,例如:

  • search for Py_FileSystemDefaultEncoding *= regex in your editor e.g.:

    $ make TAGS # to create tags table
    

    在Emacs中:M-x tags-search RET Py_FileSystemDefaultEncoding *= RETM-,继续搜索.

    in Emacs: M-x tags-search RET Py_FileSystemDefaultEncoding *= RET and M-, to continue the search.

    这篇关于在python源代码中设置了Py_FileSystemDefaultEncoding的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆