Skip to content

Commit

Permalink
pythongh-111159: Fix SyntaxError doctests for non-builtin exception…
Browse files Browse the repository at this point in the history
… classes (pythonGH-111541)

(cherry picked from commit 18c9548)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
sobolevn authored and miss-islington committed Nov 4, 2023
1 parent f7ffe4a commit 1ace32d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Lib/doctest.py
Expand Up @@ -1376,10 +1376,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
18 changes: 18 additions & 0 deletions Lib/test/test_doctest.py
Expand Up @@ -3279,6 +3279,24 @@ 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:
>>> test_syntax_error_subclass_from_stdlib()
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
@@ -0,0 +1 @@
Fix :mod:`doctest` for :exc:`SyntaxError` not-builtin subclasses.

0 comments on commit 1ace32d

Please sign in to comment.