blob: 7f7e6dafcf9008708646d3c3b00334ce9e9d862b [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
Jeremy Hyltonc960f262006-01-27 15:18:39 +000038>>> f() = 1
39Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000040SyntaxError: can't assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000041
42>>> del f()
43Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000044SyntaxError: can't delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000045
46>>> a + 1 = 2
47Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000048SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000049
50>>> (x for x in x) = 1
51Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000052SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000053
54>>> 1 = 1
55Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000056SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000057
58>>> "abc" = 1
59Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000060SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000061
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050062>>> b"" = 1
63Traceback (most recent call last):
64SyntaxError: can't assign to literal
65
Jeremy Hyltonc960f262006-01-27 15:18:39 +000066>>> `1` = 1
67Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +000068SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000069
70If the left-hand side of an assignment is a list or tuple, an illegal
71expression inside that contain should still cause a syntax error.
72This test just checks a couple of cases rather than enumerating all of
73them.
74
75>>> (a, "b", c) = (1, 2, 3)
76Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000077SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000078
79>>> [a, b, c + 1] = [1, 2, 3]
80Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000081SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000082
Thomas Wouters477c8d52006-05-27 19:21:47 +000083>>> a if 1 else b = 1
84Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +000085SyntaxError: can't assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000086
87From compiler_complex_args():
88
89>>> def f(None=1):
90... pass
91Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +000092SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000093
94
95From ast_for_arguments():
96
97>>> def f(x, y=1, z):
98... pass
99Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000100SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000101
102>>> def f(x, None):
103... pass
104Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000105SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000106
107>>> def f(*None):
108... pass
109Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000110SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000111
112>>> def f(**None):
113... pass
114Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000115SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000116
117
118From ast_for_funcdef():
119
120>>> def None(x):
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_call():
127
128>>> def f(it, *varargs):
129... return list(it)
130>>> L = range(10)
131>>> f(x for x in L)
132[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
133>>> f(x for x in L, 1)
134Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000135SyntaxError: Generator expression must be parenthesized if not sole argument
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400136>>> f(x for x in L, y for y in L)
137Traceback (most recent call last):
138SyntaxError: Generator expression must be parenthesized if not sole argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000139>>> f((x for x in L), 1)
140[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
141
142>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
143... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
144... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
145... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
146... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
147... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
148... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
149... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
150... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
151... i100, i101, i102, i103, i104, i105, i106, i107, i108,
152... i109, i110, i111, i112, i113, i114, i115, i116, i117,
153... i118, i119, i120, i121, i122, i123, i124, i125, i126,
154... i127, i128, i129, i130, i131, i132, i133, i134, i135,
155... i136, i137, i138, i139, i140, i141, i142, i143, i144,
156... i145, i146, i147, i148, i149, i150, i151, i152, i153,
157... i154, i155, i156, i157, i158, i159, i160, i161, i162,
158... i163, i164, i165, i166, i167, i168, i169, i170, i171,
159... i172, i173, i174, i175, i176, i177, i178, i179, i180,
160... i181, i182, i183, i184, i185, i186, i187, i188, i189,
161... i190, i191, i192, i193, i194, i195, i196, i197, i198,
162... i199, i200, i201, i202, i203, i204, i205, i206, i207,
163... i208, i209, i210, i211, i212, i213, i214, i215, i216,
164... i217, i218, i219, i220, i221, i222, i223, i224, i225,
165... i226, i227, i228, i229, i230, i231, i232, i233, i234,
166... i235, i236, i237, i238, i239, i240, i241, i242, i243,
167... i244, i245, i246, i247, i248, i249, i250, i251, i252,
168... i253, i254, i255)
169Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000170SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000171
172The actual error cases counts positional arguments, keyword arguments,
173and generator expression arguments separately. This test combines the
174three.
175
176>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
177... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
178... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
179... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
180... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
181... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
182... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
183... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
184... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
185... i100, i101, i102, i103, i104, i105, i106, i107, i108,
186... i109, i110, i111, i112, i113, i114, i115, i116, i117,
187... i118, i119, i120, i121, i122, i123, i124, i125, i126,
188... i127, i128, i129, i130, i131, i132, i133, i134, i135,
189... i136, i137, i138, i139, i140, i141, i142, i143, i144,
190... i145, i146, i147, i148, i149, i150, i151, i152, i153,
191... i154, i155, i156, i157, i158, i159, i160, i161, i162,
192... i163, i164, i165, i166, i167, i168, i169, i170, i171,
193... i172, i173, i174, i175, i176, i177, i178, i179, i180,
194... i181, i182, i183, i184, i185, i186, i187, i188, i189,
195... i190, i191, i192, i193, i194, i195, i196, i197, i198,
196... i199, i200, i201, i202, i203, i204, i205, i206, i207,
197... i208, i209, i210, i211, i212, i213, i214, i215, i216,
198... i217, i218, i219, i220, i221, i222, i223, i224, i225,
199... i226, i227, i228, i229, i230, i231, i232, i233, i234,
200... i235, i236, i237, i238, i239, i240, i241, i242, i243,
201... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
202... i252=1, i253=1, i254=1, i255=1)
203Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000204SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000205
206>>> f(lambda x: x[0] = 3)
207Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000208SyntaxError: lambda cannot contain assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000209
210The grammar accepts any test (basically, any expression) in the
211keyword slot of a call site. Test a few different options.
212
213>>> f(x()=2)
214Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000215SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000216>>> f(a or b=1)
217Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000218SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000219>>> f(x.y=1)
220Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000221SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000222
223
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000224More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000225
226>>> (x for x in x) += 1
227Traceback (most recent call last):
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000228SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000229>>> None += 1
230Traceback (most recent call last):
Benjamin Peterson442f2092012-12-06 17:41:04 -0500231SyntaxError: can't assign to keyword
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000232>>> f() += 1
233Traceback (most recent call last):
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000234SyntaxError: can't assign to function call
Thomas Wouters89f507f2006-12-13 04:49:30 +0000235
236
237Test continue in finally in weird combinations.
238
Ezio Melotti13925002011-03-16 11:05:33 +0200239continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000240
241 >>> def test():
242 ... try:
243 ... pass
244 ... finally:
245 ... for abc in range(10):
246 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000247 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248 >>> test()
249 9
250
251Start simple, a continue in a finally should not be allowed.
252
253 >>> def test():
254 ... for abc in range(10):
255 ... try:
256 ... pass
257 ... finally:
258 ... continue
259 Traceback (most recent call last):
260 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000261 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000262
263This is essentially a continue in a finally which should not be allowed.
264
265 >>> def test():
266 ... for abc in range(10):
267 ... try:
268 ... pass
269 ... finally:
270 ... try:
271 ... continue
272 ... except:
273 ... pass
274 Traceback (most recent call last):
275 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000276 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277
278 >>> def foo():
279 ... try:
280 ... pass
281 ... finally:
282 ... continue
283 Traceback (most recent call last):
284 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000285 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000286
287 >>> def foo():
288 ... for a in ():
289 ... try:
290 ... pass
291 ... finally:
292 ... continue
293 Traceback (most recent call last):
294 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000295 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000296
297 >>> def foo():
298 ... for a in ():
299 ... try:
300 ... pass
301 ... finally:
302 ... try:
303 ... continue
304 ... finally:
305 ... pass
306 Traceback (most recent call last):
307 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000308 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000309
310 >>> def foo():
311 ... for a in ():
312 ... try: pass
313 ... finally:
314 ... try:
315 ... pass
316 ... except:
317 ... continue
318 Traceback (most recent call last):
319 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000320 SyntaxError: 'continue' not supported inside 'finally' clause
Thomas Wouters89f507f2006-12-13 04:49:30 +0000321
322There is one test for a break that is not in a loop. The compiler
323uses a single data structure to keep track of try-finally and loops,
324so we need to be sure that a break is actually inside a loop. If it
325isn't, there should be a syntax error.
326
327 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000328 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000329 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000330 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000331 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000332 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000333 Traceback (most recent call last):
334 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000335 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000336
Benjamin Petersone09ed542016-07-14 22:00:03 -0700337This raises a SyntaxError, it used to raise a SystemError.
338Context for this change can be found on issue #27514
339
Thomas Wouters89f507f2006-12-13 04:49:30 +0000340In 2.5 there was a missing exception and an assert was triggered in a debug
341build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
342
343 >>> while 1:
344 ... while 2:
345 ... while 3:
346 ... while 4:
347 ... while 5:
348 ... while 6:
349 ... while 8:
350 ... while 9:
351 ... while 10:
352 ... while 11:
353 ... while 12:
354 ... while 13:
355 ... while 14:
356 ... while 15:
357 ... while 16:
358 ... while 17:
359 ... while 18:
360 ... while 19:
361 ... while 20:
362 ... while 21:
363 ... while 22:
364 ... break
365 Traceback (most recent call last):
366 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700367 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000368
Guido van Rossum6cff8742016-09-09 09:36:26 -0700369Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
370
371 >>> def f():
372 ... x = 1
373 ... global x
374 Traceback (most recent call last):
375 ...
376 SyntaxError: name 'x' is assigned to before global declaration
377
378 >>> def f():
379 ... x = 1
380 ... def g():
381 ... print(x)
382 ... nonlocal x
383 Traceback (most recent call last):
384 ...
385 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000386
387 >>> def f(x):
388 ... nonlocal x
389 Traceback (most recent call last):
390 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000391 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000392
Jeremy Hylton81e95022007-02-27 06:50:52 +0000393 >>> def f():
394 ... global x
395 ... nonlocal x
396 Traceback (most recent call last):
397 ...
398 SyntaxError: name 'x' is nonlocal and global
399
400 >>> def f():
401 ... nonlocal x
402 Traceback (most recent call last):
403 ...
404 SyntaxError: no binding for nonlocal 'x' found
405
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000406From SF bug #1705365
407 >>> nonlocal x
408 Traceback (most recent call last):
409 ...
410 SyntaxError: nonlocal declaration not allowed at module level
411
Jeremy Hylton81e95022007-02-27 06:50:52 +0000412TODO(jhylton): Figure out how to test SyntaxWarning with doctest.
413
414## >>> def f(x):
415## ... def f():
416## ... print(x)
417## ... nonlocal x
418## Traceback (most recent call last):
419## ...
420## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
Guido van Rossumd8faa362007-04-27 19:54:29 +0000421
Jeremy Hylton81e95022007-02-27 06:50:52 +0000422## >>> def f():
423## ... x = 1
424## ... nonlocal x
425## Traceback (most recent call last):
426## ...
427## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
428
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600429 From https://bugs.python.org/issue25973
430 >>> class A:
431 ... def f(self):
432 ... nonlocal __x
433 Traceback (most recent call last):
434 ...
435 SyntaxError: no binding for nonlocal '_A__x' found
436
Guido van Rossumd8faa362007-04-27 19:54:29 +0000437
438This tests assignment-context; there was a bug in Python 2.5 where compiling
439a complex 'if' (one with 'elif') would fail to notice an invalid suite,
440leading to spurious errors.
441
442 >>> if 1:
443 ... x() = 1
444 ... elif 1:
445 ... pass
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 ... pass
452 ... elif 1:
453 ... x() = 1
454 Traceback (most recent call last):
455 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000456 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000457
458 >>> if 1:
459 ... x() = 1
460 ... elif 1:
461 ... pass
462 ... else:
463 ... pass
464 Traceback (most recent call last):
465 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000466 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000467
468 >>> if 1:
469 ... pass
470 ... elif 1:
471 ... x() = 1
472 ... else:
473 ... pass
474 Traceback (most recent call last):
475 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000476 SyntaxError: can't assign to function call
Guido van Rossumd8faa362007-04-27 19:54:29 +0000477
478 >>> if 1:
479 ... pass
480 ... elif 1:
481 ... pass
482 ... else:
483 ... x() = 1
484 Traceback (most recent call last):
485 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000486 SyntaxError: can't assign to function call
Jeremy Hylton81e95022007-02-27 06:50:52 +0000487
Collin Winter828f04a2007-08-31 00:04:24 +0000488Make sure that the old "raise X, Y[, Z]" form is gone:
489 >>> raise X, Y
490 Traceback (most recent call last):
491 ...
492 SyntaxError: invalid syntax
493 >>> raise X, Y, Z
494 Traceback (most recent call last):
495 ...
496 SyntaxError: invalid syntax
497
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000498
499>>> f(a=23, a=234)
500Traceback (most recent call last):
501 ...
502SyntaxError: keyword argument repeated
503
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000504>>> {1, 2, 3} = 42
505Traceback (most recent call last):
506SyntaxError: can't assign to literal
507
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +0000508Corner-cases that used to fail to raise the correct error:
509
510 >>> def f(*, x=lambda __debug__:0): pass
511 Traceback (most recent call last):
512 SyntaxError: assignment to keyword
513
514 >>> def f(*args:(lambda __debug__:0)): pass
515 Traceback (most recent call last):
516 SyntaxError: assignment to keyword
517
518 >>> def f(**kwargs:(lambda __debug__:0)): pass
519 Traceback (most recent call last):
520 SyntaxError: assignment to keyword
521
522 >>> with (lambda *:0): pass
523 Traceback (most recent call last):
524 SyntaxError: named arguments must follow bare *
525
526Corner-cases that used to crash:
527
528 >>> def f(**__debug__): pass
529 Traceback (most recent call last):
530 SyntaxError: assignment to keyword
531
532 >>> def f(*xx, __debug__): pass
533 Traceback (most recent call last):
534 SyntaxError: assignment to keyword
535
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000536"""
537
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000538import re
539import unittest
Jeremy Hylton42d90162003-07-15 20:24:27 +0000540import warnings
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000541
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000542from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000543
544class SyntaxTestCase(unittest.TestCase):
545
546 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200547 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000548 """Check that compiling code raises SyntaxError with errtext.
549
550 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551 test of the exception raised. If subclass is specified it
552 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000553 """
554 try:
555 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +0000556 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000557 if subclass and not isinstance(err, subclass):
558 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000559 mo = re.search(errtext, str(err))
560 if mo is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000561 self.fail("SyntaxError did not contain '%r'" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200562 self.assertEqual(err.filename, filename)
563 if lineno is not None:
564 self.assertEqual(err.lineno, lineno)
565 if offset is not None:
566 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000567 else:
568 self.fail("compile() did not raise SyntaxError")
569
570 def test_assign_call(self):
571 self._check_error("f() = 1", "assign")
572
573 def test_assign_del(self):
574 self._check_error("del f()", "delete")
575
Jeremy Hylton42d90162003-07-15 20:24:27 +0000576 def test_global_err_then_warn(self):
Serhiy Storchaka8b583392016-12-11 14:39:01 +0200577 # Bug #763201: The SyntaxError raised for one global statement
Jeremy Hylton42d90162003-07-15 20:24:27 +0000578 # shouldn't be clobbered by a SyntaxWarning issued for a later one.
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +0000579 source = """if 1:
580 def error(a):
581 global a # SyntaxError
582 def warning():
583 b = 1
584 global b # SyntaxWarning
585 """
Jeremy Hylton42d90162003-07-15 20:24:27 +0000586 warnings.filterwarnings(action='ignore', category=SyntaxWarning)
587 self._check_error(source, "global")
588 warnings.filters.pop(0)
589
Neal Norwitzfcf44352005-11-27 20:37:43 +0000590 def test_break_outside_loop(self):
591 self._check_error("break", "outside loop")
592
Thomas Wouters477c8d52006-05-27 19:21:47 +0000593 def test_unexpected_indent(self):
594 self._check_error("foo()\n bar()\n", "unexpected indent",
595 subclass=IndentationError)
596
597 def test_no_indent(self):
598 self._check_error("if 1:\nfoo()", "expected an indented block",
599 subclass=IndentationError)
600
601 def test_bad_outdent(self):
602 self._check_error("if 1:\n foo()\n bar()",
603 "unindent does not match .* level",
604 subclass=IndentationError)
605
606 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400607 self._check_error("int(base=10, '2')",
608 "positional argument follows keyword argument")
609
610 def test_kwargs_last2(self):
611 self._check_error("int(**{base: 10}, '2')",
612 "positional argument follows "
613 "keyword argument unpacking")
614
615 def test_kwargs_last3(self):
616 self._check_error("int(**{base: 10}, *['2'])",
617 "iterable argument unpacking follows "
618 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000619
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000620def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000621 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000622 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000623 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000624
625if __name__ == "__main__":
626 test_main()