bpo-34485, Windows: LC_CTYPE set to user preference (GH-8988) · pythoncapi/cpython@177d921 · GitHub
Skip to content

Commit 177d921

Browse files
authored
bpo-34485, Windows: LC_CTYPE set to user preference (pythonGH-8988)
On Windows, the LC_CTYPE is now set to the user preferred locale at startup: _Py_SetLocaleFromEnv(LC_CTYPE) is now called during the Python initialization. Previously, the LC_CTYPE locale was "C" at startup, but changed when calling setlocale(LC_CTYPE, "") or setlocale(LC_ALL, ""). pymain_read_conf() now also calls _Py_SetLocaleFromEnv(LC_CTYPE) to behave as _Py_InitializeCore(). Moreover, it doesn't save/restore the LC_ALL anymore. On Windows, standard streams like sys.stdout now always use surrogateescape error handler by default (ignore the locale).
1 parent 315877d commit 177d921

3 files changed

Lines changed: 13 additions & 19 deletions

File tree

Lines changed: 3 additions & 0 deletions

Modules/main.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,25 +1280,18 @@ pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
12801280
}
12811281

12821282

1283-
/* Read the configuration, but initialize also the LC_CTYPE locale:
1284-
enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538) */
1283+
/* Read the configuration and initialize the LC_CTYPE locale:
1284+
enable UTF-8 mode (PEP 540) and/or coerce the C locale (PEP 538). */
12851285
static int
12861286
pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
12871287
_PyCmdline *cmdline)
12881288
{
12891289
int init_utf8_mode = Py_UTF8Mode;
12901290
_PyCoreConfig save_config = _PyCoreConfig_INIT;
1291-
char *oldloc = NULL;
12921291
int res = -1;
12931292

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

13031296
int locale_coerced = 0;
13041297
int loops = 0;
@@ -1386,10 +1379,6 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
13861379

13871380
done:
13881381
_PyCoreConfig_Clear(&save_config);
1389-
if (oldloc != NULL) {
1390-
setlocale(LC_ALL, oldloc);
1391-
PyMem_RawFree(oldloc);
1392-
}
13931382
Py_UTF8Mode = init_utf8_mode ;
13941383
return res;
13951384
}

Python/pylifecycle.c

Lines changed: 6 additions & 4 deletions

0 commit comments

Comments
 (0)