mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
Doctest has new traceback gimmicks in 2.4. While trying to document
them (which they are now), I had to rewrite the code to understand it. This has got to be the most DWIM part of doctest -- but in context is really necessary.
This commit is contained in:
parent
f076953eb1
commit
41a65ea7fe
2 changed files with 106 additions and 34 deletions
|
@ -1191,16 +1191,27 @@ class DocTestRunner:
|
|||
#/////////////////////////////////////////////////////////////////
|
||||
|
||||
# A regular expression for handling `want` strings that contain
|
||||
# expected exceptions. It divides `want` into two pieces: the
|
||||
# pre-exception output (`out`) and the exception message (`exc`),
|
||||
# as generated by traceback.format_exception_only(). (I assume
|
||||
# that the exception_only message is the first non-indented line
|
||||
# starting with word characters after the "Traceback ...".)
|
||||
_EXCEPTION_RE = re.compile(('^(?P<out>.*)'
|
||||
'^(?P<hdr>Traceback \((?:%s|%s)\):)\s*$.*?'
|
||||
'^(?P<exc>\w+.*)') %
|
||||
('most recent call last', 'innermost last'),
|
||||
re.MULTILINE | re.DOTALL)
|
||||
# expected exceptions. It divides `want` into three pieces:
|
||||
# - the pre-exception output (`want`)
|
||||
# - the traceback header line (`hdr`)
|
||||
# - 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\ \(
|
||||
(?: most\ recent\ call\ last
|
||||
| innermost\ last
|
||||
) \) :
|
||||
)
|
||||
\s* $ # toss trailing whitespace on traceback header
|
||||
.*? # don't blink: absorb stuff until a line *starts* with \w
|
||||
^ (?P<msg> \w+ .*)
|
||||
""", re.VERBOSE | re.MULTILINE | re.DOTALL)
|
||||
|
||||
def __run(self, test, compileflags, out):
|
||||
"""
|
||||
|
@ -1274,20 +1285,19 @@ class DocTestRunner:
|
|||
exc_info)
|
||||
failures += 1
|
||||
else:
|
||||
exc_hdr = m.group('hdr')+'\n' # Exception header
|
||||
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(m.group('out'), got,
|
||||
if (self._checker.check_output(e_want, got,
|
||||
self.optionflags) and
|
||||
self._checker.check_output(m.group('exc'), exc_msg,
|
||||
self._checker.check_output(e_msg, exc_msg,
|
||||
self.optionflags)):
|
||||
# Is +exc_msg the right thing here??
|
||||
self.report_success(out, test, example,
|
||||
got+_exception_traceback(exc_info))
|
||||
got + _exception_traceback(exc_info))
|
||||
else:
|
||||
self.report_failure(out, test, example,
|
||||
got+_exception_traceback(exc_info))
|
||||
got + _exception_traceback(exc_info))
|
||||
failures += 1
|
||||
|
||||
# Restore the option flags (in case they were modified)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue