Skip to content
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

gh-111159: Fix SyntaxError doctests for non-builtin exception classes #111541

Merged
merged 6 commits into from Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/doctest.py
Expand Up @@ -1399,10 +1399,14 @@ def __run(self, test, compileflags, out):
# 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__}:",
)
exc_msg_index = next(
index
for index, line in enumerate(formatted_ex)
if line.startswith(f"{exception[0].__name__}:")
if line.startswith(exception_line_prefixes)
)
formatted_ex = formatted_ex[exc_msg_index:]

Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_doctest.py
Expand Up @@ -3310,6 +3310,25 @@ def test_syntax_error_with_note(cls, multiline=False):
raise exc


def test_syntax_error_subclass_from_stdlib():
"""
`ParseError` is a subclass of `SyntaxError`, but it is not a builtin:

>>> from xml.etree.ElementTree import ParseError
>>> test_syntax_error_subclass_from_stdlib()
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
Traceback (most recent call last):
...
xml.etree.ElementTree.ParseError: error
error
Note
Line
"""
from xml.etree.ElementTree import ParseError
exc = ParseError("error\nerror")
exc.add_note('Note\nLine')
raise exc


def test_syntax_error_with_incorrect_expected_note():
"""
>>> def f(x):
Expand Down