diff --git a/Lib/doctest.py b/Lib/doctest.py index e95c333f48..72d0524d79 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -1476,10 +1476,6 @@ def out(s): self.save_linecache_getlines = linecache.getlines linecache.getlines = self.__patched_linecache_getlines - # Make sure sys.displayhook just prints the value to stdout - save_displayhook = sys.displayhook - sys.displayhook = sys.__displayhook__ - try: return self.__run(test, compileflags, out) finally: @@ -1487,7 +1483,6 @@ def out(s): pdb.set_trace = save_set_trace sys.settrace(save_trace) linecache.getlines = self.save_linecache_getlines - sys.displayhook = save_displayhook if clear_globs: test.globs.clear() import builtins diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 6f51b1bc4f..84bcc6fb4a 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -1142,6 +1142,7 @@ def displayhook(): r""" Test that changing sys.displayhook doesn't matter for doctest. >>> import sys + >>> from unittest.mock import patch >>> orig_displayhook = sys.displayhook >>> def my_displayhook(x): ... print('hi!') @@ -1152,7 +1153,8 @@ def displayhook(): r""" ... 3 ... ''' >>> test = doctest.DocTestFinder().find(f)[0] - >>> r = doctest.DocTestRunner(verbose=False).run(test) + >>> with patch('sys.displayhook', orig_displayhook): + ... r = doctest.DocTestRunner(verbose=False).run(test) >>> post_displayhook = sys.displayhook We need to restore sys.displayhook now, so that we'll be able to test