summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorNico-Posada <102486290+Nico-Posada@users.noreply.github.com>2024-12-20 19:20:31 (GMT)
committerGitHub <noreply@github.com>2024-12-20 19:20:31 (GMT)
commit3879ca0100942ae15a09ac22889cbe3e46d424eb (patch)
tree366ec8e3dcc8f834b8666916af4da447eaa7f48d /Python
parent5a584c8f54bbeceae7ffa501291e29b7ddc8a0b9 (diff)
downloadcpython-3879ca0100942ae15a09ac22889cbe3e46d424eb.zip
cpython-3879ca0100942ae15a09ac22889cbe3e46d424eb.tar.gz
cpython-3879ca0100942ae15a09ac22889cbe3e46d424eb.tar.bz2
gh-128049: Fix type confusion bug with the return value of a custom ExceptionGroup split function (#128079)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 3cf11b6..e92a11b 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2134,8 +2134,25 @@ _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)) {
+ 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;
+ }
+
+ // allow tuples of length > 2 for backwards compatibility
+ if (PyTuple_GET_SIZE(pair) < 2) {
+ PyErr_Format(PyExc_TypeError,
+ "%.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;
+ }
+
*match = Py_NewRef(PyTuple_GET_ITEM(pair, 0));
*rest = Py_NewRef(PyTuple_GET_ITEM(pair, 1));
Py_DECREF(pair);