bpo-35336: Fix PYTHONCOERCECLOCALE=1 (GH-10806) (GH-10813)
Fix PYTHONCOERCECLOCALE=1 environment variable: only coerce the C
locale if the LC_CTYPE locale is "C".
(cherry picked from commit 55e498058faf8c97840556f6d791c2c392732dc3)
diff --git a/Modules/main.c b/Modules/main.c
index 6dbe6a3..af2c191 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -2191,11 +2191,17 @@
static void
config_init_locale(_PyCoreConfig *config)
{
- if (config->coerce_c_locale < 0) {
+ /* Test also if coerce_c_locale equals 1: PYTHONCOERCECLOCALE=1 doesn't
+ imply that the C locale is always coerced. It is only coerced if
+ if the LC_CTYPE locale is "C". */
+ if (config->coerce_c_locale != 0) {
/* The C locale enables the C locale coercion (PEP 538) */
if (_Py_LegacyLocaleDetected()) {
config->coerce_c_locale = 1;
}
+ else {
+ config->coerce_c_locale = 0;
+ }
}
#ifndef MS_WINDOWS
@@ -2376,7 +2382,7 @@
}
}
- if (config->utf8_mode < 0 || config->coerce_c_locale < 0) {
+ if (config->coerce_c_locale != 0 || config->utf8_mode < 0) {
config_init_locale(config);
}