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:
parent
af9889f99a
commit
9e1b61dedd
81
py/objstr.c
81
py/objstr.c
|
@ -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);
|
mp_int_t val = mp_obj_get_int(item);
|
||||||
#if MICROPY_CPYTHON_COMPAT
|
#if MICROPY_CPYTHON_COMPAT
|
||||||
if (val < 0 || val > 255) {
|
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
|
#endif
|
||||||
vstr_add_byte(&vstr, val);
|
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);
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||||
|
|
||||||
wrong_args:
|
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
|
// 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;
|
mp_uint_t required_len = 0;
|
||||||
for (mp_uint_t i = 0; i < seq_len; i++) {
|
for (mp_uint_t i = 0; i < seq_len; i++) {
|
||||||
if (mp_obj_get_type(seq_items[i]) != self_type) {
|
if (mp_obj_get_type(seq_items[i]) != self_type) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
mp_raise_msg(&mp_type_TypeError,
|
||||||
"join expects a list of str/bytes objects consistent with self object"));
|
"join expects a list of str/bytes objects consistent with self object");
|
||||||
}
|
}
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
required_len += sep_len;
|
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);
|
const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
|
||||||
|
|
||||||
if (sep_len == 0) {
|
if (sep_len == 0) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
|
mp_raise_ValueError("empty separator");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
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);
|
const char *sep_str = mp_obj_str_get_data(sep, &sep_len);
|
||||||
|
|
||||||
if (sep_len == 0) {
|
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;
|
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) {
|
if (p == NULL) {
|
||||||
// not found
|
// not found
|
||||||
if (is_index) {
|
if (is_index) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "substring not found"));
|
mp_raise_ValueError("substring not found");
|
||||||
} else {
|
} else {
|
||||||
return MP_OBJ_NEW_SMALL_INT(-1);
|
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) {
|
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) {
|
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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_msg(&mp_type_ValueError,
|
||||||
"single '}' encountered in format string"));
|
"single '}' encountered in format string");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (*str != '{') {
|
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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
|
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_ValueError("bad conversion specifier");
|
||||||
"bad conversion specifier"));
|
|
||||||
} else {
|
} else {
|
||||||
if (str >= top) {
|
if (str >= top) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_msg(&mp_type_ValueError,
|
||||||
"end of format while looking for conversion specifier"));
|
"end of format while looking for conversion specifier");
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
||||||
"unknown conversion specifier %c", *str));
|
"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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_ValueError("unmatched '{' in format");
|
||||||
"unmatched '{' in format"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (*str != '}') {
|
if (*str != '}') {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_ValueError("expected ':' after format specifier");
|
||||||
"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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_msg(&mp_type_ValueError,
|
||||||
"can't switch from automatic field numbering to manual field specification"));
|
"can't switch from automatic field numbering to manual field specification");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
field_name = str_to_int(field_name, field_name_top, &index);
|
field_name = str_to_int(field_name, field_name_top, &index);
|
||||||
if ((uint)index >= n_args - 1) {
|
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 = args[index + 1];
|
||||||
*arg_i = -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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_msg(&mp_type_ValueError,
|
||||||
"can't switch from manual field specification to automatic field numbering"));
|
"can't switch from manual field specification to automatic field numbering");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((uint)*arg_i >= n_args - 1) {
|
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 = args[(*arg_i) + 1];
|
||||||
(*arg_i)++;
|
(*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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_ValueError("invalid format specifier");
|
||||||
"invalid format specifier"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vstr_clear(&format_spec_vstr);
|
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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_msg(&mp_type_ValueError,
|
||||||
"sign not allowed in string format specifier"));
|
"sign not allowed in string format specifier");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type == 'c') {
|
if (type == 'c') {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_msg(&mp_type_ValueError,
|
||||||
"sign not allowed with integer format specifier 'c'"));
|
"sign not allowed with integer format specifier 'c'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_msg(&mp_type_ValueError,
|
||||||
"'=' alignment not allowed in string format specifier"));
|
"'=' 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) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_ValueError("incomplete format key");
|
||||||
"incomplete format key"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++str;
|
++str;
|
||||||
|
@ -1431,8 +1426,7 @@ incomplete_format:
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_str_format_value_error();
|
terse_str_format_value_error();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
mp_raise_ValueError("incomplete format");
|
||||||
"incomplete format"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1440,7 +1434,7 @@ incomplete_format:
|
||||||
if (arg == MP_OBJ_NULL) {
|
if (arg == MP_OBJ_NULL) {
|
||||||
if ((uint)arg_i >= n_args) {
|
if ((uint)arg_i >= n_args) {
|
||||||
not_enough_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++];
|
arg = args[arg_i++];
|
||||||
}
|
}
|
||||||
|
@ -1450,16 +1444,14 @@ not_enough_args:
|
||||||
mp_uint_t slen;
|
mp_uint_t slen;
|
||||||
const char *s = mp_obj_str_get_data(arg, &slen);
|
const char *s = mp_obj_str_get_data(arg, &slen);
|
||||||
if (slen != 1) {
|
if (slen != 1) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
mp_raise_TypeError("%%c requires int or char");
|
||||||
"%%c requires int or char"));
|
|
||||||
}
|
}
|
||||||
mp_print_strn(&print, s, 1, flags, ' ', width);
|
mp_print_strn(&print, s, 1, flags, ' ', width);
|
||||||
} else if (arg_looks_integer(arg)) {
|
} else if (arg_looks_integer(arg)) {
|
||||||
char ch = mp_obj_get_int(arg);
|
char ch = mp_obj_get_int(arg);
|
||||||
mp_print_strn(&print, &ch, 1, flags, ' ', width);
|
mp_print_strn(&print, &ch, 1, flags, ' ', width);
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
mp_raise_TypeError("integer required");
|
||||||
"integer required"));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -1529,7 +1521,7 @@ not_enough_args:
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((uint)arg_i != n_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);
|
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);
|
GET_STR_DATA_LEN(arg, sep, sep_len);
|
||||||
|
|
||||||
if (sep_len == 0) {
|
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];
|
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) {
|
STATIC void bad_implicit_conversion(mp_obj_t self_in) {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
mp_raise_TypeError("can't convert to str implicitly");
|
||||||
"can't convert to str implicitly"));
|
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
"can't convert '%s' object to str implicitly",
|
"can't convert '%s' object to str implicitly",
|
||||||
|
|
16
py/runtime.c
16
py/runtime.c
|
@ -1390,6 +1390,18 @@ void *m_malloc_fail(size_t num_bytes) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NORETURN void mp_not_implemented(const char *msg) {
|
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, 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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,6 +134,10 @@ 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);
|
||||||
|
|
||||||
// Raise NotImplementedError with given message
|
// 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_not_implemented(const char *msg);
|
||||||
NORETURN void mp_exc_recursion_depth(void);
|
NORETURN void mp_exc_recursion_depth(void);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue