mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-111159: Fix SyntaxError
doctests for non-builtin exception classes (#111541)
This commit is contained in:
parent
a8e1f474c2
commit
18c954849b
3 changed files with 24 additions and 1 deletions
|
@ -1399,10 +1399,14 @@ class DocTestRunner:
|
||||||
# we don't care about the carets / suggestions / etc
|
# we don't care about the carets / suggestions / etc
|
||||||
# We only care about the error message and notes.
|
# We only care about the error message and notes.
|
||||||
# They start with `SyntaxError:` (or any other class name)
|
# 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(
|
exc_msg_index = next(
|
||||||
index
|
index
|
||||||
for index, line in enumerate(formatted_ex)
|
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:]
|
formatted_ex = formatted_ex[exc_msg_index:]
|
||||||
|
|
||||||
|
|
|
@ -3310,6 +3310,24 @@ def test_syntax_error_with_note(cls, multiline=False):
|
||||||
raise exc
|
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 test_syntax_error_with_incorrect_expected_note():
|
||||||
"""
|
"""
|
||||||
>>> def f(x):
|
>>> def f(x):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix :mod:`doctest` for :exc:`SyntaxError` not-builtin subclasses.
|
Loading…
Add table
Add a link
Reference in a new issue