py/parsenum: Throw an exception for invalid int literals like "01".

This includes making int("01") parse in base 10 like standard Python.
When a base of 0 is specified it means auto-detect based on the prefix, and
literals begining with 0 (except when the literal is all 0's) like "01" are
then invalid and now throw an exception.

The new error message is different from CPython. It says e.g.,
`SyntaxError: invalid syntax for integer with base 0: '09'`

Additional test cases were added to cover the changed & added code.

Co-authored-by: Damien George <damien@micropython.org>
Signed-off-by: Jeff Epler <jepler@gmail.com>
This commit is contained in:
Jeff Epler
2024-01-03 19:31:35 -06:00
committed by Damien George
parent 7b3f189b17
commit 13b13d1fdd
5 changed files with 37 additions and 21 deletions

View File

@@ -151,13 +151,13 @@ value_error:
raise_exc(exc, lex);
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
MP_ERROR_TEXT("invalid syntax for integer with base %d"), base);
MP_ERROR_TEXT("invalid syntax for integer with base %d"), base == 1 ? 0 : base);
raise_exc(exc, lex);
#else
vstr_t vstr;
mp_print_t print;
vstr_init_print(&vstr, 50, &print);
mp_printf(&print, "invalid syntax for integer with base %d: ", base);
mp_printf(&print, "invalid syntax for integer with base %d: ", base == 1 ? 0 : base);
mp_str_print_quoted(&print, str_val_start, top - str_val_start, true);
mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError,
mp_obj_new_str_from_utf8_vstr(&vstr));