Skip to content

gh-128049: Fix type confusion bug with the return value of a custom ExceptionGroup split function #128079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,20 @@ 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
@@ -0,0 +1,3 @@
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.
11 changes: 9 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2095,8 +2095,15 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
if (pair == NULL) {
return -1;
}
assert(PyTuple_CheckExact(pair));
assert(PyTuple_GET_SIZE(pair) == 2);

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

*match = Py_NewRef(PyTuple_GET_ITEM(pair, 0));
*rest = Py_NewRef(PyTuple_GET_ITEM(pair, 1));
Py_DECREF(pair);
Expand Down
Loading