[docs] [issue3722] print followed by exception eats print with doctest

Terry J. Reedy report at bugs.python.org
Thu Mar 10 00:02:19 CET 2011

Terry J. Reedy <tjreedy at udel.edu> added the comment:

Temporary output will break all doctests, not just those with exception traceback. One should fix, disable debug output, and then rerun doctest to make sure fix did not break anything else.

A function that prints and raises *can* be tested as by separately testing both output and silencing of expected exception as follows:

>>> def test():
...   print("hello")
...   raise IndexError()
>>> try:
...   test()
...   raise BaseException()
... except IndexError:
...   pass

This passes but will not if function prints anything else or does anything other than raise that exception (or a subclass thereof). In practice, catching subclasses should be ok, but if not, the except clause can be expanded to

... except IndexError as msg:
...   if type(msg) is not IndexError:
...     raise BaseException

I am only leaving this open for a possible doc addition showing something like the simpler fix above.

assignee:  -> docs at python
components: +Documentation -Library (Lib)
nosy: +docs at python
stage:  -> needs patch
Added file: http://bugs.python.org/file21065/doctest_solution.py

Python tracker <report at bugs.python.org>

More information about the docs mailing list