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