summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-11-04 09:54:10 (GMT)
committerGitHub <noreply@github.com>2023-11-04 09:54:10 (GMT)
commit18c954849bcdd5acb6ef91cd90d92f3b5c685134 (patch)
tree35c267c07653bedf6b2f8e0dd7b95f7aceba8110
parenta8e1f474c20ab15140dd0cfcb96b696857907a60 (diff)
downloadcpython-18c954849bcdd5acb6ef91cd90d92f3b5c685134.zip
cpython-18c954849bcdd5acb6ef91cd90d92f3b5c685134.tar.gz
cpython-18c954849bcdd5acb6ef91cd90d92f3b5c685134.tar.bz2
gh-111159: Fix `SyntaxError` doctests for non-builtin exception classes (#111541)
-rw-r--r--Lib/doctest.py6
-rw-r--r--Lib/test/test_doctest.py18
-rw-r--r--Misc/NEWS.d/next/Library/2023-11-04-10-24-25.gh-issue-111541.x0RBI1.rst1
3 files changed, 24 insertions, 1 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index f00d935..2f14aa0 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1399,10 +1399,14 @@ class DocTestRunner:
# 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:]
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index 5c59b00..cb4e215 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -3310,6 +3310,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):
diff --git a/Misc/NEWS.d/next/Library/2023-11-04-10-24-25.gh-issue-111541.x0RBI1.rst b/Misc/NEWS.d/next/Library/2023-11-04-10-24-25.gh-issue-111541.x0RBI1.rst
new file mode 100644
index 0000000..719b63d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-11-04-10-24-25.gh-issue-111541.x0RBI1.rst
@@ -0,0 +1 @@
+Fix :mod:`doctest` for :exc:`SyntaxError` not-builtin subclasses.