Skip to content

bpo-34485: stdout uses surrogateescape on POSIX locale #8986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,10 @@ def test_getfilesystemencoding(self):
expected = None
self.check_fsencoding(fs_encoding, expected)

def c_locale_get_error_handler(self, isolated=False, encoding=None):
def c_locale_get_error_handler(self, locale, isolated=False, encoding=None):
# Force the POSIX locale
env = os.environ.copy()
env["LC_ALL"] = "C"
env["LC_ALL"] = locale
env["PYTHONCOERCECLOCALE"] = "0"
code = '\n'.join((
'import sys',
Expand All @@ -683,44 +683,50 @@ def c_locale_get_error_handler(self, isolated=False, encoding=None):
stdout, stderr = p.communicate()
return stdout

def test_c_locale_surrogateescape(self):
out = self.c_locale_get_error_handler(isolated=True)
def check_locale_surrogateescape(self, locale):
out = self.c_locale_get_error_handler(locale, isolated=True)
self.assertEqual(out,
'stdin: surrogateescape\n'
'stdout: surrogateescape\n'
'stderr: backslashreplace\n')

# replace the default error handler
out = self.c_locale_get_error_handler(encoding=':ignore')
out = self.c_locale_get_error_handler(locale, encoding=':ignore')
self.assertEqual(out,
'stdin: ignore\n'
'stdout: ignore\n'
'stderr: backslashreplace\n')

# force the encoding
out = self.c_locale_get_error_handler(encoding='iso8859-1')
out = self.c_locale_get_error_handler(locale, encoding='iso8859-1')
self.assertEqual(out,
'stdin: strict\n'
'stdout: strict\n'
'stderr: backslashreplace\n')
out = self.c_locale_get_error_handler(encoding='iso8859-1:')
out = self.c_locale_get_error_handler(locale, encoding='iso8859-1:')
self.assertEqual(out,
'stdin: strict\n'
'stdout: strict\n'
'stderr: backslashreplace\n')

# have no any effect
out = self.c_locale_get_error_handler(encoding=':')
out = self.c_locale_get_error_handler(locale, encoding=':')
self.assertEqual(out,
'stdin: surrogateescape\n'
'stdout: surrogateescape\n'
'stderr: backslashreplace\n')
out = self.c_locale_get_error_handler(encoding='')
out = self.c_locale_get_error_handler(locale, encoding='')
self.assertEqual(out,
'stdin: surrogateescape\n'
'stdout: surrogateescape\n'
'stderr: backslashreplace\n')

def test_c_locale_surrogateescape(self):
self.check_locale_surrogateescape('C')

def test_posix_locale_surrogateescape(self):
self.check_locale_surrogateescape('POSIX')

def test_implementation(self):
# This test applies to all implementations equally.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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).
31 changes: 22 additions & 9 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down