diff options
| author | Jeff Epler <jepler@gmail.com> | 2024-01-03 19:31:35 -0600 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-01-26 22:54:58 +1100 |
| commit | 13b13d1fdd05549d504eeded0b5aa8871d5e5dcf (patch) | |
| tree | 0cd66eb9e2e2cb2ca0c6904c0093bf59ed77c1cb /tests/basics/int1.py | |
| parent | 7b3f189b1723fe642f122a3b7826d16fe32f801a (diff) | |
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>
Diffstat (limited to 'tests/basics/int1.py')
| -rw-r--r-- | tests/basics/int1.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/basics/int1.py b/tests/basics/int1.py index 2d92105c7..94723af4d 100644 --- a/tests/basics/int1.py +++ b/tests/basics/int1.py @@ -13,6 +13,7 @@ print(int('1')) print(int('+1')) print(int('-1')) print(int('01')) +print(int('00')) print(int('9')) print(int('10')) print(int('+10')) @@ -31,6 +32,7 @@ print(int(' -3 ')) print(int('0', 10)) print(int('1', 10)) print(int(' \t 1 \t ', 10)) +print(int(' \t 00 \t ', 10)) print(int('11', 10)) print(int('11', 16)) print(int('11', 8)) @@ -52,6 +54,17 @@ print(int(' \t 0o12', 8)) print(int('0o12 \t ', 8)) print(int(b"12", 10)) print(int(b"12")) +print(int('000 ', 0)) +print(int('000 ', 2)) +print(int('000 ', 8)) +print(int('000 ', 10)) +print(int('000 ', 16)) +print(int('000 ', 36)) +print(int('010 ', 2)) +print(int('010 ', 8)) +print(int('010 ', 10)) +print(int('010 ', 16)) +print(int('010 ', 36)) def test(value, base): @@ -79,6 +92,8 @@ test('0o8', 8) test('0xg', 16) test('1 1', 16) test('123', 37) +test('01', 0) +test('01 ', 0) # check that we don't parse this as a floating point number print(0x1e+1) |
