py/objstr: Simplify error handling for bad conversion specifier.

This commit is contained in:
Damien George 2015-08-30 12:43:21 +01:00
parent c9fa667252
commit 000730ecaa
2 changed files with 20 additions and 10 deletions

View File

@ -894,9 +894,17 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
} else { } else {
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 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"end of format while looking for conversion specifier")); "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"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unknown conversion specifier %c", *str));
}
} }
} }
} }
@ -989,15 +997,9 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
mp_print_kind_t print_kind; mp_print_kind_t print_kind;
if (conversion == 's') { if (conversion == 's') {
print_kind = PRINT_STR; print_kind = PRINT_STR;
} else if (conversion == 'r') {
print_kind = PRINT_REPR;
} else { } else {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { assert(conversion == 'r');
terse_str_format_value_error(); print_kind = PRINT_REPR;
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"unknown conversion specifier %c", conversion));
}
} }
vstr_t arg_vstr; vstr_t arg_vstr;
mp_print_t arg_print; mp_print_t arg_print;

View File

@ -140,6 +140,13 @@ try:
except ValueError: except ValueError:
print('ValueError') print('ValueError')
# end of format parsing conversion specifier
try:
'{!'.format('a')
except ValueError:
print('ValueError')
# unknown conversion specifier
try: try:
'abc{!d}'.format('1') 'abc{!d}'.format('1')
except ValueError: except ValueError:
@ -150,6 +157,7 @@ try:
except ValueError: except ValueError:
print('ValueError') print('ValueError')
# expected ':' after specifier
try: try:
'{!s :}'.format(2) '{!s :}'.format(2)
except ValueError: except ValueError: