| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -952,6 +952,49 @@ def derive(self, excs): | |||
| 952 | 952 | self.assertExceptionIsLike(tes, FalsyEG("eg", [TypeError(1)])) | |
| 953 | 953 | self.assertExceptionIsLike(ves, FalsyEG("eg", [ValueError(2)])) | |
| 954 | 954 | ||
| 955 | + def test_exception_group_subclass_with_bad_split_func(self): | ||
| 956 | + # see gh-128049. | ||
| 957 | + class BadEG1(ExceptionGroup): | ||
| 958 | + def split(self, *args): | ||
| 959 | + return "NOT A 2-TUPLE!" | ||
| 960 | + | ||
| 961 | + class BadEG2(ExceptionGroup): | ||
| 962 | + def split(self, *args): | ||
| 963 | + return ("NOT A 2-TUPLE!",) | ||
| 964 | + | ||
| 965 | + eg_list = [ | ||
| 966 | + (BadEG1("eg", [OSError(123), ValueError(456)]), | ||
| 967 | + r"split must return a tuple, not str"), | ||
| 968 | + (BadEG2("eg", [OSError(123), ValueError(456)]), | ||
| 969 | + r"split must return a 2-tuple, got tuple of size 1") | ||
| 970 | + ] | ||
| 971 | + | ||
| 972 | + for eg_class, msg in eg_list: | ||
| 973 | + with self.assertRaisesRegex(TypeError, msg) as m: | ||
| 974 | + try: | ||
| 975 | + raise eg_class | ||
| 976 | + except* ValueError: | ||
| 977 | + pass | ||
| 978 | + except* OSError: | ||
| 979 | + pass | ||
| 980 | + | ||
| 981 | + self.assertExceptionIsLike(m.exception.__context__, eg_class) | ||
| 982 | + | ||
| 983 | + # we allow tuples of length > 2 for backwards compatibility | ||
| 984 | + class WeirdEG(ExceptionGroup): | ||
| 985 | + def split(self, *args): | ||
| 986 | + return super().split(*args) + ("anything", 123456, None) | ||
| 987 | + | ||
| 988 | + try: | ||
| 989 | + raise WeirdEG("eg", [OSError(123), ValueError(456)]) | ||
| 990 | + except* OSError as e: | ||
| 991 | + oeg = e | ||
| 992 | + except* ValueError as e: | ||
| 993 | + veg = e | ||
| 994 | + | ||
| 995 | + self.assertExceptionIsLike(oeg, WeirdEG("eg", [OSError(123)])) | ||
| 996 | + self.assertExceptionIsLike(veg, WeirdEG("eg", [ValueError(456)])) | ||
| 997 | + | ||
| 955 | 998 | ||
| 956 | 999 | class TestExceptStarCleanup(ExceptStarTest): | |
| 957 | 1000 | def test_sys_exception_restored(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + Fix a bug where :keyword:`except* <except_star>` does not properly check the | ||
| 2 | + return value of an :exc:`ExceptionGroup`'s :meth:`~BaseExceptionGroup.split` | ||
| 3 | + function, leading to a crash in some cases. Now when :meth:`~BaseExceptionGroup.split` | ||
| 4 | + returns an invalid object, :keyword:`except* <except_star>` raises a :exc:`TypeError` | ||
| 5 | + with the original raised :exc:`ExceptionGroup` object chained to it. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2134,8 +2134,25 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, | |||
| 2134 | 2134 | if (pair == NULL) { | |
| 2135 | 2135 | return -1; | |
| 2136 | 2136 | } | |
| 2137 | - assert(PyTuple_CheckExact(pair)); | ||
| 2138 | - assert(PyTuple_GET_SIZE(pair) == 2); | ||
| 2137 | + | ||
| 2138 | + if (!PyTuple_CheckExact(pair)) { | ||
| 2139 | + PyErr_Format(PyExc_TypeError, | ||
| 2140 | + "%.200s.split must return a tuple, not %.200s", | ||
| 2141 | + Py_TYPE(exc_value)->tp_name, Py_TYPE(pair)->tp_name); | ||
| 2142 | + Py_DECREF(pair); | ||
| 2143 | + return -1; | ||
| 2144 | + } | ||
| 2145 | + | ||
| 2146 | + // allow tuples of length > 2 for backwards compatibility | ||
| 2147 | + if (PyTuple_GET_SIZE(pair) < 2) { | ||
| 2148 | + PyErr_Format(PyExc_TypeError, | ||
| 2149 | + "%.200s.split must return a 2-tuple, " | ||
| 2150 | + "got tuple of size %zd", | ||
| 2151 | + Py_TYPE(exc_value)->tp_name, PyTuple_GET_SIZE(pair)); | ||
| 2152 | + Py_DECREF(pair); | ||
| 2153 | + return -1; | ||
| 2154 | + } | ||
| 2155 | + | ||
| 2139 | 2156 | *match = Py_NewRef(PyTuple_GET_ITEM(pair, 0)); | |
| 2140 | 2157 | *rest = Py_NewRef(PyTuple_GET_ITEM(pair, 1)); | |
| 2141 | 2158 | Py_DECREF(pair); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments