Index: Lib/test/regrtest.py =================================================================== --- Lib/test/regrtest.py (revision 85314) +++ Lib/test/regrtest.py (working copy) @@ -252,8 +252,6 @@ on the command line. """ - replace_stdout() - support.record_original_stdout(sys.stdout) try: opts, args = getopt.getopt(sys.argv[1:], 'hvqxsSrf:lu:t:TD:NLR:FwWM:nj:', @@ -733,17 +731,6 @@ tests.append(modname) return stdtests + sorted(tests) -def replace_stdout(): - """Set stdout encoder error handler to backslashreplace (as stderr error - handler) to avoid UnicodeEncodeError when printing a traceback""" - if os.name == "nt": - # Replace sys.stdout breaks the stdout newlines on Windows: issue #8533 - return - stdout = sys.stdout - sys.stdout = open(stdout.fileno(), 'w', - encoding=stdout.encoding, - errors="backslashreplace") - def runtest(test, verbose, quiet, testdir=None, huntrleaks=False, debug=False, use_resources=None): """Run a single test. Index: Lib/unittest/runner.py =================================================================== --- Lib/unittest/runner.py (revision 85314) +++ Lib/unittest/runner.py (working copy) @@ -2,6 +2,8 @@ import sys import time +import io +import os from . import result from .signals import registerResult @@ -12,6 +14,17 @@ class _WritelnDecorator(object): """Used to decorate file-like objects with a handy 'writeln' method""" def __init__(self,stream): + if stream.errors != "backslashreplace": + fd = os.dup(stream.fileno()) + try: + buf = io.open(fd, "wb") + except: + os.close(fd) + raise + buf.raw.name = stream.name + line_buffering = buf.raw.isatty() + stream = io.TextIOWrapper(buf, + stream.encoding, "backslashreplace", "\n", line_buffering) self.stream = stream def __getattr__(self, attr):