Skip to content

Commit 5e76e94

Browse files
committed
Merged revisions 80578 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r80578 | nick.coghlan | 2010-04-29 00:29:06 +1000 (Thu, 29 Apr 2010) | 1 line Issue 7490: make IGNORE_EXCEPTION_DETAIL also ignore details of the module containing the exception under test (original patch by Lennart Regebro) ........
1 parent 0681785 commit 5e76e94

File tree

5 files changed

+108
-14
lines changed

5 files changed

+108
-14
lines changed

Doc/library/doctest.rst

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ Some details you should read once, but won't need to remember:
444444
with an alphanumeric is taken to be the start of the exception detail. Of
445445
course this does the right thing for genuine tracebacks.
446446

447-
* When the :const:`IGNORE_EXCEPTION_DETAIL` doctest option is is specified,
448-
everything following the leftmost colon is ignored.
447+
* When the :const:`IGNORE_EXCEPTION_DETAIL` doctest option is specified,
448+
everything following the leftmost colon and any module information in the
449+
exception name is ignored.
449450

450451
* The interactive shell omits the traceback header line for some
451452
:exc:`SyntaxError`\ s. But doctest uses the traceback header line to
@@ -535,20 +536,38 @@ doctest decides whether actual output matches an example's expected output:
535536
exception raised is ``ValueError: 3*14``, but will fail, e.g., if
536537
:exc:`TypeError` is raised.
537538

538-
Note that a similar effect can be obtained using :const:`ELLIPSIS`, and
539-
:const:`IGNORE_EXCEPTION_DETAIL` may go away when Python releases prior to 2.4
540-
become uninteresting. Until then, :const:`IGNORE_EXCEPTION_DETAIL` is the only
541-
clear way to write a doctest that doesn't care about the exception detail yet
542-
continues to pass under Python releases prior to 2.4 (doctest directives appear
543-
to be comments to them). For example, ::
539+
It will also ignore the module name used in Python 3 doctest reports. Hence
540+
both these variations will work regardless of whether the test is run under
541+
Python 2.7 or Python 3.2 (or later versions):
542+
543+
>>> raise CustomError('message') #doctest: +IGNORE_EXCEPTION_DETAIL
544+
Traceback (most recent call last):
545+
CustomError: message
546+
547+
>>> raise CustomError('message') #doctest: +IGNORE_EXCEPTION_DETAIL
548+
Traceback (most recent call last):
549+
my_module.CustomError: message
550+
551+
Note that :const:`ELLIPSIS` can also be used to ignore the
552+
details of the exception message, but such a test may still fail based
553+
on whether or not the module details are printed as part of the
554+
exception name. Using :const:`IGNORE_EXCEPTION_DETAIL` and the details
555+
from Python 2.3 is also the only clear way to write a doctest that doesn't
556+
care about the exception detail yet continues to pass under Python 2.3 or
557+
earlier (those releases do not support doctest directives and ignore them
558+
as irrelevant comments). For example, ::
544559

545560
>>> (1, 2)[3] = 'moo' #doctest: +IGNORE_EXCEPTION_DETAIL
546561
Traceback (most recent call last):
547562
File "<stdin>", line 1, in ?
548563
TypeError: object doesn't support item assignment
549564

550-
passes under Python 2.4 and Python 2.3. The detail changed in 2.4, to say "does
551-
not" instead of "doesn't".
565+
passes under Python 2.3 and later Python versions, even though the detail
566+
changed in Python 2.4 to say "does not" instead of "doesn't".
567+
568+
.. versionchanged:: 3.2
569+
:const:`IGNORE_EXCEPTION_DETAIL` now also ignores any information
570+
relating to the module containing the exception under test
552571

553572

554573
.. data:: SKIP
@@ -663,7 +682,6 @@ usually the only meaningful choice. However, option flags can also be passed to
663682
functions that run doctests, establishing different defaults. In such cases,
664683
disabling an option via ``-`` in a directive can be useful.
665684

666-
667685
There's also a way to register new option flag names, although this isn't useful
668686
unless you intend to extend :mod:`doctest` internals via subclassing:
669687

Lib/doctest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,9 +1277,9 @@ def __run(self, test, compileflags, out):
12771277

12781278
# Another chance if they didn't care about the detail.
12791279
elif self.optionflags & IGNORE_EXCEPTION_DETAIL:
1280-
m1 = re.match(r'[^:]*:', example.exc_msg)
1281-
m2 = re.match(r'[^:]*:', exc_msg)
1282-
if m1 and m2 and check(m1.group(0), m2.group(0),
1280+
m1 = re.match(r'(?:[^:]*\.)?([^:]*:)', example.exc_msg)
1281+
m2 = re.match(r'(?:[^:]*\.)?([^:]*:)', exc_msg)
1282+
if m1 and m2 and check(m1.group(1), m2.group(1),
12831283
self.optionflags):
12841284
outcome = SUCCESS
12851285

Lib/test/test_doctest.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,77 @@ def exceptions(): r"""
864864
>>> doctest.DocTestRunner(verbose=False).run(test)
865865
TestResults(failed=0, attempted=1)
866866
867+
IGNORE_EXCEPTION_DETAIL also ignores difference in exception formatting
868+
between Python versions. For example, in Python 2.x, the module path of
869+
the exception is not in the output, but this will fail under Python 3:
870+
871+
>>> def f(x):
872+
... r'''
873+
... >>> from http.client import HTTPException
874+
... >>> raise HTTPException('message')
875+
... Traceback (most recent call last):
876+
... HTTPException: message
877+
... '''
878+
>>> test = doctest.DocTestFinder().find(f)[0]
879+
>>> doctest.DocTestRunner(verbose=False).run(test)
880+
... # doctest: +ELLIPSIS
881+
**********************************************************************
882+
File ..., line 4, in f
883+
Failed example:
884+
raise HTTPException('message')
885+
Expected:
886+
Traceback (most recent call last):
887+
HTTPException: message
888+
Got:
889+
Traceback (most recent call last):
890+
...
891+
http.client.HTTPException: message
892+
TestResults(failed=1, attempted=2)
893+
894+
But in Python 3 the module path is included, and therefore a test must look
895+
like the following test to succeed in Python 3. But that test will fail under
896+
Python 2.
897+
898+
>>> def f(x):
899+
... r'''
900+
... >>> from http.client import HTTPException
901+
... >>> raise HTTPException('message')
902+
... Traceback (most recent call last):
903+
... http.client.HTTPException: message
904+
... '''
905+
>>> test = doctest.DocTestFinder().find(f)[0]
906+
>>> doctest.DocTestRunner(verbose=False).run(test)
907+
TestResults(failed=0, attempted=2)
908+
909+
However, with IGNORE_EXCEPTION_DETAIL, the module name of the exception
910+
(or its unexpected absence) will be ignored:
911+
912+
>>> def f(x):
913+
... r'''
914+
... >>> from http.client import HTTPException
915+
... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL
916+
... Traceback (most recent call last):
917+
... HTTPException: message
918+
... '''
919+
>>> test = doctest.DocTestFinder().find(f)[0]
920+
>>> doctest.DocTestRunner(verbose=False).run(test)
921+
TestResults(failed=0, attempted=2)
922+
923+
The module path will be completely ignored, so two different module paths will
924+
still pass if IGNORE_EXCEPTION_DETAIL is given. This is intentional, so it can
925+
be used when exceptions have changed module.
926+
927+
>>> def f(x):
928+
... r'''
929+
... >>> from http.client import HTTPException
930+
... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL
931+
... Traceback (most recent call last):
932+
... foo.bar.HTTPException: message
933+
... '''
934+
>>> test = doctest.DocTestFinder().find(f)[0]
935+
>>> doctest.DocTestRunner(verbose=False).run(test)
936+
TestResults(failed=0, attempted=2)
937+
867938
But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type:
868939
869940
>>> def f(x):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ Marc Recht
638638
John Redford
639639
Terry Reedy
640640
Steve Reeves
641+
Lennart Regebro
641642
Ofir Reichenberg
642643
Sean Reifschneider
643644
Michael P. Reilly

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7490: to facilitate sharing of doctests between 2.x and 3.x test
16+
suites, the IGNORE_EXCEPTION_DETAIL directive now also ignores the module
17+
location of the raised exception.
18+
1519
- Issue #8969: On Windows, use mbcs codec in strict mode to encode and decode
1620
filenames and enable os.fsencode().
1721

0 commit comments

Comments
 (0)