Issue #8048: Prevent doctests from failing when sys.displayhook has
authorGeorg Brandl <georg@python.org>
Fri, 30 Jul 2010 09:23:23 +0000 (09:23 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 30 Jul 2010 09:23:23 +0000 (09:23 +0000)
been reassigned.

Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS

index 5111a732e59990bbf01571379fbf262f2de82354..0db75b4343dcf7cacd450d47045a38f937846044 100644 (file)
@@ -1379,12 +1379,17 @@ class DocTestRunner:
         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
index 873e495d7f0d556ac33872e4e26f449003563e55..b6eeaedce4a77f17fbb7e96a7e97ad64a37a7a81 100644 (file)
@@ -979,6 +979,35 @@ unexpected exception:
         ...
         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.
index 5c61594424f476237e0f0620a51e9dee1cb6ac4a..dc8b72c8dfb13b06418f12ce49c458ade29cd297 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.