blob: ce1de4b319f20f958b01ebbdacffa92f1c3b2a64 [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):
Nick Coghlan650f0d02007-04-15 12:05:43 +00008SyntaxError: name 'x' is parameter and global
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):
Guido van Rossume7ba4952007-06-06 23:52:48 +000032SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000033
34>>> None = 1
35Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020036SyntaxError: cannot assign to None
37
38>>> obj.True = 1
39Traceback (most recent call last):
40SyntaxError: invalid syntax
41
42>>> True = 1
43Traceback (most recent call last):
44SyntaxError: cannot assign to True
45
46>>> obj.__debug__ = 1
47Traceback (most recent call last):
48SyntaxError: cannot assign to __debug__
49
50>>> __debug__ = 1
51Traceback (most recent call last):
52SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +000053
Jeremy Hyltonc960f262006-01-27 15:18:39 +000054>>> f() = 1
55Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020056SyntaxError: cannot assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000057
58>>> del f()
59Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020060SyntaxError: cannot delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000061
62>>> a + 1 = 2
63Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020064SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000065
66>>> (x for x in x) = 1
67Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020068SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000069
70>>> 1 = 1
71Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020072SyntaxError: cannot assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000073
74>>> "abc" = 1
75Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020076SyntaxError: cannot assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000077
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050078>>> b"" = 1
79Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020080SyntaxError: cannot assign to literal
81
82>>> ... = 1
83Traceback (most recent call last):
84SyntaxError: cannot assign to Ellipsis
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050085
Jeremy Hyltonc960f262006-01-27 15:18:39 +000086>>> `1` = 1
87Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +000088SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000089
90If the left-hand side of an assignment is a list or tuple, an illegal
91expression inside that contain should still cause a syntax error.
92This test just checks a couple of cases rather than enumerating all of
93them.
94
95>>> (a, "b", c) = (1, 2, 3)
96Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020097SyntaxError: cannot assign to literal
98
99>>> (a, True, c) = (1, 2, 3)
100Traceback (most recent call last):
101SyntaxError: cannot assign to True
102
103>>> (a, __debug__, c) = (1, 2, 3)
104Traceback (most recent call last):
105SyntaxError: cannot assign to __debug__
106
107>>> (a, *True, c) = (1, 2, 3)
108Traceback (most recent call last):
109SyntaxError: cannot assign to True
110
111>>> (a, *__debug__, c) = (1, 2, 3)
112Traceback (most recent call last):
113SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000114
115>>> [a, b, c + 1] = [1, 2, 3]
116Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200117SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000118
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119>>> a if 1 else b = 1
120Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200121SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000122
123From compiler_complex_args():
124
125>>> def f(None=1):
126... pass
127Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000128SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000129
130
131From ast_for_arguments():
132
133>>> def f(x, y=1, z):
134... pass
135Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000136SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000137
138>>> def f(x, None):
139... pass
140Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000141SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000142
143>>> def f(*None):
144... pass
145Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000146SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000147
148>>> def f(**None):
149... pass
150Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000151SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000152
153
154From ast_for_funcdef():
155
156>>> def None(x):
157... pass
158Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000159SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000160
161
162From ast_for_call():
163
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200164>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000165... return list(it)
166>>> L = range(10)
167>>> f(x for x in L)
168[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
169>>> f(x for x in L, 1)
170Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200171SyntaxError: Generator expression must be parenthesized
172>>> f(x for x in L, y=1)
173Traceback (most recent call last):
174SyntaxError: Generator expression must be parenthesized
175>>> f(x for x in L, *[])
176Traceback (most recent call last):
177SyntaxError: Generator expression must be parenthesized
178>>> f(x for x in L, **{})
179Traceback (most recent call last):
180SyntaxError: Generator expression must be parenthesized
181>>> f(L, x for x in L)
182Traceback (most recent call last):
183SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400184>>> f(x for x in L, y for y in L)
185Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200186SyntaxError: Generator expression must be parenthesized
187>>> f(x for x in L,)
188Traceback (most recent call last):
189SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000190>>> f((x for x in L), 1)
191[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200192>>> class C(x for x in L):
193... pass
194Traceback (most recent call last):
195SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000196
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200197>>> def g(*args, **kwargs):
198... print(args, sorted(kwargs.items()))
199>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
200... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
201... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
202... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
203... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
204... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
205... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
206... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
207... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
208... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
209... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
210... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
211... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
212... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
213... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
214... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
215... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
216... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
217... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
218... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
219(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000220
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200221>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
222... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
223... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
224... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
225... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
226... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
227... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
228... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
229... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
230... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
231... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
232... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
233... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
234... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
235... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
236... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
237... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
238... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
239... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
240... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
241... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
242... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
243... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
244... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
245... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
246... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
247... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
248... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
249... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
250... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
251... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
252... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
253... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
254... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
255... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
256... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
257... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
258... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
259... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
260... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
261... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
262... # doctest: +ELLIPSIS
263() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000264
Yury Selivanovf2392132016-12-13 19:03:51 -0500265>>> class C:
266... def meth(self, *args):
267... return args
268>>> obj = C()
269>>> obj.meth(
270... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
271... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
272... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
273... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
274... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
275... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
276... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
277... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
278... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
279... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
280... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
281... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
282... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
283... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
284... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
285... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
286... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
287... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
288... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
289... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
290(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
291
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000292>>> f(lambda x: x[0] = 3)
293Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200294SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000295
296The grammar accepts any test (basically, any expression) in the
297keyword slot of a call site. Test a few different options.
298
299>>> f(x()=2)
300Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200301SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000302>>> f(a or b=1)
303Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200304SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000305>>> f(x.y=1)
306Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200307SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -0700308>>> f((x)=2)
309Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200310SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
311>>> f(True=2)
312Traceback (most recent call last):
313SyntaxError: cannot assign to True
314>>> f(__debug__=1)
315Traceback (most recent call last):
316SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000317
318
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000319More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000320
321>>> (x for x in x) += 1
322Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200323SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000324>>> None += 1
325Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200326SyntaxError: cannot assign to None
327>>> __debug__ += 1
328Traceback (most recent call last):
329SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000330>>> f() += 1
331Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200332SyntaxError: cannot assign to function call
Thomas Wouters89f507f2006-12-13 04:49:30 +0000333
334
335Test continue in finally in weird combinations.
336
Ezio Melotti13925002011-03-16 11:05:33 +0200337continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000338
339 >>> def test():
340 ... try:
341 ... pass
342 ... finally:
343 ... for abc in range(10):
344 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000345 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346 >>> test()
347 9
348
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200349continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350
351 >>> def test():
352 ... for abc in range(10):
353 ... try:
354 ... pass
355 ... finally:
356 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200357 ... print(abc)
358 >>> test()
359 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000360
361 >>> def test():
362 ... for abc in range(10):
363 ... try:
364 ... pass
365 ... finally:
366 ... try:
367 ... continue
368 ... except:
369 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200370 ... print(abc)
371 >>> test()
372 9
373
374 >>> def test():
375 ... for abc in range(10):
376 ... try:
377 ... pass
378 ... finally:
379 ... try:
380 ... pass
381 ... except:
382 ... continue
383 ... print(abc)
384 >>> test()
385 9
386
387A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000388
389 >>> def foo():
390 ... try:
391 ... pass
392 ... finally:
393 ... continue
394 Traceback (most recent call last):
395 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200396 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000397
398There is one test for a break that is not in a loop. The compiler
399uses a single data structure to keep track of try-finally and loops,
400so we need to be sure that a break is actually inside a loop. If it
401isn't, there should be a syntax error.
402
403 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000404 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000405 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000406 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000407 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000408 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000409 Traceback (most recent call last):
410 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000411 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000412
Benjamin Petersone09ed542016-07-14 22:00:03 -0700413This raises a SyntaxError, it used to raise a SystemError.
414Context for this change can be found on issue #27514
415
Thomas Wouters89f507f2006-12-13 04:49:30 +0000416In 2.5 there was a missing exception and an assert was triggered in a debug
417build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
418
419 >>> while 1:
420 ... while 2:
421 ... while 3:
422 ... while 4:
423 ... while 5:
424 ... while 6:
425 ... while 8:
426 ... while 9:
427 ... while 10:
428 ... while 11:
429 ... while 12:
430 ... while 13:
431 ... while 14:
432 ... while 15:
433 ... while 16:
434 ... while 17:
435 ... while 18:
436 ... while 19:
437 ... while 20:
438 ... while 21:
439 ... while 22:
440 ... break
441 Traceback (most recent call last):
442 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700443 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000444
Guido van Rossum6cff8742016-09-09 09:36:26 -0700445Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
446
447 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200448 ... print(x)
449 ... global x
450 Traceback (most recent call last):
451 ...
452 SyntaxError: name 'x' is used prior to global declaration
453
454 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700455 ... x = 1
456 ... global x
457 Traceback (most recent call last):
458 ...
459 SyntaxError: name 'x' is assigned to before global declaration
460
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200461 >>> def f(x):
462 ... global x
463 Traceback (most recent call last):
464 ...
465 SyntaxError: name 'x' is parameter and global
466
Guido van Rossum6cff8742016-09-09 09:36:26 -0700467 >>> def f():
468 ... x = 1
469 ... def g():
470 ... print(x)
471 ... nonlocal x
472 Traceback (most recent call last):
473 ...
474 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000475
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300476 >>> def f():
477 ... x = 1
478 ... def g():
479 ... x = 2
480 ... nonlocal x
481 Traceback (most recent call last):
482 ...
483 SyntaxError: name 'x' is assigned to before nonlocal declaration
484
Jeremy Hylton81e95022007-02-27 06:50:52 +0000485 >>> def f(x):
486 ... nonlocal x
487 Traceback (most recent call last):
488 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000489 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000490
Jeremy Hylton81e95022007-02-27 06:50:52 +0000491 >>> def f():
492 ... global x
493 ... nonlocal x
494 Traceback (most recent call last):
495 ...
496 SyntaxError: name 'x' is nonlocal and global
497
498 >>> def f():
499 ... nonlocal x
500 Traceback (most recent call last):
501 ...
502 SyntaxError: no binding for nonlocal 'x' found
503
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000504From SF bug #1705365
505 >>> nonlocal x
506 Traceback (most recent call last):
507 ...
508 SyntaxError: nonlocal declaration not allowed at module level
509
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300510From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600511 >>> class A:
512 ... def f(self):
513 ... nonlocal __x
514 Traceback (most recent call last):
515 ...
516 SyntaxError: no binding for nonlocal '_A__x' found
517
Guido van Rossumd8faa362007-04-27 19:54:29 +0000518
519This tests assignment-context; there was a bug in Python 2.5 where compiling
520a complex 'if' (one with 'elif') would fail to notice an invalid suite,
521leading to spurious errors.
522
523 >>> if 1:
524 ... x() = 1
525 ... elif 1:
526 ... pass
527 Traceback (most recent call last):
528 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200529 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000530
531 >>> if 1:
532 ... pass
533 ... elif 1:
534 ... x() = 1
535 Traceback (most recent call last):
536 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200537 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000538
539 >>> if 1:
540 ... x() = 1
541 ... elif 1:
542 ... pass
543 ... else:
544 ... pass
545 Traceback (most recent call last):
546 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200547 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000548
549 >>> if 1:
550 ... pass
551 ... elif 1:
552 ... x() = 1
553 ... else:
554 ... pass
555 Traceback (most recent call last):
556 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200557 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000558
559 >>> if 1:
560 ... pass
561 ... elif 1:
562 ... pass
563 ... else:
564 ... x() = 1
565 Traceback (most recent call last):
566 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200567 SyntaxError: cannot assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000568
Collin Winter828f04a2007-08-31 00:04:24 +0000569Make sure that the old "raise X, Y[, Z]" form is gone:
570 >>> raise X, Y
571 Traceback (most recent call last):
572 ...
573 SyntaxError: invalid syntax
574 >>> raise X, Y, Z
575 Traceback (most recent call last):
576 ...
577 SyntaxError: invalid syntax
578
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000579
580>>> f(a=23, a=234)
581Traceback (most recent call last):
582 ...
583SyntaxError: keyword argument repeated
584
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000585>>> {1, 2, 3} = 42
586Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200587SyntaxError: cannot assign to set display
588
589>>> {1: 2, 3: 4} = 42
590Traceback (most recent call last):
591SyntaxError: cannot assign to dict display
592
593>>> f'{x}' = 42
594Traceback (most recent call last):
595SyntaxError: cannot assign to f-string expression
596
597>>> f'{x}-{y}' = 42
598Traceback (most recent call last):
599SyntaxError: cannot assign to f-string expression
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000600
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000601Corner-cases that used to fail to raise the correct error:
602
603 >>> def f(*, x=lambda __debug__:0): pass
604 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200605 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000606
607 >>> def f(*args:(lambda __debug__:0)): pass
608 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200609 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000610
611 >>> def f(**kwargs:(lambda __debug__:0)): pass
612 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200613 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000614
615 >>> with (lambda *:0): pass
616 Traceback (most recent call last):
617 SyntaxError: named arguments must follow bare *
618
619Corner-cases that used to crash:
620
621 >>> def f(**__debug__): pass
622 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200623 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000624
625 >>> def f(*xx, __debug__): pass
626 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200627 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000628
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000629"""
630
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000631import re
632import unittest
633
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000634from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000635
636class SyntaxTestCase(unittest.TestCase):
637
638 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200639 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000640 """Check that compiling code raises SyntaxError with errtext.
641
642 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000643 test of the exception raised. If subclass is specified it
644 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000645 """
646 try:
647 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000648 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 if subclass and not isinstance(err, subclass):
650 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000651 mo = re.search(errtext, str(err))
652 if mo is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000653 self.fail("SyntaxError did not contain '%r'" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200654 self.assertEqual(err.filename, filename)
655 if lineno is not None:
656 self.assertEqual(err.lineno, lineno)
657 if offset is not None:
658 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000659 else:
660 self.fail("compile() did not raise SyntaxError")
661
662 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
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200668 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000669 source = """if 1:
670 def error(a):
671 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200672 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000673 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200674 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000675 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200676 self._check_error(source, "parameter and global", lineno=3)
677
678 def test_nonlocal_param_err_first(self):
679 source = """if 1:
680 def error(a):
681 nonlocal a # SyntaxError
682 def error2():
683 b = 1
684 global b # SyntaxError
685 """
686 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +0000687
Neal Norwitzfcf44352005-11-27 20:37:43 +0000688 def test_break_outside_loop(self):
689 self._check_error("break", "outside loop")
690
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691 def test_unexpected_indent(self):
692 self._check_error("foo()\n bar()\n", "unexpected indent",
693 subclass=IndentationError)
694
695 def test_no_indent(self):
696 self._check_error("if 1:\nfoo()", "expected an indented block",
697 subclass=IndentationError)
698
699 def test_bad_outdent(self):
700 self._check_error("if 1:\n foo()\n bar()",
701 "unindent does not match .* level",
702 subclass=IndentationError)
703
704 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400705 self._check_error("int(base=10, '2')",
706 "positional argument follows keyword argument")
707
708 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200709 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400710 "positional argument follows "
711 "keyword argument unpacking")
712
713 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200714 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400715 "iterable argument unpacking follows "
716 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000718def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000719 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000720 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000721 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000722
723if __name__ == "__main__":
724 test_main()