mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Only recognize the expected output as an exception if it *starts* with
a traceback message. I.e., examples that raise exceptions may no longer generate pre-exception output. This restores the behavior of doctest in python 2.3. The ability to check pre-exception output is being removed because it makes the documentation simpler; and because there are very few use cases for it.
This commit is contained in:
parent
d2afee47b1
commit
19b1958730
3 changed files with 55 additions and 54 deletions
|
@ -1281,15 +1281,14 @@ class DocTestRunner:
|
|||
|
||||
# A regular expression for handling `want` strings that contain
|
||||
# expected exceptions. It divides `want` into three pieces:
|
||||
# - the pre-exception output (`want`)
|
||||
# - the traceback header line (`hdr`)
|
||||
# - the traceback stack (`stack`)
|
||||
# - the exception message (`msg`), as generated by
|
||||
# traceback.format_exception_only()
|
||||
# `msg` may have multiple lines. We assume/require that the
|
||||
# exception message is the first non-indented line starting with a word
|
||||
# character following the traceback header line.
|
||||
_EXCEPTION_RE = re.compile(r"""
|
||||
(?P<want> .*?) # suck up everything until traceback header
|
||||
# Grab the traceback header. Different versions of Python have
|
||||
# said different things on the first traceback line.
|
||||
^(?P<hdr> Traceback\ \(
|
||||
|
@ -1297,9 +1296,9 @@ class DocTestRunner:
|
|||
| innermost\ last
|
||||
) \) :
|
||||
)
|
||||
\s* $ # toss trailing whitespace on traceback header
|
||||
.*? # don't blink: absorb stuff until a line *starts* with \w
|
||||
^ (?P<msg> \w+ .*)
|
||||
\s* $ # toss trailing whitespace on the header.
|
||||
(?P<stack> .*?) # don't blink: absorb stuff until...
|
||||
^ (?P<msg> \w+ .*) # a line *starts* with alphanum.
|
||||
""", re.VERBOSE | re.MULTILINE | re.DOTALL)
|
||||
|
||||
def __run(self, test, compileflags, out):
|
||||
|
@ -1374,13 +1373,10 @@ class DocTestRunner:
|
|||
exc_info)
|
||||
failures += 1
|
||||
else:
|
||||
e_want, e_msg = m.group('want', 'msg')
|
||||
# The test passes iff the pre-exception output and
|
||||
# the exception description match the values given
|
||||
# in `want`.
|
||||
if (self._checker.check_output(e_want, got,
|
||||
self.optionflags) and
|
||||
self._checker.check_output(e_msg, exc_msg,
|
||||
# The test passes iff the expected exception
|
||||
# message (`m.group('msg')`) matches the actual
|
||||
# exception message (`exc_msg`).
|
||||
if (self._checker.check_output(m.group('msg'), exc_msg,
|
||||
self.optionflags)):
|
||||
self.report_success(out, test, example,
|
||||
got + _exception_traceback(exc_info))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue