Skip to content

Commit 33327d0

Browse files
sergey-miryanovmiss-islington
authored andcommitted
GH-146096: Fix segfault in BaseExceptionGroup repr (GH-146141)
(cherry picked from commit ced6460) Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
1 parent a8a1871 commit 33327d0

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
@@ -236,6 +236,18 @@ class MyEG(ExceptionGroup):
236236
"ExceptionGroup('test', deque([ValueError(1), TypeError(2)]))"
237237
)
238238

239+
def test_repr_small_size_args(self):
240+
eg = ExceptionGroup("msg", [ValueError()])
241+
eg.args = ()
242+
# repr of the ExceptionGroup with empty args should not crash
243+
self.assertEqual(repr(eg), "ExceptionGroup('msg', (ValueError(),))")
244+
245+
eg.args = (1,)
246+
# repr of the ExceptionGroup with 1-size args should not crash
247+
self.assertEqual(repr(eg), "ExceptionGroup('msg', (ValueError(),))")
248+
249+
250+
239251
def test_repr_raises(self):
240252
class MySeq(collections.abc.Sequence):
241253
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
@@ -1104,7 +1104,8 @@ BaseExceptionGroup_repr(PyObject *op)
11041104
* value of self.args[1]; but this can be mutable and go out-of-sync
11051105
* with self.exceptions. Instead, use self.exceptions for accuracy,
11061106
* making it look like self.args[1] for backwards compatibility. */
1107-
if (PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
1107+
assert(PyTuple_Check(self->args));
1108+
if (PyTuple_GET_SIZE(self->args) == 2 && PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
11081109
PyObject *exceptions_list = PySequence_List(self->excs);
11091110
if (!exceptions_list) {
11101111
return NULL;

0 commit comments

Comments
 (0)