diff -r d1c11a78b43a Lib/test/test_sys.py --- a/Lib/test/test_sys.py Tue Nov 10 19:52:20 2015 +0200 +++ b/Lib/test/test_sys.py Tue Nov 10 22:42:22 2015 +0200 @@ -691,7 +691,7 @@ class SysModuleTest(unittest.TestCase): args = [sys.executable, "-c", code] if isolated: args.append("-I") - elif encoding: + elif encoding is not None: env['PYTHONIOENCODING'] = encoding p = subprocess.Popen(args, stdout=subprocess.PIPE, @@ -709,14 +709,31 @@ class SysModuleTest(unittest.TestCase): 'stderr: backslashreplace\n') # replace the default error handler - out = self.c_locale_get_error_handler(encoding=':strict') + out = self.c_locale_get_error_handler(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') + self.assertEqual(out, + 'stdin: strict\n' + 'stdout: strict\n' + 'stderr: backslashreplace\n') + out = self.c_locale_get_error_handler(encoding='iso8859-1:') self.assertEqual(out, 'stdin: strict\n' 'stdout: strict\n' 'stderr: backslashreplace\n') - # force the encoding - out = self.c_locale_get_error_handler(encoding='iso8859-1') + # have no any effect + out = self.c_locale_get_error_handler(encoding=':') + self.assertEqual(out, + 'stdin: surrogateescape\n' + 'stdout: surrogateescape\n' + 'stderr: backslashreplace\n') + out = self.c_locale_get_error_handler(encoding='') self.assertEqual(out, 'stdin: surrogateescape\n' 'stdout: surrogateescape\n' diff -r d1c11a78b43a Python/pylifecycle.c --- a/Python/pylifecycle.c Tue Nov 10 19:52:20 2015 +0200 +++ b/Python/pylifecycle.c Tue Nov 10 22:42:22 2015 +0200 @@ -1138,15 +1138,6 @@ initstdio(void) encoding = _Py_StandardStreamEncoding; errors = _Py_StandardStreamErrors; if (!encoding || !errors) { - if (!errors) { - /* When the LC_CTYPE locale is the POSIX locale ("C locale"), - stdin and stdout use the surrogateescape error handler by - default, instead of the strict error handler. */ - char *loc = setlocale(LC_CTYPE, NULL); - if (loc != NULL && strcmp(loc, "C") == 0) - errors = "surrogateescape"; - } - pythonioencoding = Py_GETENV("PYTHONIOENCODING"); if (pythonioencoding) { char *err; @@ -1159,7 +1150,7 @@ initstdio(void) if (err) { *err = '\0'; err++; - if (*err && !_Py_StandardStreamErrors) { + if (*err && !errors) { errors = err; } } @@ -1167,6 +1158,14 @@ initstdio(void) encoding = pythonioencoding; } } + if (!errors && !(pythonioencoding && *pythonioencoding)) { + /* When the LC_CTYPE locale is the POSIX locale ("C locale"), + stdin and stdout use the surrogateescape error handler by + default, instead of the strict error handler. */ + char *loc = setlocale(LC_CTYPE, NULL); + if (loc != NULL && strcmp(loc, "C") == 0) + errors = "surrogateescape"; + } } /* Set sys.stdin */