Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ class MyEG(ExceptionGroup):
"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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed segmentation fault when called repr for BaseExceptionGroup with empty
or 1-size tuple args.
2 changes: 1 addition & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ 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))) {
if (PyTuple_GET_SIZE(self->args) == 2 && PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check that it's a Tuple first?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add an assert just so it reads like it makes sense.

PyObject *exceptions_list = PySequence_List(self->excs);
if (!exceptions_list) {
return NULL;
Expand Down
Loading