Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (révision 84163) +++ Python/pythonrun.c (copie de travail) @@ -134,18 +134,13 @@ return flag; } -#if defined(HAVE_LANGINFO_H) && defined(CODESET) static char* -get_codeset(void) +get_codec_name(const char *encoding) { - char* codeset, *name_str; + char *name_utf8, *name_str; PyObject *codec, *name = NULL; - codeset = nl_langinfo(CODESET); - if (!codeset || codeset[0] == '\0') - return NULL; - - codec = _PyCodec_Lookup(codeset); + codec = _PyCodec_Lookup(encoding); if (!codec) goto error; @@ -154,18 +149,28 @@ if (!name) goto error; - name_str = _PyUnicode_AsString(name); + name_utf8 = _PyUnicode_AsString(name); if (name == NULL) goto error; - codeset = strdup(name_str); + name_str = strdup(name_utf8); Py_DECREF(name); - return codeset; + return name_str; error: Py_XDECREF(codec); Py_XDECREF(name); return NULL; } + +#if defined(HAVE_LANGINFO_H) && defined(CODESET) +static char* +get_codeset(void) +{ + char* codeset = nl_langinfo(CODESET); + if (!codeset || codeset[0] == '\0') + return NULL; + return get_codec_name(codeset); +} #endif void @@ -709,12 +714,17 @@ char *codeset; if (Py_FileSystemDefaultEncoding == NULL) { - /* On Unix, set the file system encoding according to the - user's preference, if the CODESET names a well-known - Python codec, and Py_FileSystemDefaultEncoding isn't - initialized by other means. Also set the encoding of - stdin and stdout if these are terminals. */ - codeset = get_codeset(); + const char *env_encoding = Py_GETENV("PYTHONFSENCODING"); + if (env_encoding != NULL) + codeset = get_codec_name(env_encoding); + else { + /* On Unix, set the file system encoding according to the + user's preference, if the CODESET names a well-known + Python codec, and Py_FileSystemDefaultEncoding isn't + initialized by other means. Also set the encoding of + stdin and stdout if these are terminals. */ + codeset = get_codeset(); + } if (codeset != NULL) { Py_FileSystemDefaultEncoding = codeset; Py_HasFileSystemDefaultEncoding = 0; Index: Doc/using/cmdline.rst =================================================================== --- Doc/using/cmdline.rst (révision 84162) +++ Doc/using/cmdline.rst (copie de travail) @@ -442,6 +442,14 @@ import of source modules. +.. envvar:: PYTHONFSENCODING + + Overrides the encoding used for the filesystem encoding (see + :func:`sys.getfilesystemencoding`). + + .. versionadded:: 3.2 + + .. envvar:: PYTHONIOENCODING Overrides the encoding used for stdin/stdout/stderr, in the syntax Index: Doc/whatsnew/3.2.rst =================================================================== --- Doc/whatsnew/3.2.rst (révision 84162) +++ Doc/whatsnew/3.2.rst (copie de travail) @@ -232,6 +232,15 @@ * Stub + +Unicode +======= + +The filesystem encoding can be specified by setting the +:envvar:`PYTHONFSENCODING` environment varible before running the intepreter. +The value should be a string in the form ````, e.g. ``utf-8``. + + IDLE ==== Index: Lib/test/test_pep277.py =================================================================== --- Lib/test/test_pep277.py (révision 84162) +++ Lib/test/test_pep277.py (copie de travail) @@ -43,7 +43,7 @@ # Is it Unicode-friendly? if not os.path.supports_unicode_filenames: - fsencoding = sys.getfilesystemencoding() or sys.getdefaultencoding() + fsencoding = sys.getfilesystemencoding() try: for name in filenames: name.encode(fsencoding) Index: Lib/test/test_sys.py =================================================================== --- Lib/test/test_sys.py (révision 84162) +++ Lib/test/test_sys.py (copie de travail) @@ -872,7 +872,13 @@ fs_encoding = sys.getfilesystemencoding() check_fsencoding(fs_encoding) - # Even in C locale + def get_fsencoding(env): + output = subprocess.check_output( + [sys.executable, "-c", + "import sys; print(sys.getfilesystemencoding())"], + env=env) + return output.rstrip().decode('ascii') + try: sys.executable.encode('ascii') except UnicodeEncodeError: @@ -880,15 +886,22 @@ # see issue #8611 pass else: + # Even in C locale env = os.environ.copy() env['LANG'] = 'C' - output = subprocess.check_output( - [sys.executable, "-c", - "import sys; print(sys.getfilesystemencoding())"], - env=env) - fs_encoding = output.rstrip().decode('ascii') - check_fsencoding(fs_encoding) + check_fsencoding(get_fsencoding(env)) + self.assertEqual(fs_encoding, 'ascii') + # Filesystem encoding is hardcoded on Windows and Mac OS X + if sys.platform not in ('win32', 'darwin'): + for encoding in ('ascii', 'cp850', 'iso8859-1', 'utf-8'): + env = os.environ.copy() + env['PYTHONFSENCODING'] = encoding + fs_encoding = get_fsencoding(env) + check_fsencoding(fs_encoding, encoding) + self.assertEqual(fs_encoding, encoding) + + def test_setfilesystemencoding(self): old = sys.getfilesystemencoding() try: