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