Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions Lib/test/test_except_star.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,53 @@ def derive(self, excs):
self.assertExceptionIsLike(exc, FalsyEG("eg", [TypeError(1)]))
self.assertExceptionIsLike(tes, FalsyEG("eg", [TypeError(1)]))
self.assertExceptionIsLike(ves, FalsyEG("eg", [ValueError(2)]))

def test_bad_exception_group_subclass_split_func(self):
# See https://github.com/python/cpython/issues/128049
# tuples that return less than 2 values should
# result in a type error with the original eg chained to it
class BadEG1(ExceptionGroup):
def split(self, *args):
return "NOT A 2-TUPLE!"

class BadEG2(ExceptionGroup):
def split(self, *args):
return ("NOT A 2-TUPLE!",)

eg_list = [
(BadEG1("eg", [OSError(123), ValueError(456)]),
r"split must return a tuple, not str"),
(BadEG2("eg", [OSError(123), ValueError(456)]),
r"split must return a 2-tuple, got tuple of size 1")
]

for EG, MSG in eg_list:
with self.assertRaisesRegex(TypeError, MSG) as m:
try:
raise EG
except* ValueError:
pass
except* OSError:
pass

self.assertExceptionIsLike(m.exception.__context__, EG)

# although it isn't expected, still allow tuples of length > 2
# all tuple items past the second one will be ignored
# this quirk may be deprecated in the future
class WeirdEG(ExceptionGroup):
def split(self, *args):
return super().split(*args) + ("anything", 123456, None)

try:
raise WeirdEG("eg", [OSError(123), ValueError(456)])
except* OSError as e:
oeg = e
except* ValueError as e:
veg = e

self.assertExceptionIsLike(oeg, WeirdEG("eg", [OSError(123)]))
self.assertExceptionIsLike(veg, WeirdEG("eg", [ValueError(456)]))


class TestExceptStarCleanup(ExceptStarTest):
Expand Down
14 changes: 0 additions & 14 deletions Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,20 +576,6 @@ def test_iteration_full_tracebacks(self):
expected_tbs[i])


class CustomExceptionGroupSplitTest(ExceptionGroupTestBase):
# See https://github.com/python/cpython/issues/128049
def test_invalid_split_return_value(self):
class Evil(BaseExceptionGroup):
def split(self, types):
return "NOT A TUPLE"

with self.assertRaises(TypeError):
try:
raise Evil("message here", [Exception()])
except* Exception:
pass


class ExceptionGroupSplitTestBase(ExceptionGroupTestBase):

def split_exception_group(self, eg, types):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Fixed a type confusion bug where the caller of an :class:`ExceptionGroup`
object's :meth:`~BaseExceptionGroup.split` function had improper checks
implemented leading to any return value being interpreted as a tuple.
Fix a bug where :keyword:`except* <except_star>` does not properly check the
return value of an :exc:`ExceptionGroup`'s :meth:`~BaseExceptionGroup.split`
function, leading to a crash in some cases. Now when :meth:`~BaseExceptionGroup.split`
returns an invalid object, :keyword:`except* <except_star>` raises a :exc:`TypeError`
with the original raised :exc:`ExceptionGroup` object chained to it.
19 changes: 16 additions & 3 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2096,10 +2096,23 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
return -1;
}

if (!PyTuple_CheckExact(pair) || PyTuple_Size(pair) != 2) {
if (!PyTuple_CheckExact(pair))
{
PyErr_Format(PyExc_TypeError,
"%.200s.split must return a tuple, not %.200s",
Py_TYPE(exc_value)->tp_name, Py_TYPE(pair)->tp_name);
Py_DECREF(pair);
return -1;
}

// NOTE due to a previous bug which allowed tuples of length > 2 to
// work without problem, we are still allowing them to work even
// though the error says otherwise
if (PyTuple_GET_SIZE(pair) < 2) {
PyErr_Format(PyExc_TypeError,
"%s.split must return a 2-tuple",
Py_TYPE(exc_value)->tp_name);
"%.200s.split must return a 2-tuple, "
"got tuple of size %zd",
Py_TYPE(exc_value)->tp_name, PyTuple_GET_SIZE(pair));
Py_DECREF(pair);
return -1;
}
Expand Down
Loading