diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/doctest.py | 20 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 16 |
2 files changed, 21 insertions, 15 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 43f21b2..01f7cb3 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -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)) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index d5e9ef5..5d0cf90 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -623,8 +623,10 @@ replaced with any other string: >>> doctest.DocTestRunner(verbose=False).run(test) (0, 2) -An example may generate output before it raises an exception; if it -does, then the output must match the expected output: +An example may not generate output before it raises an exception; if +it does, then the traceback message will not be recognized as +signaling an expected exception, so the example will be reported as an +unexpected exception: >>> def f(x): ... ''' @@ -636,7 +638,15 @@ does, then the output must match the expected output: ... ''' >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) - (0, 2) + ... # doctest: +ELLIPSIS + ********************************************************************** + Line 3, in f + Failed example: + print 'pre-exception output', x/0 + Exception raised: + ... + ZeroDivisionError: integer division or modulo by zero + (1, 2) Exception messages may contain newlines: |