Index: Lib/doctest.py =================================================================== --- Lib/doctest.py (revision 78630) +++ Lib/doctest.py (working copy) @@ -1332,6 +1332,11 @@ else: return self.save_linecache_getlines(filename, module_globals) + def _displayhook(self, obj): + if obj is not None: + sys.stdout.write(repr(obj)) + sys.stdout.write('\n') + def run(self, test, compileflags=None, out=None, clear_globs=True): """ Run the examples in `test`, and display the results using the @@ -1377,12 +1382,17 @@ 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 = self._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() Index: Lib/test/test_doctest.py =================================================================== --- Lib/test/test_doctest.py (revision 78630) +++ Lib/test/test_doctest.py (working copy) @@ -910,6 +910,36 @@ 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. @@ -1607,13 +1637,23 @@ >>> import tempfile >>> real_stdin = sys.stdin >>> sys.stdin = _FakeInput([ + ... 'return', # return from displayhook + ... 'next', # finish returning from displayhook ... 'print x', # print data defined by the example ... 'continue', # stop debugging ... '']) - >>> try: runner.run(test) + >>> try: runner.run(test) # doctest: +ELLIPSIS ... finally: sys.stdin = real_stdin + --Call-- + > ..._displayhook() + -> def _displayhook(self, obj): + (Pdb) return --Return-- + > ..._displayhook()->None + -> ... + (Pdb) next + --Return-- > (1)()->None -> import pdb; pdb.set_trace() (Pdb) print x @@ -1675,10 +1715,12 @@ ... 'next', # return from g() ... 'list', # list source from example 1 ... 'next', # return from f() + ... 'return', # return from displayhook + ... 'next', # finish returning from displayhook ... 'list', # list source from example 3 ... 'continue', # stop debugging ... '']) - >>> try: runner.run(test) + >>> try: runner.run(test) # doctest: +ELLIPSIS ... finally: sys.stdin = real_stdin ... # doctest: +NORMALIZE_WHITESPACE --Return-- @@ -1698,7 +1740,15 @@ 2 -> g(x*2) [EOF] (Pdb) next + --Call-- + > ..._displayhook() + -> def _displayhook(self, obj): + (Pdb) return --Return-- + > ..._displayhook()->None + -> ... + (Pdb) next + --Return-- > (1)()->None -> f(3) (Pdb) list Index: Lib/test/test_pdb.py =================================================================== --- Lib/test/test_pdb.py (revision 78630) +++ Lib/test/test_pdb.py (working copy) @@ -92,12 +92,14 @@ ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() ... mod.foo_pony(callback) - >>> with PdbTestInput([ + >>> with PdbTestInput([ # doctest: +ELLIPSIS ... 'step', ... 'step', ... 'step', ... 'step', ... 'step', + ... 'return', # return from displayhook + ... 'next', # finish returning from displayhook ... 'continue', ... ]): ... skip_module() @@ -120,7 +122,15 @@ > (5)skip_module()->None -> mod.foo_pony(callback) (Pdb) step - > (10)() + --Call-- + > ..._displayhook() + -> def _displayhook(self, obj): + (Pdb) return + --Return-- + > ..._displayhook()->None + -> ... + (Pdb) next + > (12)() -> pass # provides something to "step" to (Pdb) continue """