[3.12] gh-111159: Fix doctest output comparison for exceptions with notes (GH-111160) (#111169)

gh-111159: Fix `doctest` output comparison for exceptions with notes (GH-111160)
(cherry picked from commit fd60549c0a)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2023-10-21 20:39:34 +02:00 committed by GitHub
parent 1ea93024d4
commit f6cde99bdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 159 additions and 1 deletions

View file

@ -1376,7 +1376,20 @@ class DocTestRunner:
# The example raised an exception: check if it was expected.
else:
exc_msg = traceback.format_exception_only(*exception[:2])[-1]
formatted_ex = traceback.format_exception_only(*exception[:2])
if issubclass(exception[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)
exc_msg_index = next(
index
for index, line in enumerate(formatted_ex)
if line.startswith(f"{exception[0].__name__}:")
)
formatted_ex = formatted_ex[exc_msg_index:]
exc_msg = "".join(formatted_ex)
if not quiet:
got += _exception_traceback(exception)