[Python-Dev] [Python-checkins] r45321 - in python/trunk: Lib/test/test_traceback.py Lib/traceback.py Misc/NEWS

Tim Peters tim.peters at gmail.com
Sat Apr 15 06:42:28 CEST 2006


[John J Lee]
>> Assuming this is fixed in 2.5 final, is there some way to write doctests that
>> work on both 2.4 and 2.5?  If not, should something like
>> doctest.IGNORE_EXCEPTION_DETAIL be added -- say
>> IGNORE_EXCEPTION_MODULE?

[also John]
> Sorry, please ignore the post of mine I'm replying to here.
>
> I missed part of the thread, and Tim has already answered my question...

That's news to Tim ;-)  It's not possible to add a new doctest option
in 2.5 that would allow a doctest to work under both 2.4 and 2.5:  the
test would blow up under 2.4, because 2.4's doctest wouldn't recognize
the (new in 2.5) option, and would raise a ValueError of its own
griping about the unknown option name.

It would be possible to add a new doctest option in 2.5 that would
(just) allow a doctest running _under_ 2.5 to accept a bare "name" in
an exception despite that the traceback module changed in 2.5 to
produce "some.dotted.name" instead.  That doesn't seem very useful,
though (as above, it would force the test to fail when run under 2.4).

Of course a doctest can contain any Python code, so there are many
ways to write a doctest that passes under all versions of Python,
without active help from doctest.  For example, using the running
example in this thread, this way works under all versions of Python
with a decimal module:

"""
>>> import decimal
>>> try:
>>>     1 / decimal.Decimal(0)
>>> except decimal.DivisionByZero:
>>>     print "good"
good
"""

Oddly enough, ELLIPSIS doesn't actually work for this purpose:

"""
>>> import decimal
>>> 1 / decimal.Decimal(0) #doctest: +ELLIPSIS
Traceback (most recent call last):
   [etc]
...DivisionByZero: x / 0
"""

That test fails under 2.4, and would also fail under the presumably
changed 2.5.  The problem is that, as the docs say, when trying to
guess where an exception message starts, doctest skips down to the
first line after the "Traceback" line indented the same as the
"Traceback" line and starting with an _alphanumeric_ character.  There
is no such line in this example (the exception line starts with a
period), so doctest believes the example is showing expected stdout,
not that it's showing an exception.  The delightfully baffling result
is that doctest complains that the example raises an exception when an
exception wasn't expected.  Stinking magic ;-)


More information about the Python-Dev mailing list