Skip to content

Commit 84b0129

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

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
@@ -475,6 +475,13 @@ void
475475
_Py_CoerceLegacyLocale(const _PyCoreConfig *config)
476476
{
477477
#ifdef PY_COERCE_C_LOCALE
478+
char *oldloc = NULL;
479+
480+
oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
481+
if (oldloc == NULL) {
482+
return;
483+
}
484+
478485
const char *locale_override = getenv("LC_ALL");
479486
if (locale_override == NULL || *locale_override == '\0') {
480487
/* LC_ALL is also not set (or is set to an empty string) */
@@ -496,11 +503,16 @@ defined(HAVE_LANGINFO_H) && defined(CODESET)
496503
#endif
497504
/* Successfully configured locale, so make it the default */
498505
_coerce_default_locale_settings(config, target);
499-
return;
506+
goto done;
500507
}
501508
}
502509
}
503510
/* No C locale warning here, as Py_Initialize will emit one later */
511+
512+
setlocale(LC_CTYPE, oldloc);
513+
514+
done:
515+
PyMem_RawFree(oldloc);
504516
#endif
505517
}
506518

0 commit comments

Comments
 (0)