Skip to content

Commit 8ea0911

Browse files
authored
_Py_CoerceLegacyLocale() restores LC_CTYPE on fail (GH-9044)
bpo-34544: If _Py_CoerceLegacyLocale() fails to coerce the C locale, restore the LC_CTYPE locale to the its previous value.
1 parent f01b2a1 commit 8ea0911

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Python/pylifecycle.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ void
363363
_Py_CoerceLegacyLocale(int warn)
364364
{
365365
#ifdef PY_COERCE_C_LOCALE
366+
char *oldloc = NULL;
367+
368+
oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
369+
if (oldloc == NULL) {
370+
return;
371+
}
372+
366373
const char *locale_override = getenv("LC_ALL");
367374
if (locale_override == NULL || *locale_override == '\0') {
368375
/* LC_ALL is also not set (or is set to an empty string) */
@@ -384,11 +391,16 @@ defined(HAVE_LANGINFO_H) && defined(CODESET)
384391
#endif
385392
/* Successfully configured locale, so make it the default */
386393
_coerce_default_locale_settings(warn, target);
387-
return;
394+
goto done;
388395
}
389396
}
390397
}
391398
/* No C locale warning here, as Py_Initialize will emit one later */
399+
400+
setlocale(LC_CTYPE, oldloc);
401+
402+
done:
403+
PyMem_RawFree(oldloc);
392404
#endif
393405
}
394406

0 commit comments

Comments
 (0)