From 0a94494298529143b0ee06d96ecd4c503af61911 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 23 Nov 2018 12:19:28 +0100 Subject: [PATCH 1/2] bpo-34523: Fix C locale coercion on FreeBSD CURRENT 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. Add _Py_ResetForceASCII(). _Py_SetLocaleFromEnv() now calls _Py_ResetForceASCII(). --- Include/internal/pycore_fileutils.h | 8 ++++++++ Python/fileutils.c | 7 +++++++ Python/pylifecycle.c | 12 ++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Include/internal/pycore_fileutils.h b/Include/internal/pycore_fileutils.h index 98eb258fe61e6b..25006653a58960 100644 --- a/Include/internal/pycore_fileutils.h +++ b/Include/internal/pycore_fileutils.h @@ -32,6 +32,14 @@ PyAPI_FUNC(wchar_t*) _Py_DecodeUTF8_surrogateescape( 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); + + PyAPI_FUNC(int) _Py_GetLocaleconvNumeric( struct lconv *lc, PyObject **decimal_point, diff --git a/Python/fileutils.c b/Python/fileutils.c index 033c2ff71b9133..abefcfce943d5f 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -231,6 +231,13 @@ _Py_GetForceASCII(void) } +void +_Py_ResetForceASCII(void) +{ + force_ascii = -1; +} + + static int encode_ascii(const wchar_t *text, char **str, size_t *error_pos, const char **reason, diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index af3d5ef055faec..6de32decc5aed3 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -5,6 +5,7 @@ #include "Python-ast.h" #undef Yield /* undefine macro conflicting with */ #include "pycore_context.h" +#include "pycore_fileutils.h" #include "pycore_hamt.h" #include "pycore_pathconfig.h" #include "pycore_pylifecycle.h" @@ -394,6 +395,7 @@ defined(HAVE_LANGINFO_H) && defined(CODESET) char * _Py_SetLocaleFromEnv(int category) { + char *res; #ifdef __ANDROID__ const char *locale; const char **pvar; @@ -440,10 +442,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; } From b593de276549e9ee287a5277c3eee5eef6be8dd3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 23 Nov 2018 12:50:22 +0100 Subject: [PATCH 2/2] define _Py_ResetForceASCII() on Windows On Windows, the function does nothing. --- Python/fileutils.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Python/fileutils.c b/Python/fileutils.c index abefcfce943d5f..366bd007e1c937 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -303,6 +303,12 @@ _Py_GetForceASCII(void) { return 0; } + +void +_Py_ResetForceASCII(void) +{ + /* nothing to do */ +} #endif /* !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS) */