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