blob: 103b4c110936c7d85882bb0173b6d92e169c8179 [file] [log] [blame]
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001"""This module tests SyntaxErrors.
2
3Here's an example of the sort of thing that is tested.
4
5>>> def f(x):
6... global x
7Traceback (most recent call last):
Benjamin Peterson08473322008-08-16 22:11:33 +00008SyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00009
10The tests are all raise SyntaxErrors. They were created by checking
11each C call that raises SyntaxError. There are several modules that
12raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
13symtable.c.
14
15The parser itself outlaws a lot of invalid syntax. None of these
16errors are tested here at the moment. We should add some tests; since
17there are infinitely many programs with invalid syntax, we would need
18to be judicious in selecting some.
19
20The compiler generates a synthetic module name for code executed by
21doctest. 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
23order of tests in this module means renumbering all the errors after
24it. (Maybe we should enable the ellipsis option for these tests.)
25
26In ast.c, syntax errors are raised by calling ast_error().
27
28Errors from set_context():
29
Jeremy Hyltonc960f262006-01-27 15:18:39 +000030>>> obj.None = 1
31Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000032 File "<doctest test.test_syntax[1]>", line 1
33SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +000034
35>>> None = 1
36Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000037 File "<doctest test.test_syntax[2]>", line 1
38SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +000039
40It's a syntax error to assign to the empty tuple. Why isn't it an
41error to assign to the empty list? It will always raise some error at
42runtime.
43
44>>> () = 1
45Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000046 File "<doctest test.test_syntax[3]>", line 1
47SyntaxError: can't assign to ()
Jeremy Hyltonc960f262006-01-27 15:18:39 +000048
49>>> f() = 1
50Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000051 File "<doctest test.test_syntax[4]>", line 1
52SyntaxError: can't assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000053
54>>> del f()
55Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000056 File "<doctest test.test_syntax[5]>", line 1
57SyntaxError: can't delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000058
59>>> a + 1 = 2
60Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000061 File "<doctest test.test_syntax[6]>", line 1
62SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000063
64>>> (x for x in x) = 1
65Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000066 File "<doctest test.test_syntax[7]>", line 1
67SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000068
69>>> 1 = 1
70Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000071 File "<doctest test.test_syntax[8]>", line 1
72SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000073
74>>> "abc" = 1
75Traceback (most recent call last):
Serhiy Storchaka0bcba372018-01-04 10:36:14 +020076 File "<doctest test.test_syntax[9]>", line 1
Benjamin Peterson52b96202009-04-07 15:15:04 +000077SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000078
Serhiy Storchaka0bcba372018-01-04 10:36:14 +020079>>> b"" = 1
Jeremy Hyltonc960f262006-01-27 15:18:39 +000080Traceback (most recent call last):
Serhiy Storchaka0bcba372018-01-04 10:36:14 +020081SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000082
83If the left-hand side of an assignment is a list or tuple, an illegal
84expression inside that contain should still cause a syntax error.
85This test just checks a couple of cases rather than enumerating all of
86them.
87
88>>> (a, "b", c) = (1, 2, 3)
89Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000090 File "<doctest test.test_syntax[11]>", line 1
91SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000092
93>>> [a, b, c + 1] = [1, 2, 3]
94Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000095 File "<doctest test.test_syntax[12]>", line 1
96SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000097
Neal Norwitz373f0a72006-05-15 07:04:36 +000098>>> a if 1 else b = 1
99Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000100 File "<doctest test.test_syntax[13]>", line 1
101SyntaxError: can't assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000102
103From compiler_complex_args():
104
105>>> def f(None=1):
106... pass
107Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000108 File "<doctest test.test_syntax[14]>", line 1
109SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000110
111
112From ast_for_arguments():
113
114>>> def f(x, y=1, z):
115... pass
116Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000117 File "<doctest test.test_syntax[15]>", line 1
118SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000119
120>>> def f(x, None):
121... pass
122Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000123 File "<doctest test.test_syntax[16]>", line 1
124SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000125
126>>> def f(*None):
127... pass
128Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000129 File "<doctest test.test_syntax[17]>", line 1
130SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000131
132>>> def f(**None):
133... pass
134Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000135 File "<doctest test.test_syntax[18]>", line 1
136SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000137
138
139From ast_for_funcdef():
140
141>>> def None(x):
142... pass
143Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000144 File "<doctest test.test_syntax[19]>", line 1
145SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000146
147
148From ast_for_call():
149
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200150>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000151... return list(it)
152>>> L = range(10)
153>>> f(x for x in L)
154[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
155>>> f(x for x in L, 1)
156Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000157 File "<doctest test.test_syntax[23]>", line 1
158SyntaxError: Generator expression must be parenthesized if not sole argument
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200159>>> f(x for x in L, y=1)
160Traceback (most recent call last):
161SyntaxError: Generator expression must be parenthesized if not sole argument
162>>> f(L, x for x in L)
163Traceback (most recent call last):
164SyntaxError: Generator expression must be parenthesized if not sole argument
165>>> f(x for x in L, y for y in L)
166Traceback (most recent call last):
167SyntaxError: Generator expression must be parenthesized if not sole argument
168>>> f(x for x in L,)
169[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000170>>> f((x for x in L), 1)
171[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
172
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200173>>> def g(*args, **kwargs):
174... print args, sorted(kwargs.items())
175>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
176... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
177... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
178... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
179... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
180... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
181... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
182... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
183... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
184... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
185... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
186... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
187... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
188... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
189... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
190... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
191... 248, 249, 250, 251, 252, 253, 254) # doctest: +ELLIPSIS
192(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 252, 253, 254) []
193>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
194... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
195... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
196... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
197... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
198... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
199... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
200... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
201... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
202... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
203... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
204... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
205... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
206... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
207... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
208... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
209... 248, 249, 250, 251, 252, 253, 254, 255)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000210Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000211 File "<doctest test.test_syntax[25]>", line 1
212SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000213
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200214>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
215... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
216... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
217... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
218... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
219... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
220... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
221... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
222... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
223... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
224... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
225... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
226... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
227... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
228... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
229... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
230... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
231... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
232... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
233... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
234... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
235... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
236... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
237... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
238... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
239... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
240... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
241... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
242... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
243... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
244... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
245... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
246... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
247... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
248... a251=251, a252=252, a253=253, a254=254) # doctest: +ELLIPSIS
249() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a253', 253), ('a254', 254)]
250>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
251... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
252... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
253... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
254... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
255... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
256... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
257... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
258... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
259... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
260... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
261... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
262... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
263... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
264... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
265... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
266... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
267... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
268... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
269... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
270... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
271... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
272... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
273... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
274... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
275... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
276... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
277... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
278... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
279... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
280... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
281... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
282... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
283... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
284... a251=251, a252=252, a253=253, a254=254, a255=255)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000285Traceback (most recent call last):
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200286 File "<doctest test.test_syntax[35]>", line 1
287SyntaxError: more than 255 arguments
288
289>>> class C:
290... def meth(self, *args):
291... return args
292>>> obj = C()
293>>> obj.meth(
294... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
295... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
296... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
297... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
298... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
299... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
300... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
301... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
302... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
303... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
304... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
305... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
306... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
307... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
308... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
309... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
310... 248, 249, 250, 251, 252, 253, 254) # doctest: +ELLIPSIS
311(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 252, 253, 254)
312>>> obj.meth(
313... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
314... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
315... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
316... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
317... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
318... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
319... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
320... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
321... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
322... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
323... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
324... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
325... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
326... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
327... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
328... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
329... 248, 249, 250, 251, 252, 253, 254, 255)
330Traceback (most recent call last):
331 File "<doctest test.test_syntax[38]>", line 1
Benjamin Peterson52b96202009-04-07 15:15:04 +0000332SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000333
334>>> f(lambda x: x[0] = 3)
335Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000336 File "<doctest test.test_syntax[27]>", line 1
337SyntaxError: lambda cannot contain assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000338
339The grammar accepts any test (basically, any expression) in the
340keyword slot of a call site. Test a few different options.
341
342>>> f(x()=2)
343Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000344 File "<doctest test.test_syntax[28]>", line 1
345SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000346>>> f(a or b=1)
347Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000348 File "<doctest test.test_syntax[29]>", line 1
349SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000350>>> f(x.y=1)
351Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000352 File "<doctest test.test_syntax[30]>", line 1
353SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000354
355
Benjamin Petersona5a57282009-06-08 23:44:13 +0000356More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000357
358>>> (x for x in x) += 1
359Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000360 File "<doctest test.test_syntax[31]>", line 1
Benjamin Petersona5a57282009-06-08 23:44:13 +0000361SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000362>>> None += 1
363Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000364 File "<doctest test.test_syntax[32]>", line 1
365SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000366>>> f() += 1
367Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000368 File "<doctest test.test_syntax[33]>", line 1
Benjamin Petersona5a57282009-06-08 23:44:13 +0000369SyntaxError: can't assign to function call
Neal Norwitz4f096d92006-08-21 19:47:08 +0000370
371
372Test continue in finally in weird combinations.
373
Ezio Melottic2077b02011-03-16 12:34:31 +0200374continue in for loop under finally should be ok.
Neal Norwitz4f096d92006-08-21 19:47:08 +0000375
376 >>> def test():
377 ... try:
378 ... pass
379 ... finally:
380 ... for abc in range(10):
381 ... continue
382 ... print abc
383 >>> test()
384 9
385
386Start simple, a continue in a finally should not be allowed.
387
388 >>> def test():
389 ... for abc in range(10):
390 ... try:
391 ... pass
392 ... finally:
393 ... continue
394 Traceback (most recent call last):
395 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000396 File "<doctest test.test_syntax[36]>", line 6
397 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000398
399This is essentially a continue in a finally which should not be allowed.
400
401 >>> def test():
402 ... for abc in range(10):
403 ... try:
404 ... pass
405 ... finally:
406 ... try:
407 ... continue
408 ... except:
409 ... pass
410 Traceback (most recent call last):
411 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000412 File "<doctest test.test_syntax[37]>", line 6
413 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000414
415 >>> def foo():
416 ... try:
417 ... pass
418 ... finally:
419 ... continue
420 Traceback (most recent call last):
421 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000422 File "<doctest test.test_syntax[38]>", line 5
423 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000424
425 >>> def foo():
426 ... for a in ():
427 ... try:
428 ... pass
429 ... finally:
430 ... continue
431 Traceback (most recent call last):
432 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000433 File "<doctest test.test_syntax[39]>", line 6
434 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000435
436 >>> def foo():
437 ... for a in ():
438 ... try:
439 ... pass
440 ... finally:
441 ... try:
442 ... continue
443 ... finally:
444 ... pass
445 Traceback (most recent call last):
446 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000447 File "<doctest test.test_syntax[40]>", line 7
448 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000449
450 >>> def foo():
451 ... for a in ():
452 ... try: pass
453 ... finally:
454 ... try:
455 ... pass
456 ... except:
457 ... continue
458 Traceback (most recent call last):
459 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000460 File "<doctest test.test_syntax[41]>", line 8
461 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000462
Jeremy Hylton82271f12006-10-04 02:24:52 +0000463There is one test for a break that is not in a loop. The compiler
464uses a single data structure to keep track of try-finally and loops,
465so we need to be sure that a break is actually inside a loop. If it
466isn't, there should be a syntax error.
467
468 >>> try:
469 ... print 1
470 ... break
471 ... print 2
472 ... finally:
473 ... print 3
474 Traceback (most recent call last):
475 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000476 File "<doctest test.test_syntax[42]>", line 3
477 SyntaxError: 'break' outside loop
Neal Norwitz21997af2006-10-28 21:19:07 +0000478
Benjamin Peterson6c4fa702016-07-14 22:00:03 -0700479This raises a SyntaxError, it used to raise a SystemError.
480Context for this change can be found on issue #27514
481
Neal Norwitz21997af2006-10-28 21:19:07 +0000482In 2.5 there was a missing exception and an assert was triggered in a debug
483build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
484
485 >>> while 1:
486 ... while 2:
487 ... while 3:
488 ... while 4:
489 ... while 5:
490 ... while 6:
491 ... while 8:
492 ... while 9:
493 ... while 10:
494 ... while 11:
495 ... while 12:
496 ... while 13:
497 ... while 14:
498 ... while 15:
499 ... while 16:
500 ... while 17:
501 ... while 18:
502 ... while 19:
503 ... while 20:
504 ... while 21:
505 ... while 22:
506 ... break
507 Traceback (most recent call last):
508 ...
Benjamin Peterson6c4fa702016-07-14 22:00:03 -0700509 SyntaxError: too many statically nested blocks
Neal Norwitz21997af2006-10-28 21:19:07 +0000510
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200511Misuse of the global statement can lead to a few unique syntax errors.
512
513 >>> def f(x):
514 ... global x
515 ... # doctest: +ELLIPSIS
516 Traceback (most recent call last):
517 ...
518 SyntaxError: name 'x' is local and global (<doctest ...>, line 1)
519
520
Collin Winter77c67bd2007-03-16 04:11:30 +0000521This tests assignment-context; there was a bug in Python 2.5 where compiling
522a complex 'if' (one with 'elif') would fail to notice an invalid suite,
523leading to spurious errors.
524
525 >>> if 1:
526 ... x() = 1
527 ... elif 1:
528 ... pass
529 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000530 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000531 File "<doctest test.test_syntax[44]>", line 2
532 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000533
534 >>> if 1:
535 ... pass
536 ... elif 1:
537 ... x() = 1
538 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000539 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000540 File "<doctest test.test_syntax[45]>", line 4
541 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000542
543 >>> if 1:
544 ... x() = 1
545 ... elif 1:
546 ... pass
547 ... else:
548 ... pass
549 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000550 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000551 File "<doctest test.test_syntax[46]>", line 2
552 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000553
554 >>> if 1:
555 ... pass
556 ... elif 1:
557 ... x() = 1
558 ... else:
559 ... pass
560 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000561 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000562 File "<doctest test.test_syntax[47]>", line 4
563 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000564
565 >>> if 1:
566 ... pass
567 ... elif 1:
568 ... pass
569 ... else:
570 ... x() = 1
571 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000572 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000573 File "<doctest test.test_syntax[48]>", line 6
574 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000575
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200576Test the "raise X, Y[, Z]" form:
577
578 >>> raise ArithmeticError, 'bad number'
579 Traceback (most recent call last):
580 ...
581 ArithmeticError: bad number
582 >>> raise ArithmeticError, 'bad number', None
583 Traceback (most recent call last):
584 ...
585 ArithmeticError: bad number
586
587
Benjamin Peterson175e4d92008-07-01 19:34:52 +0000588>>> f(a=23, a=234)
589Traceback (most recent call last):
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200590 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000591 File "<doctest test.test_syntax[49]>", line 1
592SyntaxError: keyword argument repeated
Benjamin Peterson175e4d92008-07-01 19:34:52 +0000593
Benjamin Peterson52c4bec2009-06-13 17:08:53 +0000594>>> del ()
595Traceback (most recent call last):
596 ...
597 File "<doctest test.test_syntax[50]>", line 1
598SyntaxError: can't delete ()
599
Benjamin Peterson947ce582010-06-24 00:12:40 +0000600>>> {1, 2, 3} = 42
601Traceback (most recent call last):
602 ...
603 File "<doctest test.test_syntax[50]>", line 1
604SyntaxError: can't assign to literal
605
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200606Corner-case that used to fail to raise the correct error:
607
608 >>> def f(x=lambda __debug__:0): pass
609 Traceback (most recent call last):
610 SyntaxError: cannot assign to __debug__
611
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000612Corner-case that used to crash:
613
614 >>> def f(*xx, **__debug__): pass
615 Traceback (most recent call last):
616 SyntaxError: cannot assign to __debug__
617
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000618"""
619
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000620import re
621import unittest
Jeremy Hylton42d90162003-07-15 20:24:27 +0000622import warnings
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000623
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200624from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000625
626class SyntaxTestCase(unittest.TestCase):
627
628 def _check_error(self, code, errtext,
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200629 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000630 """Check that compiling code raises SyntaxError with errtext.
631
632 errtest is a regular expression that must be present in the
Martin v. Löwis77736712006-05-04 05:51:03 +0000633 test of the exception raised. If subclass is specified it
634 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000635 """
636 try:
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200637 compile(code, filename or "<testcase>", mode)
638 except SyntaxError as err:
Martin v. Löwis77736712006-05-04 05:51:03 +0000639 if subclass and not isinstance(err, subclass):
640 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000641 mo = re.search(errtext, str(err))
642 if mo is None:
Benjamin Peterson99a50232009-11-19 22:54:57 +0000643 self.fail("%s did not contain '%r'" % (err, errtext,))
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200644 if filename is not None:
645 self.assertEqual(err.filename, filename)
646 if lineno is not None:
647 self.assertEqual(err.lineno, lineno)
648 if offset is not None:
649 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000650 else:
651 self.fail("compile() did not raise SyntaxError")
652
Benjamin Peterson99a50232009-11-19 22:54:57 +0000653 def test_paren_arg_with_default(self):
654 self._check_error("def f((x)=23): pass",
655 "parenthesized arg with default")
656
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200657 def test_assign_repr(self):
658 with support.check_py3k_warnings(('backquote not supported',
659 SyntaxWarning)):
660 self._check_error("`1` = 1", "assign to repr")
661
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000662 def test_assign_call(self):
663 self._check_error("f() = 1", "assign")
664
665 def test_assign_del(self):
666 self._check_error("del f()", "delete")
667
Jeremy Hylton42d90162003-07-15 20:24:27 +0000668 def test_global_err_then_warn(self):
669 # Bug tickler: The SyntaxError raised for one global statement
670 # shouldn't be clobbered by a SyntaxWarning issued for a later one.
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200671 source = """if 1:
672 def error(a):
673 global a # SyntaxError
674 def warning():
675 b = 1
676 global b # SyntaxWarning
677 """
678 with support.check_warnings((".*assigned to before global declaration",
679 SyntaxWarning)):
680 self._check_error(source, "local and global", lineno=2)
681
682 def test_misuse_global(self):
683 source = """if 1:
684 def f():
685 print(x)
686 global x
687 """
688 with support.check_warnings(('.*used prior to global declaration',
689 SyntaxWarning)):
690 compile(source, '<testcase>', 'exec')
691
692 def test_misuse_global_2(self):
693 source = """if 1:
694 def f():
695 x = 1
696 global x
697 """
698 with support.check_warnings(('.*assigned to before global declaration',
699 SyntaxWarning)):
700 compile(source, '<testcase>', 'exec')
Jeremy Hylton42d90162003-07-15 20:24:27 +0000701
Neal Norwitzfcf44352005-11-27 20:37:43 +0000702 def test_break_outside_loop(self):
703 self._check_error("break", "outside loop")
704
705 def test_delete_deref(self):
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200706 source = """if 1:
707 def foo(x):
708 def bar():
709 print(x)
710 del x
711 """
712 self._check_error(source, "nested scope", filename=None)
Neal Norwitzfcf44352005-11-27 20:37:43 +0000713
Martin v. Löwis77736712006-05-04 05:51:03 +0000714 def test_unexpected_indent(self):
715 self._check_error("foo()\n bar()\n", "unexpected indent",
716 subclass=IndentationError)
717
718 def test_no_indent(self):
719 self._check_error("if 1:\nfoo()", "expected an indented block",
720 subclass=IndentationError)
721
722 def test_bad_outdent(self):
723 self._check_error("if 1:\n foo()\n bar()",
724 "unindent does not match .* level",
725 subclass=IndentationError)
726
Neal Norwitz5ef92242006-05-19 06:43:50 +0000727 def test_kwargs_last(self):
728 self._check_error("int(base=10, '2')", "non-keyword arg")
729
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000730def test_main():
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200731 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000732 from test import test_syntax
Serhiy Storchaka0bcba372018-01-04 10:36:14 +0200733 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000734
735if __name__ == "__main__":
736 test_main()