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: Py_FileSystemDefaultEncoding should be updated on locale.setlocale()
Type: Stage: resolved
Components: Unicode Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, loewis, mvo, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2012-10-08 11:53 by mvo, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
alllow-chaning-of-Py_FileSystemDefaultEncoding.diff mvo, 2012-10-08 11:53
Messages (4)
msg172373 - (view) Author: Michael Vogt (mvo) * Date: 2012-10-08 11:53
The Py_FileSystemDefaultEncoding is very static right now and only set on interpreter statup AFAICT. There appears to be no way to switch that later.

I think that Py_FileSystemDefaultEncoding should get updated when locale.setlocale() is run automatically and attach a proof-of-concept patch for this. 

The reason is that if a python application is started without a environment (dbus activation will do that for example) its impossible to work with utf8 encoded filenames. The only workaround is to setup a environment and then os.execv() which seems not ideal.
msg172389 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-10-08 15:43
It actually *is* possible to work with UTF-8-encoded file names even in an ASCII locale. It should work automatically, using the PEP 383 mechanism.

I'm -0 on allowing changes to the file system encoding. It may lead to mojibake, if some file names were read from the file system before the locale was changed, and then accessed later.

I don't understand why you think that it is, in some cases, impossible to pass environment variables. In case of dbus activiation, it is surely possible to pass environment variables somehow. The easiest solution should be to put

#!/bin/sh
s=''''
LANG=en_US.UTF-8 exec /usr/bin/python $0 "$@"
'''

into the beginning of the script.
msg172390 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-10-08 15:46
> I think that Py_FileSystemDefaultEncoding should get updated when locale.setlocale() is run automatically

This is not a good idea. The two following expressions must be True on UNIX:
os.fsencode(os.fsdecode(name)) == name
os.fsdecode(os.fsencode(name)) == name

Said differently: if you change the filesystem encoding, you cannot
encode or decode (depending on the type, str or bytes) back the
filename.

For example, sys.path is a list of filenames filled in an early stage
of Python initialization. If you change the filesystem encoding after
this list is filled, you may fail to import modules.

See also this thread:
http://mail.python.org/pipermail/python-dev/2010-October/104509.html

--

If you want to change the locale encoding, please do it outside Python
(before running Python), and then don't change it again! Example on
Ubuntu:

$ LC_CTYPE=fr_FR.iso885915@euro python -c 'import sys;
print(sys.getfilesystemencoding())'
ISO-8859-15
msg172476 - (view) Author: Michael Vogt (mvo) * Date: 2012-10-09 12:02
Thanks for this detailed explaination! I will workaround this outside of python (that is easy ;) - I just thought that it would be a good idea to be able to change the fsencoding (and therefore send the patch), but in the light of e.g. sys.path it seems to be indeed a pretty bad idea.
History
Date User Action Args
2022-04-11 14:57:36adminsetgithub: 60366
2012-10-09 12:02:16mvosetmessages: + msg172476
2012-10-08 16:45:36r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: resolved
2012-10-08 15:46:36vstinnersetmessages: + msg172390
2012-10-08 15:43:22loewissetnosy: + loewis
messages: + msg172389
2012-10-08 15:10:47r.david.murraysetnosy: + pitrou, vstinner
2012-10-08 11:53:07mvocreate