Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cb8a72b

Browse filesBrowse files
gh-134857: Improve error report for doctests run with unittest (GH-134858)
Remove doctest module frames from tracebacks and redundant newline character from a failure message.
1 parent dafd141 commit cb8a72b
Copy full SHA for cb8a72b

File tree

Expand file treeCollapse file tree

3 files changed

+22
-135
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+22
-135
lines changed

‎Lib/doctest.py

Copy file name to clipboardExpand all lines: Lib/doctest.py
+13-11Lines changed: 13 additions & 11 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,11 @@ 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
1401-
except:
1402-
exception = sys.exc_info()
1403+
except BaseException as exc:
1404+
exc_info = type(exc), exc, exc.__traceback__.tb_next
14031405
self.debugger.set_continue() # ==== Example Finished ====
14041406

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

14091411
# If the example executed without raising any exceptions,
14101412
# verify its output.
1411-
if exception is None:
1413+
if exc_info is None:
14121414
if check(example.want, got, self.optionflags):
14131415
outcome = SUCCESS
14141416

14151417
# The example raised an exception: check if it was expected.
14161418
else:
1417-
formatted_ex = traceback.format_exception_only(*exception[:2])
1418-
if issubclass(exception[0], SyntaxError):
1419+
formatted_ex = traceback.format_exception_only(*exc_info[:2])
1420+
if issubclass(exc_info[0], SyntaxError):
14191421
# SyntaxError / IndentationError is special:
14201422
# we don't care about the carets / suggestions / etc
14211423
# We only care about the error message and notes.
14221424
# They start with `SyntaxError:` (or any other class name)
14231425
exception_line_prefixes = (
1424-
f"{exception[0].__qualname__}:",
1425-
f"{exception[0].__module__}.{exception[0].__qualname__}:",
1426+
f"{exc_info[0].__qualname__}:",
1427+
f"{exc_info[0].__module__}.{exc_info[0].__qualname__}:",
14261428
)
14271429
exc_msg_index = next(
14281430
index
@@ -1433,7 +1435,7 @@ def __run(self, test, compileflags, out):
14331435

14341436
exc_msg = "".join(formatted_ex)
14351437
if not quiet:
1436-
got += _exception_traceback(exception)
1438+
got += _exception_traceback(exc_info)
14371439

14381440
# If `example.exc_msg` is None, then we weren't expecting
14391441
# an exception.
@@ -1462,7 +1464,7 @@ def __run(self, test, compileflags, out):
14621464
elif outcome is BOOM:
14631465
if not quiet:
14641466
self.report_unexpected_exception(out, test, example,
1465-
exception)
1467+
exc_info)
14661468
failures += 1
14671469
else:
14681470
assert False, ("unknown outcome", outcome)
@@ -2324,7 +2326,7 @@ def runTest(self):
23242326
sys.stdout = old
23252327

23262328
if results.failed:
2327-
raise self.failureException(self.format_failure(new.getvalue()))
2329+
raise self.failureException(self.format_failure(new.getvalue().rstrip('\n')))
23282330

23292331
def format_failure(self, err):
23302332
test = self._dt_test

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.