This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sys.getfilesystemencoding
Type: Stage:
Components: Documentation Versions: Python 2.4
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: fwiemann, georg.brandl, naktinis, r.david.murray
Priority: normal Keywords:

Created on 2006-05-25 18:12 by fwiemann, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg28643 - (view) Author: Felix Wiemann (fwiemann) Date: 2006-05-25 18:12
There seems to be a mistake in the documentation of
sys.getfilesystemdocumentation in module-sys.html:

"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."

This does not seem to be the case on my system:

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

FYI my locale environment:

$ locale
LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE=POSIX
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=
msg28644 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-05-25 18:21
Logged In: YES 
user_id=849994

You must first set your current locale to the system default
locale; on start, Python's locale defaults to C.

After 'locale.setlocale(locale.LC_ALL, "")' the nl_langinfo
call should yield "UTF-8".
msg96095 - (view) Author: Rimvydas Naktinis (naktinis) Date: 2009-12-08 00:26
I guess this is still not answered.
Documentation (http://docs.python.org/library/sys.html) clearly states:
"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."

So in case setlocale is not yet called the actual output should look
like this (according to documentation):
>>> import sys
>>> print sys.getfilesystemencoding()
None

According to the documentation if nl_langinfo(CODESET) returns
something, then sys.getfilesystemencoding() should return the very same
thing. Apparently this is not the case in the provided code. I also get
different output on my machine.

Or am I missing something?
msg96129 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-12-08 15:32
This is surprisingly non-transparent.  However, it works as documented
in py3.

It sorta works as documented in p2, except that there's a bit left out
of the docs.

In py2, the code that results in getfilesystemencoding's value sets the
locale based on the environment, does the nl_langinfo(CODESET), and then
restores the locale to the default (C).

In py3, python initialization sets LC_CTYPE to the enviroment, and uses
that to determine getsystemfilencoding's default value.  LC_CTYPE is
left set to the environment.

This means that in py2, using locale.nl_langinfo doesn't get you the
same value that is used to set getfilesystemencoding until after you've
done a call to set the locale to the environment.  In py3, however,
locale.nl_langinfo(locale.CODESET) will by default return the value that
is used to set getfilesystemencoding's value.

If you'd like to submit a doc patch for p2, please feel free, and I'll
apply it.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43412
2009-12-08 15:32:46r.david.murraysetnosy: + r.david.murray
messages: + msg96129
2009-12-08 00:26:22naktinissetnosy: + naktinis
messages: + msg96095
2006-05-25 18:12:43fwiemanncreate