Skip to content

Commit

Permalink
Issue #8048: Prevent doctests from failing when sys.displayhook has
Browse files Browse the repository at this point in the history
been reassigned.
  • Loading branch information
birkenfeld committed Jul 30, 2010
1 parent 46b9afc commit 25fbb89
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,12 +1379,17 @@ 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:
sys.stdout = save_stdout
pdb.set_trace = save_set_trace
linecache.getlines = self.save_linecache_getlines
sys.displayhook = save_displayhook
if clear_globs:
test.globs.clear()
import builtins
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,35 @@ def exceptions(): r"""
...
ZeroDivisionError: integer division or modulo by zero
TestResults(failed=1, attempted=1)
"""
def displayhook(): r"""
Test that changing sys.displayhook doesn't matter for doctest.
>>> import sys
>>> orig_displayhook = sys.displayhook
>>> def my_displayhook(x):
... print('hi!')
>>> sys.displayhook = my_displayhook
>>> def f():
... '''
... >>> 3
... 3
... '''
>>> test = doctest.DocTestFinder().find(f)[0]
>>> 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
results.
>>> sys.displayhook = orig_displayhook
Ok, now we can check that everything is ok.
>>> r
TestResults(failed=0, attempted=1)
>>> post_displayhook is my_displayhook
True
"""
def optionflags(): r"""
Tests of `DocTestRunner`'s option flag handling.
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ C-API
Library
-------

- Issue #8048: Prevent doctests from failing when sys.displayhook has
been reassigned.

- Issue #8015: In pdb, do not crash when an empty line is entered as
a breakpoint command.

Expand Down

0 comments on commit 25fbb89

Please sign in to comment.