From a92557193c85a1eebd2978cc5ee7b55d22265021 Mon Sep 17 00:00:00 2001 From: killcerr Date: Fri, 30 Jan 2026 23:15:39 +0800 Subject: [PATCH] fix #440 --- src/interpreter/ceval.c | 22 +++++++++++++++------- tests/280_exception.py | 8 ++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index f5991acb..13ba5a8e 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -1143,23 +1143,31 @@ __NEXT_STEP: } case OP_EXCEPTION_MATCH: { bool ok = false; + bool has_invalid = false; if(TOP()->type == tp_type) { ok = py_isinstance(&self->unhandled_exc, py_totype(TOP())); } else if(TOP()->type == tp_tuple) { int len = py_tuple_len(TOP()); py_ObjectRef data = py_tuple_data(TOP()); for(int i = 0; i < len; i++) { - if(data[i].type != tp_type) { - py_clearexc(TOP()); - TypeError( - "catching classes that do not inherit from BaseException is not allowed"); - ok = false; + if((data + i)->type != tp_type) { + has_invalid = true; break; } - ok = py_isinstance(data + i, py_totype(TOP())); + } + if(!has_invalid) { + for(int i = 0; i < len; i++) { + if(py_isinstance(&self->unhandled_exc, py_totype(data + i))) { + ok = true; + break; + } + } } } else { - py_clearexc(TOP()); + has_invalid = true; + } + if(has_invalid) { + py_clearexc(NULL); TypeError("catching classes that do not inherit from BaseException is not allowed"); } py_newbool(TOP(), ok); diff --git a/tests/280_exception.py b/tests/280_exception.py index 104fa4ac..2ef09218 100644 --- a/tests/280_exception.py +++ b/tests/280_exception.py @@ -206,6 +206,14 @@ try: except (ZeroDivisionError, TypeError) as e: assert type(e) == ZeroDivisionError +try: + try: + result = 10 / 0 + except (ZeroDivisionError, 1) as e: + assert type(e) == ZeroDivisionError +except Exception as e: + print(e) + """ # finally, only def finally_only():