Skip to content

Commit 98c49c6

Browse files
authored
bpo-34485: Fix _Py_InitializeCore() for C locale coercion (GH-8979) (GH-8981)
* _Py_InitializeCore() now sets the LC_CTYPE locale to the user preferred locale before checking if the C locale should be coerced or not in _PyCoreConfig_Read(). * Fix pymain_read_conf(): remember if the C locale has been coerced when the configuration should be read again if the encoding has changed. (cherry picked from commit 2c8ddcf)
1 parent 65ef742 commit 98c49c6

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Modules/main.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,16 +1854,18 @@ config_read_env_vars(_PyCoreConfig *config)
18541854
config->malloc_stats = 1;
18551855
}
18561856

1857-
if (config->coerce_c_locale < 0) {
1858-
const char *env = config_get_env_var("PYTHONCOERCECLOCALE");
1859-
if (env) {
1860-
if (strcmp(env, "0") == 0) {
1857+
const char *env = config_get_env_var("PYTHONCOERCECLOCALE");
1858+
if (env) {
1859+
if (strcmp(env, "0") == 0) {
1860+
if (config->coerce_c_locale < 0) {
18611861
config->coerce_c_locale = 0;
18621862
}
1863-
else if (strcmp(env, "warn") == 0) {
1864-
config->coerce_c_locale_warn = 1;
1865-
}
1866-
else {
1863+
}
1864+
else if (strcmp(env, "warn") == 0) {
1865+
config->coerce_c_locale_warn = 1;
1866+
}
1867+
else {
1868+
if (config->coerce_c_locale < 0) {
18671869
config->coerce_c_locale = 1;
18681870
}
18691871
}
@@ -2044,7 +2046,7 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
20442046
* See the documentation of the PYTHONCOERCECLOCALE setting for more
20452047
* details.
20462048
*/
2047-
if (config->coerce_c_locale == 1 && !locale_coerced) {
2049+
if (config->coerce_c_locale && !locale_coerced) {
20482050
locale_coerced = 1;
20492051
_Py_CoerceLegacyLocale(config);
20502052
encoding_changed = 1;
@@ -2071,6 +2073,7 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
20712073
pymain_read_conf_impl(). Reset Py_IsolatedFlag and Py_NoSiteFlag
20722074
modified by _PyCoreConfig_Read(). */
20732075
int new_utf8_mode = config->utf8_mode;
2076+
int new_coerce_c_locale = config->coerce_c_locale;
20742077
Py_IgnoreEnvironmentFlag = init_ignore_env;
20752078
if (_PyCoreConfig_Copy(config, &save_config) < 0) {
20762079
pymain->err = _Py_INIT_NO_MEMORY();
@@ -2082,6 +2085,7 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
20822085
cmdline_get_global_config(cmdline);
20832086
_PyCoreConfig_GetGlobalConfig(config);
20842087
config->utf8_mode = new_utf8_mode;
2088+
config->coerce_c_locale = new_coerce_c_locale;
20852089

20862090
/* The encoding changed: read again the configuration
20872091
with the new encoding */

Python/pylifecycle.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,6 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
672672
_PyRuntime.finalizing = NULL;
673673

674674
#ifndef MS_WINDOWS
675-
/* Set up the LC_CTYPE locale, so we can obtain
676-
the locale's charset without having to switch
677-
locales. */
678-
_Py_SetLocaleFromEnv(LC_CTYPE);
679675
_emit_stderr_warning_for_legacy_locale(core_config);
680676
#endif
681677

@@ -829,6 +825,12 @@ _Py_InitializeCore(PyInterpreterState **interp_p,
829825
(and the input configuration is read only). */
830826
_PyCoreConfig config = _PyCoreConfig_INIT;
831827

828+
#ifndef MS_WINDOWS
829+
/* Set up the LC_CTYPE locale, so we can obtain the locale's charset
830+
without having to switch locales. */
831+
_Py_SetLocaleFromEnv(LC_CTYPE);
832+
#endif
833+
832834
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
833835
if (_PyCoreConfig_Copy(&config, src_config) >= 0) {
834836
err = _PyCoreConfig_Read(&config);

0 commit comments

Comments
 (0)