blob: a0f487d4ed76b489ad2c7beb1bda8d2f2b64dbe6 [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
Stéphane Wirtel3ad91672019-02-21 11:11:53 +010054>>> (__debug__ := 1)
55Traceback (most recent call last):
56SyntaxError: cannot assign to __debug__
57
Jeremy Hyltonc960f262006-01-27 15:18:39 +000058>>> f() = 1
59Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020060SyntaxError: cannot assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000061
62>>> del f()
63Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020064SyntaxError: cannot delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000065
66>>> a + 1 = 2
67Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020068SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000069
70>>> (x for x in x) = 1
71Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020072SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000073
74>>> 1 = 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
78>>> "abc" = 1
79Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020080SyntaxError: cannot assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000081
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050082>>> b"" = 1
83Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020084SyntaxError: cannot assign to literal
85
86>>> ... = 1
87Traceback (most recent call last):
88SyntaxError: cannot assign to Ellipsis
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050089
Jeremy Hyltonc960f262006-01-27 15:18:39 +000090>>> `1` = 1
91Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +000092SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000093
94If the left-hand side of an assignment is a list or tuple, an illegal
95expression inside that contain should still cause a syntax error.
96This test just checks a couple of cases rather than enumerating all of
97them.
98
99>>> (a, "b", c) = (1, 2, 3)
100Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200101SyntaxError: cannot assign to literal
102
103>>> (a, True, c) = (1, 2, 3)
104Traceback (most recent call last):
105SyntaxError: cannot assign to True
106
107>>> (a, __debug__, c) = (1, 2, 3)
108Traceback (most recent call last):
109SyntaxError: cannot assign to __debug__
110
111>>> (a, *True, c) = (1, 2, 3)
112Traceback (most recent call last):
113SyntaxError: cannot assign to True
114
115>>> (a, *__debug__, c) = (1, 2, 3)
116Traceback (most recent call last):
117SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000118
119>>> [a, b, c + 1] = [1, 2, 3]
120Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200121SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000122
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123>>> a if 1 else b = 1
124Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200125SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000126
127From compiler_complex_args():
128
129>>> def f(None=1):
130... pass
131Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000132SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000133
134
135From ast_for_arguments():
136
137>>> def f(x, y=1, z):
138... pass
139Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000140SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000141
142>>> def f(x, None):
143... pass
144Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000145SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000146
147>>> def f(*None):
148... pass
149Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000150SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000151
152>>> def f(**None):
153... pass
154Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000155SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000156
157
158From ast_for_funcdef():
159
160>>> def None(x):
161... pass
162Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000163SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000164
165
166From ast_for_call():
167
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200168>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000169... return list(it)
170>>> L = range(10)
171>>> f(x for x in L)
172[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
173>>> f(x for x in L, 1)
174Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200175SyntaxError: Generator expression must be parenthesized
176>>> f(x for x in L, y=1)
177Traceback (most recent call last):
178SyntaxError: Generator expression must be parenthesized
179>>> f(x for x in L, *[])
180Traceback (most recent call last):
181SyntaxError: Generator expression must be parenthesized
182>>> f(x for x in L, **{})
183Traceback (most recent call last):
184SyntaxError: Generator expression must be parenthesized
185>>> f(L, x for x in L)
186Traceback (most recent call last):
187SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400188>>> f(x for x in L, y for y in L)
189Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200190SyntaxError: Generator expression must be parenthesized
191>>> f(x for x in L,)
192Traceback (most recent call last):
193SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000194>>> f((x for x in L), 1)
195[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200196>>> class C(x for x in L):
197... pass
198Traceback (most recent call last):
199SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000200
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200201>>> def g(*args, **kwargs):
202... print(args, sorted(kwargs.items()))
203>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
204... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
205... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
206... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
207... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
208... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
209... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
210... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
211... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
212... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
213... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
214... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
215... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
216... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
217... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
218... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
219... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
220... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
221... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
222... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
223(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000224
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200225>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
226... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
227... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
228... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
229... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
230... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
231... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
232... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
233... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
234... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
235... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
236... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
237... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
238... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
239... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
240... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
241... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
242... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
243... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
244... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
245... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
246... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
247... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
248... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
249... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
250... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
251... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
252... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
253... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
254... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
255... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
256... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
257... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
258... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
259... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
260... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
261... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
262... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
263... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
264... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
265... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
266... # doctest: +ELLIPSIS
267() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000268
Yury Selivanovf2392132016-12-13 19:03:51 -0500269>>> class C:
270... def meth(self, *args):
271... return args
272>>> obj = C()
273>>> obj.meth(
274... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
275... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
276... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
277... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
278... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
279... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
280... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
281... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
282... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
283... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
284... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
285... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
286... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
287... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
288... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
289... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
290... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
291... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
292... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
293... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
294(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
295
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000296>>> f(lambda x: x[0] = 3)
297Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200298SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000299
300The grammar accepts any test (basically, any expression) in the
301keyword slot of a call site. Test a few different options.
302
303>>> f(x()=2)
304Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200305SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000306>>> f(a or b=1)
307Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200308SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000309>>> f(x.y=1)
310Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200311SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -0700312>>> f((x)=2)
313Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200314SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
315>>> f(True=2)
316Traceback (most recent call last):
317SyntaxError: cannot assign to True
318>>> f(__debug__=1)
319Traceback (most recent call last):
320SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000321
322
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000323More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000324
325>>> (x for x in x) += 1
326Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200327SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000328>>> None += 1
329Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200330SyntaxError: cannot assign to None
331>>> __debug__ += 1
332Traceback (most recent call last):
333SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334>>> f() += 1
335Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200336SyntaxError: cannot assign to function call
Thomas Wouters89f507f2006-12-13 04:49:30 +0000337
338
339Test continue in finally in weird combinations.
340
Ezio Melotti13925002011-03-16 11:05:33 +0200341continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000342
343 >>> def test():
344 ... try:
345 ... pass
346 ... finally:
347 ... for abc in range(10):
348 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000349 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350 >>> test()
351 9
352
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200353continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354
355 >>> def test():
356 ... for abc in range(10):
357 ... try:
358 ... pass
359 ... finally:
360 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200361 ... print(abc)
362 >>> test()
363 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000364
365 >>> def test():
366 ... for abc in range(10):
367 ... try:
368 ... pass
369 ... finally:
370 ... try:
371 ... continue
372 ... except:
373 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200374 ... print(abc)
375 >>> test()
376 9
377
378 >>> def test():
379 ... for abc in range(10):
380 ... try:
381 ... pass
382 ... finally:
383 ... try:
384 ... pass
385 ... except:
386 ... continue
387 ... print(abc)
388 >>> test()
389 9
390
391A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000392
393 >>> def foo():
394 ... try:
395 ... pass
396 ... finally:
397 ... continue
398 Traceback (most recent call last):
399 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200400 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000401
402There is one test for a break that is not in a loop. The compiler
403uses a single data structure to keep track of try-finally and loops,
404so we need to be sure that a break is actually inside a loop. If it
405isn't, there should be a syntax error.
406
407 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000408 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000409 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000410 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000411 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000412 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000413 Traceback (most recent call last):
414 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000415 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000416
Benjamin Petersone09ed542016-07-14 22:00:03 -0700417This raises a SyntaxError, it used to raise a SystemError.
418Context for this change can be found on issue #27514
419
Thomas Wouters89f507f2006-12-13 04:49:30 +0000420In 2.5 there was a missing exception and an assert was triggered in a debug
421build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
422
423 >>> while 1:
424 ... while 2:
425 ... while 3:
426 ... while 4:
427 ... while 5:
428 ... while 6:
429 ... while 8:
430 ... while 9:
431 ... while 10:
432 ... while 11:
433 ... while 12:
434 ... while 13:
435 ... while 14:
436 ... while 15:
437 ... while 16:
438 ... while 17:
439 ... while 18:
440 ... while 19:
441 ... while 20:
442 ... while 21:
443 ... while 22:
444 ... break
445 Traceback (most recent call last):
446 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700447 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000448
Guido van Rossum6cff8742016-09-09 09:36:26 -0700449Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
450
451 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200452 ... print(x)
453 ... global x
454 Traceback (most recent call last):
455 ...
456 SyntaxError: name 'x' is used prior to global declaration
457
458 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700459 ... x = 1
460 ... global x
461 Traceback (most recent call last):
462 ...
463 SyntaxError: name 'x' is assigned to before global declaration
464
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200465 >>> def f(x):
466 ... global x
467 Traceback (most recent call last):
468 ...
469 SyntaxError: name 'x' is parameter and global
470
Guido van Rossum6cff8742016-09-09 09:36:26 -0700471 >>> def f():
472 ... x = 1
473 ... def g():
474 ... print(x)
475 ... nonlocal x
476 Traceback (most recent call last):
477 ...
478 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000479
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300480 >>> def f():
481 ... x = 1
482 ... def g():
483 ... x = 2
484 ... nonlocal x
485 Traceback (most recent call last):
486 ...
487 SyntaxError: name 'x' is assigned to before nonlocal declaration
488
Jeremy Hylton81e95022007-02-27 06:50:52 +0000489 >>> def f(x):
490 ... nonlocal x
491 Traceback (most recent call last):
492 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000493 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000494
Jeremy Hylton81e95022007-02-27 06:50:52 +0000495 >>> def f():
496 ... global x
497 ... nonlocal x
498 Traceback (most recent call last):
499 ...
500 SyntaxError: name 'x' is nonlocal and global
501
502 >>> def f():
503 ... nonlocal x
504 Traceback (most recent call last):
505 ...
506 SyntaxError: no binding for nonlocal 'x' found
507
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000508From SF bug #1705365
509 >>> nonlocal x
510 Traceback (most recent call last):
511 ...
512 SyntaxError: nonlocal declaration not allowed at module level
513
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300514From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600515 >>> class A:
516 ... def f(self):
517 ... nonlocal __x
518 Traceback (most recent call last):
519 ...
520 SyntaxError: no binding for nonlocal '_A__x' found
521
Guido van Rossumd8faa362007-04-27 19:54:29 +0000522
523This tests assignment-context; there was a bug in Python 2.5 where compiling
524a complex 'if' (one with 'elif') would fail to notice an invalid suite,
525leading to spurious errors.
526
527 >>> if 1:
528 ... x() = 1
529 ... elif 1:
530 ... pass
531 Traceback (most recent call last):
532 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200533 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000534
535 >>> if 1:
536 ... pass
537 ... elif 1:
538 ... x() = 1
539 Traceback (most recent call last):
540 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200541 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000542
543 >>> if 1:
544 ... x() = 1
545 ... elif 1:
546 ... pass
547 ... else:
548 ... pass
549 Traceback (most recent call last):
550 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200551 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000552
553 >>> if 1:
554 ... pass
555 ... elif 1:
556 ... x() = 1
557 ... else:
558 ... pass
559 Traceback (most recent call last):
560 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200561 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000562
563 >>> if 1:
564 ... pass
565 ... elif 1:
566 ... pass
567 ... else:
568 ... x() = 1
569 Traceback (most recent call last):
570 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200571 SyntaxError: cannot assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000572
Collin Winter828f04a2007-08-31 00:04:24 +0000573Make sure that the old "raise X, Y[, Z]" form is gone:
574 >>> raise X, Y
575 Traceback (most recent call last):
576 ...
577 SyntaxError: invalid syntax
578 >>> raise X, Y, Z
579 Traceback (most recent call last):
580 ...
581 SyntaxError: invalid syntax
582
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000583
584>>> f(a=23, a=234)
585Traceback (most recent call last):
586 ...
587SyntaxError: keyword argument repeated
588
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000589>>> {1, 2, 3} = 42
590Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200591SyntaxError: cannot assign to set display
592
593>>> {1: 2, 3: 4} = 42
594Traceback (most recent call last):
595SyntaxError: cannot assign to dict display
596
597>>> f'{x}' = 42
598Traceback (most recent call last):
599SyntaxError: cannot assign to f-string expression
600
601>>> f'{x}-{y}' = 42
602Traceback (most recent call last):
603SyntaxError: cannot assign to f-string expression
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000604
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000605Corner-cases that used to fail to raise the correct error:
606
607 >>> def f(*, x=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(*args:(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 >>> def f(**kwargs:(lambda __debug__:0)): pass
616 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200617 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000618
619 >>> with (lambda *:0): pass
620 Traceback (most recent call last):
621 SyntaxError: named arguments must follow bare *
622
623Corner-cases that used to crash:
624
625 >>> def f(**__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
629 >>> def f(*xx, __debug__): pass
630 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200631 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000632
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000633"""
634
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000635import re
636import unittest
637
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000638from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000639
640class SyntaxTestCase(unittest.TestCase):
641
642 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200643 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000644 """Check that compiling code raises SyntaxError with errtext.
645
646 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647 test of the exception raised. If subclass is specified it
648 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000649 """
650 try:
651 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000652 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 if subclass and not isinstance(err, subclass):
654 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000655 mo = re.search(errtext, str(err))
656 if mo is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000657 self.fail("SyntaxError did not contain '%r'" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200658 self.assertEqual(err.filename, filename)
659 if lineno is not None:
660 self.assertEqual(err.lineno, lineno)
661 if offset is not None:
662 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000663 else:
664 self.fail("compile() did not raise SyntaxError")
665
666 def test_assign_call(self):
667 self._check_error("f() = 1", "assign")
668
669 def test_assign_del(self):
670 self._check_error("del f()", "delete")
671
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200672 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000673 source = """if 1:
674 def error(a):
675 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200676 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000677 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200678 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000679 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200680 self._check_error(source, "parameter and global", lineno=3)
681
682 def test_nonlocal_param_err_first(self):
683 source = """if 1:
684 def error(a):
685 nonlocal a # SyntaxError
686 def error2():
687 b = 1
688 global b # SyntaxError
689 """
690 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +0000691
Neal Norwitzfcf44352005-11-27 20:37:43 +0000692 def test_break_outside_loop(self):
693 self._check_error("break", "outside loop")
694
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 def test_unexpected_indent(self):
696 self._check_error("foo()\n bar()\n", "unexpected indent",
697 subclass=IndentationError)
698
699 def test_no_indent(self):
700 self._check_error("if 1:\nfoo()", "expected an indented block",
701 subclass=IndentationError)
702
703 def test_bad_outdent(self):
704 self._check_error("if 1:\n foo()\n bar()",
705 "unindent does not match .* level",
706 subclass=IndentationError)
707
708 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400709 self._check_error("int(base=10, '2')",
710 "positional argument follows keyword argument")
711
712 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200713 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400714 "positional argument follows "
715 "keyword argument unpacking")
716
717 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200718 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400719 "iterable argument unpacking follows "
720 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000721
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000722def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000723 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000724 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000725 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000726
727if __name__ == "__main__":
728 test_main()