bpo-34544: Fix setlocale() in pymain_read_conf() (GH-9041) · python/cpython@f01b2a1

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;