[Python-checkins] bpo-34544: Fix setlocale() in pymain_read_conf() (GH-9041)

Victor Stinner webhook-mailer at python.org
Mon Sep 3 08:38:24 EDT 2018


https://github.com/python/cpython/commit/f01b2a1b84ee08df73a78cf1017eecf15e3cb995
commit: f01b2a1b84ee08df73a78cf1017eecf15e3cb995
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-09-03T14:38:21+02:00
summary:

bpo-34544: Fix setlocale() in pymain_read_conf() (GH-9041)

bpo-34485, bpo-34544: On some FreeBSD, nl_langinfo(CODESET) fails if
LC_ALL or LC_CTYPE is set to an invalid locale name. Replace
_Py_SetLocaleFromEnv(LC_CTYPE) with _Py_SetLocaleFromEnv(LC_ALL) to
initialize properly locales.

Partially revert commit 177d921c8c03d30daa32994362023f777624b10d.

files:
M Modules/main.c

diff --git a/Modules/main.c b/Modules/main.c
index 1ab555b42de4..974a0a6b78b9 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -1291,10 +1291,17 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
     int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
 #endif
     _PyCoreConfig save_config = _PyCoreConfig_INIT;
+    char *oldloc = NULL;
     int res = -1;
 
-    /* Set LC_CTYPE to the user preferred locale */
-    _Py_SetLocaleFromEnv(LC_CTYPE);
+    oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
+    if (oldloc == NULL) {
+        pymain->err = _Py_INIT_NO_MEMORY();
+        goto done;
+    }
+
+    /* Reconfigure the locale to the default for this process */
+    _Py_SetLocaleFromEnv(LC_ALL);
 
     int locale_coerced = 0;
     int loops = 0;
@@ -1385,6 +1392,10 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
 
 done:
     _PyCoreConfig_Clear(&save_config);
+    if (oldloc != NULL) {
+        setlocale(LC_ALL, oldloc);
+        PyMem_RawFree(oldloc);
+    }
     Py_UTF8Mode = init_utf8_mode ;
 #ifdef MS_WINDOWS
     Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;



More information about the Python-checkins mailing list