Comment 11 for bug 1844684

Revision history for this message
Victor Stinner (vstinner) wrote :

About the "RuntimeError: Failed to change the filesystem default encoding" error in _patch_filesystem_default_encoding(), IMO brz should re-exec itself (os.execv) to enabled Python 3.7 UTF-8 Mode:
https://www.python.org/dev/peps/pep-0540/

Example:
---
import os
import sys

# FIXME: skip this code on Python 3.6 and older ;-)
if not sys.flags.utf8_mode:
    print("re-exec!")
    argv = [sys.executable, "-X", "utf8"] + sys.argv
    try:
        os.execv(argv[0], argv)
    except Exception:
        print("os.execv failed, too bad: %r" % exc)
        sys.exit(1)

print("UTF-8 Mode:", sys.flags.utf8_mode)
print("filesystem encoding:", sys.getfilesystemencoding())
print("argv:", sys.argv)
---

Output:
---
$ python3 script.py arg1 arg2
re-exec!
UTF-8 Mode: 1
filesystem encoding: utf-8
argv: ['x.py', 'arg1', 'arg2']
---

With the C and POSIX locales, UTF-8 is enabled by default, no need to re-exec:
---
vstinner@apu$ LANG= python3 x.py arg1 arg2
UTF-8 Mode: 1
filesystem encoding: utf-8
argv: ['x.py', 'arg1', 'arg2']
---