Skip to content

Commit 9ce6acd

Browse files
[3.13] GH-146096: Fix segfault in BaseExceptionGroup repr (GH-146141) (GH-156903)
(cherry picked from commit ced6460) Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
1 parent 1050f7c commit 9ce6acd

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

Lib/test/test_exception_group.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ class MyEG(ExceptionGroup):
234234
"ExceptionGroup('test', deque([ValueError(1), TypeError(2)]))"
235235
)
236236

237+
def test_repr_small_size_args(self):
238+
eg = ExceptionGroup("msg", [ValueError()])
239+
eg.args = ()
240+
# repr of the ExceptionGroup with empty args should not crash
241+
self.assertEqual(repr(eg), "ExceptionGroup('msg', (ValueError(),))")
242+
243+
eg.args = (1,)
244+
# repr of the ExceptionGroup with 1-size args should not crash
245+
self.assertEqual(repr(eg), "ExceptionGroup('msg', (ValueError(),))")
246+
247+
248+
237249
def test_repr_raises(self):
238250
class MySeq(collections.abc.Sequence):
239251
def __init__(self, raises):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed segmentation fault when called repr for BaseExceptionGroup with empty
2+
or 1-size tuple args.

Objects/exceptions.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,8 @@ BaseExceptionGroup_repr(PyBaseExceptionGroupObject *self)
914914
* value of self.args[1]; but this can be mutable and go out-of-sync
915915
* with self.exceptions. Instead, use self.exceptions for accuracy,
916916
* making it look like self.args[1] for backwards compatibility. */
917-
if (PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
917+
assert(PyTuple_Check(self->args));
918+
if (PyTuple_GET_SIZE(self->args) == 2 && PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
918919
PyObject *exceptions_list = PySequence_List(self->excs);
919920
if (!exceptions_list) {
920921
return NULL;

0 commit comments

Comments
 (0)