Index: Python/ceval.c =================================================================== --- Python/ceval.c (revision 61494) +++ Python/ceval.c (working copy) @@ -4055,9 +4055,36 @@ (PyType_Check((x)) && \ PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)) -#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \ - "BaseException is not allowed in 3.x." +static int +except_target_check(PyObject *w) +{ + if (PyTuple_Check(w)) { + Py_ssize_t i, length; + length = PyTuple_GET_SIZE(w); + for (i = 0; i < length; ++i) { + PyObject *exc = PyTuple_GET_ITEM(w, i); + if (except_target_check(exc) == -1) + return -1; + } + } + else { + if (PyString_Check(w) && + PyErr_WarnEx(PyExc_DeprecationWarning, + "catching of string " + "exceptions is deprecated", 1) == -1) + return -1; + if (Py_Py3kWarningFlag && + !Py3kExceptionClass_Check(w) && + PyErr_WarnEx(PyExc_DeprecationWarning, + "catching of exceptions other than " + "instances of BaseException " + "is not allowed in 3.x", 1) == -1) + return -1; + } + return 0; +} + static PyObject * cmp_outcome(int op, register PyObject *v, register PyObject *w) { @@ -4081,55 +4108,8 @@ res = !res; break; case PyCmp_EXC_MATCH: - if (PyTuple_Check(w)) { - Py_ssize_t i, length; - length = PyTuple_Size(w); - for (i = 0; i < length; i += 1) { - PyObject *exc = PyTuple_GET_ITEM(w, i); - if (PyString_Check(exc)) { - int ret_val; - ret_val = PyErr_WarnEx( - PyExc_DeprecationWarning, - "catching of string " - "exceptions is deprecated", 1); - if (ret_val == -1) - return NULL; - } - else if (Py_Py3kWarningFlag && - !PyTuple_Check(exc) && - !Py3kExceptionClass_Check(exc)) - { - int ret_val; - ret_val = PyErr_WarnEx( - PyExc_DeprecationWarning, - CANNOT_CATCH_MSG, 1); - if (ret_val == -1) - return NULL; - } - } - } - else { - if (PyString_Check(w)) { - int ret_val; - ret_val = PyErr_WarnEx( - PyExc_DeprecationWarning, - "catching of string " - "exceptions is deprecated", 1); - if (ret_val == -1) - return NULL; - } - else if (Py_Py3kWarningFlag && - !PyTuple_Check(w) && - !Py3kExceptionClass_Check(w)) - { - int ret_val; - ret_val = PyErr_WarnEx( - PyExc_DeprecationWarning, - CANNOT_CATCH_MSG, 1); - if (ret_val == -1) - return NULL; - } - } + if (except_target_check(w) == -1) + return NULL; res = PyErr_GivenExceptionMatches(v, w); break; default: