diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-21 20:46:38 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-21 20:46:38 +0000 |
commit | 7b4b78bc33fdb9b0007060877fd7c1ca2392bceb (patch) | |
tree | df82f1d9ce7ecc33281790d11c912251189aec33 /py/parsenum.c | |
parent | b035db355a995222588635d937585a7f5ab7dc93 (diff) | |
download | micropython-7b4b78bc33fdb9b0007060877fd7c1ca2392bceb.tar.gz micropython-7b4b78bc33fdb9b0007060877fd7c1ca2392bceb.zip |
py: Put back proper ValueError for badly parsed integers.
Diffstat (limited to 'py/parsenum.c')
-rw-r--r-- | py/parsenum.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/py/parsenum.c b/py/parsenum.c index 6be042fe89..7be53897a7 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -41,6 +41,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { // string should be an integer number machine_int_t int_val = 0; + const char *restrict str_val_start = str; for (; str < top; str++) { machine_int_t old_val = int_val; int dig = *str; @@ -69,6 +70,11 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { } } + // check we parsed something + if (str == str_val_start) { + goto value_error; + } + // negate value if needed if (neg) { int_val = -int_val; @@ -80,12 +86,15 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { // check we reached the end of the string if (str != top) { - nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); + goto value_error; } // return the object return MP_OBJ_NEW_SMALL_INT(int_val); +value_error: + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, str)); + overflow: // TODO reparse using bignum nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "overflow parsing integer")); |