Where Py_FileSystemDefaultEncoding is installed in python source code

I am wondering how the python source code set the value to Py_FileSystemDefaultEncoding. And I get a strange thing.

Since the python doc about sys.getfilesystemencoding () said that:

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

I am using python 2.7.6

`` ``

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

`` ``
Here's the question: why is the value of getfilesystemencoding () different from the value of locale.nl_landinfo (), as the document says that getfilesystemencoding () comes from locale.nl_landinfo ().

Here is the output of the locale command 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= 
+3
python locale
source share
1 answer

Summary: sys.getfilesystemencoding() behaves as documented. The confusion is due to the difference between setlocale(LC_CTYPE, "") (user preference) and the default locale C.


The script always starts with the default locale C:

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

But getfilesystemencoding() uses a custom language:

 >>> import sys >>> sys.getfilesystemencoding() 'UTF-8' >>> locale.setlocale(locale.LC_CTYPE, '') 'en_US.UTF-8' >>> locale.nl_langinfo(locale.CODESET) 'UTF-8' 

An empty string as the locale name selects the locale based on the user selecting 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 for setting up Py_FileSystemDefaultEncoding.

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


Can you give me some tips on finding some keywords in python source code.

To find these places:

  • clone Python 2.7 source code :

     $ hg clone https://hg.python.org/cpython && cd cpython $ hg update 2.7 
  • look for Py_FileSystemDefaultEncoding *= regex in the editor, for example:

     $ make TAGS # to create tags table 

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

+4
source share

All Articles