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): |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 8 | SyntaxError: name 'x' is parameter and global |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 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 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 30 | >>> obj.None = 1 |
| 31 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 32 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 33 | |
| 34 | >>> None = 1 |
| 35 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 36 | SyntaxError: assignment to keyword |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 37 | |
| 38 | It's a syntax error to assign to the empty tuple. Why isn't it an |
| 39 | error to assign to the empty list? It will always raise some error at |
| 40 | runtime. |
| 41 | |
| 42 | >>> () = 1 |
| 43 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 44 | SyntaxError: can't assign to () |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 45 | |
| 46 | >>> f() = 1 |
| 47 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 48 | SyntaxError: can't assign to function call |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 49 | |
| 50 | >>> del f() |
| 51 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 52 | SyntaxError: can't delete function call |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 53 | |
| 54 | >>> a + 1 = 2 |
| 55 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 56 | SyntaxError: can't assign to operator |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 57 | |
| 58 | >>> (x for x in x) = 1 |
| 59 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 60 | SyntaxError: can't assign to generator expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 61 | |
| 62 | >>> 1 = 1 |
| 63 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 64 | SyntaxError: can't assign to literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 65 | |
| 66 | >>> "abc" = 1 |
| 67 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 68 | SyntaxError: can't assign to literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 70 | >>> b"" = 1 |
| 71 | Traceback (most recent call last): |
| 72 | SyntaxError: can't assign to literal |
| 73 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 74 | >>> `1` = 1 |
| 75 | Traceback (most recent call last): |
Brett Cannon | 8933cb4 | 2006-08-25 04:12:10 +0000 | [diff] [blame] | 76 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 77 | |
| 78 | If the left-hand side of an assignment is a list or tuple, an illegal |
| 79 | expression inside that contain should still cause a syntax error. |
| 80 | This test just checks a couple of cases rather than enumerating all of |
| 81 | them. |
| 82 | |
| 83 | >>> (a, "b", c) = (1, 2, 3) |
| 84 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 85 | SyntaxError: can't assign to literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 86 | |
| 87 | >>> [a, b, c + 1] = [1, 2, 3] |
| 88 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 89 | SyntaxError: can't assign to operator |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 91 | >>> a if 1 else b = 1 |
| 92 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 93 | SyntaxError: can't assign to conditional expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 94 | |
| 95 | From compiler_complex_args(): |
| 96 | |
| 97 | >>> def f(None=1): |
| 98 | ... pass |
| 99 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 100 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | From ast_for_arguments(): |
| 104 | |
| 105 | >>> def f(x, y=1, z): |
| 106 | ... pass |
| 107 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 108 | SyntaxError: non-default argument follows default argument |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 109 | |
| 110 | >>> def f(x, None): |
| 111 | ... pass |
| 112 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 113 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 114 | |
| 115 | >>> def f(*None): |
| 116 | ... pass |
| 117 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 118 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 119 | |
| 120 | >>> def f(**None): |
| 121 | ... pass |
| 122 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 123 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | From ast_for_funcdef(): |
| 127 | |
| 128 | >>> def None(x): |
| 129 | ... pass |
| 130 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 131 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 132 | |
| 133 | |
| 134 | From ast_for_call(): |
| 135 | |
| 136 | >>> def f(it, *varargs): |
| 137 | ... return list(it) |
| 138 | >>> L = range(10) |
| 139 | >>> f(x for x in L) |
| 140 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 141 | >>> f(x for x in L, 1) |
| 142 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 143 | SyntaxError: Generator expression must be parenthesized if not sole argument |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 144 | >>> f((x for x in L), 1) |
| 145 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 146 | |
| 147 | >>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, |
| 148 | ... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, |
| 149 | ... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, |
| 150 | ... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, |
| 151 | ... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, |
| 152 | ... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, |
| 153 | ... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, |
| 154 | ... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, |
| 155 | ... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, |
| 156 | ... i100, i101, i102, i103, i104, i105, i106, i107, i108, |
| 157 | ... i109, i110, i111, i112, i113, i114, i115, i116, i117, |
| 158 | ... i118, i119, i120, i121, i122, i123, i124, i125, i126, |
| 159 | ... i127, i128, i129, i130, i131, i132, i133, i134, i135, |
| 160 | ... i136, i137, i138, i139, i140, i141, i142, i143, i144, |
| 161 | ... i145, i146, i147, i148, i149, i150, i151, i152, i153, |
| 162 | ... i154, i155, i156, i157, i158, i159, i160, i161, i162, |
| 163 | ... i163, i164, i165, i166, i167, i168, i169, i170, i171, |
| 164 | ... i172, i173, i174, i175, i176, i177, i178, i179, i180, |
| 165 | ... i181, i182, i183, i184, i185, i186, i187, i188, i189, |
| 166 | ... i190, i191, i192, i193, i194, i195, i196, i197, i198, |
| 167 | ... i199, i200, i201, i202, i203, i204, i205, i206, i207, |
| 168 | ... i208, i209, i210, i211, i212, i213, i214, i215, i216, |
| 169 | ... i217, i218, i219, i220, i221, i222, i223, i224, i225, |
| 170 | ... i226, i227, i228, i229, i230, i231, i232, i233, i234, |
| 171 | ... i235, i236, i237, i238, i239, i240, i241, i242, i243, |
| 172 | ... i244, i245, i246, i247, i248, i249, i250, i251, i252, |
| 173 | ... i253, i254, i255) |
| 174 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 175 | SyntaxError: more than 255 arguments |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 176 | |
| 177 | The actual error cases counts positional arguments, keyword arguments, |
| 178 | and generator expression arguments separately. This test combines the |
| 179 | three. |
| 180 | |
| 181 | >>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, |
| 182 | ... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, |
| 183 | ... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, |
| 184 | ... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, |
| 185 | ... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, |
| 186 | ... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, |
| 187 | ... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, |
| 188 | ... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, |
| 189 | ... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, |
| 190 | ... i100, i101, i102, i103, i104, i105, i106, i107, i108, |
| 191 | ... i109, i110, i111, i112, i113, i114, i115, i116, i117, |
| 192 | ... i118, i119, i120, i121, i122, i123, i124, i125, i126, |
| 193 | ... i127, i128, i129, i130, i131, i132, i133, i134, i135, |
| 194 | ... i136, i137, i138, i139, i140, i141, i142, i143, i144, |
| 195 | ... i145, i146, i147, i148, i149, i150, i151, i152, i153, |
| 196 | ... i154, i155, i156, i157, i158, i159, i160, i161, i162, |
| 197 | ... i163, i164, i165, i166, i167, i168, i169, i170, i171, |
| 198 | ... i172, i173, i174, i175, i176, i177, i178, i179, i180, |
| 199 | ... i181, i182, i183, i184, i185, i186, i187, i188, i189, |
| 200 | ... i190, i191, i192, i193, i194, i195, i196, i197, i198, |
| 201 | ... i199, i200, i201, i202, i203, i204, i205, i206, i207, |
| 202 | ... i208, i209, i210, i211, i212, i213, i214, i215, i216, |
| 203 | ... i217, i218, i219, i220, i221, i222, i223, i224, i225, |
| 204 | ... i226, i227, i228, i229, i230, i231, i232, i233, i234, |
| 205 | ... i235, i236, i237, i238, i239, i240, i241, i242, i243, |
| 206 | ... (x for x in i244), i245, i246, i247, i248, i249, i250, i251, |
| 207 | ... i252=1, i253=1, i254=1, i255=1) |
| 208 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 209 | SyntaxError: more than 255 arguments |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 210 | |
| 211 | >>> f(lambda x: x[0] = 3) |
| 212 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 213 | SyntaxError: lambda cannot contain assignment |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 214 | |
| 215 | The grammar accepts any test (basically, any expression) in the |
| 216 | keyword slot of a call site. Test a few different options. |
| 217 | |
| 218 | >>> f(x()=2) |
| 219 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 220 | SyntaxError: keyword can't be an expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 221 | >>> f(a or b=1) |
| 222 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 223 | SyntaxError: keyword can't be an expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 224 | >>> f(x.y=1) |
| 225 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 226 | SyntaxError: keyword can't be an expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 227 | |
| 228 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 229 | More set_context(): |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 230 | |
| 231 | >>> (x for x in x) += 1 |
| 232 | Traceback (most recent call last): |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 233 | SyntaxError: can't assign to generator expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 234 | >>> None += 1 |
| 235 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 236 | SyntaxError: assignment to keyword |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 237 | >>> f() += 1 |
| 238 | Traceback (most recent call last): |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 239 | SyntaxError: can't assign to function call |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | Test continue in finally in weird combinations. |
| 243 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 244 | continue in for loop under finally should be ok. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 245 | |
| 246 | >>> def test(): |
| 247 | ... try: |
| 248 | ... pass |
| 249 | ... finally: |
| 250 | ... for abc in range(10): |
| 251 | ... continue |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 252 | ... print(abc) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 253 | >>> test() |
| 254 | 9 |
| 255 | |
| 256 | Start simple, a continue in a finally should not be allowed. |
| 257 | |
| 258 | >>> def test(): |
| 259 | ... for abc in range(10): |
| 260 | ... try: |
| 261 | ... pass |
| 262 | ... finally: |
| 263 | ... continue |
| 264 | Traceback (most recent call last): |
| 265 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 266 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 267 | |
| 268 | This is essentially a continue in a finally which should not be allowed. |
| 269 | |
| 270 | >>> def test(): |
| 271 | ... for abc in range(10): |
| 272 | ... try: |
| 273 | ... pass |
| 274 | ... finally: |
| 275 | ... try: |
| 276 | ... continue |
| 277 | ... except: |
| 278 | ... pass |
| 279 | Traceback (most recent call last): |
| 280 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 281 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 282 | |
| 283 | >>> def foo(): |
| 284 | ... try: |
| 285 | ... pass |
| 286 | ... finally: |
| 287 | ... continue |
| 288 | Traceback (most recent call last): |
| 289 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 290 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 291 | |
| 292 | >>> def foo(): |
| 293 | ... for a in (): |
| 294 | ... try: |
| 295 | ... pass |
| 296 | ... finally: |
| 297 | ... continue |
| 298 | Traceback (most recent call last): |
| 299 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 300 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 301 | |
| 302 | >>> def foo(): |
| 303 | ... for a in (): |
| 304 | ... try: |
| 305 | ... pass |
| 306 | ... finally: |
| 307 | ... try: |
| 308 | ... continue |
| 309 | ... finally: |
| 310 | ... pass |
| 311 | Traceback (most recent call last): |
| 312 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 313 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 314 | |
| 315 | >>> def foo(): |
| 316 | ... for a in (): |
| 317 | ... try: pass |
| 318 | ... finally: |
| 319 | ... try: |
| 320 | ... pass |
| 321 | ... except: |
| 322 | ... continue |
| 323 | Traceback (most recent call last): |
| 324 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 325 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 326 | |
| 327 | There is one test for a break that is not in a loop. The compiler |
| 328 | uses a single data structure to keep track of try-finally and loops, |
| 329 | so we need to be sure that a break is actually inside a loop. If it |
| 330 | isn't, there should be a syntax error. |
| 331 | |
| 332 | >>> try: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 333 | ... print(1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 334 | ... break |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 335 | ... print(2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 336 | ... finally: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 337 | ... print(3) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 338 | Traceback (most recent call last): |
| 339 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 340 | SyntaxError: 'break' outside loop |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 341 | |
| 342 | This should probably raise a better error than a SystemError (or none at all). |
| 343 | In 2.5 there was a missing exception and an assert was triggered in a debug |
| 344 | build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 |
| 345 | |
| 346 | >>> while 1: |
| 347 | ... while 2: |
| 348 | ... while 3: |
| 349 | ... while 4: |
| 350 | ... while 5: |
| 351 | ... while 6: |
| 352 | ... while 8: |
| 353 | ... while 9: |
| 354 | ... while 10: |
| 355 | ... while 11: |
| 356 | ... while 12: |
| 357 | ... while 13: |
| 358 | ... while 14: |
| 359 | ... while 15: |
| 360 | ... while 16: |
| 361 | ... while 17: |
| 362 | ... while 18: |
| 363 | ... while 19: |
| 364 | ... while 20: |
| 365 | ... while 21: |
| 366 | ... while 22: |
| 367 | ... break |
| 368 | Traceback (most recent call last): |
| 369 | ... |
| 370 | SystemError: too many statically nested blocks |
| 371 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 372 | Misuse of the nonlocal statement can lead to a few unique syntax errors. |
| 373 | |
| 374 | >>> def f(x): |
| 375 | ... nonlocal x |
| 376 | Traceback (most recent call last): |
| 377 | ... |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 378 | SyntaxError: name 'x' is parameter and nonlocal |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 379 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 380 | >>> def f(): |
| 381 | ... global x |
| 382 | ... nonlocal x |
| 383 | Traceback (most recent call last): |
| 384 | ... |
| 385 | SyntaxError: name 'x' is nonlocal and global |
| 386 | |
| 387 | >>> def f(): |
| 388 | ... nonlocal x |
| 389 | Traceback (most recent call last): |
| 390 | ... |
| 391 | SyntaxError: no binding for nonlocal 'x' found |
| 392 | |
Nick Coghlan | 4138bfe | 2007-04-23 10:14:27 +0000 | [diff] [blame] | 393 | From SF bug #1705365 |
| 394 | >>> nonlocal x |
| 395 | Traceback (most recent call last): |
| 396 | ... |
| 397 | SyntaxError: nonlocal declaration not allowed at module level |
| 398 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 399 | TODO(jhylton): Figure out how to test SyntaxWarning with doctest. |
| 400 | |
| 401 | ## >>> def f(x): |
| 402 | ## ... def f(): |
| 403 | ## ... print(x) |
| 404 | ## ... nonlocal x |
| 405 | ## Traceback (most recent call last): |
| 406 | ## ... |
| 407 | ## SyntaxWarning: name 'x' is assigned to before nonlocal declaration |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 408 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 409 | ## >>> def f(): |
| 410 | ## ... x = 1 |
| 411 | ## ... nonlocal x |
| 412 | ## Traceback (most recent call last): |
| 413 | ## ... |
| 414 | ## SyntaxWarning: name 'x' is assigned to before nonlocal declaration |
| 415 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 416 | |
| 417 | This tests assignment-context; there was a bug in Python 2.5 where compiling |
| 418 | a complex 'if' (one with 'elif') would fail to notice an invalid suite, |
| 419 | leading to spurious errors. |
| 420 | |
| 421 | >>> if 1: |
| 422 | ... x() = 1 |
| 423 | ... elif 1: |
| 424 | ... pass |
| 425 | Traceback (most recent call last): |
| 426 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 427 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 428 | |
| 429 | >>> if 1: |
| 430 | ... pass |
| 431 | ... elif 1: |
| 432 | ... x() = 1 |
| 433 | Traceback (most recent call last): |
| 434 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 435 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 436 | |
| 437 | >>> if 1: |
| 438 | ... x() = 1 |
| 439 | ... elif 1: |
| 440 | ... pass |
| 441 | ... else: |
| 442 | ... pass |
| 443 | Traceback (most recent call last): |
| 444 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 445 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 446 | |
| 447 | >>> if 1: |
| 448 | ... pass |
| 449 | ... elif 1: |
| 450 | ... x() = 1 |
| 451 | ... else: |
| 452 | ... pass |
| 453 | Traceback (most recent call last): |
| 454 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 455 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 456 | |
| 457 | >>> if 1: |
| 458 | ... pass |
| 459 | ... elif 1: |
| 460 | ... pass |
| 461 | ... else: |
| 462 | ... x() = 1 |
| 463 | Traceback (most recent call last): |
| 464 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 465 | SyntaxError: can't assign to function call |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 466 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 467 | Make sure that the old "raise X, Y[, Z]" form is gone: |
| 468 | >>> raise X, Y |
| 469 | Traceback (most recent call last): |
| 470 | ... |
| 471 | SyntaxError: invalid syntax |
| 472 | >>> raise X, Y, Z |
| 473 | Traceback (most recent call last): |
| 474 | ... |
| 475 | SyntaxError: invalid syntax |
| 476 | |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 477 | |
| 478 | >>> f(a=23, a=234) |
| 479 | Traceback (most recent call last): |
| 480 | ... |
| 481 | SyntaxError: keyword argument repeated |
| 482 | |
Amaury Forgeot d'Arc | a1e5c69 | 2010-08-19 21:35:59 +0000 | [diff] [blame] | 483 | Corner-cases that used to fail to raise the correct error: |
| 484 | |
Amaury Forgeot d'Arc | 7235633 | 2010-08-19 22:29:49 +0000 | [diff] [blame] | 485 | >>> def f(*, x=lambda *:0): pass |
Amaury Forgeot d'Arc | a1e5c69 | 2010-08-19 21:35:59 +0000 | [diff] [blame] | 486 | Traceback (most recent call last): |
Amaury Forgeot d'Arc | 7235633 | 2010-08-19 22:29:49 +0000 | [diff] [blame] | 487 | SyntaxError: named arguments must follow bare * |
Amaury Forgeot d'Arc | a1e5c69 | 2010-08-19 21:35:59 +0000 | [diff] [blame] | 488 | |
Amaury Forgeot d'Arc | 7235633 | 2010-08-19 22:29:49 +0000 | [diff] [blame] | 489 | >>> def f(*args:(lambda *:0)): pass |
Amaury Forgeot d'Arc | a1e5c69 | 2010-08-19 21:35:59 +0000 | [diff] [blame] | 490 | Traceback (most recent call last): |
Amaury Forgeot d'Arc | 7235633 | 2010-08-19 22:29:49 +0000 | [diff] [blame] | 491 | SyntaxError: named arguments must follow bare * |
Amaury Forgeot d'Arc | a1e5c69 | 2010-08-19 21:35:59 +0000 | [diff] [blame] | 492 | |
Amaury Forgeot d'Arc | 7235633 | 2010-08-19 22:29:49 +0000 | [diff] [blame] | 493 | >>> def f(**kwargs:(lambda *:0)): pass |
Amaury Forgeot d'Arc | a1e5c69 | 2010-08-19 21:35:59 +0000 | [diff] [blame] | 494 | Traceback (most recent call last): |
Amaury Forgeot d'Arc | 7235633 | 2010-08-19 22:29:49 +0000 | [diff] [blame] | 495 | SyntaxError: named arguments must follow bare * |
Amaury Forgeot d'Arc | a1e5c69 | 2010-08-19 21:35:59 +0000 | [diff] [blame] | 496 | |
| 497 | >>> with (lambda *:0): pass |
| 498 | Traceback (most recent call last): |
| 499 | SyntaxError: named arguments must follow bare * |
| 500 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 501 | """ |
| 502 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 503 | import re |
| 504 | import unittest |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 505 | import warnings |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 506 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 507 | from test import support |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 508 | |
| 509 | class SyntaxTestCase(unittest.TestCase): |
| 510 | |
| 511 | def _check_error(self, code, errtext, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 512 | filename="<testcase>", mode="exec", subclass=None): |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 513 | """Check that compiling code raises SyntaxError with errtext. |
| 514 | |
| 515 | errtest is a regular expression that must be present in the |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 516 | test of the exception raised. If subclass is specified it |
| 517 | is the expected subclass of SyntaxError (e.g. IndentationError). |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 518 | """ |
| 519 | try: |
| 520 | compile(code, filename, mode) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 521 | except SyntaxError as err: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 522 | if subclass and not isinstance(err, subclass): |
| 523 | self.fail("SyntaxError is not a %s" % subclass.__name__) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 524 | mo = re.search(errtext, str(err)) |
| 525 | if mo is None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 526 | self.fail("SyntaxError did not contain '%r'" % (errtext,)) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 527 | else: |
| 528 | self.fail("compile() did not raise SyntaxError") |
| 529 | |
| 530 | def test_assign_call(self): |
| 531 | self._check_error("f() = 1", "assign") |
| 532 | |
| 533 | def test_assign_del(self): |
| 534 | self._check_error("del f()", "delete") |
| 535 | |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 536 | def test_global_err_then_warn(self): |
| 537 | # Bug tickler: The SyntaxError raised for one global statement |
| 538 | # shouldn't be clobbered by a SyntaxWarning issued for a later one. |
| 539 | source = re.sub('(?m)^ *:', '', """\ |
| 540 | :def error(a): |
| 541 | : global a # SyntaxError |
| 542 | :def warning(): |
| 543 | : b = 1 |
| 544 | : global b # SyntaxWarning |
| 545 | :""") |
| 546 | warnings.filterwarnings(action='ignore', category=SyntaxWarning) |
| 547 | self._check_error(source, "global") |
| 548 | warnings.filters.pop(0) |
| 549 | |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 550 | def test_break_outside_loop(self): |
| 551 | self._check_error("break", "outside loop") |
| 552 | |
| 553 | def test_delete_deref(self): |
| 554 | source = re.sub('(?m)^ *:', '', """\ |
| 555 | :def foo(x): |
| 556 | : def bar(): |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 557 | : print(x) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 558 | : del x |
| 559 | :""") |
| 560 | self._check_error(source, "nested scope") |
| 561 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 562 | def test_unexpected_indent(self): |
| 563 | self._check_error("foo()\n bar()\n", "unexpected indent", |
| 564 | subclass=IndentationError) |
| 565 | |
| 566 | def test_no_indent(self): |
| 567 | self._check_error("if 1:\nfoo()", "expected an indented block", |
| 568 | subclass=IndentationError) |
| 569 | |
| 570 | def test_bad_outdent(self): |
| 571 | self._check_error("if 1:\n foo()\n bar()", |
| 572 | "unindent does not match .* level", |
| 573 | subclass=IndentationError) |
| 574 | |
| 575 | def test_kwargs_last(self): |
| 576 | self._check_error("int(base=10, '2')", "non-keyword arg") |
| 577 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 578 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 579 | support.run_unittest(SyntaxTestCase) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 580 | from test import test_syntax |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 581 | support.run_doctest(test_syntax, verbosity=True) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 582 | |
| 583 | if __name__ == "__main__": |
| 584 | test_main() |