blob: 4df5535b0053b0769168ae73fd9ab411fcf56216 [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
Shantanu27c0d9b2020-05-11 14:53:58 -070066>>> del f()
67Traceback (most recent call last):
68SyntaxError: cannot delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000069
70>>> a + 1 = 2
71Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020072SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000073
74>>> (x for x in x) = 1
75Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020076SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000077
78>>> 1 = 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
82>>> "abc" = 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
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050086>>> b"" = 1
87Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020088SyntaxError: cannot assign to literal
89
90>>> ... = 1
91Traceback (most recent call last):
92SyntaxError: cannot assign to Ellipsis
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050093
Jeremy Hyltonc960f262006-01-27 15:18:39 +000094>>> `1` = 1
95Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +000096SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000097
98If the left-hand side of an assignment is a list or tuple, an illegal
99expression inside that contain should still cause a syntax error.
100This test just checks a couple of cases rather than enumerating all of
101them.
102
Pablo Galindo16ab0702020-05-15 02:04:52 +0100103>>> (a, "b", c) = (1, 2, 3)
104Traceback (most recent call last):
105SyntaxError: cannot assign to literal
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200106
Pablo Galindo16ab0702020-05-15 02:04:52 +0100107>>> (a, True, c) = (1, 2, 3)
108Traceback (most recent call last):
109SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200110
111>>> (a, __debug__, c) = (1, 2, 3)
112Traceback (most recent call last):
113SyntaxError: cannot assign to __debug__
114
Pablo Galindo16ab0702020-05-15 02:04:52 +0100115>>> (a, *True, c) = (1, 2, 3)
116Traceback (most recent call last):
117SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200118
119>>> (a, *__debug__, c) = (1, 2, 3)
120Traceback (most recent call last):
121SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000122
Pablo Galindo16ab0702020-05-15 02:04:52 +0100123>>> [a, b, c + 1] = [1, 2, 3]
124Traceback (most recent call last):
125SyntaxError: cannot assign to operator
126
127>>> [a, b[1], c + 1] = [1, 2, 3]
128Traceback (most recent call last):
129SyntaxError: cannot assign to operator
130
131>>> [a, b.c.d, c + 1] = [1, 2, 3]
132Traceback (most recent call last):
133SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000134
Thomas Wouters477c8d52006-05-27 19:21:47 +0000135>>> a if 1 else b = 1
136Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200137SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000138
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300139>>> a, b += 1, 2
140Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100141SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300142
143>>> (a, b) += 1, 2
144Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100145SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300146
147>>> [a, b] += 1, 2
148Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100149SyntaxError: 'list' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300150
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000151From compiler_complex_args():
152
153>>> def f(None=1):
154... pass
155Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000156SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000157
158
159From ast_for_arguments():
160
161>>> def f(x, y=1, z):
162... pass
163Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000164SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000165
166>>> def f(x, None):
167... pass
168Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000169SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000170
171>>> def f(*None):
172... pass
173Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000174SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000175
176>>> def f(**None):
177... pass
178Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000179SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000180
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300181>>> import ast; ast.parse('''
182... def f(
183... *, # type: int
184... a, # type: int
185... ):
186... pass
187... ''', type_comments=True)
188Traceback (most recent call last):
189SyntaxError: bare * has associated type comment
190
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000191
192From ast_for_funcdef():
193
194>>> def None(x):
195... pass
196Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000197SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000198
199
200From ast_for_call():
201
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200202>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000203... return list(it)
204>>> L = range(10)
205>>> f(x for x in L)
206[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
207>>> f(x for x in L, 1)
208Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200209SyntaxError: Generator expression must be parenthesized
210>>> f(x for x in L, y=1)
211Traceback (most recent call last):
212SyntaxError: Generator expression must be parenthesized
213>>> f(x for x in L, *[])
214Traceback (most recent call last):
215SyntaxError: Generator expression must be parenthesized
216>>> f(x for x in L, **{})
217Traceback (most recent call last):
218SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300219>>> f(L, x for x in L)
220Traceback (most recent call last):
221SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400222>>> f(x for x in L, y for y in L)
223Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200224SyntaxError: Generator expression must be parenthesized
225>>> f(x for x in L,)
226Traceback (most recent call last):
227SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000228>>> f((x for x in L), 1)
229[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200230>>> class C(x for x in L):
231... pass
232Traceback (most recent call last):
233SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000234
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200235>>> def g(*args, **kwargs):
236... print(args, sorted(kwargs.items()))
237>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
238... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
239... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
240... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
241... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
242... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
243... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
244... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
245... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
246... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
247... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
248... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
249... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
250... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
251... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
252... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
253... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
254... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
255... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
256... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
257(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000258
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200259>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
260... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
261... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
262... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
263... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
264... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
265... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
266... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
267... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
268... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
269... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
270... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
271... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
272... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
273... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
274... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
275... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
276... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
277... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
278... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
279... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
280... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
281... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
282... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
283... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
284... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
285... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
286... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
287... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
288... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
289... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
290... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
291... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
292... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
293... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
294... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
295... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
296... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
297... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
298... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
299... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
300... # doctest: +ELLIPSIS
301() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000302
Yury Selivanovf2392132016-12-13 19:03:51 -0500303>>> class C:
304... def meth(self, *args):
305... return args
306>>> obj = C()
307>>> obj.meth(
308... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
309... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
310... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
311... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
312... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
313... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
314... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
315... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
316... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
317... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
318... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
319... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
320... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
321... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
322... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
323... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
324... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
325... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
326... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
327... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
328(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
329
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100330# >>> f(lambda x: x[0] = 3)
331# Traceback (most recent call last):
332# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000333
334The grammar accepts any test (basically, any expression) in the
335keyword slot of a call site. Test a few different options.
336
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100337# >>> f(x()=2)
338# Traceback (most recent call last):
339# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
340# >>> f(a or b=1)
341# Traceback (most recent call last):
342# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
343# >>> f(x.y=1)
344# Traceback (most recent call last):
345# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
346# >>> f((x)=2)
347# Traceback (most recent call last):
348# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
349# >>> f(True=2)
350# Traceback (most recent call last):
351# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200352>>> f(__debug__=1)
353Traceback (most recent call last):
354SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100355>>> __debug__: int
356Traceback (most recent call last):
357SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000358
359
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000360More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000361
362>>> (x for x in x) += 1
363Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100364SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000365>>> None += 1
366Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100367SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200368>>> __debug__ += 1
369Traceback (most recent call last):
370SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000371>>> f() += 1
372Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100373SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000374
375
376Test continue in finally in weird combinations.
377
Ezio Melotti13925002011-03-16 11:05:33 +0200378continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000379
380 >>> def test():
381 ... try:
382 ... pass
383 ... finally:
384 ... for abc in range(10):
385 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000386 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000387 >>> test()
388 9
389
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200390continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000391
392 >>> def test():
393 ... for abc in range(10):
394 ... try:
395 ... pass
396 ... finally:
397 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200398 ... print(abc)
399 >>> test()
400 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000401
402 >>> def test():
403 ... for abc in range(10):
404 ... try:
405 ... pass
406 ... finally:
407 ... try:
408 ... continue
409 ... except:
410 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200411 ... print(abc)
412 >>> test()
413 9
414
415 >>> def test():
416 ... for abc in range(10):
417 ... try:
418 ... pass
419 ... finally:
420 ... try:
421 ... pass
422 ... except:
423 ... continue
424 ... print(abc)
425 >>> test()
426 9
427
428A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000429
430 >>> def foo():
431 ... try:
432 ... pass
433 ... finally:
434 ... continue
435 Traceback (most recent call last):
436 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200437 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000438
439There is one test for a break that is not in a loop. The compiler
440uses a single data structure to keep track of try-finally and loops,
441so we need to be sure that a break is actually inside a loop. If it
442isn't, there should be a syntax error.
443
444 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000445 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000446 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000447 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000448 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000449 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000450 Traceback (most recent call last):
451 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000452 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000453
Benjamin Petersone09ed542016-07-14 22:00:03 -0700454This raises a SyntaxError, it used to raise a SystemError.
455Context for this change can be found on issue #27514
456
Thomas Wouters89f507f2006-12-13 04:49:30 +0000457In 2.5 there was a missing exception and an assert was triggered in a debug
458build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
459
460 >>> while 1:
461 ... while 2:
462 ... while 3:
463 ... while 4:
464 ... while 5:
465 ... while 6:
466 ... while 8:
467 ... while 9:
468 ... while 10:
469 ... while 11:
470 ... while 12:
471 ... while 13:
472 ... while 14:
473 ... while 15:
474 ... while 16:
475 ... while 17:
476 ... while 18:
477 ... while 19:
478 ... while 20:
479 ... while 21:
480 ... while 22:
481 ... break
482 Traceback (most recent call last):
483 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700484 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000485
Guido van Rossum6cff8742016-09-09 09:36:26 -0700486Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
487
488 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200489 ... print(x)
490 ... global x
491 Traceback (most recent call last):
492 ...
493 SyntaxError: name 'x' is used prior to global declaration
494
495 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700496 ... x = 1
497 ... global x
498 Traceback (most recent call last):
499 ...
500 SyntaxError: name 'x' is assigned to before global declaration
501
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200502 >>> def f(x):
503 ... global x
504 Traceback (most recent call last):
505 ...
506 SyntaxError: name 'x' is parameter and global
507
Guido van Rossum6cff8742016-09-09 09:36:26 -0700508 >>> def f():
509 ... x = 1
510 ... def g():
511 ... print(x)
512 ... nonlocal x
513 Traceback (most recent call last):
514 ...
515 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000516
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300517 >>> def f():
518 ... x = 1
519 ... def g():
520 ... x = 2
521 ... nonlocal x
522 Traceback (most recent call last):
523 ...
524 SyntaxError: name 'x' is assigned to before nonlocal declaration
525
Jeremy Hylton81e95022007-02-27 06:50:52 +0000526 >>> def f(x):
527 ... nonlocal x
528 Traceback (most recent call last):
529 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000530 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000531
Jeremy Hylton81e95022007-02-27 06:50:52 +0000532 >>> def f():
533 ... global x
534 ... nonlocal x
535 Traceback (most recent call last):
536 ...
537 SyntaxError: name 'x' is nonlocal and global
538
539 >>> def f():
540 ... nonlocal x
541 Traceback (most recent call last):
542 ...
543 SyntaxError: no binding for nonlocal 'x' found
544
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000545From SF bug #1705365
546 >>> nonlocal x
547 Traceback (most recent call last):
548 ...
549 SyntaxError: nonlocal declaration not allowed at module level
550
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300551From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600552 >>> class A:
553 ... def f(self):
554 ... nonlocal __x
555 Traceback (most recent call last):
556 ...
557 SyntaxError: no binding for nonlocal '_A__x' found
558
Guido van Rossumd8faa362007-04-27 19:54:29 +0000559
560This tests assignment-context; there was a bug in Python 2.5 where compiling
561a complex 'if' (one with 'elif') would fail to notice an invalid suite,
562leading to spurious errors.
563
564 >>> if 1:
565 ... x() = 1
566 ... elif 1:
567 ... pass
568 Traceback (most recent call last):
569 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200570 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000571
572 >>> if 1:
573 ... pass
574 ... elif 1:
575 ... x() = 1
576 Traceback (most recent call last):
577 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200578 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000579
580 >>> if 1:
581 ... x() = 1
582 ... elif 1:
583 ... pass
584 ... else:
585 ... pass
586 Traceback (most recent call last):
587 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200588 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000589
590 >>> if 1:
591 ... pass
592 ... elif 1:
593 ... x() = 1
594 ... else:
595 ... pass
596 Traceback (most recent call last):
597 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200598 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000599
600 >>> if 1:
601 ... pass
602 ... elif 1:
603 ... pass
604 ... else:
605 ... x() = 1
606 Traceback (most recent call last):
607 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200608 SyntaxError: cannot assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000609
Collin Winter828f04a2007-08-31 00:04:24 +0000610Make sure that the old "raise X, Y[, Z]" form is gone:
611 >>> raise X, Y
612 Traceback (most recent call last):
613 ...
614 SyntaxError: invalid syntax
615 >>> raise X, Y, Z
616 Traceback (most recent call last):
617 ...
618 SyntaxError: invalid syntax
619
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000620
621>>> f(a=23, a=234)
622Traceback (most recent call last):
623 ...
Pablo Galindo254ec782020-04-03 20:37:13 +0100624SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000625
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000626>>> {1, 2, 3} = 42
627Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200628SyntaxError: cannot assign to set display
629
630>>> {1: 2, 3: 4} = 42
631Traceback (most recent call last):
632SyntaxError: cannot assign to dict display
633
634>>> f'{x}' = 42
635Traceback (most recent call last):
636SyntaxError: cannot assign to f-string expression
637
638>>> f'{x}-{y}' = 42
639Traceback (most recent call last):
640SyntaxError: cannot assign to f-string expression
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000641
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300642>>> from t import x,
643Traceback (most recent call last):
644SyntaxError: trailing comma not allowed without surrounding parentheses
645
646>>> from t import x,y,
647Traceback (most recent call last):
648SyntaxError: trailing comma not allowed without surrounding parentheses
649
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000650Corner-cases that used to fail to raise the correct error:
651
652 >>> def f(*, x=lambda __debug__:0): pass
653 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200654 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000655
656 >>> def f(*args:(lambda __debug__:0)): pass
657 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200658 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000659
660 >>> def f(**kwargs:(lambda __debug__:0)): pass
661 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200662 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000663
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300664 >>> with (lambda *:0): pass
665 Traceback (most recent call last):
666 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000667
668Corner-cases that used to crash:
669
670 >>> def f(**__debug__): pass
671 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200672 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000673
674 >>> def f(*xx, __debug__): pass
675 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200676 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000677
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +0300678 >>> import ä £
679 Traceback (most recent call last):
680 SyntaxError: invalid character '£' (U+00A3)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000681"""
682
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000683import re
684import unittest
685
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000686from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000687
688class SyntaxTestCase(unittest.TestCase):
689
690 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200691 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000692 """Check that compiling code raises SyntaxError with errtext.
693
694 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 test of the exception raised. If subclass is specified it
696 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000697 """
698 try:
699 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000700 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701 if subclass and not isinstance(err, subclass):
702 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000703 mo = re.search(errtext, str(err))
704 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -0700705 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200706 self.assertEqual(err.filename, filename)
707 if lineno is not None:
708 self.assertEqual(err.lineno, lineno)
709 if offset is not None:
710 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000711 else:
712 self.fail("compile() did not raise SyntaxError")
713
714 def test_assign_call(self):
715 self._check_error("f() = 1", "assign")
716
Pablo Galindo16ab0702020-05-15 02:04:52 +0100717 @unittest.skipIf(support.use_old_parser(), "The old parser cannot generate these error messages")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000718 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -0700719 self._check_error("del (,)", "invalid syntax")
720 self._check_error("del 1", "delete literal")
721 self._check_error("del (1, 2)", "delete literal")
722 self._check_error("del None", "delete None")
723 self._check_error("del *x", "delete starred")
724 self._check_error("del (*x)", "delete starred")
725 self._check_error("del (*x,)", "delete starred")
726 self._check_error("del [*x,]", "delete starred")
727 self._check_error("del f()", "delete function call")
728 self._check_error("del f(a, b)", "delete function call")
729 self._check_error("del o.f()", "delete function call")
730 self._check_error("del a[0]()", "delete function call")
731 self._check_error("del x, f()", "delete function call")
732 self._check_error("del f(), x", "delete function call")
733 self._check_error("del [a, b, ((c), (d,), e.f())]", "delete function call")
734 self._check_error("del (a if True else b)", "delete conditional")
735 self._check_error("del +a", "delete operator")
736 self._check_error("del a, +b", "delete operator")
737 self._check_error("del a + b", "delete operator")
738 self._check_error("del (a + b, c)", "delete operator")
739 self._check_error("del (c[0], a + b)", "delete operator")
740 self._check_error("del a.b.c + 2", "delete operator")
741 self._check_error("del a.b.c[0] + 2", "delete operator")
742 self._check_error("del (a, b, (c, d.e.f + 2))", "delete operator")
743 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "delete operator")
744 self._check_error("del (a := 5)", "delete named expression")
745 # We don't have a special message for this, but make sure we don't
746 # report "cannot delete name"
747 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000748
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200749 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000750 source = """if 1:
751 def error(a):
752 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200753 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000754 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200755 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000756 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200757 self._check_error(source, "parameter and global", lineno=3)
758
759 def test_nonlocal_param_err_first(self):
760 source = """if 1:
761 def error(a):
762 nonlocal a # SyntaxError
763 def error2():
764 b = 1
765 global b # SyntaxError
766 """
767 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +0000768
Neal Norwitzfcf44352005-11-27 20:37:43 +0000769 def test_break_outside_loop(self):
770 self._check_error("break", "outside loop")
771
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100772 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100773 self._check_error("if 0: yield", "outside function")
774 self._check_error("if 0: yield\nelse: x=1", "outside function")
775 self._check_error("if 1: pass\nelse: yield", "outside function")
776 self._check_error("while 0: yield", "outside function")
777 self._check_error("while 0: yield\nelse: x=1", "outside function")
778 self._check_error("class C:\n if 0: yield", "outside function")
779 self._check_error("class C:\n if 1: pass\n else: yield",
780 "outside function")
781 self._check_error("class C:\n while 0: yield", "outside function")
782 self._check_error("class C:\n while 0: yield\n else: x = 1",
783 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100784
785 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100786 self._check_error("if 0: return", "outside function")
787 self._check_error("if 0: return\nelse: x=1", "outside function")
788 self._check_error("if 1: pass\nelse: return", "outside function")
789 self._check_error("while 0: return", "outside function")
790 self._check_error("class C:\n if 0: return", "outside function")
791 self._check_error("class C:\n while 0: return", "outside function")
792 self._check_error("class C:\n while 0: return\n else: x=1",
793 "outside function")
794 self._check_error("class C:\n if 0: return\n else: x= 1",
795 "outside function")
796 self._check_error("class C:\n if 1: pass\n else: return",
797 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100798
799 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100800 self._check_error("if 0: break", "outside loop")
801 self._check_error("if 0: break\nelse: x=1", "outside loop")
802 self._check_error("if 1: pass\nelse: break", "outside loop")
803 self._check_error("class C:\n if 0: break", "outside loop")
804 self._check_error("class C:\n if 1: pass\n else: break",
805 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100806
807 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100808 self._check_error("if 0: continue", "not properly in loop")
809 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
810 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
811 self._check_error("class C:\n if 0: continue", "not properly in loop")
812 self._check_error("class C:\n if 1: pass\n else: continue",
813 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100814
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815 def test_unexpected_indent(self):
816 self._check_error("foo()\n bar()\n", "unexpected indent",
817 subclass=IndentationError)
818
819 def test_no_indent(self):
820 self._check_error("if 1:\nfoo()", "expected an indented block",
821 subclass=IndentationError)
822
823 def test_bad_outdent(self):
824 self._check_error("if 1:\n foo()\n bar()",
825 "unindent does not match .* level",
826 subclass=IndentationError)
827
828 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400829 self._check_error("int(base=10, '2')",
830 "positional argument follows keyword argument")
831
832 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200833 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400834 "positional argument follows "
835 "keyword argument unpacking")
836
837 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200838 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400839 "iterable argument unpacking follows "
840 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000842def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000843 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000844 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000845 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000846
847if __name__ == "__main__":
848 test_main()