This commit is contained in:
killcerr 2026-01-30 23:15:39 +08:00
parent 1b2ad4cf2a
commit a92557193c
2 changed files with 23 additions and 7 deletions

View File

@ -1143,23 +1143,31 @@ __NEXT_STEP:
} }
case OP_EXCEPTION_MATCH: { case OP_EXCEPTION_MATCH: {
bool ok = false; bool ok = false;
bool has_invalid = false;
if(TOP()->type == tp_type) { if(TOP()->type == tp_type) {
ok = py_isinstance(&self->unhandled_exc, py_totype(TOP())); ok = py_isinstance(&self->unhandled_exc, py_totype(TOP()));
} else if(TOP()->type == tp_tuple) { } else if(TOP()->type == tp_tuple) {
int len = py_tuple_len(TOP()); int len = py_tuple_len(TOP());
py_ObjectRef data = py_tuple_data(TOP()); py_ObjectRef data = py_tuple_data(TOP());
for(int i = 0; i < len; i++) { for(int i = 0; i < len; i++) {
if(data[i].type != tp_type) { if((data + i)->type != tp_type) {
py_clearexc(TOP()); has_invalid = true;
TypeError(
"catching classes that do not inherit from BaseException is not allowed");
ok = false;
break; 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 { } 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"); TypeError("catching classes that do not inherit from BaseException is not allowed");
} }
py_newbool(TOP(), ok); py_newbool(TOP(), ok);

View File

@ -206,6 +206,14 @@ try:
except (ZeroDivisionError, TypeError) as e: except (ZeroDivisionError, TypeError) as e:
assert type(e) == ZeroDivisionError 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 # finally, only
def finally_only(): def finally_only():