Skip to content

gh-134857: Improve error report for doctests run with unittest #134858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def _test():
from _colorize import ANSIColors, can_colorize


__unittest = True

class TestResults(namedtuple('TestResults', 'failed attempted')):
def __new__(cls, failed, attempted, *, skipped=0):
results = super().__new__(cls, failed, attempted)
Expand Down Expand Up @@ -1395,11 +1397,11 @@ def __run(self, test, compileflags, out):
exec(compile(example.source, filename, "single",
compileflags, True), test.globs)
self.debugger.set_continue() # ==== Example Finished ====
exception = None
exc_info = None
except KeyboardInterrupt:
raise
except:
exception = sys.exc_info()
except BaseException as exc:
exc_info = type(exc), exc, exc.__traceback__.tb_next
self.debugger.set_continue() # ==== Example Finished ====

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

# If the example executed without raising any exceptions,
# verify its output.
if exception is None:
if exc_info is None:
if check(example.want, got, self.optionflags):
outcome = SUCCESS

# The example raised an exception: check if it was expected.
else:
formatted_ex = traceback.format_exception_only(*exception[:2])
if issubclass(exception[0], SyntaxError):
formatted_ex = traceback.format_exception_only(*exc_info[:2])
if issubclass(exc_info[0], SyntaxError):
# SyntaxError / IndentationError is special:
# we don't care about the carets / suggestions / etc
# We only care about the error message and notes.
# They start with `SyntaxError:` (or any other class name)
exception_line_prefixes = (
f"{exception[0].__qualname__}:",
f"{exception[0].__module__}.{exception[0].__qualname__}:",
f"{exc_info[0].__qualname__}:",
f"{exc_info[0].__module__}.{exc_info[0].__qualname__}:",
)
exc_msg_index = next(
index
Expand All @@ -1433,7 +1435,7 @@ def __run(self, test, compileflags, out):

exc_msg = "".join(formatted_ex)
if not quiet:
got += _exception_traceback(exception)
got += _exception_traceback(exc_info)

# If `example.exc_msg` is None, then we weren't expecting
# an exception.
Expand Down Expand Up @@ -1462,7 +1464,7 @@ def __run(self, test, compileflags, out):
elif outcome is BOOM:
if not quiet:
self.report_unexpected_exception(out, test, example,
exception)
exc_info)
failures += 1
else:
assert False, ("unknown outcome", outcome)
Expand Down Expand Up @@ -2324,7 +2326,7 @@ def runTest(self):
sys.stdout = old

if results.failed:
raise self.failureException(self.format_failure(new.getvalue()))
raise self.failureException(self.format_failure(new.getvalue().rstrip('\n')))

def format_failure(self, err):
test = self._dt_test
Expand Down
Loading
Loading