Introduce, use mp_raise_arg1

This raises an exception with a given object value.  Saves a bit of
code size.
This commit is contained in:
Jeff Epler 2020-11-19 16:13:54 -06:00
parent d5f6748d1b
commit c06fc8e02d
10 changed files with 21 additions and 16 deletions

View File

@ -43,7 +43,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_arg1(&mp_type_IndexError, no_in);
} }
const char *start = self->caps[no * 2]; const char *start = self->caps[no * 2];
@ -82,7 +82,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_arg1(&mp_type_IndexError, args[1]);
} }
} }
@ -326,7 +326,7 @@ STATIC mp_obj_t re_sub_helper(mp_obj_t self_in, size_t n_args, const mp_obj_t *a
} }
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_arg1(&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

@ -179,7 +179,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_arg1(&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_arg1(&mp_type_IndexError, py_e);
} }
nlr_raise(mp_obj_new_exception_arg1(&mp_type_Exception, py_e)); mp_raise_arg1(&mp_type_Exception, py_e);
} }
} }

View File

@ -96,7 +96,7 @@ STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) { if (n_args == 0) {
exc = mp_obj_new_exception(&mp_type_SystemExit); exc = mp_obj_new_exception(&mp_type_SystemExit);
} else { } else {
exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]); mp_raise_arg1(&mp_type_SystemExit, args[0]);
} }
nlr_raise(exc); nlr_raise(exc);
} }

View File

@ -169,7 +169,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_arg1(&mp_type_KeyError, index);
} else { } else {
return elem->value; return elem->value;
} }
@ -185,7 +185,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_arg1(&mp_type_KeyError, index);
} else { } else {
return elem->value; return elem->value;
} }
@ -272,7 +272,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_arg1(&mp_type_KeyError, args[1]);
} else { } else {
value = mp_const_none; value = mp_const_none;
} }

View File

@ -378,7 +378,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_arg1(&mp_type_KeyError, item);
} }
return mp_const_none; return mp_const_none;
} }

View File

@ -1087,7 +1087,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_arg1(&mp_type_KeyError, field_q);
} }
arg = key_elem->value; arg = key_elem->value;
} }

View File

@ -43,8 +43,8 @@ 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_arg1(&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

@ -1514,6 +1514,10 @@ NORETURN void m_malloc_fail(size_t num_bytes) {
translate("memory allocation failed, allocating %u bytes"), (uint)num_bytes); translate("memory allocation failed, allocating %u bytes"), (uint)num_bytes);
} }
NORETURN void mp_raise_arg1(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_msg(const mp_obj_type_t *exc_type, const compressed_string_t *msg) { NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const compressed_string_t *msg) {
if (msg == NULL) { if (msg == NULL) {
nlr_raise(mp_obj_new_exception(exc_type)); nlr_raise(mp_obj_new_exception(exc_type));
@ -1580,7 +1584,7 @@ NORETURN void mp_raise_TypeError_varg(const compressed_string_t *fmt, ...) {
} }
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_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_));
} }
NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg) { NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg) {
@ -1607,7 +1611,7 @@ NORETURN void mp_raise_ConnectionError(const compressed_string_t *msg) {
} }
NORETURN void mp_raise_BrokenPipeError(void) { NORETURN void mp_raise_BrokenPipeError(void) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_BrokenPipeError, MP_OBJ_NEW_SMALL_INT(MP_EPIPE))); mp_raise_arg1(&mp_type_BrokenPipeError, MP_OBJ_NEW_SMALL_INT(MP_EPIPE));
} }
NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg) { NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg) {

View File

@ -150,6 +150,7 @@ mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
mp_obj_t mp_import_from(mp_obj_t module, qstr name); mp_obj_t mp_import_from(mp_obj_t module, qstr name);
void mp_import_all(mp_obj_t module); void mp_import_all(mp_obj_t module);
NORETURN void mp_raise_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const compressed_string_t *msg); NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const compressed_string_t *msg);
NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, const compressed_string_t *fmt, ...); NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, const compressed_string_t *fmt, ...);
NORETURN void mp_raise_msg_vlist(const mp_obj_type_t *exc_type, const compressed_string_t *fmt, va_list argptr); NORETURN void mp_raise_msg_vlist(const mp_obj_type_t *exc_type, const compressed_string_t *fmt, va_list argptr);