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

Terry J. Reedy report at bugs.python.org
Mon Mar 21 00:22:02 CET 2011

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

You misunderstood my last response. The first paragraph *dismisses* the case of temporary print (raised by Charles-Axle) as out of scope for doctests and hence this issue. The next ones addresses *your* case of code *permanently* intended to print and raise and gives a workable solution to your example.

After failing to find a simpler example that works with 3.x*, I am now thinking of the following doc fix.

1. Add 'directly' before 'supported' in Tim's first sentence: "Examples containing both expected output and an exception are not supported." 

2. After the next sentence of explanation, add

"""Instead, try something like:

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

* I discovered a subtle consequence of the print change: "print 1, 1/0" will print '1' on a line by itself and then a traceback. "print(1,1/0)" just prints a traceback. Something like

class C():
    def __str__(self):
        raise IndexError
print(1, C(), sep='\n')

is required to print a value by itself on a line and then a traceback.
That is no simpler and probably less realistic than test() above.

keywords: +patch

Python tracker <report at bugs.python.org>

More information about the docs mailing list