[Python-checkins] bpo-34523: Fix C locale coercion on FreeBSD CURRENT (GH-10672) (GH-10673)

Victor Stinner webhook-mailer at python.org
Fri Nov 23 07:37:46 EST 2018


https://github.com/python/cpython/commit/f6e323ce322cf54b1a9e9252b13f93ebc28b5c24
commit: f6e323ce322cf54b1a9e9252b13f93ebc28b5c24
branch: 3.7
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-11-23T13:37:42+01:00
summary:

bpo-34523: Fix C locale coercion on FreeBSD CURRENT (GH-10672) (GH-10673)

bpo-34523, bpo-35290: C locale coercion now resets the Python
internal "force ASCII" mode. This change fix the filesystem encoding
on FreeBSD CURRENT, which has a new "C.UTF-8" locale, when
the UTF-8 mode is disabled.

Add _Py_ResetForceASCII(): _Py_SetLocaleFromEnv() now calls it.

(cherry picked from commit 353933e712b6c7f7ba9a9a50bd5bd472db7c35d0)

files:
M Include/fileutils.h
M Python/fileutils.c
M Python/pylifecycle.c

diff --git a/Include/fileutils.h b/Include/fileutils.h
index d75189a95c50..419d49ab75b0 100644
--- a/Include/fileutils.h
+++ b/Include/fileutils.h
@@ -185,6 +185,13 @@ PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
 
 #ifdef Py_BUILD_CORE
 PyAPI_FUNC(int) _Py_GetForceASCII(void);
+
+/* Reset "force ASCII" mode (if it was initialized).
+
+   This function should be called when Python changes the LC_CTYPE locale,
+   so the "force ASCII" mode can be detected again on the new locale
+   encoding. */
+PyAPI_FUNC(void) _Py_ResetForceASCII(void);
 #endif
 
 #ifdef __cplusplus
diff --git a/Python/fileutils.c b/Python/fileutils.c
index b77e490ce236..5e71d375260a 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -191,6 +191,12 @@ _Py_GetForceASCII(void)
 }
 
 
+void
+_Py_ResetForceASCII(void)
+{
+    force_ascii = -1;
+}
+
 
 static int
 encode_ascii(const wchar_t *text, char **str,
@@ -252,6 +258,12 @@ _Py_GetForceASCII(void)
 {
     return 0;
 }
+
+void
+_Py_ResetForceASCII(void)
+{
+    /* nothing to do */
+}
 #endif   /* !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS) */
 
 
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 86f95de8336f..4b08c9c2b2ce 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -523,6 +523,7 @@ defined(HAVE_LANGINFO_H) && defined(CODESET)
 char *
 _Py_SetLocaleFromEnv(int category)
 {
+    char *res;
 #ifdef __ANDROID__
     const char *locale;
     const char **pvar;
@@ -569,10 +570,12 @@ _Py_SetLocaleFromEnv(int category)
         }
     }
 #endif
-    return setlocale(category, utf8_locale);
-#else /* __ANDROID__ */
-    return setlocale(category, "");
-#endif /* __ANDROID__ */
+    res = setlocale(category, utf8_locale);
+#else /* !defined(__ANDROID__) */
+    res = setlocale(category, "");
+#endif
+    _Py_ResetForceASCII();
+    return res;
 }
 
 



More information about the Python-checkins mailing list