objexcept: Add mp_obj_exception_get_value() convenience function.

This gets "value" of exceptions in the sense as it's defined for
StopIteration.value (i.e. args[0] or None).

TODO: This really should be inline function.
This commit is contained in:
Paul Sokolovsky 2014-03-26 19:17:20 +02:00
parent 38f0c607b0
commit af1ae30399
2 changed files with 12 additions and 5 deletions

View File

@ -313,6 +313,7 @@ bool mp_obj_is_exception_instance(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, machine_uint_t line, qstr block);
void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
// str
extern const mp_obj_type_t str_type;

View File

@ -59,16 +59,22 @@ STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_
return o;
}
// Get exception "value" - that is, first argument, or None
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in) {
mp_obj_exception_t *self = self_in;
if (self->args.len == 0) {
return mp_const_none;
} else {
return self->args.items[0];
}
}
STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
mp_obj_exception_t *self = self_in;
if (attr == MP_QSTR_args) {
dest[0] = &self->args;
} else if (self->base.type == &mp_type_StopIteration && attr == MP_QSTR_value) {
if (self->args.len == 0) {
dest[0] = mp_const_none;
} else {
dest[0] = self->args.items[0];
}
dest[0] = mp_obj_exception_get_value(self);
}
}