]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-111159: Fix `SyntaxError` doctests for non-builtin exception classes (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 4 Nov 2023 10:21:06 +0000 (11:21 +0100)
committerGitHub <noreply@github.com>
Sat, 4 Nov 2023 10:21:06 +0000 (10:21 +0000)
gh-111159: Fix `SyntaxError` doctests for non-builtin exception classes (GH-111541)
(cherry picked from commit 18c954849bcdd5acb6ef91cd90d92f3b5c685134)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS.d/next/Library/2023-11-04-10-24-25.gh-issue-111541.x0RBI1.rst [new file with mode: 0644]

index aeb28e8d292f6cbe9049bf36ce8fd88c96735be3..d7eaa8b131ed2fc1f25c152df99e9170801e29e1 100644 (file)
@@ -1382,10 +1382,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:]
 
index 09c378fcac80e084657b5bb17aea3dbe182402ba..0706049a90e41b08508b2e6de255839213ee5a00 100644 (file)
@@ -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):
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 (file)
index 0000000..719b63d
--- /dev/null
@@ -0,0 +1 @@
+Fix :mod:`doctest` for :exc:`SyntaxError` not-builtin subclasses.