]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-146096: Fix segfault in BaseExceptionGroup repr (#146141)
authorSergey Miryanov <sergey.miryanov@gmail.com>
Thu, 19 Mar 2026 11:25:15 +0000 (16:25 +0500)
committerGitHub <noreply@github.com>
Thu, 19 Mar 2026 11:25:15 +0000 (11:25 +0000)
Lib/test/test_exception_group.py
Misc/NEWS.d/next/Core_and_Builtins/2026-03-19-01-19-34.gh-issue-146096.R9tkJX.rst [new file with mode: 0644]
Objects/exceptions.c

index ace7ec72917934a2bd0d825600f287bf46b174d7..35ffc9a0a4cf30afcf2fd92a4488a3942477e32b 100644 (file)
@@ -234,6 +234,18 @@ class StrAndReprTests(unittest.TestCase):
             "ExceptionGroup('test', deque([ValueError(1), TypeError(2)]))"
         )
 
+    def test_repr_small_size_args(self):
+        eg = ExceptionGroup("msg", [ValueError()])
+        eg.args = ()
+        # repr of the ExceptionGroup with empty args should not crash
+        self.assertEqual(repr(eg), "ExceptionGroup('msg', (ValueError(),))")
+
+        eg.args = (1,)
+        # repr of the ExceptionGroup with 1-size args should not crash
+        self.assertEqual(repr(eg), "ExceptionGroup('msg', (ValueError(),))")
+
+
+
     def test_repr_raises(self):
         class MySeq(collections.abc.Sequence):
             def __init__(self, raises):
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-19-01-19-34.gh-issue-146096.R9tkJX.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-19-01-19-34.gh-issue-146096.R9tkJX.rst
new file mode 100644 (file)
index 0000000..8d7e177
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed segmentation fault when called repr for BaseExceptionGroup with empty
+or 1-size tuple args.
index 4e090e5dd863f110e08fd66995d3a1b4ba1c57f7..32b0fcec6c4542e854cd46007350bc78b8128681 100644 (file)
@@ -1091,7 +1091,8 @@ BaseExceptionGroup_repr(PyObject *op)
          * value of self.args[1]; but this can be mutable and go out-of-sync
          * with self.exceptions. Instead, use self.exceptions for accuracy,
          * making it look like self.args[1] for backwards compatibility. */
-        if (PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
+        assert(PyTuple_Check(self->args));
+        if (PyTuple_GET_SIZE(self->args) == 2 && PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
             PyObject *exceptions_list = PySequence_List(self->excs);
             if (!exceptions_list) {
                 return NULL;