Exception inside loop wrongly failing doctest

S

Steven D'Aprano

One of my doctests is failing, and I suspect a bug.

The test involves matching an exception in a for-loop. Here are two
simplified versions of the test, both should pass but only the first does.


As a doctest, this passes:
.... print (x, 1.0/x)
....
(3, 0.33333333333333331)
(2, 0.5)
(1, 1.0)


However, this fails:
.... print (x, 1.0/x)
....
(3, 0.33333333333333331)
(2, 0.5)
(1, 1.0)
Traceback (most recent call last):
...
ZeroDivisionError: float division


Attached is a simple test script which runs doctest.testmod(). Both tests
should pass, with no output printing. However, it does this:

$ python doctest_error.py
**********************************************************************
File "doctest_error.py", line 14, in __main__
Failed example:
for x in [3, 2, 1, 0]:
print (x, 1.0/x)
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python2.5/doctest.py", line 1212, in __run
compileflags, 1) in test.globs
File "<doctest __main__[1]>", line 2, in <module>
print (x, 1.0/x)
ZeroDivisionError: float division
**********************************************************************
1 items had failures:
1 of 2 in __main__
***Test Failed*** 1 failures.



I've tested this in Python 2.4, 2.5 and 2.6 and get the same result for all
three.

Have I missed something, or should I report this as a bug?
 
L

Lie Ryan

Steven said:
One of my doctests is failing, and I suspect a bug.

The test involves matching an exception in a for-loop. Here are two
simplified versions of the test, both should pass but only the first does.

tell me, what's the result of 1/0?
 
L

Lie Ryan

Lie said:
tell me, what's the result of 1/0?

Whooopss.. replied too fast...

This seems to work:
.... print (i, 1.0/i)
....
Traceback (most recent call last):
....
ZeroDivisionError: float division

it seems that if the expected result is a traceback, the doc result must
starts with the traceback keyword:

"""
The expected output for an exception must *start* with a traceback
header, which may be either of the following two lines, indented the
same as the first line of the example:

Traceback (most recent call last):
Traceback (innermost last):
"""

(emphasizes added)
 
S

Steven D'Aprano

Lie said:
....
it seems that if the expected result is a traceback, the doc result must
starts with the traceback keyword:


Ah, that would be it. Not a bug then, a limitation of the doctest module.

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,521
Latest member
JamieCooch

Latest Threads

Top