1# test syntax errors 2 3try: 4 exec 5except NameError: 6 print("SKIP") 7 raise SystemExit 8 9def test_syntax(code): 10 try: 11 exec(code) 12 print("no SyntaxError") 13 except IndentationError: 14 print("IndentationError") 15 except SyntaxError: 16 print("SyntaxError") 17 18# non-newline after line-continuation character (lexer error) 19test_syntax("a \\a\n") 20 21# dedent mismatch (lexer error) 22test_syntax("def f():\n a\n a\n") 23 24# unclosed string (lexer error) 25test_syntax("'abc") 26 27# invalid (lexer error) 28test_syntax("!") 29test_syntax("$") 30test_syntax("`") 31 32# bad indentation (lexer error) 33test_syntax(" a\n") 34 35# malformed integer literal (parser error) 36test_syntax("123z") 37 38# input doesn't match the grammar (parser error) 39test_syntax('1 or 2 or') 40test_syntax('{1:') 41 42# can't assign to literals 43test_syntax("1 = 2") 44test_syntax("'' = 1") 45test_syntax("{} = 1") 46 47# can't assign to comprehension 48test_syntax("(i for i in a) = 1") 49 50# can't assign to function 51test_syntax("f() = 1") 52 53# can't assign to power 54test_syntax("f**2 = 1") 55 56# can't assign to power of composite 57test_syntax("f[0]**2 = 1") 58 59# can't have *x on RHS 60test_syntax("x = *x") 61 62# can't have multiple *x on LHS 63test_syntax("*a, *b = c") 64 65# can't do augmented assignment to tuple 66test_syntax("a, b += c") 67test_syntax("(a, b) += c") 68 69# can't do augmented assignment to list 70test_syntax("[a, b] += c") 71 72# non-default argument can't follow default argument 73test_syntax("def f(a=1, b): pass") 74 75# can't delete these things 76test_syntax("del f()") 77test_syntax("del f[0]**2") 78test_syntax("del (a for a in a)") 79 80# must be in a "loop" 81test_syntax("break") 82test_syntax("continue") 83 84# must be in a function 85test_syntax("yield") 86test_syntax("nonlocal a") 87test_syntax("await 1") 88 89# error on uPy, warning on CPy 90#test_syntax("def f():\n a = 1\n global a") 91 92# default except must be last 93test_syntax("try:\n a\nexcept:\n pass\nexcept:\n pass") 94 95# LHS of keywords must be id's 96test_syntax("f(1=2)") 97 98# non-keyword after keyword 99test_syntax("f(a=1, 2)") 100 101# doesn't error on uPy but should 102#test_syntax("f(1, i for i in i)") 103 104# all elements of dict/set must be pairs or singles 105test_syntax("{1:2, 3}") 106test_syntax("{1, 2:3}") 107 108# can't mix non-bytes with bytes when concatenating 109test_syntax("'abc' b'def'") 110 111# can't reuse same name for argument 112test_syntax("def f(a, a): pass") 113 114# nonlocal must exist in outer function/class scope 115test_syntax("def f():\n def g():\n nonlocal a") 116 117# param can't be redefined as global 118test_syntax('def f(x):\n global x') 119 120# param can't be redefined as nonlocal 121test_syntax('def f(x):\n nonlocal x') 122 123# can define variable to be both nonlocal and global 124test_syntax('def f():\n nonlocal x\n global x') 125 126# can't have multiple *'s 127test_syntax('def f(x, *a, *):\n pass') 128test_syntax('lambda x, *a, *: 1') 129 130# **kw must be last 131test_syntax('def f(x, *a, **kw, r):\n pass') 132test_syntax('lambda x, *a, **kw, r: 1') 133