py: Introduce and use mp_raise_type_arg helper.

To reduce code size.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-07-14 22:56:52 +10:00
parent bb00125aaa
commit 38a204ed96
10 changed files with 21 additions and 17 deletions

View File

@ -68,7 +68,7 @@ STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t no = mp_obj_get_int(no_in); mp_int_t no = mp_obj_get_int(no_in);
if (no < 0 || no >= self->num_matches) { if (no < 0 || no >= self->num_matches) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in)); mp_raise_type_arg(&mp_type_IndexError, no_in);
} }
const char *start = self->caps[no * 2]; const char *start = self->caps[no * 2];
@ -107,7 +107,7 @@ STATIC void match_span_helper(size_t n_args, const mp_obj_t *args, mp_obj_t span
if (n_args == 2) { if (n_args == 2) {
no = mp_obj_get_int(args[1]); no = mp_obj_get_int(args[1]);
if (no < 0 || no >= self->num_matches) { if (no < 0 || no >= self->num_matches) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, args[1])); mp_raise_type_arg(&mp_type_IndexError, args[1]);
} }
} }
@ -334,7 +334,7 @@ STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) {
} }
if (match_no >= (unsigned int)match->num_matches) { if (match_no >= (unsigned int)match->num_matches) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, MP_OBJ_NEW_SMALL_INT(match_no))); mp_raise_type_arg(&mp_type_IndexError, MP_OBJ_NEW_SMALL_INT(match_no));
} }
const char *start_match = match->caps[match_no * 2]; const char *start_match = match->caps[match_no * 2];

View File

@ -201,7 +201,7 @@ STATIC mp_obj_t mod_uzlib_decompress(size_t n_args, const mp_obj_t *args) {
return res; return res;
error: error:
nlr_raise(mp_obj_new_exception_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st))); mp_raise_type_arg(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st));
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_uzlib_decompress_obj, 1, 3, mod_uzlib_decompress); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_uzlib_decompress_obj, 1, 3, mod_uzlib_decompress);

View File

@ -102,9 +102,9 @@ STATIC void check_exception(void) {
mp_obj_t py_e = new_jobject(exc); mp_obj_t py_e = new_jobject(exc);
JJ1(ExceptionClear); JJ1(ExceptionClear);
if (JJ(IsInstanceOf, exc, IndexException_class)) { if (JJ(IsInstanceOf, exc, IndexException_class)) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, py_e)); mp_raise_type_arg(&mp_type_IndexError, py_e);
} }
nlr_raise(mp_obj_new_exception_arg1(&mp_type_Exception, py_e)); mp_raise_type_arg(&mp_type_Exception, py_e);
} }
} }

View File

@ -215,6 +215,7 @@ static inline mp_obj_t mp_obj_len_dyn(mp_obj_t o) {
#define mp_obj_new_exception_arg1(e_type, arg) (mp_obj_new_exception_arg1_dyn((e_type), (arg))) #define mp_obj_new_exception_arg1(e_type, arg) (mp_obj_new_exception_arg1_dyn((e_type), (arg)))
#define nlr_raise(o) (mp_raise_dyn(o)) #define nlr_raise(o) (mp_raise_dyn(o))
#define mp_raise_type_arg(type, arg) (mp_raise_dyn(mp_obj_new_exception_arg1_dyn((type), (arg))))
#define mp_raise_msg(type, msg) (mp_fun_table.raise_msg((type), (msg))) #define mp_raise_msg(type, msg) (mp_fun_table.raise_msg((type), (msg)))
#define mp_raise_OSError(er) (mp_raise_OSError_dyn(er)) #define mp_raise_OSError(er) (mp_raise_OSError_dyn(er))
#define mp_raise_NotImplementedError(msg) (mp_raise_msg(&mp_type_NotImplementedError, (msg))) #define mp_raise_NotImplementedError(msg) (mp_raise_msg(&mp_type_NotImplementedError, (msg)))

View File

@ -190,7 +190,7 @@ mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP); mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
if (elem == NULL) { if (elem == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index)); mp_raise_type_arg(&mp_type_KeyError, index);
} else { } else {
return elem->value; return elem->value;
} }
@ -206,7 +206,7 @@ STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP); mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
if (elem == NULL) { if (elem == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index)); mp_raise_type_arg(&mp_type_KeyError, index);
} else { } else {
return elem->value; return elem->value;
} }
@ -295,7 +295,7 @@ STATIC mp_obj_t dict_get_helper(size_t n_args, const mp_obj_t *args, mp_map_look
if (elem == NULL || elem->value == MP_OBJ_NULL) { if (elem == NULL || elem->value == MP_OBJ_NULL) {
if (n_args == 2) { if (n_args == 2) {
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) { if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, args[1])); mp_raise_type_arg(&mp_type_KeyError, args[1]);
} else { } else {
value = mp_const_none; value = mp_const_none;
} }

View File

@ -372,7 +372,7 @@ STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
check_set(self_in); check_set(self_in);
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) { if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, item)); mp_raise_type_arg(&mp_type_KeyError, item);
} }
return mp_const_none; return mp_const_none;
} }

View File

@ -1081,7 +1081,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
field_name = lookup; field_name = lookup;
mp_map_elem_t *key_elem = mp_map_lookup(kwargs, field_q, MP_MAP_LOOKUP); mp_map_elem_t *key_elem = mp_map_lookup(kwargs, field_q, MP_MAP_LOOKUP);
if (key_elem == NULL) { if (key_elem == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, field_q)); mp_raise_type_arg(&mp_type_KeyError, field_q);
} }
arg = key_elem->value; arg = key_elem->value;
} }

View File

@ -43,8 +43,7 @@ void *mp_pystack_alloc(size_t n_bytes) {
#endif #endif
if (MP_STATE_THREAD(pystack_cur) + n_bytes > MP_STATE_THREAD(pystack_end)) { if (MP_STATE_THREAD(pystack_cur) + n_bytes > MP_STATE_THREAD(pystack_end)) {
// out of memory in the pystack // out of memory in the pystack
nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError, mp_raise_type_arg(&mp_type_RuntimeError, MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted));
MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted)));
} }
void *ptr = MP_STATE_THREAD(pystack_cur); void *ptr = MP_STATE_THREAD(pystack_cur);
MP_STATE_THREAD(pystack_cur) += n_bytes; MP_STATE_THREAD(pystack_cur) += n_bytes;

View File

@ -1565,21 +1565,24 @@ NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg) {
#endif #endif
NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg) {
nlr_raise(mp_obj_new_exception_arg1(exc_type, arg));
}
NORETURN void mp_raise_StopIteration(mp_obj_t arg) { NORETURN void mp_raise_StopIteration(mp_obj_t arg) {
if (arg == MP_OBJ_NULL) { if (arg == MP_OBJ_NULL) {
mp_raise_type(&mp_type_StopIteration); mp_raise_type(&mp_type_StopIteration);
} else { } else {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_StopIteration, arg)); mp_raise_type_arg(&mp_type_StopIteration, arg);
} }
} }
NORETURN void mp_raise_OSError(int errno_) { NORETURN void mp_raise_OSError(int errno_) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_))); mp_raise_type_arg(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_));
} }
#if MICROPY_STACK_CHECK || MICROPY_ENABLE_PYSTACK #if MICROPY_STACK_CHECK || MICROPY_ENABLE_PYSTACK
NORETURN void mp_raise_recursion_depth(void) { NORETURN void mp_raise_recursion_depth(void) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError, mp_raise_type_arg(&mp_type_RuntimeError, MP_OBJ_NEW_QSTR(MP_QSTR_maximum_space_recursion_space_depth_space_exceeded));
MP_OBJ_NEW_QSTR(MP_QSTR_maximum_space_recursion_space_depth_space_exceeded)));
} }
#endif #endif

View File

@ -184,6 +184,7 @@ NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg);
NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg); NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg);
#endif #endif
NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg);
NORETURN void mp_raise_StopIteration(mp_obj_t arg); NORETURN void mp_raise_StopIteration(mp_obj_t arg);
NORETURN void mp_raise_OSError(int errno_); NORETURN void mp_raise_OSError(int errno_);
NORETURN void mp_raise_recursion_depth(void); NORETURN void mp_raise_recursion_depth(void);