diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-08-29 07:58:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-29 07:58:12 (GMT) |
commit | 315877dc361d554bec34b4b62c270479ad36a1be (patch) | |
tree | 0783fe0301841be7a065316dfc442c64f90a3a4f /Python | |
parent | 21786f5186383e8912e761eccd0f4ac1cca83217 (diff) | |
download | cpython-315877dc361d554bec34b4b62c270479ad36a1be.zip cpython-315877dc361d554bec34b4b62c270479ad36a1be.tar.gz cpython-315877dc361d554bec34b4b62c270479ad36a1be.tar.bz2 |
bpo-34485: stdout uses surrogateescape on POSIX locale (GH-8986)
Standard streams like sys.stdout now use the "surrogateescape" error
handler, instead of "strict", on the POSIX locale (when the C locale is not
coerced and the UTF-8 Mode is disabled).
Add tests on sys.stdout.errors with LC_ALL=POSIX.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pylifecycle.c | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8c77859..33af06e 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -345,13 +345,13 @@ get_stdio_errors(void) { const char *ctype_loc = setlocale(LC_CTYPE, NULL); if (ctype_loc != NULL) { - /* "surrogateescape" is the default in the legacy C locale */ - if (strcmp(ctype_loc, "C") == 0) { + /* surrogateescape is the default in the legacy C and POSIX locales */ + if (strcmp(ctype_loc, "C") == 0 || strcmp(ctype_loc, "POSIX") == 0) { return "surrogateescape"; } #ifdef PY_COERCE_C_LOCALE - /* "surrogateescape" is the default in locale coercion target locales */ + /* surrogateescape is the default in locale coercion target locales */ const _LocaleCoercionTarget *target = NULL; for (target = _TARGET_LOCALES; target->locale_name; target++) { if (strcmp(ctype_loc, target->locale_name) == 0) { @@ -1791,16 +1791,29 @@ init_sys_streams(PyInterpreterState *interp) if (err) { *err = '\0'; err++; - if (*err && !errors) { - errors = err; + if (!err[0]) { + err = NULL; } } - if (!encoding && *pythonioencoding) { - encoding = pythonioencoding; - if (!errors) { - errors = "strict"; + + /* Does PYTHONIOENCODING contain an encoding? */ + if (pythonioencoding[0]) { + if (!encoding) { + encoding = pythonioencoding; + } + + /* If the encoding is set but not the error handler, + use "strict" error handler by default. + PYTHONIOENCODING=latin1 behaves as + PYTHONIOENCODING=latin1:strict. */ + if (!err) { + err = "strict"; } } + + if (!errors && err != NULL) { + errors = err; + } } if (interp->core_config.utf8_mode) { |