py/objexcept: Check for incompletely constructed exceptions.

This turns the reproducer into a sensible-ish crash:

    TypeError: exceptions must derive from BaseException

Closes: #17117

Signed-off-by: Jeff Epler <jepler@unpythonic.net>
This commit is contained in:
Jeff Epler
2025-10-14 18:03:43 -05:00
committed by Damien George
parent aaa30abbcd
commit c199ba9f71
2 changed files with 9 additions and 2 deletions

View File

@@ -553,7 +553,14 @@ bool mp_obj_is_exception_type(mp_obj_t self_in) {
// return true if the given object is an instance of an exception type
bool mp_obj_is_exception_instance(mp_obj_t self_in) {
return mp_obj_is_exception_type(MP_OBJ_FROM_PTR(mp_obj_get_type(self_in)));
if (mp_obj_is_native_exception_instance(self_in)) {
return true;
}
if (!mp_obj_is_exception_type(MP_OBJ_FROM_PTR(mp_obj_get_type(self_in)))) {
return false;
}
mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
return self->subobj[0] != MP_OBJ_FROM_PTR((void *)&mp_native_base_init_wrapper_obj);
}
// Return true if exception (type or instance) is a subclass of given

View File

@@ -1508,7 +1508,7 @@ mp_obj_t mp_make_raise_obj(mp_obj_t o) {
}
if (mp_obj_is_exception_instance(o)) {
// o is an instance of an exception, so use it as the exception
// o is a fully-constructed instance of an exception, so use it as the exception
return o;
} else {
// o cannot be used as an exception, so return a type error (which will be raised by the caller)