| """This module tests SyntaxErrors. |
| |
| Here's an example of the sort of thing that is tested. |
| |
| >>> def f(x): |
| ... global x |
| Traceback (most recent call last): |
| SyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1) |
| |
| The tests are all raise SyntaxErrors. They were created by checking |
| each C call that raises SyntaxError. There are several modules that |
| raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and |
| symtable.c. |
| |
| The parser itself outlaws a lot of invalid syntax. None of these |
| errors are tested here at the moment. We should add some tests; since |
| there are infinitely many programs with invalid syntax, we would need |
| to be judicious in selecting some. |
| |
| The compiler generates a synthetic module name for code executed by |
| doctest. Since all the code comes from the same module, a suffix like |
| [1] is appended to the module name, As a consequence, changing the |
| order of tests in this module means renumbering all the errors after |
| it. (Maybe we should enable the ellipsis option for these tests.) |
| |
| In ast.c, syntax errors are raised by calling ast_error(). |
| |
| Errors from set_context(): |
| |
| >>> obj.None = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[1]>", line 1 |
| SyntaxError: cannot assign to None |
| |
| >>> None = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[2]>", line 1 |
| SyntaxError: cannot assign to None |
| |
| It's a syntax error to assign to the empty tuple. Why isn't it an |
| error to assign to the empty list? It will always raise some error at |
| runtime. |
| |
| >>> () = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[3]>", line 1 |
| SyntaxError: can't assign to () |
| |
| >>> f() = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[4]>", line 1 |
| SyntaxError: can't assign to function call |
| |
| >>> del f() |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[5]>", line 1 |
| SyntaxError: can't delete function call |
| |
| >>> a + 1 = 2 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[6]>", line 1 |
| SyntaxError: can't assign to operator |
| |
| >>> (x for x in x) = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[7]>", line 1 |
| SyntaxError: can't assign to generator expression |
| |
| >>> 1 = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[8]>", line 1 |
| SyntaxError: can't assign to literal |
| |
| >>> "abc" = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[9]>", line 1 |
| SyntaxError: can't assign to literal |
| |
| >>> b"" = 1 |
| Traceback (most recent call last): |
| SyntaxError: can't assign to literal |
| |
| If the left-hand side of an assignment is a list or tuple, an illegal |
| expression inside that contain should still cause a syntax error. |
| This test just checks a couple of cases rather than enumerating all of |
| them. |
| |
| >>> (a, "b", c) = (1, 2, 3) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[11]>", line 1 |
| SyntaxError: can't assign to literal |
| |
| >>> [a, b, c + 1] = [1, 2, 3] |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[12]>", line 1 |
| SyntaxError: can't assign to operator |
| |
| >>> a if 1 else b = 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[13]>", line 1 |
| SyntaxError: can't assign to conditional expression |
| |
| From compiler_complex_args(): |
| |
| >>> def f(None=1): |
| ... pass |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[14]>", line 1 |
| SyntaxError: cannot assign to None |
| |
| |
| From ast_for_arguments(): |
| |
| >>> def f(x, y=1, z): |
| ... pass |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[15]>", line 1 |
| SyntaxError: non-default argument follows default argument |
| |
| >>> def f(x, None): |
| ... pass |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[16]>", line 1 |
| SyntaxError: cannot assign to None |
| |
| >>> def f(*None): |
| ... pass |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[17]>", line 1 |
| SyntaxError: cannot assign to None |
| |
| >>> def f(**None): |
| ... pass |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[18]>", line 1 |
| SyntaxError: cannot assign to None |
| |
| |
| From ast_for_funcdef(): |
| |
| >>> def None(x): |
| ... pass |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[19]>", line 1 |
| SyntaxError: cannot assign to None |
| |
| |
| From ast_for_call(): |
| |
| >>> def f(it, *varargs, **kwargs): |
| ... return list(it) |
| >>> L = range(10) |
| >>> f(x for x in L) |
| [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| >>> f(x for x in L, 1) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[23]>", line 1 |
| SyntaxError: Generator expression must be parenthesized if not sole argument |
| >>> f(x for x in L, y=1) |
| Traceback (most recent call last): |
| SyntaxError: Generator expression must be parenthesized if not sole argument |
| >>> f(L, x for x in L) |
| Traceback (most recent call last): |
| SyntaxError: Generator expression must be parenthesized if not sole argument |
| >>> f(x for x in L, y for y in L) |
| Traceback (most recent call last): |
| SyntaxError: Generator expression must be parenthesized if not sole argument |
| >>> f(x for x in L,) |
| [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| >>> f((x for x in L), 1) |
| [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| |
| >>> def g(*args, **kwargs): |
| ... print args, sorted(kwargs.items()) |
| >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| ... 248, 249, 250, 251, 252, 253, 254) # doctest: +ELLIPSIS |
| (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 252, 253, 254) [] |
| >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| ... 248, 249, 250, 251, 252, 253, 254, 255) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[25]>", line 1 |
| SyntaxError: more than 255 arguments |
| |
| >>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8, |
| ... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16, |
| ... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24, |
| ... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32, |
| ... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40, |
| ... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48, |
| ... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56, |
| ... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64, |
| ... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72, |
| ... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80, |
| ... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88, |
| ... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96, |
| ... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103, |
| ... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110, |
| ... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117, |
| ... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124, |
| ... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131, |
| ... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138, |
| ... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145, |
| ... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152, |
| ... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159, |
| ... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166, |
| ... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173, |
| ... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180, |
| ... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187, |
| ... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194, |
| ... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201, |
| ... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208, |
| ... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215, |
| ... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222, |
| ... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229, |
| ... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236, |
| ... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243, |
| ... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250, |
| ... a251=251, a252=252, a253=253, a254=254) # doctest: +ELLIPSIS |
| () [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a253', 253), ('a254', 254)] |
| >>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8, |
| ... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16, |
| ... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24, |
| ... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32, |
| ... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40, |
| ... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48, |
| ... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56, |
| ... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64, |
| ... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72, |
| ... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80, |
| ... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88, |
| ... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96, |
| ... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103, |
| ... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110, |
| ... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117, |
| ... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124, |
| ... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131, |
| ... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138, |
| ... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145, |
| ... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152, |
| ... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159, |
| ... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166, |
| ... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173, |
| ... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180, |
| ... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187, |
| ... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194, |
| ... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201, |
| ... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208, |
| ... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215, |
| ... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222, |
| ... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229, |
| ... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236, |
| ... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243, |
| ... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250, |
| ... a251=251, a252=252, a253=253, a254=254, a255=255) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[35]>", line 1 |
| SyntaxError: more than 255 arguments |
| |
| >>> class C: |
| ... def meth(self, *args): |
| ... return args |
| >>> obj = C() |
| >>> obj.meth( |
| ... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| ... 248, 249, 250, 251, 252, 253, 254) # doctest: +ELLIPSIS |
| (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 252, 253, 254) |
| >>> obj.meth( |
| ... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| ... 248, 249, 250, 251, 252, 253, 254, 255) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[38]>", line 1 |
| SyntaxError: more than 255 arguments |
| |
| >>> f(lambda x: x[0] = 3) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[27]>", line 1 |
| SyntaxError: lambda cannot contain assignment |
| |
| The grammar accepts any test (basically, any expression) in the |
| keyword slot of a call site. Test a few different options. |
| |
| >>> f(x()=2) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[28]>", line 1 |
| SyntaxError: keyword can't be an expression |
| >>> f(a or b=1) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[29]>", line 1 |
| SyntaxError: keyword can't be an expression |
| >>> f(x.y=1) |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[30]>", line 1 |
| SyntaxError: keyword can't be an expression |
| |
| |
| More set_context(): |
| |
| >>> (x for x in x) += 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[31]>", line 1 |
| SyntaxError: can't assign to generator expression |
| >>> None += 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[32]>", line 1 |
| SyntaxError: cannot assign to None |
| >>> f() += 1 |
| Traceback (most recent call last): |
| File "<doctest test.test_syntax[33]>", line 1 |
| SyntaxError: can't assign to function call |
| |
| |
| Test continue in finally in weird combinations. |
| |
| continue in for loop under finally should be ok. |
| |
| >>> def test(): |
| ... try: |
| ... pass |
| ... finally: |
| ... for abc in range(10): |
| ... continue |
| ... print abc |
| >>> test() |
| 9 |
| |
| Start simple, a continue in a finally should not be allowed. |
| |
| >>> def test(): |
| ... for abc in range(10): |
| ... try: |
| ... pass |
| ... finally: |
| ... continue |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[36]>", line 6 |
| SyntaxError: 'continue' not supported inside 'finally' clause |
| |
| This is essentially a continue in a finally which should not be allowed. |
| |
| >>> def test(): |
| ... for abc in range(10): |
| ... try: |
| ... pass |
| ... finally: |
| ... try: |
| ... continue |
| ... except: |
| ... pass |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[37]>", line 6 |
| SyntaxError: 'continue' not supported inside 'finally' clause |
| |
| >>> def foo(): |
| ... try: |
| ... pass |
| ... finally: |
| ... continue |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[38]>", line 5 |
| SyntaxError: 'continue' not supported inside 'finally' clause |
| |
| >>> def foo(): |
| ... for a in (): |
| ... try: |
| ... pass |
| ... finally: |
| ... continue |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[39]>", line 6 |
| SyntaxError: 'continue' not supported inside 'finally' clause |
| |
| >>> def foo(): |
| ... for a in (): |
| ... try: |
| ... pass |
| ... finally: |
| ... try: |
| ... continue |
| ... finally: |
| ... pass |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[40]>", line 7 |
| SyntaxError: 'continue' not supported inside 'finally' clause |
| |
| >>> def foo(): |
| ... for a in (): |
| ... try: pass |
| ... finally: |
| ... try: |
| ... pass |
| ... except: |
| ... continue |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[41]>", line 8 |
| SyntaxError: 'continue' not supported inside 'finally' clause |
| |
| There is one test for a break that is not in a loop. The compiler |
| uses a single data structure to keep track of try-finally and loops, |
| so we need to be sure that a break is actually inside a loop. If it |
| isn't, there should be a syntax error. |
| |
| >>> try: |
| ... print 1 |
| ... break |
| ... print 2 |
| ... finally: |
| ... print 3 |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[42]>", line 3 |
| SyntaxError: 'break' outside loop |
| |
| This raises a SyntaxError, it used to raise a SystemError. |
| Context for this change can be found on issue #27514 |
| |
| In 2.5 there was a missing exception and an assert was triggered in a debug |
| build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 |
| |
| >>> while 1: |
| ... while 2: |
| ... while 3: |
| ... while 4: |
| ... while 5: |
| ... while 6: |
| ... while 8: |
| ... while 9: |
| ... while 10: |
| ... while 11: |
| ... while 12: |
| ... while 13: |
| ... while 14: |
| ... while 15: |
| ... while 16: |
| ... while 17: |
| ... while 18: |
| ... while 19: |
| ... while 20: |
| ... while 21: |
| ... while 22: |
| ... break |
| Traceback (most recent call last): |
| ... |
| SyntaxError: too many statically nested blocks |
| |
| Misuse of the global statement can lead to a few unique syntax errors. |
| |
| >>> def f(x): |
| ... global x |
| ... # doctest: +ELLIPSIS |
| Traceback (most recent call last): |
| ... |
| SyntaxError: name 'x' is local and global (<doctest ...>, line 1) |
| |
| |
| This tests assignment-context; there was a bug in Python 2.5 where compiling |
| a complex 'if' (one with 'elif') would fail to notice an invalid suite, |
| leading to spurious errors. |
| |
| >>> if 1: |
| ... x() = 1 |
| ... elif 1: |
| ... pass |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[44]>", line 2 |
| SyntaxError: can't assign to function call |
| |
| >>> if 1: |
| ... pass |
| ... elif 1: |
| ... x() = 1 |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[45]>", line 4 |
| SyntaxError: can't assign to function call |
| |
| >>> if 1: |
| ... x() = 1 |
| ... elif 1: |
| ... pass |
| ... else: |
| ... pass |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[46]>", line 2 |
| SyntaxError: can't assign to function call |
| |
| >>> if 1: |
| ... pass |
| ... elif 1: |
| ... x() = 1 |
| ... else: |
| ... pass |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[47]>", line 4 |
| SyntaxError: can't assign to function call |
| |
| >>> if 1: |
| ... pass |
| ... elif 1: |
| ... pass |
| ... else: |
| ... x() = 1 |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[48]>", line 6 |
| SyntaxError: can't assign to function call |
| |
| Test the "raise X, Y[, Z]" form: |
| |
| >>> raise ArithmeticError, 'bad number' |
| Traceback (most recent call last): |
| ... |
| ArithmeticError: bad number |
| >>> raise ArithmeticError, 'bad number', None |
| Traceback (most recent call last): |
| ... |
| ArithmeticError: bad number |
| |
| |
| >>> f(a=23, a=234) |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[49]>", line 1 |
| SyntaxError: keyword argument repeated |
| |
| >>> del () |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[50]>", line 1 |
| SyntaxError: can't delete () |
| |
| >>> {1, 2, 3} = 42 |
| Traceback (most recent call last): |
| ... |
| File "<doctest test.test_syntax[50]>", line 1 |
| SyntaxError: can't assign to literal |
| |
| Corner-case that used to fail to raise the correct error: |
| |
| >>> def f(x=lambda __debug__:0): pass |
| Traceback (most recent call last): |
| SyntaxError: cannot assign to __debug__ |
| |
| Corner-case that used to crash: |
| |
| >>> def f(*xx, **__debug__): pass |
| Traceback (most recent call last): |
| SyntaxError: cannot assign to __debug__ |
| |
| """ |
| |
| import re |
| import unittest |
| import warnings |
| |
| from test import support |
| |
| class SyntaxTestCase(unittest.TestCase): |
| |
| def _check_error(self, code, errtext, |
| filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None): |
| """Check that compiling code raises SyntaxError with errtext. |
| |
| errtest is a regular expression that must be present in the |
| test of the exception raised. If subclass is specified it |
| is the expected subclass of SyntaxError (e.g. IndentationError). |
| """ |
| try: |
| compile(code, filename or "<testcase>", mode) |
| except SyntaxError as err: |
| if subclass and not isinstance(err, subclass): |
| self.fail("SyntaxError is not a %s" % subclass.__name__) |
| mo = re.search(errtext, str(err)) |
| if mo is None: |
| self.fail("%s did not contain '%r'" % (err, errtext,)) |
| if filename is not None: |
| self.assertEqual(err.filename, filename) |
| if lineno is not None: |
| self.assertEqual(err.lineno, lineno) |
| if offset is not None: |
| self.assertEqual(err.offset, offset) |
| else: |
| self.fail("compile() did not raise SyntaxError") |
| |
| def test_paren_arg_with_default(self): |
| self._check_error("def f((x)=23): pass", |
| "parenthesized arg with default") |
| |
| def test_assign_repr(self): |
| with support.check_py3k_warnings(('backquote not supported', |
| SyntaxWarning)): |
| self._check_error("`1` = 1", "assign to repr") |
| |
| def test_assign_call(self): |
| self._check_error("f() = 1", "assign") |
| |
| def test_assign_del(self): |
| self._check_error("del f()", "delete") |
| |
| def test_global_err_then_warn(self): |
| # Bug tickler: The SyntaxError raised for one global statement |
| # shouldn't be clobbered by a SyntaxWarning issued for a later one. |
| source = """if 1: |
| def error(a): |
| global a # SyntaxError |
| def warning(): |
| b = 1 |
| global b # SyntaxWarning |
| """ |
| with support.check_warnings((".*assigned to before global declaration", |
| SyntaxWarning)): |
| self._check_error(source, "local and global", lineno=2) |
| |
| def test_misuse_global(self): |
| source = """if 1: |
| def f(): |
| print(x) |
| global x |
| """ |
| with support.check_warnings(('.*used prior to global declaration', |
| SyntaxWarning)): |
| compile(source, '<testcase>', 'exec') |
| |
| def test_misuse_global_2(self): |
| source = """if 1: |
| def f(): |
| x = 1 |
| global x |
| """ |
| with support.check_warnings(('.*assigned to before global declaration', |
| SyntaxWarning)): |
| compile(source, '<testcase>', 'exec') |
| |
| def test_break_outside_loop(self): |
| self._check_error("break", "outside loop") |
| |
| def test_delete_deref(self): |
| source = """if 1: |
| def foo(x): |
| def bar(): |
| print(x) |
| del x |
| """ |
| self._check_error(source, "nested scope", filename=None) |
| |
| def test_unexpected_indent(self): |
| self._check_error("foo()\n bar()\n", "unexpected indent", |
| subclass=IndentationError) |
| |
| def test_no_indent(self): |
| self._check_error("if 1:\nfoo()", "expected an indented block", |
| subclass=IndentationError) |
| |
| def test_bad_outdent(self): |
| self._check_error("if 1:\n foo()\n bar()", |
| "unindent does not match .* level", |
| subclass=IndentationError) |
| |
| def test_kwargs_last(self): |
| self._check_error("int(base=10, '2')", "non-keyword arg") |
| |
| def test_main(): |
| support.run_unittest(SyntaxTestCase) |
| from test import test_syntax |
| support.run_doctest(test_syntax, verbosity=True) |
| |
| if __name__ == "__main__": |
| test_main() |