blob: 06636ae8a149a402a6aa78a3433716eaddd37042 [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 Galindoc5fc1562020-04-22 23:29:27 +0100103# All of the following also produce different error messages with pegen
104# >>> (a, "b", c) = (1, 2, 3)
105# Traceback (most recent call last):
106# SyntaxError: cannot assign to literal
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200107
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100108# >>> (a, True, c) = (1, 2, 3)
109# Traceback (most recent call last):
110# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200111
112>>> (a, __debug__, c) = (1, 2, 3)
113Traceback (most recent call last):
114SyntaxError: cannot assign to __debug__
115
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100116# >>> (a, *True, c) = (1, 2, 3)
117# Traceback (most recent call last):
118# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200119
120>>> (a, *__debug__, c) = (1, 2, 3)
121Traceback (most recent call last):
122SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000123
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100124# >>> [a, b, c + 1] = [1, 2, 3]
125# Traceback (most recent call last):
126# SyntaxError: cannot assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000127
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128>>> a if 1 else b = 1
129Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200130SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000131
132From compiler_complex_args():
133
134>>> def f(None=1):
135... pass
136Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000137SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000138
139
140From ast_for_arguments():
141
142>>> def f(x, y=1, z):
143... pass
144Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000145SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000146
147>>> def f(x, None):
148... pass
149Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000150SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000151
152>>> def f(*None):
153... pass
154Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000155SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000156
157>>> def f(**None):
158... pass
159Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000160SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000161
162
163From ast_for_funcdef():
164
165>>> def None(x):
166... pass
167Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000168SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000169
170
171From ast_for_call():
172
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200173>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000174... return list(it)
175>>> L = range(10)
176>>> f(x for x in L)
177[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
178>>> f(x for x in L, 1)
179Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200180SyntaxError: Generator expression must be parenthesized
181>>> f(x for x in L, y=1)
182Traceback (most recent call last):
183SyntaxError: Generator expression must be parenthesized
184>>> f(x for x in L, *[])
185Traceback (most recent call last):
186SyntaxError: Generator expression must be parenthesized
187>>> f(x for x in L, **{})
188Traceback (most recent call last):
189SyntaxError: Generator expression must be parenthesized
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100190
191# >>> f(L, x for x in L)
192# Traceback (most recent call last):
193# SyntaxError: Generator expression must be parenthesized
194
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400195>>> f(x for x in L, y for y in L)
196Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200197SyntaxError: Generator expression must be parenthesized
198>>> f(x for x in L,)
199Traceback (most recent call last):
200SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000201>>> f((x for x in L), 1)
202[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200203>>> class C(x for x in L):
204... pass
205Traceback (most recent call last):
206SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000207
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200208>>> def g(*args, **kwargs):
209... print(args, sorted(kwargs.items()))
210>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
211... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
212... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
213... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
214... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
215... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
216... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
217... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
218... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
219... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
220... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
221... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
222... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
223... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
224... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
225... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
226... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
227... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
228... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
229... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
230(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000231
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200232>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
233... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
234... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
235... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
236... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
237... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
238... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
239... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
240... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
241... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
242... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
243... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
244... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
245... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
246... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
247... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
248... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
249... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
250... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
251... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
252... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
253... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
254... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
255... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
256... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
257... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
258... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
259... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
260... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
261... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
262... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
263... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
264... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
265... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
266... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
267... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
268... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
269... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
270... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
271... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
272... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
273... # doctest: +ELLIPSIS
274() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000275
Yury Selivanovf2392132016-12-13 19:03:51 -0500276>>> class C:
277... def meth(self, *args):
278... return args
279>>> obj = C()
280>>> obj.meth(
281... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
282... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
283... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
284... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
285... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
286... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
287... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
288... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
289... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
290... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
291... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
292... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
293... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
294... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
295... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
296... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
297... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
298... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
299... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
300... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
301(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
302
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100303# >>> f(lambda x: x[0] = 3)
304# Traceback (most recent call last):
305# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000306
307The grammar accepts any test (basically, any expression) in the
308keyword slot of a call site. Test a few different options.
309
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100310# >>> f(x()=2)
311# Traceback (most recent call last):
312# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
313# >>> f(a or b=1)
314# Traceback (most recent call last):
315# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
316# >>> f(x.y=1)
317# Traceback (most recent call last):
318# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
319# >>> f((x)=2)
320# Traceback (most recent call last):
321# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
322# >>> f(True=2)
323# Traceback (most recent call last):
324# SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200325>>> f(__debug__=1)
326Traceback (most recent call last):
327SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100328>>> __debug__: int
329Traceback (most recent call last):
330SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000331
332
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000333More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334
335>>> (x for x in x) += 1
336Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200337SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000338>>> None += 1
339Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200340SyntaxError: cannot assign to None
341>>> __debug__ += 1
342Traceback (most recent call last):
343SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344>>> f() += 1
345Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200346SyntaxError: cannot assign to function call
Thomas Wouters89f507f2006-12-13 04:49:30 +0000347
348
349Test continue in finally in weird combinations.
350
Ezio Melotti13925002011-03-16 11:05:33 +0200351continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000352
353 >>> def test():
354 ... try:
355 ... pass
356 ... finally:
357 ... for abc in range(10):
358 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000359 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000360 >>> test()
361 9
362
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200363continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000364
365 >>> def test():
366 ... for abc in range(10):
367 ... try:
368 ... pass
369 ... finally:
370 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200371 ... print(abc)
372 >>> test()
373 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000374
375 >>> def test():
376 ... for abc in range(10):
377 ... try:
378 ... pass
379 ... finally:
380 ... try:
381 ... continue
382 ... except:
383 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200384 ... print(abc)
385 >>> test()
386 9
387
388 >>> def test():
389 ... for abc in range(10):
390 ... try:
391 ... pass
392 ... finally:
393 ... try:
394 ... pass
395 ... except:
396 ... continue
397 ... print(abc)
398 >>> test()
399 9
400
401A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000402
403 >>> def foo():
404 ... try:
405 ... pass
406 ... finally:
407 ... continue
408 Traceback (most recent call last):
409 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200410 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000411
412There is one test for a break that is not in a loop. The compiler
413uses a single data structure to keep track of try-finally and loops,
414so we need to be sure that a break is actually inside a loop. If it
415isn't, there should be a syntax error.
416
417 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000418 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000419 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000420 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000421 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000422 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000423 Traceback (most recent call last):
424 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000425 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000426
Benjamin Petersone09ed542016-07-14 22:00:03 -0700427This raises a SyntaxError, it used to raise a SystemError.
428Context for this change can be found on issue #27514
429
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430In 2.5 there was a missing exception and an assert was triggered in a debug
431build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
432
433 >>> while 1:
434 ... while 2:
435 ... while 3:
436 ... while 4:
437 ... while 5:
438 ... while 6:
439 ... while 8:
440 ... while 9:
441 ... while 10:
442 ... while 11:
443 ... while 12:
444 ... while 13:
445 ... while 14:
446 ... while 15:
447 ... while 16:
448 ... while 17:
449 ... while 18:
450 ... while 19:
451 ... while 20:
452 ... while 21:
453 ... while 22:
454 ... break
455 Traceback (most recent call last):
456 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700457 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000458
Guido van Rossum6cff8742016-09-09 09:36:26 -0700459Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
460
461 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200462 ... print(x)
463 ... global x
464 Traceback (most recent call last):
465 ...
466 SyntaxError: name 'x' is used prior to global declaration
467
468 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700469 ... x = 1
470 ... global x
471 Traceback (most recent call last):
472 ...
473 SyntaxError: name 'x' is assigned to before global declaration
474
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200475 >>> def f(x):
476 ... global x
477 Traceback (most recent call last):
478 ...
479 SyntaxError: name 'x' is parameter and global
480
Guido van Rossum6cff8742016-09-09 09:36:26 -0700481 >>> def f():
482 ... x = 1
483 ... def g():
484 ... print(x)
485 ... nonlocal x
486 Traceback (most recent call last):
487 ...
488 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000489
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300490 >>> def f():
491 ... x = 1
492 ... def g():
493 ... x = 2
494 ... nonlocal x
495 Traceback (most recent call last):
496 ...
497 SyntaxError: name 'x' is assigned to before nonlocal declaration
498
Jeremy Hylton81e95022007-02-27 06:50:52 +0000499 >>> def f(x):
500 ... nonlocal x
501 Traceback (most recent call last):
502 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000503 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000504
Jeremy Hylton81e95022007-02-27 06:50:52 +0000505 >>> def f():
506 ... global x
507 ... nonlocal x
508 Traceback (most recent call last):
509 ...
510 SyntaxError: name 'x' is nonlocal and global
511
512 >>> def f():
513 ... nonlocal x
514 Traceback (most recent call last):
515 ...
516 SyntaxError: no binding for nonlocal 'x' found
517
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000518From SF bug #1705365
519 >>> nonlocal x
520 Traceback (most recent call last):
521 ...
522 SyntaxError: nonlocal declaration not allowed at module level
523
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300524From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600525 >>> class A:
526 ... def f(self):
527 ... nonlocal __x
528 Traceback (most recent call last):
529 ...
530 SyntaxError: no binding for nonlocal '_A__x' found
531
Guido van Rossumd8faa362007-04-27 19:54:29 +0000532
533This tests assignment-context; there was a bug in Python 2.5 where compiling
534a complex 'if' (one with 'elif') would fail to notice an invalid suite,
535leading to spurious errors.
536
537 >>> if 1:
538 ... x() = 1
539 ... elif 1:
540 ... pass
541 Traceback (most recent call last):
542 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200543 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000544
545 >>> if 1:
546 ... pass
547 ... elif 1:
548 ... x() = 1
549 Traceback (most recent call last):
550 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200551 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000552
553 >>> if 1:
554 ... x() = 1
555 ... elif 1:
556 ... pass
557 ... else:
558 ... pass
559 Traceback (most recent call last):
560 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200561 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000562
563 >>> if 1:
564 ... pass
565 ... elif 1:
566 ... x() = 1
567 ... else:
568 ... pass
569 Traceback (most recent call last):
570 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200571 SyntaxError: cannot assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000572
573 >>> if 1:
574 ... pass
575 ... elif 1:
576 ... pass
577 ... else:
578 ... x() = 1
579 Traceback (most recent call last):
580 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200581 SyntaxError: cannot assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000582
Collin Winter828f04a2007-08-31 00:04:24 +0000583Make sure that the old "raise X, Y[, Z]" form is gone:
584 >>> raise X, Y
585 Traceback (most recent call last):
586 ...
587 SyntaxError: invalid syntax
588 >>> raise X, Y, Z
589 Traceback (most recent call last):
590 ...
591 SyntaxError: invalid syntax
592
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000593
594>>> f(a=23, a=234)
595Traceback (most recent call last):
596 ...
Pablo Galindo254ec782020-04-03 20:37:13 +0100597SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000598
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000599>>> {1, 2, 3} = 42
600Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200601SyntaxError: cannot assign to set display
602
603>>> {1: 2, 3: 4} = 42
604Traceback (most recent call last):
605SyntaxError: cannot assign to dict display
606
607>>> f'{x}' = 42
608Traceback (most recent call last):
609SyntaxError: cannot assign to f-string expression
610
611>>> f'{x}-{y}' = 42
612Traceback (most recent call last):
613SyntaxError: cannot assign to f-string expression
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000614
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000615Corner-cases that used to fail to raise the correct error:
616
617 >>> def f(*, x=lambda __debug__:0): pass
618 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200619 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000620
621 >>> def f(*args:(lambda __debug__:0)): pass
622 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200623 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000624
625 >>> def f(**kwargs:(lambda __debug__:0)): pass
626 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200627 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000628
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +0300629 >>> with (lambda *:0): pass
630 Traceback (most recent call last):
631 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000632
633Corner-cases that used to crash:
634
635 >>> def f(**__debug__): pass
636 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200637 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000638
639 >>> def f(*xx, __debug__): pass
640 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200641 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000642
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000643"""
644
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000645import re
646import unittest
647
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000648from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000649
650class SyntaxTestCase(unittest.TestCase):
651
652 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200653 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000654 """Check that compiling code raises SyntaxError with errtext.
655
656 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 test of the exception raised. If subclass is specified it
658 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000659 """
660 try:
661 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000662 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663 if subclass and not isinstance(err, subclass):
664 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000665 mo = re.search(errtext, str(err))
666 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -0700667 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200668 self.assertEqual(err.filename, filename)
669 if lineno is not None:
670 self.assertEqual(err.lineno, lineno)
671 if offset is not None:
672 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000673 else:
674 self.fail("compile() did not raise SyntaxError")
675
676 def test_assign_call(self):
677 self._check_error("f() = 1", "assign")
678
679 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -0700680 self._check_error("del (,)", "invalid syntax")
681 self._check_error("del 1", "delete literal")
682 self._check_error("del (1, 2)", "delete literal")
683 self._check_error("del None", "delete None")
684 self._check_error("del *x", "delete starred")
685 self._check_error("del (*x)", "delete starred")
686 self._check_error("del (*x,)", "delete starred")
687 self._check_error("del [*x,]", "delete starred")
688 self._check_error("del f()", "delete function call")
689 self._check_error("del f(a, b)", "delete function call")
690 self._check_error("del o.f()", "delete function call")
691 self._check_error("del a[0]()", "delete function call")
692 self._check_error("del x, f()", "delete function call")
693 self._check_error("del f(), x", "delete function call")
694 self._check_error("del [a, b, ((c), (d,), e.f())]", "delete function call")
695 self._check_error("del (a if True else b)", "delete conditional")
696 self._check_error("del +a", "delete operator")
697 self._check_error("del a, +b", "delete operator")
698 self._check_error("del a + b", "delete operator")
699 self._check_error("del (a + b, c)", "delete operator")
700 self._check_error("del (c[0], a + b)", "delete operator")
701 self._check_error("del a.b.c + 2", "delete operator")
702 self._check_error("del a.b.c[0] + 2", "delete operator")
703 self._check_error("del (a, b, (c, d.e.f + 2))", "delete operator")
704 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "delete operator")
705 self._check_error("del (a := 5)", "delete named expression")
706 # We don't have a special message for this, but make sure we don't
707 # report "cannot delete name"
708 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000709
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200710 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000711 source = """if 1:
712 def error(a):
713 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200714 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000715 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200716 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000717 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200718 self._check_error(source, "parameter and global", lineno=3)
719
720 def test_nonlocal_param_err_first(self):
721 source = """if 1:
722 def error(a):
723 nonlocal a # SyntaxError
724 def error2():
725 b = 1
726 global b # SyntaxError
727 """
728 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +0000729
Neal Norwitzfcf44352005-11-27 20:37:43 +0000730 def test_break_outside_loop(self):
731 self._check_error("break", "outside loop")
732
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100733 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100734 self._check_error("if 0: yield", "outside function")
735 self._check_error("if 0: yield\nelse: x=1", "outside function")
736 self._check_error("if 1: pass\nelse: yield", "outside function")
737 self._check_error("while 0: yield", "outside function")
738 self._check_error("while 0: yield\nelse: x=1", "outside function")
739 self._check_error("class C:\n if 0: yield", "outside function")
740 self._check_error("class C:\n if 1: pass\n else: yield",
741 "outside function")
742 self._check_error("class C:\n while 0: yield", "outside function")
743 self._check_error("class C:\n while 0: yield\n else: x = 1",
744 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100745
746 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100747 self._check_error("if 0: return", "outside function")
748 self._check_error("if 0: return\nelse: x=1", "outside function")
749 self._check_error("if 1: pass\nelse: return", "outside function")
750 self._check_error("while 0: return", "outside function")
751 self._check_error("class C:\n if 0: return", "outside function")
752 self._check_error("class C:\n while 0: return", "outside function")
753 self._check_error("class C:\n while 0: return\n else: x=1",
754 "outside function")
755 self._check_error("class C:\n if 0: return\n else: x= 1",
756 "outside function")
757 self._check_error("class C:\n if 1: pass\n else: return",
758 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100759
760 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100761 self._check_error("if 0: break", "outside loop")
762 self._check_error("if 0: break\nelse: x=1", "outside loop")
763 self._check_error("if 1: pass\nelse: break", "outside loop")
764 self._check_error("class C:\n if 0: break", "outside loop")
765 self._check_error("class C:\n if 1: pass\n else: break",
766 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100767
768 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100769 self._check_error("if 0: continue", "not properly in loop")
770 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
771 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
772 self._check_error("class C:\n if 0: continue", "not properly in loop")
773 self._check_error("class C:\n if 1: pass\n else: continue",
774 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +0100775
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776 def test_unexpected_indent(self):
777 self._check_error("foo()\n bar()\n", "unexpected indent",
778 subclass=IndentationError)
779
780 def test_no_indent(self):
781 self._check_error("if 1:\nfoo()", "expected an indented block",
782 subclass=IndentationError)
783
784 def test_bad_outdent(self):
785 self._check_error("if 1:\n foo()\n bar()",
786 "unindent does not match .* level",
787 subclass=IndentationError)
788
789 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400790 self._check_error("int(base=10, '2')",
791 "positional argument follows keyword argument")
792
793 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200794 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400795 "positional argument follows "
796 "keyword argument unpacking")
797
798 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200799 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400800 "iterable argument unpacking follows "
801 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000803def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000804 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000805 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000806 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000807
808if __name__ == "__main__":
809 test_main()