Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 425efc1

Browse files
[3.11] gh-111159: Fix SyntaxError doctests for non-builtin exception classes (GH-111541) (#111733)
gh-111159: Fix `SyntaxError` doctests for non-builtin exception classes (GH-111541) (cherry picked from commit 18c9548) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent f7ffe4a commit 425efc1

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/doctest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,10 +1376,14 @@ def __run(self, test, compileflags, out):
13761376
# we don't care about the carets / suggestions / etc
13771377
# We only care about the error message and notes.
13781378
# They start with `SyntaxError:` (or any other class name)
1379+
exception_line_prefixes = (
1380+
f"{exception[0].__qualname__}:",
1381+
f"{exception[0].__module__}.{exception[0].__qualname__}:",
1382+
)
13791383
exc_msg_index = next(
13801384
index
13811385
for index, line in enumerate(formatted_ex)
1382-
if line.startswith(f"{exception[0].__name__}:")
1386+
if line.startswith(exception_line_prefixes)
13831387
)
13841388
formatted_ex = formatted_ex[exc_msg_index:]
13851389

Lib/test/test_doctest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,24 @@ def test_syntax_error_with_note(cls, multiline=False):
32793279
raise exc
32803280

32813281

3282+
def test_syntax_error_subclass_from_stdlib():
3283+
"""
3284+
`ParseError` is a subclass of `SyntaxError`, but it is not a builtin:
3285+
3286+
>>> test_syntax_error_subclass_from_stdlib()
3287+
Traceback (most recent call last):
3288+
...
3289+
xml.etree.ElementTree.ParseError: error
3290+
error
3291+
Note
3292+
Line
3293+
"""
3294+
from xml.etree.ElementTree import ParseError
3295+
exc = ParseError("error\nerror")
3296+
exc.add_note('Note\nLine')
3297+
raise exc
3298+
3299+
32823300
def test_syntax_error_with_incorrect_expected_note():
32833301
"""
32843302
>>> def f(x):
Lines changed: 1 addition & 0 deletions
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)