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): |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 36 | SyntaxError: can't assign to keyword |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 37 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 38 | >>> f() = 1 |
| 39 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 40 | SyntaxError: can't assign to function call |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 41 | |
| 42 | >>> del f() |
| 43 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 44 | SyntaxError: can't delete function call |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 45 | |
| 46 | >>> a + 1 = 2 |
| 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 operator |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 49 | |
| 50 | >>> (x for x in x) = 1 |
| 51 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 52 | SyntaxError: can't assign to generator expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 53 | |
| 54 | >>> 1 = 1 |
| 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 literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 57 | |
| 58 | >>> "abc" = 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 literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 61 | |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 62 | >>> b"" = 1 |
| 63 | Traceback (most recent call last): |
| 64 | SyntaxError: can't assign to literal |
| 65 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 66 | >>> `1` = 1 |
| 67 | Traceback (most recent call last): |
Brett Cannon | 8933cb4 | 2006-08-25 04:12:10 +0000 | [diff] [blame] | 68 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 69 | |
| 70 | If the left-hand side of an assignment is a list or tuple, an illegal |
| 71 | expression inside that contain should still cause a syntax error. |
| 72 | This test just checks a couple of cases rather than enumerating all of |
| 73 | them. |
| 74 | |
| 75 | >>> (a, "b", c) = (1, 2, 3) |
| 76 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 77 | SyntaxError: can't assign to literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 78 | |
| 79 | >>> [a, b, c + 1] = [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 operator |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 82 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 83 | >>> a if 1 else b = 1 |
| 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 conditional expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 86 | |
| 87 | From compiler_complex_args(): |
| 88 | |
| 89 | >>> def f(None=1): |
| 90 | ... pass |
| 91 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 92 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | From ast_for_arguments(): |
| 96 | |
| 97 | >>> def f(x, y=1, z): |
| 98 | ... pass |
| 99 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 100 | SyntaxError: non-default argument follows default argument |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 101 | |
| 102 | >>> def f(x, None): |
| 103 | ... pass |
| 104 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 105 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 106 | |
| 107 | >>> def f(*None): |
| 108 | ... pass |
| 109 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 110 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 111 | |
| 112 | >>> def f(**None): |
| 113 | ... pass |
| 114 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 115 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | From ast_for_funcdef(): |
| 119 | |
| 120 | >>> def None(x): |
| 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_call(): |
| 127 | |
| 128 | >>> def f(it, *varargs): |
| 129 | ... return list(it) |
| 130 | >>> L = range(10) |
| 131 | >>> f(x for x in L) |
| 132 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 133 | >>> f(x for x in L, 1) |
| 134 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 135 | SyntaxError: Generator expression must be parenthesized if not sole argument |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 136 | >>> f(x for x in L, y for y in L) |
| 137 | Traceback (most recent call last): |
| 138 | SyntaxError: Generator expression must be parenthesized if not sole argument |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 139 | >>> f((x for x in L), 1) |
| 140 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 141 | |
Serhiy Storchaka | 214678e | 2016-11-28 10:52:05 +0200 | [diff] [blame] | 142 | >>> def g(*args, **kwargs): |
| 143 | ... print(args, sorted(kwargs.items())) |
| 144 | >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 145 | ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 146 | ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| 147 | ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| 148 | ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| 149 | ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 150 | ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 151 | ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| 152 | ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 153 | ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| 154 | ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| 155 | ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 156 | ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| 157 | ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| 158 | ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| 159 | ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| 160 | ... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, |
| 161 | ... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, |
| 162 | ... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, |
| 163 | ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS |
| 164 | (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) [] |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 165 | |
Serhiy Storchaka | 214678e | 2016-11-28 10:52:05 +0200 | [diff] [blame] | 166 | >>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8, |
| 167 | ... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16, |
| 168 | ... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24, |
| 169 | ... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32, |
| 170 | ... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40, |
| 171 | ... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48, |
| 172 | ... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56, |
| 173 | ... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64, |
| 174 | ... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72, |
| 175 | ... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80, |
| 176 | ... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88, |
| 177 | ... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96, |
| 178 | ... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103, |
| 179 | ... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110, |
| 180 | ... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117, |
| 181 | ... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124, |
| 182 | ... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131, |
| 183 | ... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138, |
| 184 | ... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145, |
| 185 | ... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152, |
| 186 | ... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159, |
| 187 | ... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166, |
| 188 | ... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173, |
| 189 | ... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180, |
| 190 | ... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187, |
| 191 | ... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194, |
| 192 | ... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201, |
| 193 | ... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208, |
| 194 | ... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215, |
| 195 | ... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222, |
| 196 | ... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229, |
| 197 | ... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236, |
| 198 | ... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243, |
| 199 | ... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250, |
| 200 | ... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257, |
| 201 | ... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264, |
| 202 | ... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271, |
| 203 | ... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278, |
| 204 | ... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285, |
| 205 | ... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292, |
| 206 | ... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299) |
| 207 | ... # doctest: +ELLIPSIS |
| 208 | () [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)] |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 209 | |
| 210 | >>> f(lambda x: x[0] = 3) |
| 211 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 212 | SyntaxError: lambda cannot contain assignment |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 213 | |
| 214 | The grammar accepts any test (basically, any expression) in the |
| 215 | keyword slot of a call site. Test a few different options. |
| 216 | |
| 217 | >>> f(x()=2) |
| 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(a or b=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 | >>> f(x.y=1) |
| 224 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 225 | SyntaxError: keyword can't be an expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 226 | |
| 227 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 228 | More set_context(): |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 229 | |
| 230 | >>> (x for x in x) += 1 |
| 231 | Traceback (most recent call last): |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 232 | SyntaxError: can't assign to generator expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 233 | >>> None += 1 |
| 234 | Traceback (most recent call last): |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 235 | SyntaxError: can't assign to keyword |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 236 | >>> f() += 1 |
| 237 | Traceback (most recent call last): |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 238 | SyntaxError: can't assign to function call |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 239 | |
| 240 | |
| 241 | Test continue in finally in weird combinations. |
| 242 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 243 | continue in for loop under finally should be ok. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 244 | |
| 245 | >>> def test(): |
| 246 | ... try: |
| 247 | ... pass |
| 248 | ... finally: |
| 249 | ... for abc in range(10): |
| 250 | ... continue |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 251 | ... print(abc) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 252 | >>> test() |
| 253 | 9 |
| 254 | |
| 255 | Start simple, a continue in a finally should not be allowed. |
| 256 | |
| 257 | >>> def test(): |
| 258 | ... for abc in range(10): |
| 259 | ... try: |
| 260 | ... pass |
| 261 | ... finally: |
| 262 | ... continue |
| 263 | Traceback (most recent call last): |
| 264 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 265 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 266 | |
| 267 | This is essentially a continue in a finally which should not be allowed. |
| 268 | |
| 269 | >>> def test(): |
| 270 | ... for abc in range(10): |
| 271 | ... try: |
| 272 | ... pass |
| 273 | ... finally: |
| 274 | ... try: |
| 275 | ... continue |
| 276 | ... except: |
| 277 | ... pass |
| 278 | Traceback (most recent call last): |
| 279 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 280 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 281 | |
| 282 | >>> def foo(): |
| 283 | ... try: |
| 284 | ... pass |
| 285 | ... finally: |
| 286 | ... continue |
| 287 | Traceback (most recent call last): |
| 288 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 289 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 290 | |
| 291 | >>> def foo(): |
| 292 | ... for a in (): |
| 293 | ... try: |
| 294 | ... pass |
| 295 | ... finally: |
| 296 | ... continue |
| 297 | Traceback (most recent call last): |
| 298 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 299 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 300 | |
| 301 | >>> def foo(): |
| 302 | ... for a in (): |
| 303 | ... try: |
| 304 | ... pass |
| 305 | ... finally: |
| 306 | ... try: |
| 307 | ... continue |
| 308 | ... finally: |
| 309 | ... pass |
| 310 | Traceback (most recent call last): |
| 311 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 312 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 313 | |
| 314 | >>> def foo(): |
| 315 | ... for a in (): |
| 316 | ... try: pass |
| 317 | ... finally: |
| 318 | ... try: |
| 319 | ... pass |
| 320 | ... except: |
| 321 | ... continue |
| 322 | Traceback (most recent call last): |
| 323 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 324 | SyntaxError: 'continue' not supported inside 'finally' clause |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 325 | |
| 326 | There is one test for a break that is not in a loop. The compiler |
| 327 | uses a single data structure to keep track of try-finally and loops, |
| 328 | so we need to be sure that a break is actually inside a loop. If it |
| 329 | isn't, there should be a syntax error. |
| 330 | |
| 331 | >>> try: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 332 | ... print(1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 333 | ... break |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 334 | ... print(2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 335 | ... finally: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 336 | ... print(3) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 337 | Traceback (most recent call last): |
| 338 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 339 | SyntaxError: 'break' outside loop |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 340 | |
Benjamin Peterson | e09ed54 | 2016-07-14 22:00:03 -0700 | [diff] [blame] | 341 | This raises a SyntaxError, it used to raise a SystemError. |
| 342 | Context for this change can be found on issue #27514 |
| 343 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 344 | In 2.5 there was a missing exception and an assert was triggered in a debug |
| 345 | build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 |
| 346 | |
| 347 | >>> while 1: |
| 348 | ... while 2: |
| 349 | ... while 3: |
| 350 | ... while 4: |
| 351 | ... while 5: |
| 352 | ... while 6: |
| 353 | ... while 8: |
| 354 | ... while 9: |
| 355 | ... while 10: |
| 356 | ... while 11: |
| 357 | ... while 12: |
| 358 | ... while 13: |
| 359 | ... while 14: |
| 360 | ... while 15: |
| 361 | ... while 16: |
| 362 | ... while 17: |
| 363 | ... while 18: |
| 364 | ... while 19: |
| 365 | ... while 20: |
| 366 | ... while 21: |
| 367 | ... while 22: |
| 368 | ... break |
| 369 | Traceback (most recent call last): |
| 370 | ... |
Benjamin Peterson | e09ed54 | 2016-07-14 22:00:03 -0700 | [diff] [blame] | 371 | SyntaxError: too many statically nested blocks |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 373 | Misuse of the nonlocal and global statement can lead to a few unique syntax errors. |
| 374 | |
| 375 | >>> def f(): |
| 376 | ... x = 1 |
| 377 | ... global x |
| 378 | Traceback (most recent call last): |
| 379 | ... |
| 380 | SyntaxError: name 'x' is assigned to before global declaration |
| 381 | |
| 382 | >>> def f(): |
| 383 | ... x = 1 |
| 384 | ... def g(): |
| 385 | ... print(x) |
| 386 | ... nonlocal x |
| 387 | Traceback (most recent call last): |
| 388 | ... |
| 389 | SyntaxError: name 'x' is used prior to nonlocal declaration |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 390 | |
| 391 | >>> def f(x): |
| 392 | ... nonlocal x |
| 393 | Traceback (most recent call last): |
| 394 | ... |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 395 | SyntaxError: name 'x' is parameter and nonlocal |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 396 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 397 | >>> def f(): |
| 398 | ... global x |
| 399 | ... nonlocal x |
| 400 | Traceback (most recent call last): |
| 401 | ... |
| 402 | SyntaxError: name 'x' is nonlocal and global |
| 403 | |
| 404 | >>> def f(): |
| 405 | ... nonlocal x |
| 406 | Traceback (most recent call last): |
| 407 | ... |
| 408 | SyntaxError: no binding for nonlocal 'x' found |
| 409 | |
Nick Coghlan | 4138bfe | 2007-04-23 10:14:27 +0000 | [diff] [blame] | 410 | From SF bug #1705365 |
| 411 | >>> nonlocal x |
| 412 | Traceback (most recent call last): |
| 413 | ... |
| 414 | SyntaxError: nonlocal declaration not allowed at module level |
| 415 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 416 | TODO(jhylton): Figure out how to test SyntaxWarning with doctest. |
| 417 | |
| 418 | ## >>> def f(x): |
| 419 | ## ... def f(): |
| 420 | ## ... print(x) |
| 421 | ## ... nonlocal x |
| 422 | ## Traceback (most recent call last): |
| 423 | ## ... |
| 424 | ## SyntaxWarning: name 'x' is assigned to before nonlocal declaration |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 425 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 426 | ## >>> def f(): |
| 427 | ## ... x = 1 |
| 428 | ## ... nonlocal x |
| 429 | ## Traceback (most recent call last): |
| 430 | ## ... |
| 431 | ## SyntaxWarning: name 'x' is assigned to before nonlocal declaration |
| 432 | |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 433 | From https://bugs.python.org/issue25973 |
| 434 | >>> class A: |
| 435 | ... def f(self): |
| 436 | ... nonlocal __x |
| 437 | Traceback (most recent call last): |
| 438 | ... |
| 439 | SyntaxError: no binding for nonlocal '_A__x' found |
| 440 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 441 | |
| 442 | This tests assignment-context; there was a bug in Python 2.5 where compiling |
| 443 | a complex 'if' (one with 'elif') would fail to notice an invalid suite, |
| 444 | leading to spurious errors. |
| 445 | |
| 446 | >>> if 1: |
| 447 | ... x() = 1 |
| 448 | ... elif 1: |
| 449 | ... pass |
| 450 | Traceback (most recent call last): |
| 451 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 452 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 453 | |
| 454 | >>> if 1: |
| 455 | ... pass |
| 456 | ... elif 1: |
| 457 | ... x() = 1 |
| 458 | Traceback (most recent call last): |
| 459 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 460 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 461 | |
| 462 | >>> if 1: |
| 463 | ... x() = 1 |
| 464 | ... elif 1: |
| 465 | ... pass |
| 466 | ... else: |
| 467 | ... pass |
| 468 | Traceback (most recent call last): |
| 469 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 470 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 471 | |
| 472 | >>> if 1: |
| 473 | ... pass |
| 474 | ... elif 1: |
| 475 | ... x() = 1 |
| 476 | ... else: |
| 477 | ... pass |
| 478 | Traceback (most recent call last): |
| 479 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 480 | SyntaxError: can't assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 481 | |
| 482 | >>> if 1: |
| 483 | ... pass |
| 484 | ... elif 1: |
| 485 | ... pass |
| 486 | ... else: |
| 487 | ... x() = 1 |
| 488 | Traceback (most recent call last): |
| 489 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 490 | SyntaxError: can't assign to function call |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 491 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 492 | Make sure that the old "raise X, Y[, Z]" form is gone: |
| 493 | >>> raise X, Y |
| 494 | Traceback (most recent call last): |
| 495 | ... |
| 496 | SyntaxError: invalid syntax |
| 497 | >>> raise X, Y, Z |
| 498 | Traceback (most recent call last): |
| 499 | ... |
| 500 | SyntaxError: invalid syntax |
| 501 | |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 502 | |
| 503 | >>> f(a=23, a=234) |
| 504 | Traceback (most recent call last): |
| 505 | ... |
| 506 | SyntaxError: keyword argument repeated |
| 507 | |
Benjamin Peterson | 1b1a1a4 | 2010-06-24 00:17:03 +0000 | [diff] [blame] | 508 | >>> {1, 2, 3} = 42 |
| 509 | Traceback (most recent call last): |
| 510 | SyntaxError: can't assign to literal |
| 511 | |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 512 | Corner-cases that used to fail to raise the correct error: |
| 513 | |
| 514 | >>> def f(*, x=lambda __debug__:0): pass |
| 515 | Traceback (most recent call last): |
| 516 | SyntaxError: assignment to keyword |
| 517 | |
| 518 | >>> def f(*args:(lambda __debug__:0)): pass |
| 519 | Traceback (most recent call last): |
| 520 | SyntaxError: assignment to keyword |
| 521 | |
| 522 | >>> def f(**kwargs:(lambda __debug__:0)): pass |
| 523 | Traceback (most recent call last): |
| 524 | SyntaxError: assignment to keyword |
| 525 | |
| 526 | >>> with (lambda *:0): pass |
| 527 | Traceback (most recent call last): |
| 528 | SyntaxError: named arguments must follow bare * |
| 529 | |
| 530 | Corner-cases that used to crash: |
| 531 | |
| 532 | >>> def f(**__debug__): pass |
| 533 | Traceback (most recent call last): |
| 534 | SyntaxError: assignment to keyword |
| 535 | |
| 536 | >>> def f(*xx, __debug__): pass |
| 537 | Traceback (most recent call last): |
| 538 | SyntaxError: assignment to keyword |
| 539 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 540 | """ |
| 541 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 542 | import re |
| 543 | import unittest |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 544 | import warnings |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 545 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 546 | from test import support |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 547 | |
| 548 | class SyntaxTestCase(unittest.TestCase): |
| 549 | |
| 550 | def _check_error(self, code, errtext, |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 551 | filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None): |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 552 | """Check that compiling code raises SyntaxError with errtext. |
| 553 | |
| 554 | errtest is a regular expression that must be present in the |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 555 | test of the exception raised. If subclass is specified it |
| 556 | is the expected subclass of SyntaxError (e.g. IndentationError). |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 557 | """ |
| 558 | try: |
| 559 | compile(code, filename, mode) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 560 | except SyntaxError as err: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 561 | if subclass and not isinstance(err, subclass): |
| 562 | self.fail("SyntaxError is not a %s" % subclass.__name__) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 563 | mo = re.search(errtext, str(err)) |
| 564 | if mo is None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 565 | self.fail("SyntaxError did not contain '%r'" % (errtext,)) |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 566 | self.assertEqual(err.filename, filename) |
| 567 | if lineno is not None: |
| 568 | self.assertEqual(err.lineno, lineno) |
| 569 | if offset is not None: |
| 570 | self.assertEqual(err.offset, offset) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 571 | else: |
| 572 | self.fail("compile() did not raise SyntaxError") |
| 573 | |
| 574 | def test_assign_call(self): |
| 575 | self._check_error("f() = 1", "assign") |
| 576 | |
| 577 | def test_assign_del(self): |
| 578 | self._check_error("del f()", "delete") |
| 579 | |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 580 | def test_global_err_then_warn(self): |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 581 | # Bug #763201: The SyntaxError raised for one global statement |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 582 | # 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] | 583 | source = """if 1: |
| 584 | def error(a): |
| 585 | global a # SyntaxError |
| 586 | def warning(): |
| 587 | b = 1 |
| 588 | global b # SyntaxWarning |
| 589 | """ |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 590 | warnings.filterwarnings(action='ignore', category=SyntaxWarning) |
| 591 | self._check_error(source, "global") |
| 592 | warnings.filters.pop(0) |
| 593 | |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 594 | def test_break_outside_loop(self): |
| 595 | self._check_error("break", "outside loop") |
| 596 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 597 | def test_unexpected_indent(self): |
| 598 | self._check_error("foo()\n bar()\n", "unexpected indent", |
| 599 | subclass=IndentationError) |
| 600 | |
| 601 | def test_no_indent(self): |
| 602 | self._check_error("if 1:\nfoo()", "expected an indented block", |
| 603 | subclass=IndentationError) |
| 604 | |
| 605 | def test_bad_outdent(self): |
| 606 | self._check_error("if 1:\n foo()\n bar()", |
| 607 | "unindent does not match .* level", |
| 608 | subclass=IndentationError) |
| 609 | |
| 610 | def test_kwargs_last(self): |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 611 | self._check_error("int(base=10, '2')", |
| 612 | "positional argument follows keyword argument") |
| 613 | |
| 614 | def test_kwargs_last2(self): |
| 615 | self._check_error("int(**{base: 10}, '2')", |
| 616 | "positional argument follows " |
| 617 | "keyword argument unpacking") |
| 618 | |
| 619 | def test_kwargs_last3(self): |
| 620 | self._check_error("int(**{base: 10}, *['2'])", |
| 621 | "iterable argument unpacking follows " |
| 622 | "keyword argument unpacking") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 623 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 624 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 625 | support.run_unittest(SyntaxTestCase) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 626 | from test import test_syntax |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 627 | support.run_doctest(test_syntax, verbosity=True) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 628 | |
| 629 | if __name__ == "__main__": |
| 630 | test_main() |