Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/doctest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,10 @@ DocTestRunner objects
output checker, and the results are formatted by the
:meth:`!DocTestRunner.report_\*` methods.

.. versionchanged:: 3.13
Added support for testing examples with a customized
:func:`sys.displayhook` value. Previously, this wasn't allowed and
the method was always using the :func:`sys.__displayhook__`.

.. method:: summarize(verbose=None)

Expand Down
3 changes: 1 addition & 2 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,9 +1554,8 @@ 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
# Make sure sys.displayhook restored at the end
save_displayhook = sys.displayhook
sys.displayhook = sys.__displayhook__
saved_can_colorize = _colorize.can_colorize
_colorize.can_colorize = lambda: False
color_variables = {"PYTHON_COLORS": None, "FORCE_COLOR": None}
Expand Down
41 changes: 27 additions & 14 deletions Lib/test/test_doctest/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,33 +1264,46 @@ def exceptions(): r"""
>>> _colorize.COLORIZE = save_colorize
"""
def displayhook(): r"""
Test that changing sys.displayhook doesn't matter for doctest.
Test changing sys.displayhook.

Run with a custom displayhook:

>>> import sys
>>> orig_displayhook = sys.displayhook
>>> def my_displayhook(x):
... print('hi!')
... sys.__displayhook__(x)
>>> sys.displayhook = my_displayhook
>>> 3
hi!
3
>>> def f():
... '''
... >>> 3
... hi!
... 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
>>> doctest.DocTestRunner(verbose=False).run(test)
hi!
TestResults(failed=0, attempted=1)
>>> sys.displayhook = sys.__displayhook__
>>> 3
3

Ok, now we can check that everything is ok.
Test changing displayhook in the doctest:

>>> r
TestResults(failed=0, attempted=1)
>>> post_displayhook is my_displayhook
True
>>> def g():
... '''
... >>> import sys
... >>> sys.displayhook = lambda x: print('spam')
... >>> 3
... spam
... '''
>>> test = doctest.DocTestFinder().find(g)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
TestResults(failed=0, attempted=3)
>>> 42
42
"""
def optionflags(): r"""
Tests of `DocTestRunner`'s option flag handling.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow using custom sys.displayhook's with doctest. Patch by Sergey B
Kirpichev.