8000 gh-134857: Improve error report for doctests run with unittest · python/cpython@0d32417 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d32417

Browse files
gh-134857: Improve error report for doctests run with unittest
Remove doctest module frames from tracebacks and redundant newline character from a failure message.
1 parent f6324bc commit 0d32417

File tree

3 files changed

+22
-134
lines changed

3 files changed

+22
-134
lines changed

Lib/doctest.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def _test():
108108
from _colorize import ANSIColors, can_colorize
109109

110110

111+
__unittest = True
112+
111113
class TestResults(namedtuple('TestResults', 'failed attempted')):
112114
def __new__(cls, failed, attempted, *, skipped=0):
113115
results = super().__new__(cls, failed, attempted)
@@ -1395,11 +1397,12 @@ def __run(self, test, compileflags, out):
13951397
exec(compile(example.source, filename, "single",
13961398
compileflags, True), test.globs)
13971399
self.debugger.set_continue() # ==== Example Finished ====
1398-
exception = None
1400+
exc_info = None
13991401
except KeyboardInterrupt:
14001402
raise
14011403
except:
1402-
exception = sys.exc_info()
1404+
exc_info = sys.exc_info()
1405+
exc_info = (*exc_info[:2], exc_info[2].tb_next)
14031406
self.debugger.set_continue() # ==== Example Finished ====
14041407

14051408
got = self._fakeout.getvalue() # the actual output
@@ -1408,21 +1411,21 @@ def __run(self, test, compileflags, out):
14081411

14091412
# If the example executed without raising any exceptions,
14101413
# verify its output.
1411-
if exception is None:
1414+
if exc_info is None:
14121415
if check(example.want, got, self.optionflags):
14131416
outcome = SUCCESS
14141417

14151418
# The example raised an exception: check if it was expected.
14161419
else:
1417-
formatted_ex = traceback.format_exception_only(*exception[:2])
1418-
if issubclass(exception[0], SyntaxError):
1420+
formatted_ex = traceback.format_exception_only(*exc_info[:2])
1421+
if issubclass(exc_info[0], SyntaxError):
14191422
# SyntaxError / IndentationError is special:
14201423
# we don't care about the carets / suggestions / etc
14211424
# We only care about the error message and notes.
14221425
# They start with `SyntaxError:` (or any other class name)
14231426
exception_line_prefixes = (
1424-
f"{exception[0].__qualname__}:",
1425-
f"{exception[0].__module__}.{exception[0].__qualname__}:",
1427+
f"{exc_info[0].__qualname__}:",
1428+
f"{exc_info[0].__module__}.{exc_info[0].__qualname__}:",
14261429
)
14271430
exc_msg_index = next(
14281431
index
@@ -1433,7 +1436,7 @@ def __run(self, test, compileflags, out):
14331436

14341437
exc_msg = "".join(formatted_ex)
14351438
if not quiet:
1436-
got += _exception_traceback(exception)
1439+
got += _exception_traceback(exc_info)
14371440

14381441
# If `example.exc_msg` is None, then we weren't expecting
14391442
# an exception.
@@ -1462,7 +1465,7 @@ def __run(self, test, compileflags, out):
14621465
elif outcome is BOOM:
14631466
if not quiet:
14641467
self.report_unexpected_exception(out, test, example,
1465-
exception)
1468+
exc_info)
14661469
failures += 1
14671470
else:
14681471
assert False, ("unknown outcome", outcome)
@@ -2324,7 +2327,7 @@ def runTest(self):
23242327
sys.stdout = old
23252328

23262329
if results.failed:
2327-
raise self.failureException(self.format_failure(new.getvalue()))
2330+
raise self.failureException(self.format_failure(new.getvalue().rstrip('\n')))
23282331

23292332
def format_failure(self, err):
23302333
test = self._dt_test

0 commit comments

Comments
 (0)
0