Files
micropython/tests/basics/syntaxerror.py
Angus Gratton 3ec8b9a77c
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
all: Replace legacy name with MicroPython and MPy as applicable.
With the aim of getting consistency, and removing the need to learn an
additional term, replace uses of uPy/uPython with MPy/MicroPython.

Rule of thumb was to use "MPy" abbreviation where "CPy" is used nearby, but
the full word MicroPython otherwise.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-10-04 00:19:40 +10:00

133 lines
2.8 KiB
Python

# test syntax errors
try:
exec
except NameError:
print("SKIP")
raise SystemExit
def test_syntax(code):
try:
exec(code)
print("no SyntaxError")
except IndentationError:
print("IndentationError")
except SyntaxError:
print("SyntaxError")
# non-newline after line-continuation character (lexer error)
test_syntax("a \\a\n")
# dedent mismatch (lexer error)
test_syntax("def f():\n a\n a\n")
# unclosed string (lexer error)
test_syntax("'abc")
# invalid (lexer error)
test_syntax("!")
test_syntax("$")
test_syntax("`")
# bad indentation (lexer error)
test_syntax(" a\n")
# malformed integer literal (parser error)
test_syntax("123z")
# input doesn't match the grammar (parser error)
test_syntax('1 or 2 or')
test_syntax('{1:')
# can't assign to literals
test_syntax("1 = 2")
test_syntax("'' = 1")
test_syntax("{} = 1")
# can't assign to comprehension
test_syntax("(i for i in a) = 1")
# can't assign to function
test_syntax("f() = 1")
# can't assign to power
test_syntax("f**2 = 1")
# can't assign to power of composite
test_syntax("f[0]**2 = 1")
# can't have *x on RHS
test_syntax("x = *x")
# can't have multiple *x on LHS
test_syntax("*a, *b = c")
# can't do augmented assignment to tuple
test_syntax("a, b += c")
test_syntax("(a, b) += c")
# can't do augmented assignment to list
test_syntax("[a, b] += c")
# non-default argument can't follow default argument
test_syntax("def f(a=1, b): pass")
# can't delete these things
test_syntax("del f()")
test_syntax("del f[0]**2")
test_syntax("del (a for a in a)")
# must be in a "loop"
test_syntax("break")
test_syntax("continue")
# must be in a function
test_syntax("yield")
test_syntax("nonlocal a")
test_syntax("await 1")
# error on MPy, warning on CPy
#test_syntax("def f():\n a = 1\n global a")
# default except must be last
test_syntax("try:\n a\nexcept:\n pass\nexcept:\n pass")
# LHS of keywords must be id's
test_syntax("f(1=2)")
# non-keyword after keyword
test_syntax("f(a=1, 2)")
# doesn't error on MPy but should
#test_syntax("f(1, i for i in i)")
# all elements of dict/set must be pairs or singles
test_syntax("{1:2, 3}")
test_syntax("{1, 2:3}")
# can't mix non-bytes with bytes when concatenating
test_syntax("'abc' b'def'")
# can't reuse same name for argument
test_syntax("def f(a, a): pass")
# nonlocal must exist in outer function/class scope
test_syntax("def f():\n def g():\n nonlocal a")
# param can't be redefined as global
test_syntax('def f(x):\n global x')
# param can't be redefined as nonlocal
test_syntax('def f(x):\n nonlocal x')
# can define variable to be both nonlocal and global
test_syntax('def f():\n nonlocal x\n global x')
# can't have multiple *'s
test_syntax('def f(x, *a, *):\n pass')
test_syntax('lambda x, *a, *: 1')
# **kw must be last
test_syntax('def f(x, *a, **kw, r):\n pass')
test_syntax('lambda x, *a, **kw, r: 1')