Skip to content

Commit 18c9548

Browse files
authored
gh-111159: Fix SyntaxError doctests for non-builtin exception classes (#111541)
1 parent a8e1f47 commit 18c9548

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/doctest.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1399,10 +1399,14 @@ def __run(self, test, compileflags, out):
13991399
# we don't care about the carets / suggestions / etc
14001400
# We only care about the error message and notes.
14011401
# They start with `SyntaxError:` (or any other class name)
1402+
exception_line_prefixes = (
1403+
f"{exception[0].__qualname__}:",
1404+
f"{exception[0].__module__}.{exception[0].__qualname__}:",
1405+
)
14021406
exc_msg_index = next(
14031407
index
14041408
for index, line in enumerate(formatted_ex)
1405-
if line.startswith(f"{exception[0].__name__}:")
1409+
if line.startswith(exception_line_prefixes)
14061410
)
14071411
formatted_ex = formatted_ex[exc_msg_index:]
14081412

Lib/test/test_doctest.py

+18
Original file line numberDiff line numberDiff line change
@@ -3310,6 +3310,24 @@ def test_syntax_error_with_note(cls, multiline=False):
33103310
raise exc
33113311

33123312

3313+
def test_syntax_error_subclass_from_stdlib():
3314+
"""
3315+
`ParseError` is a subclass of `SyntaxError`, but it is not a builtin:
3316+
3317+
>>> test_syntax_error_subclass_from_stdlib()
3318+
Traceback (most recent call last):
3319+
...
3320+
xml.etree.ElementTree.ParseError: error
3321+
error
3322+
Note
3323+
Line
3324+
"""
3325+
from xml.etree.ElementTree import ParseError
3326+
exc = ParseError("error\nerror")
3327+
exc.add_note('Note\nLine')
3328+
raise exc
3329+
3330+
33133331
def test_syntax_error_with_incorrect_expected_note():
33143332
"""
33153333
>>> def f(x):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`doctest` for :exc:`SyntaxError` not-builtin subclasses.

0 commit comments

Comments
 (0)