diff options
author | Damien George <damien.p.george@gmail.com> | 2014-09-24 14:05:40 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-09-25 15:49:26 +0100 |
commit | 4bcd04bcaddeeb53779f2c067a4d1b533c888f9d (patch) | |
tree | 4207ca05d97dabf326c0a22d17e976767657bb13 /py/runtime.c | |
parent | 16ef60fba6b8517bf6b6f71c19ae6baa44a3f9af (diff) |
py: Tidy up exception matching; allow matching of tuple of exceptions.
Addresses issue #864.
Diffstat (limited to 'py/runtime.c')
-rw-r--r-- | py/runtime.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/py/runtime.c b/py/runtime.c index 403e98c7c..1600caa06 100644 --- a/py/runtime.c +++ b/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) { // rhs must be issubclass(rhs, BaseException) if (mp_obj_is_exception_type(rhs)) { - // if lhs is an instance of an exception, then extract and use its type - if (mp_obj_is_exception_instance(lhs)) { - lhs = mp_obj_get_type(lhs); - } - if (mp_obj_is_subclass_fast(lhs, rhs)) { + if (mp_obj_exception_match(lhs, rhs)) { return mp_const_true; } else { 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); - return mp_const_false; + goto unsupported_op; } if (MP_OBJ_IS_SMALL_INT(lhs)) { |