syntaxerror.py 2.9 KB

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