Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25fbb89

Browse files
committed
Issue #8048: Prevent doctests from failing when sys.displayhook has
been reassigned.
1 parent 46b9afc commit 25fbb89

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

Lib/doctest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,12 +1379,17 @@ def out(s):
13791379
self.save_linecache_getlines = linecache.getlines
13801380
linecache.getlines = self.__patched_linecache_getlines
13811381

1382+
# Make sure sys.displayhook just prints the value to stdout
1383+
save_displayhook = sys.displayhook
1384+
sys.displayhook = sys.__displayhook__
1385+
13821386
try:
13831387
return self.__run(test, compileflags, out)
13841388
finally:
13851389
sys.stdout = save_stdout
13861390
pdb.set_trace = save_set_trace
13871391
linecache.getlines = self.save_linecache_getlines
1392+
sys.displayhook = save_displayhook
13881393
if clear_globs:
13891394
test.globs.clear()
13901395
import builtins

Lib/test/test_doctest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,35 @@ def exceptions(): r"""
979979
...
980980
ZeroDivisionError: integer division or modulo by zero
981981
TestResults(failed=1, attempted=1)
982+
"""
983+
def displayhook(): r"""
984+
Test that changing sys.displayhook doesn't matter for doctest.
985+
986+
>>> import sys
987+
>>> orig_displayhook = sys.displayhook
988+
>>> def my_displayhook(x):
989+
... print('hi!')
990+
>>> sys.displayhook = my_displayhook
991+
>>> def f():
992+
... '''
993+
... >>> 3
994+
... 3
995+
... '''
996+
>>> test = doctest.DocTestFinder().find(f)[0]
997+
>>> r = doctest.DocTestRunner(verbose=False).run(test)
998+
>>> post_displayhook = sys.displayhook
999+
1000+
We need to restore sys.displayhook now, so that we'll be able to test
1001+
results.
1002+
1003+
>>> sys.displayhook = orig_displayhook
1004+
1005+
Ok, now we can check that everything is ok.
1006+
1007+
>>> r
1008+
TestResults(failed=0, attempted=1)
1009+
>>> post_displayhook is my_displayhook
1010+
True
9821011
"""
9831012
def optionflags(): r"""
9841013
Tests of `DocTestRunner`'s option flag handling.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ C-API
475475
Library
476476
-------
477477

478+
- Issue #8048: Prevent doctests from failing when sys.displayhook has
479+
been reassigned.
480+
478481
- Issue #8015: In pdb, do not crash when an empty line is entered as
479482
a breakpoint command.
480483

0 commit comments

Comments
 (0)