Skip to content

Commit 7b4b78b

Browse files
committed
py: Put back proper ValueError for badly parsed integers.
1 parent b035db3 commit 7b4b78b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

py/parsenum.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
4141

4242
// string should be an integer number
4343
machine_int_t int_val = 0;
44+
const char *restrict str_val_start = str;
4445
for (; str < top; str++) {
4546
machine_int_t old_val = int_val;
4647
int dig = *str;
@@ -69,6 +70,11 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
6970
}
7071
}
7172

73+
// check we parsed something
74+
if (str == str_val_start) {
75+
goto value_error;
76+
}
77+
7278
// negate value if needed
7379
if (neg) {
7480
int_val = -int_val;
@@ -80,12 +86,15 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
8086

8187
// check we reached the end of the string
8288
if (str != top) {
83-
nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
89+
goto value_error;
8490
}
8591

8692
// return the object
8793
return MP_OBJ_NEW_SMALL_INT(int_val);
8894

95+
value_error:
96+
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, str));
97+
8998
overflow:
9099
// TODO reparse using bignum
91100
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "overflow parsing integer"));

0 commit comments

Comments
 (0)