summaryrefslogtreecommitdiff
path: root/py/objexcept.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-09-24 14:05:40 +0100
committerDamien George <damien.p.george@gmail.com>2014-09-25 15:49:26 +0100
commit4bcd04bcaddeeb53779f2c067a4d1b533c888f9d (patch)
tree4207ca05d97dabf326c0a22d17e976767657bb13 /py/objexcept.c
parent16ef60fba6b8517bf6b6f71c19ae6baa44a3f9af (diff)
py: Tidy up exception matching; allow matching of tuple of exceptions.
Addresses issue #864.
Diffstat (limited to 'py/objexcept.c')
-rw-r--r--py/objexcept.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/py/objexcept.c b/py/objexcept.c
index 6cf9cbc4b..7f0736543 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -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 true if exception (type or instance) is a subclass of given
-// exception type.
-bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
- // TODO: move implementation from RT_BINARY_OP_EXCEPTION_MATCH here.
- return mp_binary_op(MP_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true;
+// Return true if exception (type or instance) is a subclass of given
+// exception type. Assumes exc_type is a subclass of BaseException, as
+// defined by mp_obj_is_exception_type(exc_type).
+bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type) {
+ // 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