Exception inside loop wrongly failing doctest

Steven D'Aprano steve at REMOVETHIS.cybersource.com.au
Fri Jun 12 05:07:19 EDT 2009


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:

>>> for x in [3, 2, 1]:
...     print (x, 1.0/x)
...
(3, 0.33333333333333331)
(2, 0.5)
(1, 1.0)


However, this fails:

>>> for x in [3, 2, 1, 0]:
...     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?



-- 
Steven

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: doctest_error.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20090612/b772af2c/attachment.ksh>


More information about the Python-list mailing list