@@ -15,7 +15,7 @@ wmain(int argc, wchar_t **argv)
1515}
1616#else
1717
18- /* Helpers to better handle the legacy C locale
18+ /* Access private pylifecycle helper API to better handle the legacy C locale
1919 *
2020 * The legacy C locale assumes ASCII as the default text encoding, which
2121 * causes problems not only for the CPython runtime, but also other
@@ -27,107 +27,8 @@ wmain(int argc, wchar_t **argv)
2727 * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
2828 *
2929 */
30-
31- #ifdef PY_COERCE_C_LOCALE
32- /* Access private pylifecycle API to check PYTHONCOERCECLOCALE */
33- extern int _Py_CLocaleCoercionIsExpected (void );
34-
35- static const char * _C_LOCALE_COERCION_WARNING =
36- "Python detected LC_CTYPE=C: %.20s coerced to %.20s (set another locale "
37- "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n" ;
38-
39- typedef struct _CandidateLocale {
40- const char * locale_name ;
41- int category ;
42- } _LocaleCoercionTarget ;
43-
44- static _LocaleCoercionTarget _TARGET_LOCALES [] = {
45- { "C.UTF-8" , LC_ALL },
46- { "C.utf8" , LC_ALL },
47- { "UTF-8" , LC_CTYPE },
48- { NULL , 0 }
49- };
50-
51- void
52- _coerce_default_locale_settings (const _LocaleCoercionTarget * target )
53- {
54- const char * newloc = target -> locale_name ;
55- int category = target -> category ;
56-
57- /* Reset locale back to currently configured defaults */
58- setlocale (LC_ALL , "" );
59-
60- /* Set the relevant locale environment variables */
61- if (category == LC_ALL ) {
62- const char * env_vars_updated = "LC_ALL & LANG" ;
63- if (setenv ("LC_ALL" , newloc , 1 )) {
64- fprintf (stderr ,
65- "Error setting LC_ALL, skipping C locale coercion\n" );
66- return ;
67- }
68- if (setenv ("LANG" , newloc , 1 )) {
69- fprintf (stderr ,
70- "Error setting LANG during C locale coercion\n" );
71- env_vars_updated = "LC_ALL" ;
72- }
73- fprintf (stderr , _C_LOCALE_COERCION_WARNING , env_vars_updated , newloc );
74- } else if (category == LC_CTYPE ) {
75- if (setenv ("LC_CTYPE" , newloc , 1 )) {
76- fprintf (stderr ,
77- "Error setting LC_CTYPE, skipping C locale coercion\n" );
78- return ;
79- }
80- fprintf (stderr , _C_LOCALE_COERCION_WARNING , "LC_CTYPE" , newloc );
81- } else {
82- fprintf (stderr , "Locale coercion must target LC_ALL or LC_CTYPE\n" );
83- return ;
84- }
85-
86- /* Set standard stream encoding if PYTHONIOENCODING is not set
87- *
88- * We avoid setting PYTHONIOENCODING, as that can confuse Python 2
89- * instances in subprocesses that inherit the environment (as Python
90- * 2 has no 'surrogateescape' error handler).
91- *
92- * If PEP 540 is also implemented, this check will be replaced with
93- * unconditionally setting PYTHONUTF8=1
94- */
95- const char * io_encoding = getenv ("PYTHONIOENCODING" );
96- if ((io_encoding == NULL ) || (strnlen (io_encoding , 1 ) == 0 )) {
97- Py_SetStandardStreamEncoding ("utf-8" , "surrogateescape" );
98- }
99-
100- /* Reconfigure with the overridden environment variables */
101- setlocale (LC_ALL , "" );
102- }
103-
104- void
105- _handle_legacy_c_locale (void )
106- {
107- /* We ignore the Python -E and -I flags here, as we need to sort out
108- * the locale settings *before* we try to do anything with the command
109- * line arguments. For cross-platform debugging purposes, we also need
110- * to give end users a way to force even scripts that are otherwise
111- * isolated from their environment to use the legacy ASCII-centric C
112- * locale.
113- */
114- if (_Py_CLocaleCoercionIsExpected ()) {
115- /* PYTHONCOERCECLOCALE is not set, or is not set to exactly "0" */
116- const _LocaleCoercionTarget * target = NULL ;
117- for (target = _TARGET_LOCALES ; target -> locale_name ; target ++ ) {
118- const char * reconfigured_locale = setlocale (target -> category ,
119- target -> locale_name );
120- if (reconfigured_locale != NULL ) {
121- /* Successfully configured locale, so make it the default */
122- _coerce_default_locale_settings (target );
123- return ;
124- }
125- }
126-
127- }
128- /* No C locale warning here, as Py_Initialize will emit one later */
129- }
130- #endif
30+ extern int _Py_LegacyLocaleDetected (void );
31+ extern void _Py_CoerceLegacyLocale (void );
13132
13233int
13334main (int argc , char * * argv )
@@ -177,14 +78,9 @@ main(int argc, char **argv)
17778 setlocale (LC_ALL , "" );
17879#endif
17980
180- #ifdef PY_COERCE_C_LOCALE
181- /* When the LC_CTYPE category still claims to be using the C locale,
182- assume configuration error and try for a UTF-8 based locale instead */
183- const char * ctype_loc = setlocale (LC_CTYPE , NULL );
184- if (ctype_loc != NULL && strcmp (ctype_loc , "C" ) == 0 ) {
185- _handle_legacy_c_locale ();
81+ if (_Py_LegacyLocaleDetected ()) {
82+ _Py_CoerceLegacyLocale ();
18683 }
187- #endif
18884
18985 /* Convert from char to wchar_t based on the locale settings */
19086 for (i = 0 ; i < argc ; i ++ ) {
0 commit comments