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