Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13e6d23

Browse files
author
Tim Peters
committed
Issue #19138: doctest's IGNORE_EXCEPTION_DETAIL now allows no detail at all.
(grafted from c80083ad142db2939507800c755082293a87f2de)
1 parent 4b7f7ac commit 13e6d23

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

Lib/doctest.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,32 @@ def _comment_line(line):
314314
else:
315315
return '#'
316316

317+
def _strip_exception_details(msg):
318+
# Support for IGNORE_EXCEPTION_DETAIL.
319+
# Get rid of everything except the exception name; in particular, drop
320+
# the possibly dotted module path (if any) and the exception message (if
321+
# any). We assume that a colon is never part of a dotted name, or of an
322+
# exception name.
323+
# E.g., given
324+
# "foo.bar.MyError: la di da"
325+
# return "MyError"
326+
# Or for "abc.def" or "abc.def:\n" return "def".
327+
328+
start, end = 0, len(msg)
329+
# The exception name must appear on the first line.
330+
i = msg.find("\n")
331+
if i >= 0:
332+
end = i
333+
# retain up to the first colon (if any)
334+
i = msg.find(':', 0, end)
335+
if i >= 0:
336+
end = i
337+
# retain just the exception name
338+
i = msg.rfind('.', 0, end)
339+
if i >= 0:
340+
start = i+1
341+
return msg[start: end]
342+
317343
class _OutputRedirectingPdb(pdb.Pdb):
318344
"""
319345
A specialized version of the python debugger that redirects stdout
@@ -1320,10 +1346,9 @@ def __run(self, test, compileflags, out):
13201346

13211347
# Another chance if they didn't care about the detail.
13221348
elif self.optionflags & IGNORE_EXCEPTION_DETAIL:
1323-
m1 = re.match(r'(?:[^:]*\.)?([^:]*:)', example.exc_msg)
1324-
m2 = re.match(r'(?:[^:]*\.)?([^:]*:)', exc_msg)
1325-
if m1 and m2 and check(m1.group(1), m2.group(1),
1326-
self.optionflags):
1349+
if check(_strip_exception_details(example.exc_msg),
1350+
_strip_exception_details(exc_msg),
1351+
self.optionflags):
13271352
outcome = SUCCESS
13281353

13291354
# Report the outcome.

Lib/test/test_doctest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,33 @@ def exceptions(): r"""
10201020
ValueError: message
10211021
TestResults(failed=1, attempted=1)
10221022
1023+
If the exception does not have a message, you can still use
1024+
IGNORE_EXCEPTION_DETAIL to normalize the modules between Python 2 and 3:
1025+
1026+
>>> def f(x):
1027+
... r'''
1028+
... >>> from http.client import HTTPException
1029+
... >>> raise HTTPException() #doctest: +IGNORE_EXCEPTION_DETAIL
1030+
... Traceback (most recent call last):
1031+
... foo.bar.HTTPException
1032+
... '''
1033+
>>> test = doctest.DocTestFinder().find(f)[0]
1034+
>>> doctest.DocTestRunner(verbose=False).run(test)
1035+
TestResults(failed=0, attempted=2)
1036+
1037+
Note that a trailing colon doesn't matter either:
1038+
1039+
>>> def f(x):
1040+
... r'''
1041+
... >>> from http.client import HTTPException
1042+
... >>> raise HTTPException() #doctest: +IGNORE_EXCEPTION_DETAIL
1043+
... Traceback (most recent call last):
1044+
... foo.bar.HTTPException:
1045+
... '''
1046+
>>> test = doctest.DocTestFinder().find(f)[0]
1047+
>>> doctest.DocTestRunner(verbose=False).run(test)
1048+
TestResults(failed=0, attempted=2)
1049+
10231050
If an exception is raised but not expected, then it is reported as an
10241051
unexpected exception:
10251052

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #19138: doctest's IGNORE_EXCEPTION_DETAIL now allows a match when
22+
no exception detail exists (no colon following the exception's name, or
23+
a colon does follow but no text follows the colon).
24+
2125
- Issue #19834: Support unpickling of exceptions pickled by Python 2.
2226

2327
- Issue #15798: Fixed subprocess.Popen() to no longer fail if file

0 commit comments

Comments
 (0)