blob: e161f56d30f2143d63dc0641cb4a7dd0e1d3598b [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):
Benjamin Peterson442f2092012-12-06 17:41:04 -050036SyntaxError: can't assign to keyword
Jeremy Hyltonc960f262006-01-27 15:18:39 +000037
Jeremy Hyltonc960f262006-01-27 15:18:39 +000038>>> f() = 1
39Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000040SyntaxError: can't assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000041
42>>> del f()
43Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000044SyntaxError: can't delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000045
46>>> a + 1 = 2
47Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000048SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000049
50>>> (x for x in x) = 1
51Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000052SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000053
54>>> 1 = 1
55Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000056SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000057
58>>> "abc" = 1
59Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000060SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000061
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050062>>> b"" = 1
63Traceback (most recent call last):
64SyntaxError: can't assign to literal
65
Jeremy Hyltonc960f262006-01-27 15:18:39 +000066>>> `1` = 1
67Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +000068SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000069
70If the left-hand side of an assignment is a list or tuple, an illegal
71expression inside that contain should still cause a syntax error.
72This test just checks a couple of cases rather than enumerating all of
73them.
74
75>>> (a, "b", c) = (1, 2, 3)
76Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000077SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000078
79>>> [a, b, c + 1] = [1, 2, 3]
80Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000081SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000082
Thomas Wouters477c8d52006-05-27 19:21:47 +000083>>> a if 1 else b = 1
84Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000085SyntaxError: can't assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000086
87From compiler_complex_args():
88
89>>> def f(None=1):
90... pass
91Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +000092SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000093
94
95From ast_for_arguments():
96
97>>> def f(x, y=1, z):
98... pass
99Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000100SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000101
102>>> def f(x, None):
103... pass
104Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000105SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000106
107>>> def f(*None):
108... pass
109Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000110SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000111
112>>> def f(**None):
113... pass
114Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000115SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000116
117
118From ast_for_funcdef():
119
120>>> def None(x):
121... pass
122Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000123SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000124
125
126From ast_for_call():
127
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200128>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000129... return list(it)
130>>> L = range(10)
131>>> f(x for x in L)
132[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
133>>> f(x for x in L, 1)
134Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200135SyntaxError: Generator expression must be parenthesized
136>>> f(x for x in L, y=1)
137Traceback (most recent call last):
138SyntaxError: Generator expression must be parenthesized
139>>> f(x for x in L, *[])
140Traceback (most recent call last):
141SyntaxError: Generator expression must be parenthesized
142>>> f(x for x in L, **{})
143Traceback (most recent call last):
144SyntaxError: Generator expression must be parenthesized
145>>> f(L, x for x in L)
146Traceback (most recent call last):
147SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400148>>> f(x for x in L, y for y in L)
149Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200150SyntaxError: Generator expression must be parenthesized
151>>> f(x for x in L,)
152Traceback (most recent call last):
153SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000154>>> f((x for x in L), 1)
155[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200156>>> class C(x for x in L):
157... pass
158Traceback (most recent call last):
159SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000160
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200161>>> def g(*args, **kwargs):
162... print(args, sorted(kwargs.items()))
163>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
164... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
165... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
166... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
167... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
168... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
169... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
170... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
171... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
172... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
173... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
174... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
175... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
176... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
177... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
178... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
179... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
180... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
181... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
182... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
183(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000184
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200185>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
186... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
187... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
188... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
189... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
190... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
191... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
192... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
193... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
194... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
195... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
196... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
197... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
198... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
199... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
200... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
201... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
202... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
203... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
204... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
205... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
206... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
207... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
208... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
209... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
210... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
211... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
212... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
213... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
214... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
215... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
216... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
217... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
218... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
219... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
220... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
221... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
222... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
223... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
224... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
225... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
226... # doctest: +ELLIPSIS
227() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000228
Yury Selivanovf2392132016-12-13 19:03:51 -0500229>>> class C:
230... def meth(self, *args):
231... return args
232>>> obj = C()
233>>> obj.meth(
234... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
235... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
236... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
237... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
238... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
239... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
240... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
241... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
242... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
243... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
244... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
245... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
246... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
247... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
248... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
249... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
250... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
251... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
252... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
253... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
254(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
255
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000256>>> f(lambda x: x[0] = 3)
257Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000258SyntaxError: lambda cannot contain assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000259
260The grammar accepts any test (basically, any expression) in the
261keyword slot of a call site. Test a few different options.
262
263>>> f(x()=2)
264Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000265SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000266>>> f(a or b=1)
267Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000268SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000269>>> f(x.y=1)
270Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000271SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000272
273
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000274More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000275
276>>> (x for x in x) += 1
277Traceback (most recent call last):
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000278SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000279>>> None += 1
280Traceback (most recent call last):
Benjamin Peterson442f2092012-12-06 17:41:04 -0500281SyntaxError: can't assign to keyword
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000282>>> f() += 1
283Traceback (most recent call last):
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000284SyntaxError: can't assign to function call
Thomas Wouters89f507f2006-12-13 04:49:30 +0000285
286
287Test continue in finally in weird combinations.
288
Ezio Melotti13925002011-03-16 11:05:33 +0200289continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000290
291 >>> def test():
292 ... try:
293 ... pass
294 ... finally:
295 ... for abc in range(10):
296 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000297 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000298 >>> test()
299 9
300
301Start simple, a continue in a finally should not be allowed.
302
303 >>> def test():
304 ... for abc in range(10):
305 ... try:
306 ... pass
307 ... finally:
308 ... continue
309 Traceback (most recent call last):
310 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000311 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000312
313This is essentially a continue in a finally which should not be allowed.
314
315 >>> def test():
316 ... for abc in range(10):
317 ... try:
318 ... pass
319 ... finally:
320 ... try:
321 ... continue
322 ... except:
323 ... pass
324 Traceback (most recent call last):
325 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000326 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327
328 >>> def foo():
329 ... try:
330 ... pass
331 ... finally:
332 ... continue
333 Traceback (most recent call last):
334 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000335 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000336
337 >>> def foo():
338 ... for a in ():
339 ... try:
340 ... pass
341 ... finally:
342 ... continue
343 Traceback (most recent call last):
344 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000345 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346
347 >>> def foo():
348 ... for a in ():
349 ... try:
350 ... pass
351 ... finally:
352 ... try:
353 ... continue
354 ... finally:
355 ... pass
356 Traceback (most recent call last):
357 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000358 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000359
360 >>> def foo():
361 ... for a in ():
362 ... try: pass
363 ... finally:
364 ... try:
365 ... pass
366 ... except:
367 ... continue
368 Traceback (most recent call last):
369 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000370 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000371
372There is one test for a break that is not in a loop. The compiler
373uses a single data structure to keep track of try-finally and loops,
374so we need to be sure that a break is actually inside a loop. If it
375isn't, there should be a syntax error.
376
377 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000378 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000379 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000380 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000381 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000382 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000383 Traceback (most recent call last):
384 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000385 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000386
Benjamin Petersone09ed542016-07-14 22:00:03 -0700387This raises a SyntaxError, it used to raise a SystemError.
388Context for this change can be found on issue #27514
389
Thomas Wouters89f507f2006-12-13 04:49:30 +0000390In 2.5 there was a missing exception and an assert was triggered in a debug
391build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
392
393 >>> while 1:
394 ... while 2:
395 ... while 3:
396 ... while 4:
397 ... while 5:
398 ... while 6:
399 ... while 8:
400 ... while 9:
401 ... while 10:
402 ... while 11:
403 ... while 12:
404 ... while 13:
405 ... while 14:
406 ... while 15:
407 ... while 16:
408 ... while 17:
409 ... while 18:
410 ... while 19:
411 ... while 20:
412 ... while 21:
413 ... while 22:
414 ... break
415 Traceback (most recent call last):
416 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700417 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000418
Guido van Rossum6cff8742016-09-09 09:36:26 -0700419Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
420
421 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200422 ... print(x)
423 ... global x
424 Traceback (most recent call last):
425 ...
426 SyntaxError: name 'x' is used prior to global declaration
427
428 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700429 ... x = 1
430 ... global x
431 Traceback (most recent call last):
432 ...
433 SyntaxError: name 'x' is assigned to before global declaration
434
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200435 >>> def f(x):
436 ... global x
437 Traceback (most recent call last):
438 ...
439 SyntaxError: name 'x' is parameter and global
440
Guido van Rossum6cff8742016-09-09 09:36:26 -0700441 >>> def f():
442 ... x = 1
443 ... def g():
444 ... print(x)
445 ... nonlocal x
446 Traceback (most recent call last):
447 ...
448 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000449
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300450 >>> def f():
451 ... x = 1
452 ... def g():
453 ... x = 2
454 ... nonlocal x
455 Traceback (most recent call last):
456 ...
457 SyntaxError: name 'x' is assigned to before nonlocal declaration
458
Jeremy Hylton81e95022007-02-27 06:50:52 +0000459 >>> def f(x):
460 ... nonlocal x
461 Traceback (most recent call last):
462 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000463 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000464
Jeremy Hylton81e95022007-02-27 06:50:52 +0000465 >>> def f():
466 ... global x
467 ... nonlocal x
468 Traceback (most recent call last):
469 ...
470 SyntaxError: name 'x' is nonlocal and global
471
472 >>> def f():
473 ... nonlocal x
474 Traceback (most recent call last):
475 ...
476 SyntaxError: no binding for nonlocal 'x' found
477
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000478From SF bug #1705365
479 >>> nonlocal x
480 Traceback (most recent call last):
481 ...
482 SyntaxError: nonlocal declaration not allowed at module level
483
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300484From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600485 >>> class A:
486 ... def f(self):
487 ... nonlocal __x
488 Traceback (most recent call last):
489 ...
490 SyntaxError: no binding for nonlocal '_A__x' found
491
Guido van Rossumd8faa362007-04-27 19:54:29 +0000492
493This tests assignment-context; there was a bug in Python 2.5 where compiling
494a complex 'if' (one with 'elif') would fail to notice an invalid suite,
495leading to spurious errors.
496
497 >>> if 1:
498 ... x() = 1
499 ... elif 1:
500 ... pass
501 Traceback (most recent call last):
502 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000503 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000504
505 >>> if 1:
506 ... pass
507 ... elif 1:
508 ... x() = 1
509 Traceback (most recent call last):
510 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000511 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000512
513 >>> if 1:
514 ... x() = 1
515 ... elif 1:
516 ... pass
517 ... else:
518 ... pass
519 Traceback (most recent call last):
520 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000521 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000522
523 >>> if 1:
524 ... pass
525 ... elif 1:
526 ... x() = 1
527 ... else:
528 ... pass
529 Traceback (most recent call last):
530 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000531 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000532
533 >>> if 1:
534 ... pass
535 ... elif 1:
536 ... pass
537 ... else:
538 ... x() = 1
539 Traceback (most recent call last):
540 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000541 SyntaxError: can't assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000542
Collin Winter828f04a2007-08-31 00:04:24 +0000543Make sure that the old "raise X, Y[, Z]" form is gone:
544 >>> raise X, Y
545 Traceback (most recent call last):
546 ...
547 SyntaxError: invalid syntax
548 >>> raise X, Y, Z
549 Traceback (most recent call last):
550 ...
551 SyntaxError: invalid syntax
552
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000553
554>>> f(a=23, a=234)
555Traceback (most recent call last):
556 ...
557SyntaxError: keyword argument repeated
558
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000559>>> {1, 2, 3} = 42
560Traceback (most recent call last):
561SyntaxError: can't assign to literal
562
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000563Corner-cases that used to fail to raise the correct error:
564
565 >>> def f(*, x=lambda __debug__:0): pass
566 Traceback (most recent call last):
567 SyntaxError: assignment to keyword
568
569 >>> def f(*args:(lambda __debug__:0)): pass
570 Traceback (most recent call last):
571 SyntaxError: assignment to keyword
572
573 >>> def f(**kwargs:(lambda __debug__:0)): pass
574 Traceback (most recent call last):
575 SyntaxError: assignment to keyword
576
577 >>> with (lambda *:0): pass
578 Traceback (most recent call last):
579 SyntaxError: named arguments must follow bare *
580
581Corner-cases that used to crash:
582
583 >>> def f(**__debug__): pass
584 Traceback (most recent call last):
585 SyntaxError: assignment to keyword
586
587 >>> def f(*xx, __debug__): pass
588 Traceback (most recent call last):
589 SyntaxError: assignment to keyword
590
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000591"""
592
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000593import re
594import unittest
595
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000596from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000597
598class SyntaxTestCase(unittest.TestCase):
599
600 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200601 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000602 """Check that compiling code raises SyntaxError with errtext.
603
604 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000605 test of the exception raised. If subclass is specified it
606 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000607 """
608 try:
609 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000610 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 if subclass and not isinstance(err, subclass):
612 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000613 mo = re.search(errtext, str(err))
614 if mo is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000615 self.fail("SyntaxError did not contain '%r'" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200616 self.assertEqual(err.filename, filename)
617 if lineno is not None:
618 self.assertEqual(err.lineno, lineno)
619 if offset is not None:
620 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000621 else:
622 self.fail("compile() did not raise SyntaxError")
623
624 def test_assign_call(self):
625 self._check_error("f() = 1", "assign")
626
627 def test_assign_del(self):
628 self._check_error("del f()", "delete")
629
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200630 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000631 source = """if 1:
632 def error(a):
633 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200634 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000635 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200636 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000637 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200638 self._check_error(source, "parameter and global", lineno=3)
639
640 def test_nonlocal_param_err_first(self):
641 source = """if 1:
642 def error(a):
643 nonlocal a # SyntaxError
644 def error2():
645 b = 1
646 global b # SyntaxError
647 """
648 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +0000649
Neal Norwitzfcf44352005-11-27 20:37:43 +0000650 def test_break_outside_loop(self):
651 self._check_error("break", "outside loop")
652
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 def test_unexpected_indent(self):
654 self._check_error("foo()\n bar()\n", "unexpected indent",
655 subclass=IndentationError)
656
657 def test_no_indent(self):
658 self._check_error("if 1:\nfoo()", "expected an indented block",
659 subclass=IndentationError)
660
661 def test_bad_outdent(self):
662 self._check_error("if 1:\n foo()\n bar()",
663 "unindent does not match .* level",
664 subclass=IndentationError)
665
666 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400667 self._check_error("int(base=10, '2')",
668 "positional argument follows keyword argument")
669
670 def test_kwargs_last2(self):
671 self._check_error("int(**{base: 10}, '2')",
672 "positional argument follows "
673 "keyword argument unpacking")
674
675 def test_kwargs_last3(self):
676 self._check_error("int(**{base: 10}, *['2'])",
677 "iterable argument unpacking follows "
678 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000680def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000681 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000682 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000683 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000684
685if __name__ == "__main__":
686 test_main()