py: Tidy up exception matching; allow matching of tuple of exceptions.
Addresses issue #864.
This commit is contained in:
parent
16ef60fba6
commit
4bcd04bcad
2
py/obj.h
2
py/obj.h
|
@ -458,7 +458,7 @@ mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
|
||||||
#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
|
#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
|
||||||
bool mp_obj_is_exception_type(mp_obj_t self_in);
|
bool mp_obj_is_exception_type(mp_obj_t self_in);
|
||||||
bool mp_obj_is_exception_instance(mp_obj_t self_in);
|
bool mp_obj_is_exception_instance(mp_obj_t self_in);
|
||||||
bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type);
|
bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
|
||||||
void mp_obj_exception_clear_traceback(mp_obj_t self_in);
|
void mp_obj_exception_clear_traceback(mp_obj_t self_in);
|
||||||
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block);
|
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block);
|
||||||
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values);
|
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values);
|
||||||
|
|
|
@ -403,11 +403,15 @@ bool mp_obj_is_exception_instance(mp_obj_t self_in) {
|
||||||
return mp_obj_is_exception_type(mp_obj_get_type(self_in));
|
return mp_obj_is_exception_type(mp_obj_get_type(self_in));
|
||||||
}
|
}
|
||||||
|
|
||||||
// return true if exception (type or instance) is a subclass of given
|
// Return true if exception (type or instance) is a subclass of given
|
||||||
// exception type.
|
// exception type. Assumes exc_type is a subclass of BaseException, as
|
||||||
bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
|
// defined by mp_obj_is_exception_type(exc_type).
|
||||||
// TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
|
bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type) {
|
||||||
return mp_binary_op(MP_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true;
|
// if exc is an instance of an exception, then extract and use its type
|
||||||
|
if (mp_obj_is_exception_instance(exc)) {
|
||||||
|
exc = mp_obj_get_type(exc);
|
||||||
|
}
|
||||||
|
return mp_obj_is_subclass_fast(exc, exc_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
// traceback handling functions
|
// traceback handling functions
|
||||||
|
|
21
py/runtime.c
21
py/runtime.c
|
@ -263,18 +263,25 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
|
||||||
if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
|
if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
|
||||||
// rhs must be issubclass(rhs, BaseException)
|
// rhs must be issubclass(rhs, BaseException)
|
||||||
if (mp_obj_is_exception_type(rhs)) {
|
if (mp_obj_is_exception_type(rhs)) {
|
||||||
// if lhs is an instance of an exception, then extract and use its type
|
if (mp_obj_exception_match(lhs, rhs)) {
|
||||||
if (mp_obj_is_exception_instance(lhs)) {
|
|
||||||
lhs = mp_obj_get_type(lhs);
|
|
||||||
}
|
|
||||||
if (mp_obj_is_subclass_fast(lhs, rhs)) {
|
|
||||||
return mp_const_true;
|
return mp_const_true;
|
||||||
} else {
|
} else {
|
||||||
return mp_const_false;
|
return mp_const_false;
|
||||||
}
|
}
|
||||||
|
} else if (MP_OBJ_IS_TYPE(rhs, &mp_type_tuple)) {
|
||||||
|
mp_obj_tuple_t *tuple = rhs;
|
||||||
|
for (mp_uint_t i = 0; i < tuple->len; i++) {
|
||||||
|
rhs = tuple->items[i];
|
||||||
|
if (!mp_obj_is_exception_type(rhs)) {
|
||||||
|
goto unsupported_op;
|
||||||
|
}
|
||||||
|
if (mp_obj_exception_match(lhs, rhs)) {
|
||||||
|
return mp_const_true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mp_const_false;
|
||||||
}
|
}
|
||||||
assert(0);
|
goto unsupported_op;
|
||||||
return mp_const_false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MP_OBJ_IS_SMALL_INT(lhs)) {
|
if (MP_OBJ_IS_SMALL_INT(lhs)) {
|
||||||
|
|
Loading…
Reference in New Issue