Skip to content

Commit f01b2a1

Browse files
authored
bpo-34544: Fix setlocale() in pymain_read_conf() (GH-9041)
bpo-34485, bpo-34544: On some FreeBSD, nl_langinfo(CODESET) fails if LC_ALL or LC_CTYPE is set to an invalid locale name. Replace _Py_SetLocaleFromEnv(LC_CTYPE) with _Py_SetLocaleFromEnv(LC_ALL) to initialize properly locales. Partially revert commit 177d921.
1 parent f326714 commit f01b2a1

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Modules/main.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,10 +1291,17 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
12911291
int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
12921292
#endif
12931293
_PyCoreConfig save_config = _PyCoreConfig_INIT;
1294+
char *oldloc = NULL;
12941295
int res = -1;
12951296

1296-
/* Set LC_CTYPE to the user preferred locale */
1297-
_Py_SetLocaleFromEnv(LC_CTYPE);
1297+
oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
1298+
if (oldloc == NULL) {
1299+
pymain->err = _Py_INIT_NO_MEMORY();
1300+
goto done;
1301+
}
1302+
1303+
/* Reconfigure the locale to the default for this process */
1304+
_Py_SetLocaleFromEnv(LC_ALL);
12981305

12991306
int locale_coerced = 0;
13001307
int loops = 0;
@@ -1385,6 +1392,10 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
13851392

13861393
done:
13871394
_PyCoreConfig_Clear(&save_config);
1395+
if (oldloc != NULL) {
1396+
setlocale(LC_ALL, oldloc);
1397+
PyMem_RawFree(oldloc);
1398+
}
13881399
Py_UTF8Mode = init_utf8_mode ;
13891400
#ifdef MS_WINDOWS
13901401
Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;

0 commit comments

Comments
 (0)