blob: 0c0fc48e0d3de14446abf4f1b6cbe28638ac9983 [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 Galindoc5fc1562020-04-22 23:29:27 +010066# Pegen does not support this yet
67# >>> del f()
68# Traceback (most recent call last):
69# SyntaxError: cannot delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000070
71>>> a + 1 = 2
72Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020073SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000074
75>>> (x for x in x) = 1
76Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020077SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000078
79>>> 1 = 1
80Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020081SyntaxError: cannot assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000082
83>>> "abc" = 1
84Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020085SyntaxError: cannot assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000086
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050087>>> b"" = 1
88Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020089SyntaxError: cannot assign to literal
90
91>>> ... = 1
92Traceback (most recent call last):
93SyntaxError: cannot assign to Ellipsis
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050094
Jeremy Hyltonc960f262006-01-27 15:18:39 +000095>>> `1` = 1
96Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +000097SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000098
99If the left-hand side of an assignment is a list or tuple, an illegal
100expression inside that contain should still cause a syntax error.
101This test just checks a couple of cases rather than enumerating all of
102them.
103
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100104# All of the following also produce different error messages with pegen
105# >>> (a, "b", c) = (1, 2, 3)
106# Traceback (most recent call last):
107# SyntaxError: cannot assign to literal
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200108
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100109# >>> (a, True, c) = (1, 2, 3)
110# Traceback (most recent call last):
111# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200112
113>>> (a, __debug__, c) = (1, 2, 3)
114Traceback (most recent call last):
115SyntaxError: cannot assign to __debug__
116
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100117# >>> (a, *True, c) = (1, 2, 3)
118# Traceback (most recent call last):
119# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200120
121>>> (a, *__debug__, c) = (1, 2, 3)
122Traceback (most recent call last):
123SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000124
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100125# >>> [a, b, c + 1] = [1, 2, 3]
126# Traceback (most recent call last):
127# SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000128
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129>>> a if 1 else b = 1
130Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200131SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000132
133From compiler_complex_args():
134
135>>> def f(None=1):
136... pass
137Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000138SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000139
140
141From ast_for_arguments():
142
143>>> def f(x, y=1, z):
144... pass
145Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000146SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000147
148>>> def f(x, None):
149... pass
150Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000151SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000152
153>>> def f(*None):
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>>> def f(**None):
159... pass
160Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000161SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000162
163
164From ast_for_funcdef():
165
166>>> def None(x):
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
172From ast_for_call():
173
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200174>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000175... return list(it)
176>>> L = range(10)
177>>> f(x for x in L)
178[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
179>>> f(x for x in L, 1)
180Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200181SyntaxError: Generator expression must be parenthesized
182>>> f(x for x in L, y=1)
183Traceback (most recent call last):
184SyntaxError: Generator expression must be parenthesized
185>>> f(x for x in L, *[])
186Traceback (most recent call last):
187SyntaxError: Generator expression must be parenthesized
188>>> f(x for x in L, **{})
189Traceback (most recent call last):
190SyntaxError: Generator expression must be parenthesized
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100191
192# >>> f(L, x for x in L)
193# Traceback (most recent call last):
194# SyntaxError: Generator expression must be parenthesized
195
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400196>>> f(x for x in L, y for y in L)
197Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200198SyntaxError: Generator expression must be parenthesized
199>>> f(x for x in L,)
200Traceback (most recent call last):
201SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000202>>> f((x for x in L), 1)
203[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200204>>> class C(x for x in L):
205... pass
206Traceback (most recent call last):
207SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000208
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200209>>> def g(*args, **kwargs):
210... print(args, sorted(kwargs.items()))
211>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
212... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
213... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
214... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
215... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
216... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
217... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
218... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
219... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
220... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
221... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
222... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
223... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
224... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
225... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
226... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
227... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
228... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
229... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
230... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
231(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000232
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200233>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
234... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
235... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
236... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
237... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
238... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
239... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
240... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
241... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
242... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
243... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
244... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
245... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
246... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
247... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
248... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
249... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
250... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
251... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
252... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
253... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
254... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
255... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
256... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
257... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
258... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
259... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
260... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
261... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
262... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
263... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
264... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
265... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
266... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
267... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
268... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
269... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
270... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
271... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
272... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
273... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
274... # doctest: +ELLIPSIS
275() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000276
Yury Selivanovf2392132016-12-13 19:03:51 -0500277>>> class C:
278... def meth(self, *args):
279... return args
280>>> obj = C()
281>>> obj.meth(
282... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
283... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
284... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
285... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
286... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
287... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
288... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
289... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
290... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
291... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
292... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
293... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
294... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
295... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
296... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
297... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
298... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
299... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
300... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
301... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
302(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
303
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100304# >>> f(lambda x: x[0] = 3)
305# Traceback (most recent call last):
306# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000307
308The grammar accepts any test (basically, any expression) in the
309keyword slot of a call site. Test a few different options.
310
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100311# >>> f(x()=2)
312# Traceback (most recent call last):
313# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
314# >>> f(a or b=1)
315# Traceback (most recent call last):
316# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
317# >>> f(x.y=1)
318# Traceback (most recent call last):
319# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
320# >>> f((x)=2)
321# Traceback (most recent call last):
322# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
323# >>> f(True=2)
324# Traceback (most recent call last):
325# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200326>>> f(__debug__=1)
327Traceback (most recent call last):
328SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100329>>> __debug__: int
330Traceback (most recent call last):
331SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000332
333
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000334More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000335
336>>> (x for x in x) += 1
337Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200338SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000339>>> None += 1
340Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200341SyntaxError: cannot assign to None
342>>> __debug__ += 1
343Traceback (most recent call last):
344SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345>>> f() += 1
346Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200347SyntaxError: cannot assign to function call
Thomas Wouters89f507f2006-12-13 04:49:30 +0000348
349
350Test continue in finally in weird combinations.
351
Ezio Melotti13925002011-03-16 11:05:33 +0200352continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000353
354 >>> def test():
355 ... try:
356 ... pass
357 ... finally:
358 ... for abc in range(10):
359 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000360 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000361 >>> test()
362 9
363
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200364continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000365
366 >>> def test():
367 ... for abc in range(10):
368 ... try:
369 ... pass
370 ... finally:
371 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200372 ... print(abc)
373 >>> test()
374 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000375
376 >>> def test():
377 ... for abc in range(10):
378 ... try:
379 ... pass
380 ... finally:
381 ... try:
382 ... continue
383 ... except:
384 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200385 ... print(abc)
386 >>> test()
387 9
388
389 >>> def test():
390 ... for abc in range(10):
391 ... try:
392 ... pass
393 ... finally:
394 ... try:
395 ... pass
396 ... except:
397 ... continue
398 ... print(abc)
399 >>> test()
400 9
401
402A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000403
404 >>> def foo():
405 ... try:
406 ... pass
407 ... finally:
408 ... continue
409 Traceback (most recent call last):
410 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200411 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000412
413There is one test for a break that is not in a loop. The compiler
414uses a single data structure to keep track of try-finally and loops,
415so we need to be sure that a break is actually inside a loop. If it
416isn't, there should be a syntax error.
417
418 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000419 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000420 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000421 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000422 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000423 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000424 Traceback (most recent call last):
425 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000426 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000427
Benjamin Petersone09ed542016-07-14 22:00:03 -0700428This raises a SyntaxError, it used to raise a SystemError.
429Context for this change can be found on issue #27514
430
Thomas Wouters89f507f2006-12-13 04:49:30 +0000431In 2.5 there was a missing exception and an assert was triggered in a debug
432build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
433
434 >>> while 1:
435 ... while 2:
436 ... while 3:
437 ... while 4:
438 ... while 5:
439 ... while 6:
440 ... while 8:
441 ... while 9:
442 ... while 10:
443 ... while 11:
444 ... while 12:
445 ... while 13:
446 ... while 14:
447 ... while 15:
448 ... while 16:
449 ... while 17:
450 ... while 18:
451 ... while 19:
452 ... while 20:
453 ... while 21:
454 ... while 22:
455 ... break
456 Traceback (most recent call last):
457 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700458 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000459
Guido van Rossum6cff8742016-09-09 09:36:26 -0700460Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
461
462 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200463 ... print(x)
464 ... global x
465 Traceback (most recent call last):
466 ...
467 SyntaxError: name 'x' is used prior to global declaration
468
469 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700470 ... x = 1
471 ... global x
472 Traceback (most recent call last):
473 ...
474 SyntaxError: name 'x' is assigned to before global declaration
475
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200476 >>> def f(x):
477 ... global x
478 Traceback (most recent call last):
479 ...
480 SyntaxError: name 'x' is parameter and global
481
Guido van Rossum6cff8742016-09-09 09:36:26 -0700482 >>> def f():
483 ... x = 1
484 ... def g():
485 ... print(x)
486 ... nonlocal x
487 Traceback (most recent call last):
488 ...
489 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000490
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300491 >>> def f():
492 ... x = 1
493 ... def g():
494 ... x = 2
495 ... nonlocal x
496 Traceback (most recent call last):
497 ...
498 SyntaxError: name 'x' is assigned to before nonlocal declaration
499
Jeremy Hylton81e95022007-02-27 06:50:52 +0000500 >>> def f(x):
501 ... nonlocal x
502 Traceback (most recent call last):
503 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000504 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000505
Jeremy Hylton81e95022007-02-27 06:50:52 +0000506 >>> def f():
507 ... global x
508 ... nonlocal x
509 Traceback (most recent call last):
510 ...
511 SyntaxError: name 'x' is nonlocal and global
512
513 >>> def f():
514 ... nonlocal x
515 Traceback (most recent call last):
516 ...
517 SyntaxError: no binding for nonlocal 'x' found
518
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000519From SF bug #1705365
520 >>> nonlocal x
521 Traceback (most recent call last):
522 ...
523 SyntaxError: nonlocal declaration not allowed at module level
524
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300525From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600526 >>> class A:
527 ... def f(self):
528 ... nonlocal __x
529 Traceback (most recent call last):
530 ...
531 SyntaxError: no binding for nonlocal '_A__x' found
532
Guido van Rossumd8faa362007-04-27 19:54:29 +0000533
534This tests assignment-context; there was a bug in Python 2.5 where compiling
535a complex 'if' (one with 'elif') would fail to notice an invalid suite,
536leading to spurious errors.
537
538 >>> if 1:
539 ... x() = 1
540 ... elif 1:
541 ... pass
542 Traceback (most recent call last):
543 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200544 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000545
546 >>> if 1:
547 ... pass
548 ... elif 1:
549 ... x() = 1
550 Traceback (most recent call last):
551 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200552 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000553
554 >>> if 1:
555 ... x() = 1
556 ... elif 1:
557 ... pass
558 ... else:
559 ... pass
560 Traceback (most recent call last):
561 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200562 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000563
564 >>> if 1:
565 ... pass
566 ... elif 1:
567 ... x() = 1
568 ... else:
569 ... pass
570 Traceback (most recent call last):
571 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200572 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000573
574 >>> if 1:
575 ... pass
576 ... elif 1:
577 ... pass
578 ... else:
579 ... x() = 1
580 Traceback (most recent call last):
581 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200582 SyntaxError: cannot assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000583
Collin Winter828f04a2007-08-31 00:04:24 +0000584Make sure that the old "raise X, Y[, Z]" form is gone:
585 >>> raise X, Y
586 Traceback (most recent call last):
587 ...
588 SyntaxError: invalid syntax
589 >>> raise X, Y, Z
590 Traceback (most recent call last):
591 ...
592 SyntaxError: invalid syntax
593
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000594
595>>> f(a=23, a=234)
596Traceback (most recent call last):
597 ...
Pablo Galindo254ec782020-04-03 20:37:13 +0100598SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000599
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000600>>> {1, 2, 3} = 42
601Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200602SyntaxError: cannot assign to set display
603
604>>> {1: 2, 3: 4} = 42
605Traceback (most recent call last):
606SyntaxError: cannot assign to dict display
607
608>>> f'{x}' = 42
609Traceback (most recent call last):
610SyntaxError: cannot assign to f-string expression
611
612>>> f'{x}-{y}' = 42
613Traceback (most recent call last):
614SyntaxError: cannot assign to f-string expression
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000615
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000616Corner-cases that used to fail to raise the correct error:
617
618 >>> def f(*, x=lambda __debug__:0): pass
619 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200620 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000621
622 >>> def f(*args:(lambda __debug__:0)): pass
623 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200624 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000625
626 >>> def f(**kwargs:(lambda __debug__:0)): pass
627 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200628 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000629
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300630 >>> with (lambda *:0): pass
631 Traceback (most recent call last):
632 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000633
634Corner-cases that used to crash:
635
636 >>> def f(**__debug__): pass
637 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200638 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000639
640 >>> def f(*xx, __debug__): pass
641 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200642 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000643
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000644"""
645
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000646import re
647import unittest
648
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000649from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000650
651class SyntaxTestCase(unittest.TestCase):
652
653 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200654 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000655 """Check that compiling code raises SyntaxError with errtext.
656
657 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 test of the exception raised. If subclass is specified it
659 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000660 """
661 try:
662 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000663 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 if subclass and not isinstance(err, subclass):
665 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000666 mo = re.search(errtext, str(err))
667 if mo is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000668 self.fail("SyntaxError did not contain '%r'" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200669 self.assertEqual(err.filename, filename)
670 if lineno is not None:
671 self.assertEqual(err.lineno, lineno)
672 if offset is not None:
673 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000674 else:
675 self.fail("compile() did not raise SyntaxError")
676
677 def test_assign_call(self):
678 self._check_error("f() = 1", "assign")
679
Victor Stinner1def7752020-04-23 03:03:24 +0200680 @support.skip_if_new_parser("Pegen does not produce a specialized error "
681 "message yet")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000682 def test_assign_del(self):
683 self._check_error("del f()", "delete")
684
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200685 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000686 source = """if 1:
687 def error(a):
688 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200689 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000690 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200691 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000692 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200693 self._check_error(source, "parameter and global", lineno=3)
694
695 def test_nonlocal_param_err_first(self):
696 source = """if 1:
697 def error(a):
698 nonlocal a # SyntaxError
699 def error2():
700 b = 1
701 global b # SyntaxError
702 """
703 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +0000704
Neal Norwitzfcf44352005-11-27 20:37:43 +0000705 def test_break_outside_loop(self):
706 self._check_error("break", "outside loop")
707
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100708 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100709 self._check_error("if 0: yield", "outside function")
710 self._check_error("if 0: yield\nelse: x=1", "outside function")
711 self._check_error("if 1: pass\nelse: yield", "outside function")
712 self._check_error("while 0: yield", "outside function")
713 self._check_error("while 0: yield\nelse: x=1", "outside function")
714 self._check_error("class C:\n if 0: yield", "outside function")
715 self._check_error("class C:\n if 1: pass\n else: yield",
716 "outside function")
717 self._check_error("class C:\n while 0: yield", "outside function")
718 self._check_error("class C:\n while 0: yield\n else: x = 1",
719 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100720
721 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100722 self._check_error("if 0: return", "outside function")
723 self._check_error("if 0: return\nelse: x=1", "outside function")
724 self._check_error("if 1: pass\nelse: return", "outside function")
725 self._check_error("while 0: return", "outside function")
726 self._check_error("class C:\n if 0: return", "outside function")
727 self._check_error("class C:\n while 0: return", "outside function")
728 self._check_error("class C:\n while 0: return\n else: x=1",
729 "outside function")
730 self._check_error("class C:\n if 0: return\n else: x= 1",
731 "outside function")
732 self._check_error("class C:\n if 1: pass\n else: return",
733 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100734
735 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100736 self._check_error("if 0: break", "outside loop")
737 self._check_error("if 0: break\nelse: x=1", "outside loop")
738 self._check_error("if 1: pass\nelse: break", "outside loop")
739 self._check_error("class C:\n if 0: break", "outside loop")
740 self._check_error("class C:\n if 1: pass\n else: break",
741 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100742
743 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100744 self._check_error("if 0: continue", "not properly in loop")
745 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
746 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
747 self._check_error("class C:\n if 0: continue", "not properly in loop")
748 self._check_error("class C:\n if 1: pass\n else: continue",
749 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100750
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751 def test_unexpected_indent(self):
752 self._check_error("foo()\n bar()\n", "unexpected indent",
753 subclass=IndentationError)
754
755 def test_no_indent(self):
756 self._check_error("if 1:\nfoo()", "expected an indented block",
757 subclass=IndentationError)
758
759 def test_bad_outdent(self):
760 self._check_error("if 1:\n foo()\n bar()",
761 "unindent does not match .* level",
762 subclass=IndentationError)
763
764 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400765 self._check_error("int(base=10, '2')",
766 "positional argument follows keyword argument")
767
768 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200769 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400770 "positional argument follows "
771 "keyword argument unpacking")
772
773 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200774 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400775 "iterable argument unpacking follows "
776 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000778def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000779 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000780 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000781 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000782
783if __name__ == "__main__":
784 test_main()