GH-146096: Fix segfault in BaseExceptionGroup repr (#146141) · python/cpython@ced6460

GitHub

File tree

Lib/test

Misc/NEWS.d/next/Core_and_Builtins

Objects

Original file line numberDiff line numberDiff line change@@ -234,6 +234,18 @@ class MyEG(ExceptionGroup):

234234"ExceptionGroup('test', deque([ValueError(1), TypeError(2)]))"

235235 )

236236237+deftest_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+237249deftest_repr_raises(self):

238250classMySeq(collections.abc.Sequence):

239251def__init__(self, raises):

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.

Original file line numberDiff line numberDiff line change@@ -1091,7 +1091,8 @@ BaseExceptionGroup_repr(PyObject *op)

10911091 * value of self.args[1]; but this can be mutable and go out-of-sync

10921092 * with self.exceptions. Instead, use self.exceptions for accuracy,

10931093 * making it look like self.args[1] for backwards compatibility. */

1094-if (PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {

1094+assert(PyTuple_Check(self->args));

1095+if (PyTuple_GET_SIZE(self->args) ==2&&PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {

10951096PyObject*exceptions_list=PySequence_List(self->excs);

10961097if (!exceptions_list) {

10971098returnNULL;