@@ -93,6 +93,7 @@ static int import_all_from(PyThreadState *, PyObject *, PyObject *);
93
93
static void format_exc_check_arg (PyThreadState * , PyObject * , const char * , PyObject * );
94
94
static void format_exc_unbound (PyThreadState * tstate , PyCodeObject * co , int oparg );
95
95
static int check_args_iterable (PyThreadState * , PyObject * func , PyObject * vararg );
96
+ static int check_except_type_valid (PyThreadState * tstate , PyObject * right );
96
97
static void format_kwargs_error (PyThreadState * , PyObject * func , PyObject * kwargs );
97
98
static void format_awaitable_error (PyThreadState * , PyTypeObject * , int , int );
98
99
static int get_exception_handler (PyCodeObject * , int , int * , int * , int * );
@@ -3715,31 +3716,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
3715
3716
}
3716
3717
3717
3718
TARGET (JUMP_IF_NOT_EXC_MATCH ) {
3718
- const char * cannot_catch_msg = "catching classes that do not "
3719
- "inherit from BaseException is not "
3720
- "allowed" ;
3721
3719
PyObject * right = POP ();
3722
3720
PyObject * left = TOP ();
3723
- if (PyTuple_Check (right )) {
3724
- Py_ssize_t i , length ;
3725
- length = PyTuple_GET_SIZE (right );
3726
- for (i = 0 ; i < length ; i ++ ) {
3727
- PyObject * exc = PyTuple_GET_ITEM (right , i );
3728
- if (!PyExceptionClass_Check (exc )) {
3729
- _PyErr_SetString (tstate , PyExc_TypeError ,
3730
- cannot_catch_msg );
3731
- Py_DECREF (right );
3732
- goto error ;
3733
- }
3734
- }
3735
- }
3736
- else {
3737
- if (!PyExceptionClass_Check (right )) {
3738
- _PyErr_SetString (tstate , PyExc_TypeError ,
3739
- cannot_catch_msg );
3740
- Py_DECREF (right );
3741
- goto error ;
3742
- }
3721
+ if (check_except_type_valid (tstate , right ) < 0 ) {
3722
+ Py_DECREF (right );
3723
+ goto error ;
3743
3724
}
3744
3725
int res = PyErr_GivenExceptionMatches (left , right );
3745
3726
Py_DECREF (right );
@@ -6892,6 +6873,35 @@ import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
6892
6873
return err ;
6893
6874
}
6894
6875
6876
+
6877
+ #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
6878
+ "BaseException is not allowed"
6879
+
6880
+ static int
6881
+ check_except_type_valid (PyThreadState * tstate , PyObject * right )
6882
+ {
6883
+ if (PyTuple_Check (right )) {
6884
+ Py_ssize_t i , length ;
6885
+ length = PyTuple_GET_SIZE (right );
6886
+ for (i = 0 ; i < length ; i ++ ) {
6887
+ PyObject * exc = PyTuple_GET_ITEM (right , i );
6888
+ if (!PyExceptionClass_Check (exc )) {
6889
+ _PyErr_SetString (tstate , PyExc_TypeError ,
6890
+ CANNOT_CATCH_MSG );
6891
+ return -1 ;
6892
+ }
6893
+ }
6894
+ }
6895
+ else {
6896
+ if (!PyExceptionClass_Check (right )) {
6897
+ _PyErr_SetString (tstate , PyExc_TypeError ,
6898
+ CANNOT_CATCH_MSG );
6899
+ return -1 ;
6900
+ }
6901
+ }
6902
+ return 0 ;
6903
+ }
6904
+
6895
6905
static int
6896
6906
check_args_iterable (PyThreadState * tstate , PyObject * func , PyObject * args )
6897
6907
{
0 commit comments