Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1 | """This module tests SyntaxErrors. |
| 2 | |
| 3 | Here's an example of the sort of thing that is tested. |
| 4 | |
| 5 | >>> def f(x): |
| 6 | ... global x |
| 7 | Traceback (most recent call last): |
| 8 | SyntaxError: name 'x' is local and global |
| 9 | |
| 10 | The tests are all raise SyntaxErrors. They were created by checking |
| 11 | each C call that raises SyntaxError. There are several modules that |
| 12 | raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and |
| 13 | symtable.c. |
| 14 | |
| 15 | The parser itself outlaws a lot of invalid syntax. None of these |
| 16 | errors are tested here at the moment. We should add some tests; since |
| 17 | there are infinitely many programs with invalid syntax, we would need |
| 18 | to be judicious in selecting some. |
| 19 | |
| 20 | The compiler generates a synthetic module name for code executed by |
| 21 | doctest. Since all the code comes from the same module, a suffix like |
| 22 | [1] is appended to the module name, As a consequence, changing the |
| 23 | order of tests in this module means renumbering all the errors after |
| 24 | it. (Maybe we should enable the ellipsis option for these tests.) |
| 25 | |
| 26 | In ast.c, syntax errors are raised by calling ast_error(). |
| 27 | |
| 28 | Errors from set_context(): |
| 29 | |
| 30 | TODO(jhylton): "assignment to None" is inconsistent with other messages |
| 31 | |
| 32 | >>> obj.None = 1 |
| 33 | Traceback (most recent call last): |
| 34 | SyntaxError: assignment to None (<doctest test.test_syntax[1]>, line 1) |
| 35 | |
| 36 | >>> None = 1 |
| 37 | Traceback (most recent call last): |
| 38 | SyntaxError: assignment to None (<doctest test.test_syntax[2]>, line 1) |
| 39 | |
| 40 | It's a syntax error to assign to the empty tuple. Why isn't it an |
| 41 | error to assign to the empty list? It will always raise some error at |
| 42 | runtime. |
| 43 | |
| 44 | >>> () = 1 |
| 45 | Traceback (most recent call last): |
| 46 | SyntaxError: can't assign to () (<doctest test.test_syntax[3]>, line 1) |
| 47 | |
| 48 | >>> f() = 1 |
| 49 | Traceback (most recent call last): |
| 50 | SyntaxError: can't assign to function call (<doctest test.test_syntax[4]>, line 1) |
| 51 | |
| 52 | >>> del f() |
| 53 | Traceback (most recent call last): |
| 54 | SyntaxError: can't delete function call (<doctest test.test_syntax[5]>, line 1) |
| 55 | |
| 56 | >>> a + 1 = 2 |
| 57 | Traceback (most recent call last): |
| 58 | SyntaxError: can't assign to operator (<doctest test.test_syntax[6]>, line 1) |
| 59 | |
| 60 | >>> (x for x in x) = 1 |
| 61 | Traceback (most recent call last): |
| 62 | SyntaxError: can't assign to generator expression (<doctest test.test_syntax[7]>, line 1) |
| 63 | |
| 64 | >>> 1 = 1 |
| 65 | Traceback (most recent call last): |
| 66 | SyntaxError: can't assign to literal (<doctest test.test_syntax[8]>, line 1) |
| 67 | |
| 68 | >>> "abc" = 1 |
| 69 | Traceback (most recent call last): |
| 70 | SyntaxError: can't assign to literal (<doctest test.test_syntax[9]>, line 1) |
| 71 | |
| 72 | >>> `1` = 1 |
| 73 | Traceback (most recent call last): |
| 74 | SyntaxError: can't assign to repr (<doctest test.test_syntax[10]>, line 1) |
| 75 | |
| 76 | If the left-hand side of an assignment is a list or tuple, an illegal |
| 77 | expression inside that contain should still cause a syntax error. |
| 78 | This test just checks a couple of cases rather than enumerating all of |
| 79 | them. |
| 80 | |
| 81 | >>> (a, "b", c) = (1, 2, 3) |
| 82 | Traceback (most recent call last): |
| 83 | SyntaxError: can't assign to literal (<doctest test.test_syntax[11]>, line 1) |
| 84 | |
| 85 | >>> [a, b, c + 1] = [1, 2, 3] |
| 86 | Traceback (most recent call last): |
| 87 | SyntaxError: can't assign to operator (<doctest test.test_syntax[12]>, line 1) |
| 88 | |
| 89 | |
| 90 | From compiler_complex_args(): |
| 91 | |
| 92 | >>> def f(None=1): |
| 93 | ... pass |
| 94 | Traceback (most recent call last): |
| 95 | SyntaxError: assignment to None (<doctest test.test_syntax[13]>, line 1) |
| 96 | |
| 97 | |
| 98 | From ast_for_arguments(): |
| 99 | |
| 100 | >>> def f(x, y=1, z): |
| 101 | ... pass |
| 102 | Traceback (most recent call last): |
| 103 | SyntaxError: non-default argument follows default argument (<doctest test.test_syntax[14]>, line 1) |
| 104 | |
| 105 | >>> def f(x, None): |
| 106 | ... pass |
| 107 | Traceback (most recent call last): |
| 108 | SyntaxError: assignment to None (<doctest test.test_syntax[15]>, line 1) |
| 109 | |
| 110 | >>> def f(*None): |
| 111 | ... pass |
| 112 | Traceback (most recent call last): |
| 113 | SyntaxError: assignment to None (<doctest test.test_syntax[16]>, line 1) |
| 114 | |
| 115 | >>> def f(**None): |
| 116 | ... pass |
| 117 | Traceback (most recent call last): |
| 118 | SyntaxError: assignment to None (<doctest test.test_syntax[17]>, line 1) |
| 119 | |
| 120 | |
| 121 | From ast_for_funcdef(): |
| 122 | |
| 123 | >>> def None(x): |
| 124 | ... pass |
| 125 | Traceback (most recent call last): |
| 126 | SyntaxError: assignment to None (<doctest test.test_syntax[18]>, line 1) |
| 127 | |
| 128 | |
| 129 | From ast_for_call(): |
| 130 | |
| 131 | >>> def f(it, *varargs): |
| 132 | ... return list(it) |
| 133 | >>> L = range(10) |
| 134 | >>> f(x for x in L) |
| 135 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 136 | >>> f(x for x in L, 1) |
| 137 | Traceback (most recent call last): |
| 138 | SyntaxError: Generator expression must be parenthesized if not sole argument (<doctest test.test_syntax[22]>, line 1) |
| 139 | >>> f((x for x in L), 1) |
| 140 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 141 | |
| 142 | >>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, |
| 143 | ... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, |
| 144 | ... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, |
| 145 | ... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, |
| 146 | ... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, |
| 147 | ... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, |
| 148 | ... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, |
| 149 | ... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, |
| 150 | ... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, |
| 151 | ... i100, i101, i102, i103, i104, i105, i106, i107, i108, |
| 152 | ... i109, i110, i111, i112, i113, i114, i115, i116, i117, |
| 153 | ... i118, i119, i120, i121, i122, i123, i124, i125, i126, |
| 154 | ... i127, i128, i129, i130, i131, i132, i133, i134, i135, |
| 155 | ... i136, i137, i138, i139, i140, i141, i142, i143, i144, |
| 156 | ... i145, i146, i147, i148, i149, i150, i151, i152, i153, |
| 157 | ... i154, i155, i156, i157, i158, i159, i160, i161, i162, |
| 158 | ... i163, i164, i165, i166, i167, i168, i169, i170, i171, |
| 159 | ... i172, i173, i174, i175, i176, i177, i178, i179, i180, |
| 160 | ... i181, i182, i183, i184, i185, i186, i187, i188, i189, |
| 161 | ... i190, i191, i192, i193, i194, i195, i196, i197, i198, |
| 162 | ... i199, i200, i201, i202, i203, i204, i205, i206, i207, |
| 163 | ... i208, i209, i210, i211, i212, i213, i214, i215, i216, |
| 164 | ... i217, i218, i219, i220, i221, i222, i223, i224, i225, |
| 165 | ... i226, i227, i228, i229, i230, i231, i232, i233, i234, |
| 166 | ... i235, i236, i237, i238, i239, i240, i241, i242, i243, |
| 167 | ... i244, i245, i246, i247, i248, i249, i250, i251, i252, |
| 168 | ... i253, i254, i255) |
| 169 | Traceback (most recent call last): |
| 170 | SyntaxError: more than 255 arguments (<doctest test.test_syntax[24]>, line 1) |
| 171 | |
| 172 | The actual error cases counts positional arguments, keyword arguments, |
| 173 | and generator expression arguments separately. This test combines the |
| 174 | three. |
| 175 | |
| 176 | >>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, |
| 177 | ... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, |
| 178 | ... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, |
| 179 | ... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, |
| 180 | ... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, |
| 181 | ... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, |
| 182 | ... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, |
| 183 | ... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, |
| 184 | ... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, |
| 185 | ... i100, i101, i102, i103, i104, i105, i106, i107, i108, |
| 186 | ... i109, i110, i111, i112, i113, i114, i115, i116, i117, |
| 187 | ... i118, i119, i120, i121, i122, i123, i124, i125, i126, |
| 188 | ... i127, i128, i129, i130, i131, i132, i133, i134, i135, |
| 189 | ... i136, i137, i138, i139, i140, i141, i142, i143, i144, |
| 190 | ... i145, i146, i147, i148, i149, i150, i151, i152, i153, |
| 191 | ... i154, i155, i156, i157, i158, i159, i160, i161, i162, |
| 192 | ... i163, i164, i165, i166, i167, i168, i169, i170, i171, |
| 193 | ... i172, i173, i174, i175, i176, i177, i178, i179, i180, |
| 194 | ... i181, i182, i183, i184, i185, i186, i187, i188, i189, |
| 195 | ... i190, i191, i192, i193, i194, i195, i196, i197, i198, |
| 196 | ... i199, i200, i201, i202, i203, i204, i205, i206, i207, |
| 197 | ... i208, i209, i210, i211, i212, i213, i214, i215, i216, |
| 198 | ... i217, i218, i219, i220, i221, i222, i223, i224, i225, |
| 199 | ... i226, i227, i228, i229, i230, i231, i232, i233, i234, |
| 200 | ... i235, i236, i237, i238, i239, i240, i241, i242, i243, |
| 201 | ... (x for x in i244), i245, i246, i247, i248, i249, i250, i251, |
| 202 | ... i252=1, i253=1, i254=1, i255=1) |
| 203 | Traceback (most recent call last): |
| 204 | SyntaxError: more than 255 arguments (<doctest test.test_syntax[25]>, line 1) |
| 205 | |
| 206 | >>> f(lambda x: x[0] = 3) |
| 207 | Traceback (most recent call last): |
| 208 | SyntaxError: lambda cannot contain assignment (<doctest test.test_syntax[26]>, line 1) |
| 209 | |
| 210 | The grammar accepts any test (basically, any expression) in the |
| 211 | keyword slot of a call site. Test a few different options. |
| 212 | |
| 213 | >>> f(x()=2) |
| 214 | Traceback (most recent call last): |
| 215 | SyntaxError: keyword can't be an expression (<doctest test.test_syntax[27]>, line 1) |
| 216 | >>> f(a or b=1) |
| 217 | Traceback (most recent call last): |
| 218 | SyntaxError: keyword can't be an expression (<doctest test.test_syntax[28]>, line 1) |
| 219 | >>> f(x.y=1) |
| 220 | Traceback (most recent call last): |
| 221 | SyntaxError: keyword can't be an expression (<doctest test.test_syntax[29]>, line 1) |
| 222 | |
| 223 | |
| 224 | From ast_for_expr_stmt(): |
| 225 | |
| 226 | >>> (x for x in x) += 1 |
| 227 | Traceback (most recent call last): |
| 228 | SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_syntax[30]>, line 1) |
| 229 | >>> None += 1 |
| 230 | Traceback (most recent call last): |
| 231 | SyntaxError: assignment to None (<doctest test.test_syntax[31]>, line 1) |
| 232 | >>> f() += 1 |
| 233 | Traceback (most recent call last): |
| 234 | SyntaxError: illegal expression for augmented assignment (<doctest test.test_syntax[32]>, line 1) |
| 235 | """ |
| 236 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 237 | import re |
| 238 | import unittest |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 239 | import warnings |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 240 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 241 | from test import test_support |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 242 | |
| 243 | class SyntaxTestCase(unittest.TestCase): |
| 244 | |
| 245 | def _check_error(self, code, errtext, |
Martin v. Löwis | 7773671 | 2006-05-04 05:51:03 +0000 | [diff] [blame] | 246 | filename="<testcase>", mode="exec", subclass=None): |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 247 | """Check that compiling code raises SyntaxError with errtext. |
| 248 | |
| 249 | errtest is a regular expression that must be present in the |
Martin v. Löwis | 7773671 | 2006-05-04 05:51:03 +0000 | [diff] [blame] | 250 | test of the exception raised. If subclass is specified it |
| 251 | is the expected subclass of SyntaxError (e.g. IndentationError). |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 252 | """ |
| 253 | try: |
| 254 | compile(code, filename, mode) |
| 255 | except SyntaxError, err: |
Martin v. Löwis | 7773671 | 2006-05-04 05:51:03 +0000 | [diff] [blame] | 256 | if subclass and not isinstance(err, subclass): |
| 257 | self.fail("SyntaxError is not a %s" % subclass.__name__) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 258 | mo = re.search(errtext, str(err)) |
| 259 | if mo is None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 260 | self.fail("SyntaxError did not contain '%r'" % (errtext,)) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 261 | else: |
| 262 | self.fail("compile() did not raise SyntaxError") |
| 263 | |
| 264 | def test_assign_call(self): |
| 265 | self._check_error("f() = 1", "assign") |
| 266 | |
| 267 | def test_assign_del(self): |
| 268 | self._check_error("del f()", "delete") |
| 269 | |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 270 | def test_global_err_then_warn(self): |
| 271 | # Bug tickler: The SyntaxError raised for one global statement |
| 272 | # shouldn't be clobbered by a SyntaxWarning issued for a later one. |
| 273 | source = re.sub('(?m)^ *:', '', """\ |
| 274 | :def error(a): |
| 275 | : global a # SyntaxError |
| 276 | :def warning(): |
| 277 | : b = 1 |
| 278 | : global b # SyntaxWarning |
| 279 | :""") |
| 280 | warnings.filterwarnings(action='ignore', category=SyntaxWarning) |
| 281 | self._check_error(source, "global") |
| 282 | warnings.filters.pop(0) |
| 283 | |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 284 | def test_break_outside_loop(self): |
| 285 | self._check_error("break", "outside loop") |
| 286 | |
| 287 | def test_delete_deref(self): |
| 288 | source = re.sub('(?m)^ *:', '', """\ |
| 289 | :def foo(x): |
| 290 | : def bar(): |
| 291 | : print x |
| 292 | : del x |
| 293 | :""") |
| 294 | self._check_error(source, "nested scope") |
| 295 | |
Martin v. Löwis | 7773671 | 2006-05-04 05:51:03 +0000 | [diff] [blame] | 296 | def test_unexpected_indent(self): |
| 297 | self._check_error("foo()\n bar()\n", "unexpected indent", |
| 298 | subclass=IndentationError) |
| 299 | |
| 300 | def test_no_indent(self): |
| 301 | self._check_error("if 1:\nfoo()", "expected an indented block", |
| 302 | subclass=IndentationError) |
| 303 | |
| 304 | def test_bad_outdent(self): |
| 305 | self._check_error("if 1:\n foo()\n bar()", |
| 306 | "unindent does not match .* level", |
| 307 | subclass=IndentationError) |
| 308 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 309 | def test_main(): |
| 310 | test_support.run_unittest(SyntaxTestCase) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 311 | from test import test_syntax |
| 312 | test_support.run_doctest(test_syntax, verbosity=True) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 313 | |
| 314 | if __name__ == "__main__": |
| 315 | test_main() |