blob: 91ca1db43a74f23ffc1b37e856f768100d65da71 [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
Serhiy Storchakad8b3a982019-03-05 20:42:06 +020046>>> (True := 1)
47Traceback (most recent call last):
Ned Batchelder37143a82019-12-31 21:40:58 -050048SyntaxError: cannot use assignment expressions with True
Serhiy Storchakad8b3a982019-03-05 20:42:06 +020049
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020050>>> obj.__debug__ = 1
51Traceback (most recent call last):
52SyntaxError: cannot assign to __debug__
53
54>>> __debug__ = 1
55Traceback (most recent call last):
56SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +000057
Stéphane Wirtel3ad91672019-02-21 11:11:53 +010058>>> (__debug__ := 1)
59Traceback (most recent call last):
60SyntaxError: cannot assign to __debug__
61
Jeremy Hyltonc960f262006-01-27 15:18:39 +000062>>> f() = 1
63Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020064SyntaxError: cannot assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000065
Pablo Galindo9f495902020-06-08 02:57:00 +010066>>> yield = 1
67Traceback (most recent call last):
68SyntaxError: assignment to yield expression not possible
69
Shantanu27c0d9b2020-05-11 14:53:58 -070070>>> del f()
71Traceback (most recent call last):
72SyntaxError: cannot delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000073
74>>> a + 1 = 2
75Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020076SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000077
78>>> (x for x in x) = 1
79Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020080SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000081
82>>> 1 = 1
83Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020084SyntaxError: cannot assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000085
86>>> "abc" = 1
87Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020088SyntaxError: cannot assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000089
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050090>>> b"" = 1
91Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020092SyntaxError: cannot assign to literal
93
94>>> ... = 1
95Traceback (most recent call last):
96SyntaxError: cannot assign to Ellipsis
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050097
Jeremy Hyltonc960f262006-01-27 15:18:39 +000098>>> `1` = 1
99Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +0000100SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000101
102If the left-hand side of an assignment is a list or tuple, an illegal
103expression inside that contain should still cause a syntax error.
104This test just checks a couple of cases rather than enumerating all of
105them.
106
Pablo Galindo16ab0702020-05-15 02:04:52 +0100107>>> (a, "b", c) = (1, 2, 3)
108Traceback (most recent call last):
109SyntaxError: cannot assign to literal
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200110
Pablo Galindo16ab0702020-05-15 02:04:52 +0100111>>> (a, True, c) = (1, 2, 3)
112Traceback (most recent call last):
113SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200114
115>>> (a, __debug__, c) = (1, 2, 3)
116Traceback (most recent call last):
117SyntaxError: cannot assign to __debug__
118
Pablo Galindo16ab0702020-05-15 02:04:52 +0100119>>> (a, *True, c) = (1, 2, 3)
120Traceback (most recent call last):
121SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200122
123>>> (a, *__debug__, c) = (1, 2, 3)
124Traceback (most recent call last):
125SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000126
Pablo Galindo16ab0702020-05-15 02:04:52 +0100127>>> [a, b, c + 1] = [1, 2, 3]
128Traceback (most recent call last):
129SyntaxError: cannot assign to operator
130
131>>> [a, b[1], c + 1] = [1, 2, 3]
132Traceback (most recent call last):
133SyntaxError: cannot assign to operator
134
135>>> [a, b.c.d, c + 1] = [1, 2, 3]
136Traceback (most recent call last):
137SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000138
Thomas Wouters477c8d52006-05-27 19:21:47 +0000139>>> a if 1 else b = 1
140Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200141SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000142
Pablo Galindo9f495902020-06-08 02:57:00 +0100143>>> True = True = 3
144Traceback (most recent call last):
145SyntaxError: cannot assign to True
146
147>>> x = y = True = z = 3
148Traceback (most recent call last):
149SyntaxError: cannot assign to True
150
151>>> x = y = yield = 1
152Traceback (most recent call last):
153SyntaxError: assignment to yield expression not possible
154
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300155>>> a, b += 1, 2
156Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100157SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300158
159>>> (a, b) += 1, 2
160Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100161SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300162
163>>> [a, b] += 1, 2
164Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100165SyntaxError: 'list' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300166
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300167Invalid targets in `for` loops and `with` statements should also
168produce a specialized error message
169
170>>> for a() in b: pass
171Traceback (most recent call last):
172SyntaxError: cannot assign to function call
173
174>>> for (a, b()) in b: pass
175Traceback (most recent call last):
176SyntaxError: cannot assign to function call
177
178>>> for [a, b()] in b: pass
179Traceback (most recent call last):
180SyntaxError: cannot assign to function call
181
182>>> for (*a, b, c+1) in b: pass
183Traceback (most recent call last):
184SyntaxError: cannot assign to operator
185
186>>> for (x, *(y, z.d())) in b: pass
187Traceback (most recent call last):
188SyntaxError: cannot assign to function call
189
190>>> for a, b() in c: pass
191Traceback (most recent call last):
192SyntaxError: cannot assign to function call
193
194>>> for a, b, (c + 1, d()): pass
195Traceback (most recent call last):
196SyntaxError: cannot assign to operator
197
198>>> for i < (): pass
199Traceback (most recent call last):
200SyntaxError: invalid syntax
201
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300202>>> for a, b
203Traceback (most recent call last):
204SyntaxError: invalid syntax
205
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300206>>> with a as b(): pass
207Traceback (most recent call last):
208SyntaxError: cannot assign to function call
209
210>>> with a as (b, c()): pass
211Traceback (most recent call last):
212SyntaxError: cannot assign to function call
213
214>>> with a as [b, c()]: pass
215Traceback (most recent call last):
216SyntaxError: cannot assign to function call
217
218>>> with a as (*b, c, d+1): pass
219Traceback (most recent call last):
220SyntaxError: cannot assign to operator
221
222>>> with a as (x, *(y, z.d())): pass
223Traceback (most recent call last):
224SyntaxError: cannot assign to function call
225
226>>> with a as b, c as d(): pass
227Traceback (most recent call last):
228SyntaxError: cannot assign to function call
229
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300230>>> with a as b
231Traceback (most recent call last):
232SyntaxError: invalid syntax
233
Pablo Galindo9f495902020-06-08 02:57:00 +0100234>>> p = p =
235Traceback (most recent call last):
236SyntaxError: invalid syntax
237
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000238From compiler_complex_args():
239
240>>> def f(None=1):
241... pass
242Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000243SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000244
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000245From ast_for_arguments():
246
247>>> def f(x, y=1, z):
248... pass
249Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000250SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000251
252>>> def f(x, None):
253... pass
254Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000255SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000256
257>>> def f(*None):
258... pass
259Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000260SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000261
262>>> def f(**None):
263... pass
264Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000265SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000266
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300267>>> import ast; ast.parse('''
268... def f(
269... *, # type: int
270... a, # type: int
271... ):
272... pass
273... ''', type_comments=True)
274Traceback (most recent call last):
275SyntaxError: bare * has associated type comment
276
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000277
278From ast_for_funcdef():
279
280>>> def None(x):
281... pass
282Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000283SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000284
285
286From ast_for_call():
287
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200288>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000289... return list(it)
290>>> L = range(10)
291>>> f(x for x in L)
292[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
293>>> f(x for x in L, 1)
294Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200295SyntaxError: Generator expression must be parenthesized
296>>> f(x for x in L, y=1)
297Traceback (most recent call last):
298SyntaxError: Generator expression must be parenthesized
299>>> f(x for x in L, *[])
300Traceback (most recent call last):
301SyntaxError: Generator expression must be parenthesized
302>>> f(x for x in L, **{})
303Traceback (most recent call last):
304SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300305>>> f(L, x for x in L)
306Traceback (most recent call last):
307SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400308>>> f(x for x in L, y for y in L)
309Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200310SyntaxError: Generator expression must be parenthesized
311>>> f(x for x in L,)
312Traceback (most recent call last):
313SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000314>>> f((x for x in L), 1)
315[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200316>>> class C(x for x in L):
317... pass
318Traceback (most recent call last):
319SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000320
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200321>>> def g(*args, **kwargs):
322... print(args, sorted(kwargs.items()))
323>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
324... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
325... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
326... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
327... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
328... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
329... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
330... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
331... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
332... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
333... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
334... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
335... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
336... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
337... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
338... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
339... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
340... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
341... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
342... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
343(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200345>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
346... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
347... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
348... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
349... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
350... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
351... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
352... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
353... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
354... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
355... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
356... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
357... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
358... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
359... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
360... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
361... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
362... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
363... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
364... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
365... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
366... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
367... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
368... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
369... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
370... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
371... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
372... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
373... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
374... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
375... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
376... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
377... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
378... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
379... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
380... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
381... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
382... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
383... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
384... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
385... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
386... # doctest: +ELLIPSIS
387() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000388
Yury Selivanovf2392132016-12-13 19:03:51 -0500389>>> class C:
390... def meth(self, *args):
391... return args
392>>> obj = C()
393>>> obj.meth(
394... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
395... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
396... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
397... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
398... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
399... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
400... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
401... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
402... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
403... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
404... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
405... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
406... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
407... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
408... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
409... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
410... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
411... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
412... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
413... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
414(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
415
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100416# >>> f(lambda x: x[0] = 3)
417# Traceback (most recent call last):
418# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000419
420The grammar accepts any test (basically, any expression) in the
421keyword slot of a call site. Test a few different options.
422
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100423# >>> f(x()=2)
424# Traceback (most recent call last):
425# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
426# >>> f(a or b=1)
427# Traceback (most recent call last):
428# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
429# >>> f(x.y=1)
430# Traceback (most recent call last):
431# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
432# >>> f((x)=2)
433# Traceback (most recent call last):
434# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
435# >>> f(True=2)
436# Traceback (most recent call last):
437# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200438>>> f(__debug__=1)
439Traceback (most recent call last):
440SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100441>>> __debug__: int
442Traceback (most recent call last):
443SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000444
445
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000446More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000447
448>>> (x for x in x) += 1
449Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100450SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000451>>> None += 1
452Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100453SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200454>>> __debug__ += 1
455Traceback (most recent call last):
456SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000457>>> f() += 1
458Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100459SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000460
461
462Test continue in finally in weird combinations.
463
Ezio Melotti13925002011-03-16 11:05:33 +0200464continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000465
466 >>> def test():
467 ... try:
468 ... pass
469 ... finally:
470 ... for abc in range(10):
471 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000472 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000473 >>> test()
474 9
475
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200476continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000477
478 >>> def test():
479 ... for abc in range(10):
480 ... try:
481 ... pass
482 ... finally:
483 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200484 ... print(abc)
485 >>> test()
486 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000487
488 >>> def test():
489 ... for abc in range(10):
490 ... try:
491 ... pass
492 ... finally:
493 ... try:
494 ... continue
495 ... except:
496 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200497 ... print(abc)
498 >>> test()
499 9
500
501 >>> def test():
502 ... for abc in range(10):
503 ... try:
504 ... pass
505 ... finally:
506 ... try:
507 ... pass
508 ... except:
509 ... continue
510 ... print(abc)
511 >>> test()
512 9
513
514A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000515
516 >>> def foo():
517 ... try:
518 ... pass
519 ... finally:
520 ... continue
521 Traceback (most recent call last):
522 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200523 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000524
525There is one test for a break that is not in a loop. The compiler
526uses a single data structure to keep track of try-finally and loops,
527so we need to be sure that a break is actually inside a loop. If it
528isn't, there should be a syntax error.
529
530 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000531 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000532 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000533 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000534 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000535 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000536 Traceback (most recent call last):
537 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000538 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000539
Benjamin Petersone09ed542016-07-14 22:00:03 -0700540This raises a SyntaxError, it used to raise a SystemError.
541Context for this change can be found on issue #27514
542
Thomas Wouters89f507f2006-12-13 04:49:30 +0000543In 2.5 there was a missing exception and an assert was triggered in a debug
544build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
545
546 >>> while 1:
547 ... while 2:
548 ... while 3:
549 ... while 4:
550 ... while 5:
551 ... while 6:
552 ... while 8:
553 ... while 9:
554 ... while 10:
555 ... while 11:
556 ... while 12:
557 ... while 13:
558 ... while 14:
559 ... while 15:
560 ... while 16:
561 ... while 17:
562 ... while 18:
563 ... while 19:
564 ... while 20:
565 ... while 21:
566 ... while 22:
567 ... break
568 Traceback (most recent call last):
569 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700570 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000571
Guido van Rossum6cff8742016-09-09 09:36:26 -0700572Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
573
574 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200575 ... print(x)
576 ... global x
577 Traceback (most recent call last):
578 ...
579 SyntaxError: name 'x' is used prior to global declaration
580
581 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700582 ... x = 1
583 ... global x
584 Traceback (most recent call last):
585 ...
586 SyntaxError: name 'x' is assigned to before global declaration
587
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200588 >>> def f(x):
589 ... global x
590 Traceback (most recent call last):
591 ...
592 SyntaxError: name 'x' is parameter and global
593
Guido van Rossum6cff8742016-09-09 09:36:26 -0700594 >>> def f():
595 ... x = 1
596 ... def g():
597 ... print(x)
598 ... nonlocal x
599 Traceback (most recent call last):
600 ...
601 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000602
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300603 >>> def f():
604 ... x = 1
605 ... def g():
606 ... x = 2
607 ... nonlocal x
608 Traceback (most recent call last):
609 ...
610 SyntaxError: name 'x' is assigned to before nonlocal declaration
611
Jeremy Hylton81e95022007-02-27 06:50:52 +0000612 >>> def f(x):
613 ... nonlocal x
614 Traceback (most recent call last):
615 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000616 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000617
Jeremy Hylton81e95022007-02-27 06:50:52 +0000618 >>> def f():
619 ... global x
620 ... nonlocal x
621 Traceback (most recent call last):
622 ...
623 SyntaxError: name 'x' is nonlocal and global
624
625 >>> def f():
626 ... nonlocal x
627 Traceback (most recent call last):
628 ...
629 SyntaxError: no binding for nonlocal 'x' found
630
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000631From SF bug #1705365
632 >>> nonlocal x
633 Traceback (most recent call last):
634 ...
635 SyntaxError: nonlocal declaration not allowed at module level
636
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300637From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600638 >>> class A:
639 ... def f(self):
640 ... nonlocal __x
641 Traceback (most recent call last):
642 ...
643 SyntaxError: no binding for nonlocal '_A__x' found
644
Guido van Rossumd8faa362007-04-27 19:54:29 +0000645
646This tests assignment-context; there was a bug in Python 2.5 where compiling
647a complex 'if' (one with 'elif') would fail to notice an invalid suite,
648leading to spurious errors.
649
650 >>> if 1:
651 ... x() = 1
652 ... elif 1:
653 ... pass
654 Traceback (most recent call last):
655 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000657
658 >>> if 1:
659 ... pass
660 ... elif 1:
661 ... x() = 1
662 Traceback (most recent call last):
663 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200664 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000665
666 >>> if 1:
667 ... x() = 1
668 ... elif 1:
669 ... pass
670 ... else:
671 ... pass
672 Traceback (most recent call last):
673 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200674 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000675
676 >>> if 1:
677 ... pass
678 ... elif 1:
679 ... x() = 1
680 ... else:
681 ... pass
682 Traceback (most recent call last):
683 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200684 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000685
686 >>> if 1:
687 ... pass
688 ... elif 1:
689 ... pass
690 ... else:
691 ... x() = 1
692 Traceback (most recent call last):
693 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200694 SyntaxError: cannot assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000695
Collin Winter828f04a2007-08-31 00:04:24 +0000696Make sure that the old "raise X, Y[, Z]" form is gone:
697 >>> raise X, Y
698 Traceback (most recent call last):
699 ...
700 SyntaxError: invalid syntax
701 >>> raise X, Y, Z
702 Traceback (most recent call last):
703 ...
704 SyntaxError: invalid syntax
705
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000706
707>>> f(a=23, a=234)
708Traceback (most recent call last):
709 ...
Pablo Galindo254ec782020-04-03 20:37:13 +0100710SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000711
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000712>>> {1, 2, 3} = 42
713Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200714SyntaxError: cannot assign to set display
715
716>>> {1: 2, 3: 4} = 42
717Traceback (most recent call last):
718SyntaxError: cannot assign to dict display
719
720>>> f'{x}' = 42
721Traceback (most recent call last):
722SyntaxError: cannot assign to f-string expression
723
724>>> f'{x}-{y}' = 42
725Traceback (most recent call last):
726SyntaxError: cannot assign to f-string expression
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000727
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300728>>> from t import x,
729Traceback (most recent call last):
730SyntaxError: trailing comma not allowed without surrounding parentheses
731
732>>> from t import x,y,
733Traceback (most recent call last):
734SyntaxError: trailing comma not allowed without surrounding parentheses
735
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300736>>> (): int
737Traceback (most recent call last):
738SyntaxError: only single target (not tuple) can be annotated
739>>> []: int
740Traceback (most recent call last):
741SyntaxError: only single target (not list) can be annotated
742>>> (()): int
743Traceback (most recent call last):
744SyntaxError: only single target (not tuple) can be annotated
745>>> ([]): int
746Traceback (most recent call last):
747SyntaxError: only single target (not list) can be annotated
748
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000749Corner-cases that used to fail to raise the correct error:
750
751 >>> def f(*, x=lambda __debug__:0): pass
752 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200753 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000754
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300755 >>> with (lambda *:0): pass
756 Traceback (most recent call last):
757 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000758
759Corner-cases that used to crash:
760
761 >>> def f(**__debug__): pass
762 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200763 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000764
765 >>> def f(*xx, __debug__): pass
766 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200767 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000768
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +0300769 >>> import ä £
770 Traceback (most recent call last):
771 SyntaxError: invalid character '£' (U+00A3)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000772"""
773
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000774import re
775import unittest
776
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000777from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000778
779class SyntaxTestCase(unittest.TestCase):
780
781 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200782 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000783 """Check that compiling code raises SyntaxError with errtext.
784
785 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786 test of the exception raised. If subclass is specified it
787 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000788 """
789 try:
790 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000791 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000792 if subclass and not isinstance(err, subclass):
793 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000794 mo = re.search(errtext, str(err))
795 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -0700796 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200797 self.assertEqual(err.filename, filename)
798 if lineno is not None:
799 self.assertEqual(err.lineno, lineno)
800 if offset is not None:
801 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000802 else:
803 self.fail("compile() did not raise SyntaxError")
804
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +0200805 def test_curly_brace_after_primary_raises_immediately(self):
806 self._check_error("f{", "invalid syntax", mode="single")
807
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000808 def test_assign_call(self):
809 self._check_error("f() = 1", "assign")
810
811 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -0700812 self._check_error("del (,)", "invalid syntax")
813 self._check_error("del 1", "delete literal")
814 self._check_error("del (1, 2)", "delete literal")
815 self._check_error("del None", "delete None")
816 self._check_error("del *x", "delete starred")
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300817 self._check_error("del (*x)", "use starred expression")
Shantanu27c0d9b2020-05-11 14:53:58 -0700818 self._check_error("del (*x,)", "delete starred")
819 self._check_error("del [*x,]", "delete starred")
820 self._check_error("del f()", "delete function call")
821 self._check_error("del f(a, b)", "delete function call")
822 self._check_error("del o.f()", "delete function call")
823 self._check_error("del a[0]()", "delete function call")
824 self._check_error("del x, f()", "delete function call")
825 self._check_error("del f(), x", "delete function call")
826 self._check_error("del [a, b, ((c), (d,), e.f())]", "delete function call")
827 self._check_error("del (a if True else b)", "delete conditional")
828 self._check_error("del +a", "delete operator")
829 self._check_error("del a, +b", "delete operator")
830 self._check_error("del a + b", "delete operator")
831 self._check_error("del (a + b, c)", "delete operator")
832 self._check_error("del (c[0], a + b)", "delete operator")
833 self._check_error("del a.b.c + 2", "delete operator")
834 self._check_error("del a.b.c[0] + 2", "delete operator")
835 self._check_error("del (a, b, (c, d.e.f + 2))", "delete operator")
836 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "delete operator")
837 self._check_error("del (a := 5)", "delete named expression")
838 # We don't have a special message for this, but make sure we don't
839 # report "cannot delete name"
840 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000841
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200842 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000843 source = """if 1:
844 def error(a):
845 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200846 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000847 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200848 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000849 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200850 self._check_error(source, "parameter and global", lineno=3)
851
852 def test_nonlocal_param_err_first(self):
853 source = """if 1:
854 def error(a):
855 nonlocal a # SyntaxError
856 def error2():
857 b = 1
858 global b # SyntaxError
859 """
860 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +0000861
Neal Norwitzfcf44352005-11-27 20:37:43 +0000862 def test_break_outside_loop(self):
863 self._check_error("break", "outside loop")
864
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100865 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100866 self._check_error("if 0: yield", "outside function")
867 self._check_error("if 0: yield\nelse: x=1", "outside function")
868 self._check_error("if 1: pass\nelse: yield", "outside function")
869 self._check_error("while 0: yield", "outside function")
870 self._check_error("while 0: yield\nelse: x=1", "outside function")
871 self._check_error("class C:\n if 0: yield", "outside function")
872 self._check_error("class C:\n if 1: pass\n else: yield",
873 "outside function")
874 self._check_error("class C:\n while 0: yield", "outside function")
875 self._check_error("class C:\n while 0: yield\n else: x = 1",
876 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100877
878 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100879 self._check_error("if 0: return", "outside function")
880 self._check_error("if 0: return\nelse: x=1", "outside function")
881 self._check_error("if 1: pass\nelse: return", "outside function")
882 self._check_error("while 0: return", "outside function")
883 self._check_error("class C:\n if 0: return", "outside function")
884 self._check_error("class C:\n while 0: return", "outside function")
885 self._check_error("class C:\n while 0: return\n else: x=1",
886 "outside function")
887 self._check_error("class C:\n if 0: return\n else: x= 1",
888 "outside function")
889 self._check_error("class C:\n if 1: pass\n else: return",
890 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100891
892 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100893 self._check_error("if 0: break", "outside loop")
894 self._check_error("if 0: break\nelse: x=1", "outside loop")
895 self._check_error("if 1: pass\nelse: break", "outside loop")
896 self._check_error("class C:\n if 0: break", "outside loop")
897 self._check_error("class C:\n if 1: pass\n else: break",
898 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100899
900 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100901 self._check_error("if 0: continue", "not properly in loop")
902 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
903 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
904 self._check_error("class C:\n if 0: continue", "not properly in loop")
905 self._check_error("class C:\n if 1: pass\n else: continue",
906 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100907
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908 def test_unexpected_indent(self):
909 self._check_error("foo()\n bar()\n", "unexpected indent",
910 subclass=IndentationError)
911
912 def test_no_indent(self):
913 self._check_error("if 1:\nfoo()", "expected an indented block",
914 subclass=IndentationError)
915
916 def test_bad_outdent(self):
917 self._check_error("if 1:\n foo()\n bar()",
918 "unindent does not match .* level",
919 subclass=IndentationError)
920
921 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400922 self._check_error("int(base=10, '2')",
923 "positional argument follows keyword argument")
924
925 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200926 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400927 "positional argument follows "
928 "keyword argument unpacking")
929
930 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200931 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400932 "iterable argument unpacking follows "
933 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000934
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +0300935 def test_empty_line_after_linecont(self):
936 # See issue-40847
937 s = r"""\
938pass
939 \
940
941pass
942"""
943 try:
944 compile(s, '<string>', 'exec')
945 except SyntaxError:
946 self.fail("Empty line after a line continuation character is valid.")
947
Mark Shannon02d126a2020-09-25 14:04:19 +0100948 @support.cpython_only
949 def test_nested_named_except_blocks(self):
950 code = ""
951 for i in range(12):
952 code += f"{' '*i}try:\n"
953 code += f"{' '*(i+1)}raise Exception\n"
954 code += f"{' '*i}except Exception as e:\n"
955 code += f"{' '*4*12}pass"
956 self._check_error(code, "too many statically nested blocks")
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +0300957
Pablo Galindo06f8c332020-10-30 23:48:42 +0000958 def test_barry_as_flufl_with_syntax_errors(self):
959 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
960 # is reading the wrong token in the presence of syntax errors later
961 # in the file. See bpo-42214 for more information.
962 code = """
963def func1():
964 if a != b:
965 raise ValueError
966
967def func2():
968 try
969 return 1
970 finally:
971 pass
972"""
973 self._check_error(code, "invalid syntax")
974
Lysandros Nikolaou02cdfc92020-10-31 20:31:41 +0200975 def test_invalid_line_continuation_left_recursive(self):
976 # Check bpo-42218: SyntaxErrors following left-recursive rules
977 # (t_primary_raw in this case) need to be tested explicitly
978 self._check_error("A.\u018a\\ ",
979 "unexpected character after line continuation character")
980 self._check_error("A.\u03bc\\\n",
981 "unexpected EOF while parsing")
982
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000983def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000984 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000985 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000986 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000987
988if __name__ == "__main__":
989 test_main()