Files
micropython/tests/basics/lexer.py
Jeff Epler 13b13d1fdd 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>
2025-01-26 22:54:58 +11:00

94 lines
1.5 KiB
Python

# test the lexer
try:
eval
exec
except NameError:
print("SKIP")
raise SystemExit
# __debug__ is a special symbol
print(type(__debug__))
# short input
exec("")
exec("\n")
exec("\n\n")
exec("\r")
exec("\r\r")
exec("\t")
exec("\r\n")
exec("\nprint(1)")
exec("\rprint(2)")
exec("\r\nprint(3)")
exec("\n5")
exec("\r6")
exec("\r\n7")
print(eval("1"))
print(eval("12"))
print(eval("123"))
print(eval("1\n"))
print(eval("12\n"))
print(eval("123\n"))
print(eval("1\r"))
print(eval("12\r"))
print(eval("123\r"))
# line continuation
print(eval("'123' \\\r '456'"))
print(eval("'123' \\\n '456'"))
print(eval("'123' \\\r\n '456'"))
print(eval("'123'\\\r'456'"))
print(eval("'123'\\\n'456'"))
print(eval("'123'\\\r\n'456'"))
# backslash used to escape a line-break in a string
print('a\
b')
# lots of indentation
def a(x):
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
print(x)
a(1)
# badly formed hex escape sequences
try:
exec(r"'\x0'")
except SyntaxError:
print("SyntaxError")
try:
exec(r"b'\x0'")
except SyntaxError:
print("SyntaxError")
try:
exec(r"'\u000'")
except SyntaxError:
print("SyntaxError")
try:
exec(r"'\U0000000'")
except SyntaxError:
print("SyntaxError")
# Properly formed integer literals
print(eval("00"))
# badly formed integer literals
try:
eval("01")
except SyntaxError:
print("SyntaxError")