py/runtime: Factor out exception raising helpers.

Introduce mp_raise_msg(), mp_raise_ValueError(), mp_raise_TypeError()
instead of previous pattern nlr_raise(mp_obj_new_exception_msg(...)).
Save few bytes on each call, which are many.
This commit is contained in:
Paul Sokolovsky 2016-08-12 21:26:12 +03:00
parent af9889f99a
commit 9e1b61dedd
3 changed files with 54 additions and 47 deletions

View File

@ -231,7 +231,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
mp_int_t val = mp_obj_get_int(item);
#if MICROPY_CPYTHON_COMPAT
if (val < 0 || val > 255) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bytes value out of range"));
mp_raise_ValueError("bytes value out of range");
}
#endif
vstr_add_byte(&vstr, val);
@ -240,7 +240,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
wrong_args:
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "wrong number of arguments"));
mp_raise_TypeError("wrong number of arguments");
}
// like strstr but with specified length and allows \0 bytes
@ -436,8 +436,8 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
mp_uint_t required_len = 0;
for (mp_uint_t i = 0; i < seq_len; i++) {
if (mp_obj_get_type(seq_items[i]) != self_type) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"join expects a list of str/bytes objects consistent with self object"));
mp_raise_msg(&mp_type_TypeError,
"join expects a list of str/bytes objects consistent with self object");
}
if (i > 0) {
required_len += sep_len;
@ -511,7 +511,7 @@ mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args) {
const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
if (sep_len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
mp_raise_ValueError("empty separator");
}
for (;;) {
@ -609,7 +609,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) {
const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
if (sep_len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
mp_raise_ValueError("empty separator");
}
const byte *beg = s;
@ -672,7 +672,7 @@ STATIC mp_obj_t str_finder(mp_uint_t n_args, const mp_obj_t *args, mp_int_t dire
if (p == NULL) {
// not found
if (is_index) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "substring not found"));
mp_raise_ValueError("substring not found");
} else {
return MP_OBJ_NEW_SMALL_INT(-1);
}
@ -878,7 +878,7 @@ STATIC mp_obj_t arg_as_int(mp_obj_t arg) {
}
STATIC NORETURN void terse_str_format_value_error(void) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad format string"));
mp_raise_ValueError("bad format string");
}
STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *arg_i, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
@ -896,8 +896,8 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"single '}' encountered in format string"));
mp_raise_msg(&mp_type_ValueError,
"single '}' encountered in format string");
}
}
if (*str != '{') {
@ -936,12 +936,11 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"bad conversion specifier"));
mp_raise_ValueError("bad conversion specifier");
} else {
if (str >= top) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"end of format while looking for conversion specifier"));
mp_raise_msg(&mp_type_ValueError,
"end of format while looking for conversion specifier");
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unknown conversion specifier %c", *str));
@ -975,16 +974,14 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"unmatched '{' in format"));
mp_raise_ValueError("unmatched '{' in format");
}
}
if (*str != '}') {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"expected ':' after format specifier"));
mp_raise_ValueError("expected ':' after format specifier");
}
}
@ -997,13 +994,13 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"can't switch from automatic field numbering to manual field specification"));
mp_raise_msg(&mp_type_ValueError,
"can't switch from automatic field numbering to manual field specification");
}
}
field_name = str_to_int(field_name, field_name_top, &index);
if ((uint)index >= n_args - 1) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
mp_raise_msg(&mp_type_IndexError, "tuple index out of range");
}
arg = args[index + 1];
*arg_i = -1;
@ -1026,12 +1023,12 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"can't switch from manual field specification to automatic field numbering"));
mp_raise_msg(&mp_type_ValueError,
"can't switch from manual field specification to automatic field numbering");
}
}
if ((uint)*arg_i >= n_args - 1) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
mp_raise_msg(&mp_type_IndexError, "tuple index out of range");
}
arg = args[(*arg_i) + 1];
(*arg_i)++;
@ -1120,8 +1117,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"invalid format specifier"));
mp_raise_ValueError("invalid format specifier");
}
}
vstr_clear(&format_spec_vstr);
@ -1142,16 +1138,16 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"sign not allowed in string format specifier"));
mp_raise_msg(&mp_type_ValueError,
"sign not allowed in string format specifier");
}
}
if (type == 'c') {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"sign not allowed with integer format specifier 'c'"));
mp_raise_msg(&mp_type_ValueError,
"sign not allowed with integer format specifier 'c'");
}
}
} else {
@ -1295,8 +1291,8 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"'=' alignment not allowed in string format specifier"));
mp_raise_msg(&mp_type_ValueError,
"'=' alignment not allowed in string format specifier");
}
}
@ -1372,8 +1368,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"incomplete format key"));
mp_raise_ValueError("incomplete format key");
}
}
++str;
@ -1431,8 +1426,7 @@ incomplete_format:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_str_format_value_error();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"incomplete format"));
mp_raise_ValueError("incomplete format");
}
}
@ -1440,7 +1434,7 @@ incomplete_format:
if (arg == MP_OBJ_NULL) {
if ((uint)arg_i >= n_args) {
not_enough_args:
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "not enough arguments for format string"));
mp_raise_TypeError("not enough arguments for format string");
}
arg = args[arg_i++];
}
@ -1450,16 +1444,14 @@ not_enough_args:
mp_uint_t slen;
const char *s = mp_obj_str_get_data(arg, &slen);
if (slen != 1) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"%%c requires int or char"));
mp_raise_TypeError("%%c requires int or char");
}
mp_print_strn(&print, s, 1, flags, ' ', width);
} else if (arg_looks_integer(arg)) {
char ch = mp_obj_get_int(arg);
mp_print_strn(&print, &ch, 1, flags, ' ', width);
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"integer required"));
mp_raise_TypeError("integer required");
}
break;
@ -1529,7 +1521,7 @@ not_enough_args:
}
if ((uint)arg_i != n_args) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "not all arguments converted during string formatting"));
mp_raise_TypeError("not all arguments converted during string formatting");
}
return mp_obj_new_str_from_vstr(is_bytes ? &mp_type_bytes : &mp_type_str, &vstr);
@ -1695,7 +1687,7 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, mp_int_t directi
GET_STR_DATA_LEN(arg, sep, sep_len);
if (sep_len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
mp_raise_ValueError("empty separator");
}
mp_obj_t result[3];
@ -2061,8 +2053,7 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
STATIC void bad_implicit_conversion(mp_obj_t self_in) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"can't convert to str implicitly"));
mp_raise_TypeError("can't convert to str implicitly");
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"can't convert '%s' object to str implicitly",

View File

@ -1390,6 +1390,18 @@ void *m_malloc_fail(size_t num_bytes) {
}
}
NORETURN void mp_not_implemented(const char *msg) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, msg));
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) {
nlr_raise(mp_obj_new_exception_msg(exc_type, msg));
}
NORETURN void mp_raise_ValueError(const char *msg) {
mp_raise_msg(&mp_type_ValueError, msg);
}
NORETURN void mp_raise_TypeError(const char *msg) {
mp_raise_msg(&mp_type_TypeError, msg);
}
NORETURN void mp_not_implemented(const char *msg) {
mp_raise_msg(&mp_type_NotImplementedError, msg);
}

View File

@ -134,6 +134,10 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name);
void mp_import_all(mp_obj_t module);
// Raise NotImplementedError with given message
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg);
//NORETURN void nlr_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...);
NORETURN void mp_raise_ValueError(const char *msg);
NORETURN void mp_raise_TypeError(const char *msg);
NORETURN void mp_not_implemented(const char *msg);
NORETURN void mp_exc_recursion_depth(void);