[Python-checkins] bpo-34523: Fix config_init_fs_encoding() for ASCII (GH-10232)

Victor Stinner webhook-mailer at python.org
Tue Oct 30 07:58:14 EDT 2018


https://github.com/python/cpython/commit/905f1ace5f7424e314ca7bed997868a2a3044839
commit: 905f1ace5f7424e314ca7bed997868a2a3044839
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-10-30T12:58:10+01:00
summary:

bpo-34523: Fix config_init_fs_encoding() for ASCII (GH-10232)

* bpo-34523, bpo-34403: Fix config_init_fs_encoding(): it now uses
  ASCII if _Py_GetForceASCII() is true.
* Fix a regression of commit b2457efc78b74a1d6d1b77d11a939e886b8a4e2c.
* Fix also a memory leak: get_locale_encoding() already allocates
  memory, no need to duplicate the string.

files:
M Include/coreconfig.h
M Python/coreconfig.c

diff --git a/Include/coreconfig.h b/Include/coreconfig.h
index ef043ab02df6..83c4e7ee4dbe 100644
--- a/Include/coreconfig.h
+++ b/Include/coreconfig.h
@@ -75,6 +75,8 @@ typedef struct {
          highest priority;
        * PYTHONIOENCODING environment variable;
        * The UTF-8 Mode uses UTF-8/surrogateescape;
+       * If Python forces the usage of the ASCII encoding (ex: C locale
+         or POSIX locale on FreeBSD or HP-UX), use ASCII/surrogateescape;
        * locale encoding: ANSI code page on Windows, UTF-8 on Android,
          LC_CTYPE locale encoding on other platforms;
        * On Windows, "surrogateescape" error handler;
diff --git a/Python/coreconfig.c b/Python/coreconfig.c
index fae32e533aa9..a82175e4fd26 100644
--- a/Python/coreconfig.c
+++ b/Python/coreconfig.c
@@ -1164,13 +1164,17 @@ config_init_fs_encoding(_PyCoreConfig *config)
         }
     }
 
-    /* Windows defaults to utf-8/surrogatepass (PEP 529) */
+    /* Windows defaults to utf-8/surrogatepass (PEP 529).
+
+       Note: UTF-8 Mode takes the same code path and the Legacy Windows FS
+             encoding has the priortiy over UTF-8 Mode. */
     if (config->filesystem_encoding == NULL) {
         config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
         if (config->filesystem_encoding == NULL) {
             return _Py_INIT_NO_MEMORY();
         }
     }
+
     if (config->filesystem_errors == NULL) {
         config->filesystem_errors = _PyMem_RawStrdup("surrogatepass");
         if (config->filesystem_errors == NULL) {
@@ -1178,30 +1182,28 @@ config_init_fs_encoding(_PyCoreConfig *config)
         }
     }
 #else
-    if (config->utf8_mode) {
-        /* UTF-8 Mode use: utf-8/surrogateescape */
-        if (config->filesystem_encoding == NULL) {
+    if (config->filesystem_encoding == NULL) {
+        if (config->utf8_mode) {
+            /* UTF-8 Mode use: utf-8/surrogateescape */
             config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
-            if (config->filesystem_encoding == NULL) {
-                return _Py_INIT_NO_MEMORY();
-            }
+            /* errors defaults to surrogateescape above */
         }
-        /* errors defaults to surrogateescape above */
-    }
-
-    if (config->filesystem_encoding == NULL) {
-        /* macOS and Android use UTF-8, other platforms use
-           the locale encoding. */
-        char *locale_encoding;
+        else if (_Py_GetForceASCII()) {
+            config->filesystem_encoding = _PyMem_RawStrdup("ascii");
+        }
+        else {
+            /* macOS and Android use UTF-8,
+               other platforms use the locale encoding. */
 #if defined(__APPLE__) || defined(__ANDROID__)
-        locale_encoding = "UTF-8";
+            config->filesystem_encoding = _PyMem_RawStrdup("utf-8");
 #else
-        _PyInitError err = get_locale_encoding(&locale_encoding);
-        if (_Py_INIT_FAILED(err)) {
-            return err;
-        }
+            _PyInitError err = get_locale_encoding(&config->filesystem_encoding);
+            if (_Py_INIT_FAILED(err)) {
+                return err;
+            }
 #endif
-        config->filesystem_encoding = _PyMem_RawStrdup(locale_encoding);
+        }
+
         if (config->filesystem_encoding == NULL) {
             return _Py_INIT_NO_MEMORY();
         }



More information about the Python-checkins mailing list