blob: 83f49f65802727756c63747fb8e5f48310c01251 [file] [log] [blame]
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001"""This module tests SyntaxErrors.
2
3Here's an example of the sort of thing that is tested.
4
5>>> def f(x):
6... global x
7Traceback (most recent call last):
Nick Coghlan650f0d02007-04-15 12:05:43 +00008SyntaxError: name 'x' is parameter and global
Jeremy Hyltonc960f262006-01-27 15:18:39 +00009
10The tests are all raise SyntaxErrors. They were created by checking
11each C call that raises SyntaxError. There are several modules that
12raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
13symtable.c.
14
15The parser itself outlaws a lot of invalid syntax. None of these
16errors are tested here at the moment. We should add some tests; since
17there are infinitely many programs with invalid syntax, we would need
18to be judicious in selecting some.
19
20The compiler generates a synthetic module name for code executed by
21doctest. Since all the code comes from the same module, a suffix like
22[1] is appended to the module name, As a consequence, changing the
23order of tests in this module means renumbering all the errors after
24it. (Maybe we should enable the ellipsis option for these tests.)
25
26In ast.c, syntax errors are raised by calling ast_error().
27
28Errors from set_context():
29
Jeremy Hyltonc960f262006-01-27 15:18:39 +000030>>> obj.None = 1
31Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +000032SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000033
34>>> None = 1
35Traceback (most recent call last):
Benjamin Peterson442f2092012-12-06 17:41:04 -050036SyntaxError: can't assign to keyword
Jeremy Hyltonc960f262006-01-27 15:18:39 +000037
38It's a syntax error to assign to the empty tuple. Why isn't it an
39error to assign to the empty list? It will always raise some error at
40runtime.
41
42>>> () = 1
43Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000044SyntaxError: can't assign to ()
Jeremy Hyltonc960f262006-01-27 15:18:39 +000045
46>>> f() = 1
47Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000048SyntaxError: can't assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000049
50>>> del f()
51Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000052SyntaxError: can't delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000053
54>>> a + 1 = 2
55Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000056SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000057
58>>> (x for x in x) = 1
59Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000060SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000061
62>>> 1 = 1
63Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000064SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000065
66>>> "abc" = 1
67Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000068SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000069
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050070>>> b"" = 1
71Traceback (most recent call last):
72SyntaxError: can't assign to literal
73
Jeremy Hyltonc960f262006-01-27 15:18:39 +000074>>> `1` = 1
75Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +000076SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000077
78If the left-hand side of an assignment is a list or tuple, an illegal
79expression inside that contain should still cause a syntax error.
80This test just checks a couple of cases rather than enumerating all of
81them.
82
83>>> (a, "b", c) = (1, 2, 3)
84Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000085SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000086
87>>> [a, b, c + 1] = [1, 2, 3]
88Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000089SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000090
Thomas Wouters477c8d52006-05-27 19:21:47 +000091>>> a if 1 else b = 1
92Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000093SyntaxError: can't assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000094
95From compiler_complex_args():
96
97>>> def f(None=1):
98... pass
99Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000100SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000101
102
103From ast_for_arguments():
104
105>>> def f(x, y=1, z):
106... pass
107Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000108SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000109
110>>> def f(x, None):
111... pass
112Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000113SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000114
115>>> def f(*None):
116... pass
117Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000118SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000119
120>>> def f(**None):
121... pass
122Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000123SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000124
125
126From ast_for_funcdef():
127
128>>> def None(x):
129... pass
130Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000131SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000132
133
134From ast_for_call():
135
136>>> def f(it, *varargs):
137... return list(it)
138>>> L = range(10)
139>>> f(x for x in L)
140[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
141>>> f(x for x in L, 1)
142Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000143SyntaxError: Generator expression must be parenthesized if not sole argument
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400144>>> f(x for x in L, y for y in L)
145Traceback (most recent call last):
146SyntaxError: Generator expression must be parenthesized if not sole argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000147>>> f((x for x in L), 1)
148[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
149
150>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
151... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
152... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
153... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
154... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
155... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
156... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
157... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
158... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
159... i100, i101, i102, i103, i104, i105, i106, i107, i108,
160... i109, i110, i111, i112, i113, i114, i115, i116, i117,
161... i118, i119, i120, i121, i122, i123, i124, i125, i126,
162... i127, i128, i129, i130, i131, i132, i133, i134, i135,
163... i136, i137, i138, i139, i140, i141, i142, i143, i144,
164... i145, i146, i147, i148, i149, i150, i151, i152, i153,
165... i154, i155, i156, i157, i158, i159, i160, i161, i162,
166... i163, i164, i165, i166, i167, i168, i169, i170, i171,
167... i172, i173, i174, i175, i176, i177, i178, i179, i180,
168... i181, i182, i183, i184, i185, i186, i187, i188, i189,
169... i190, i191, i192, i193, i194, i195, i196, i197, i198,
170... i199, i200, i201, i202, i203, i204, i205, i206, i207,
171... i208, i209, i210, i211, i212, i213, i214, i215, i216,
172... i217, i218, i219, i220, i221, i222, i223, i224, i225,
173... i226, i227, i228, i229, i230, i231, i232, i233, i234,
174... i235, i236, i237, i238, i239, i240, i241, i242, i243,
175... i244, i245, i246, i247, i248, i249, i250, i251, i252,
176... i253, i254, i255)
177Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000178SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000179
180The actual error cases counts positional arguments, keyword arguments,
181and generator expression arguments separately. This test combines the
182three.
183
184>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
185... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
186... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
187... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
188... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
189... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
190... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
191... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
192... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
193... i100, i101, i102, i103, i104, i105, i106, i107, i108,
194... i109, i110, i111, i112, i113, i114, i115, i116, i117,
195... i118, i119, i120, i121, i122, i123, i124, i125, i126,
196... i127, i128, i129, i130, i131, i132, i133, i134, i135,
197... i136, i137, i138, i139, i140, i141, i142, i143, i144,
198... i145, i146, i147, i148, i149, i150, i151, i152, i153,
199... i154, i155, i156, i157, i158, i159, i160, i161, i162,
200... i163, i164, i165, i166, i167, i168, i169, i170, i171,
201... i172, i173, i174, i175, i176, i177, i178, i179, i180,
202... i181, i182, i183, i184, i185, i186, i187, i188, i189,
203... i190, i191, i192, i193, i194, i195, i196, i197, i198,
204... i199, i200, i201, i202, i203, i204, i205, i206, i207,
205... i208, i209, i210, i211, i212, i213, i214, i215, i216,
206... i217, i218, i219, i220, i221, i222, i223, i224, i225,
207... i226, i227, i228, i229, i230, i231, i232, i233, i234,
208... i235, i236, i237, i238, i239, i240, i241, i242, i243,
209... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
210... i252=1, i253=1, i254=1, i255=1)
211Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000212SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000213
214>>> f(lambda x: x[0] = 3)
215Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000216SyntaxError: lambda cannot contain assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000217
218The grammar accepts any test (basically, any expression) in the
219keyword slot of a call site. Test a few different options.
220
221>>> f(x()=2)
222Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000223SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000224>>> f(a or b=1)
225Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000226SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000227>>> f(x.y=1)
228Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000229SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000230
231
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000232More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000233
234>>> (x for x in x) += 1
235Traceback (most recent call last):
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000236SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000237>>> None += 1
238Traceback (most recent call last):
Benjamin Peterson442f2092012-12-06 17:41:04 -0500239SyntaxError: can't assign to keyword
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000240>>> f() += 1
241Traceback (most recent call last):
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000242SyntaxError: can't assign to function call
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243
244
245Test continue in finally in weird combinations.
246
Ezio Melotti13925002011-03-16 11:05:33 +0200247continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248
249 >>> def test():
250 ... try:
251 ... pass
252 ... finally:
253 ... for abc in range(10):
254 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000255 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000256 >>> test()
257 9
258
259Start simple, a continue in a finally should not be allowed.
260
261 >>> def test():
262 ... for abc in range(10):
263 ... try:
264 ... pass
265 ... finally:
266 ... continue
267 Traceback (most recent call last):
268 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000269 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000270
271This is essentially a continue in a finally which should not be allowed.
272
273 >>> def test():
274 ... for abc in range(10):
275 ... try:
276 ... pass
277 ... finally:
278 ... try:
279 ... continue
280 ... except:
281 ... pass
282 Traceback (most recent call last):
283 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000284 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000285
286 >>> def foo():
287 ... try:
288 ... pass
289 ... finally:
290 ... continue
291 Traceback (most recent call last):
292 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000293 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000294
295 >>> def foo():
296 ... for a in ():
297 ... try:
298 ... pass
299 ... finally:
300 ... continue
301 Traceback (most recent call last):
302 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000303 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000304
305 >>> def foo():
306 ... for a in ():
307 ... try:
308 ... pass
309 ... finally:
310 ... try:
311 ... continue
312 ... finally:
313 ... pass
314 Traceback (most recent call last):
315 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000316 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000317
318 >>> def foo():
319 ... for a in ():
320 ... try: pass
321 ... finally:
322 ... try:
323 ... pass
324 ... except:
325 ... continue
326 Traceback (most recent call last):
327 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000328 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000329
330There is one test for a break that is not in a loop. The compiler
331uses a single data structure to keep track of try-finally and loops,
332so we need to be sure that a break is actually inside a loop. If it
333isn't, there should be a syntax error.
334
335 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000336 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000337 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000338 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000340 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000341 Traceback (most recent call last):
342 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000343 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000344
Benjamin Petersone09ed542016-07-14 22:00:03 -0700345This raises a SyntaxError, it used to raise a SystemError.
346Context for this change can be found on issue #27514
347
Thomas Wouters89f507f2006-12-13 04:49:30 +0000348In 2.5 there was a missing exception and an assert was triggered in a debug
349build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
350
351 >>> while 1:
352 ... while 2:
353 ... while 3:
354 ... while 4:
355 ... while 5:
356 ... while 6:
357 ... while 8:
358 ... while 9:
359 ... while 10:
360 ... while 11:
361 ... while 12:
362 ... while 13:
363 ... while 14:
364 ... while 15:
365 ... while 16:
366 ... while 17:
367 ... while 18:
368 ... while 19:
369 ... while 20:
370 ... while 21:
371 ... while 22:
372 ... break
373 Traceback (most recent call last):
374 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700375 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000376
Jeremy Hylton81e95022007-02-27 06:50:52 +0000377Misuse of the nonlocal statement can lead to a few unique syntax errors.
378
379 >>> def f(x):
380 ... nonlocal x
381 Traceback (most recent call last):
382 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000383 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000384
Jeremy Hylton81e95022007-02-27 06:50:52 +0000385 >>> def f():
386 ... global x
387 ... nonlocal x
388 Traceback (most recent call last):
389 ...
390 SyntaxError: name 'x' is nonlocal and global
391
392 >>> def f():
393 ... nonlocal x
394 Traceback (most recent call last):
395 ...
396 SyntaxError: no binding for nonlocal 'x' found
397
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000398From SF bug #1705365
399 >>> nonlocal x
400 Traceback (most recent call last):
401 ...
402 SyntaxError: nonlocal declaration not allowed at module level
403
Jeremy Hylton81e95022007-02-27 06:50:52 +0000404TODO(jhylton): Figure out how to test SyntaxWarning with doctest.
405
406## >>> def f(x):
407## ... def f():
408## ... print(x)
409## ... nonlocal x
410## Traceback (most recent call last):
411## ...
412## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
Guido van Rossumd8faa362007-04-27 19:54:29 +0000413
Jeremy Hylton81e95022007-02-27 06:50:52 +0000414## >>> def f():
415## ... x = 1
416## ... nonlocal x
417## Traceback (most recent call last):
418## ...
419## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
420
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600421 From https://bugs.python.org/issue25973
422 >>> class A:
423 ... def f(self):
424 ... nonlocal __x
425 Traceback (most recent call last):
426 ...
427 SyntaxError: no binding for nonlocal '_A__x' found
428
Guido van Rossumd8faa362007-04-27 19:54:29 +0000429
430This tests assignment-context; there was a bug in Python 2.5 where compiling
431a complex 'if' (one with 'elif') would fail to notice an invalid suite,
432leading to spurious errors.
433
434 >>> if 1:
435 ... x() = 1
436 ... elif 1:
437 ... pass
438 Traceback (most recent call last):
439 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000440 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000441
442 >>> if 1:
443 ... pass
444 ... elif 1:
445 ... x() = 1
446 Traceback (most recent call last):
447 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000448 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000449
450 >>> if 1:
451 ... x() = 1
452 ... elif 1:
453 ... pass
454 ... else:
455 ... pass
456 Traceback (most recent call last):
457 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000458 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000459
460 >>> if 1:
461 ... pass
462 ... elif 1:
463 ... x() = 1
464 ... else:
465 ... pass
466 Traceback (most recent call last):
467 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000468 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000469
470 >>> if 1:
471 ... pass
472 ... elif 1:
473 ... pass
474 ... else:
475 ... x() = 1
476 Traceback (most recent call last):
477 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000478 SyntaxError: can't assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000479
Collin Winter828f04a2007-08-31 00:04:24 +0000480Make sure that the old "raise X, Y[, Z]" form is gone:
481 >>> raise X, Y
482 Traceback (most recent call last):
483 ...
484 SyntaxError: invalid syntax
485 >>> raise X, Y, Z
486 Traceback (most recent call last):
487 ...
488 SyntaxError: invalid syntax
489
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000490
491>>> f(a=23, a=234)
492Traceback (most recent call last):
493 ...
494SyntaxError: keyword argument repeated
495
Benjamin Peterson78565b22009-06-28 19:19:51 +0000496>>> del ()
497Traceback (most recent call last):
Benjamin Peterson78565b22009-06-28 19:19:51 +0000498SyntaxError: can't delete ()
499
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000500>>> {1, 2, 3} = 42
501Traceback (most recent call last):
502SyntaxError: can't assign to literal
503
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000504Corner-cases that used to fail to raise the correct error:
505
506 >>> def f(*, x=lambda __debug__:0): pass
507 Traceback (most recent call last):
508 SyntaxError: assignment to keyword
509
510 >>> def f(*args:(lambda __debug__:0)): pass
511 Traceback (most recent call last):
512 SyntaxError: assignment to keyword
513
514 >>> def f(**kwargs:(lambda __debug__:0)): pass
515 Traceback (most recent call last):
516 SyntaxError: assignment to keyword
517
518 >>> with (lambda *:0): pass
519 Traceback (most recent call last):
520 SyntaxError: named arguments must follow bare *
521
522Corner-cases that used to crash:
523
524 >>> def f(**__debug__): pass
525 Traceback (most recent call last):
526 SyntaxError: assignment to keyword
527
528 >>> def f(*xx, __debug__): pass
529 Traceback (most recent call last):
530 SyntaxError: assignment to keyword
531
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000532"""
533
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000534import re
535import unittest
Jeremy Hylton42d90162003-07-15 20:24:27 +0000536import warnings
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000537
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000538from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000539
540class SyntaxTestCase(unittest.TestCase):
541
542 def _check_error(self, code, errtext,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000543 filename="<testcase>", mode="exec", subclass=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000544 """Check that compiling code raises SyntaxError with errtext.
545
546 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000547 test of the exception raised. If subclass is specified it
548 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000549 """
550 try:
551 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000552 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000553 if subclass and not isinstance(err, subclass):
554 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000555 mo = re.search(errtext, str(err))
556 if mo is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000557 self.fail("SyntaxError did not contain '%r'" % (errtext,))
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000558 else:
559 self.fail("compile() did not raise SyntaxError")
560
561 def test_assign_call(self):
562 self._check_error("f() = 1", "assign")
563
564 def test_assign_del(self):
565 self._check_error("del f()", "delete")
566
Jeremy Hylton42d90162003-07-15 20:24:27 +0000567 def test_global_err_then_warn(self):
568 # Bug tickler: The SyntaxError raised for one global statement
569 # shouldn't be clobbered by a SyntaxWarning issued for a later one.
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000570 source = """if 1:
571 def error(a):
572 global a # SyntaxError
573 def warning():
574 b = 1
575 global b # SyntaxWarning
576 """
Jeremy Hylton42d90162003-07-15 20:24:27 +0000577 warnings.filterwarnings(action='ignore', category=SyntaxWarning)
578 self._check_error(source, "global")
579 warnings.filters.pop(0)
580
Neal Norwitzfcf44352005-11-27 20:37:43 +0000581 def test_break_outside_loop(self):
582 self._check_error("break", "outside loop")
583
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584 def test_unexpected_indent(self):
585 self._check_error("foo()\n bar()\n", "unexpected indent",
586 subclass=IndentationError)
587
588 def test_no_indent(self):
589 self._check_error("if 1:\nfoo()", "expected an indented block",
590 subclass=IndentationError)
591
592 def test_bad_outdent(self):
593 self._check_error("if 1:\n foo()\n bar()",
594 "unindent does not match .* level",
595 subclass=IndentationError)
596
597 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400598 self._check_error("int(base=10, '2')",
599 "positional argument follows keyword argument")
600
601 def test_kwargs_last2(self):
602 self._check_error("int(**{base: 10}, '2')",
603 "positional argument follows "
604 "keyword argument unpacking")
605
606 def test_kwargs_last3(self):
607 self._check_error("int(**{base: 10}, *['2'])",
608 "iterable argument unpacking follows "
609 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000610
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000611def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000612 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000613 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000614 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000615
616if __name__ == "__main__":
617 test_main()