blob: 794564a7c1a1eaa79f5ecfe81c295793c0a54847 [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):
Benjamin Peterson08473322008-08-16 22:11:33 +00008SyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1)
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):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +000032SyntaxError: cannot assign to None (<doctest test.test_syntax[1]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +000033
34>>> None = 1
35Traceback (most recent call last):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +000036SyntaxError: cannot assign to None (<doctest test.test_syntax[2]>, line 1)
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):
44SyntaxError: can't assign to () (<doctest test.test_syntax[3]>, line 1)
45
46>>> f() = 1
47Traceback (most recent call last):
48SyntaxError: can't assign to function call (<doctest test.test_syntax[4]>, line 1)
49
50>>> del f()
51Traceback (most recent call last):
52SyntaxError: can't delete function call (<doctest test.test_syntax[5]>, line 1)
53
54>>> a + 1 = 2
55Traceback (most recent call last):
56SyntaxError: can't assign to operator (<doctest test.test_syntax[6]>, line 1)
57
58>>> (x for x in x) = 1
59Traceback (most recent call last):
60SyntaxError: can't assign to generator expression (<doctest test.test_syntax[7]>, line 1)
61
62>>> 1 = 1
63Traceback (most recent call last):
64SyntaxError: can't assign to literal (<doctest test.test_syntax[8]>, line 1)
65
66>>> "abc" = 1
67Traceback (most recent call last):
68SyntaxError: can't assign to literal (<doctest test.test_syntax[9]>, line 1)
69
70>>> `1` = 1
71Traceback (most recent call last):
72SyntaxError: can't assign to repr (<doctest test.test_syntax[10]>, line 1)
73
74If the left-hand side of an assignment is a list or tuple, an illegal
75expression inside that contain should still cause a syntax error.
76This test just checks a couple of cases rather than enumerating all of
77them.
78
79>>> (a, "b", c) = (1, 2, 3)
80Traceback (most recent call last):
81SyntaxError: can't assign to literal (<doctest test.test_syntax[11]>, line 1)
82
83>>> [a, b, c + 1] = [1, 2, 3]
84Traceback (most recent call last):
85SyntaxError: can't assign to operator (<doctest test.test_syntax[12]>, line 1)
86
Neal Norwitz373f0a72006-05-15 07:04:36 +000087>>> a if 1 else b = 1
88Traceback (most recent call last):
89SyntaxError: can't assign to conditional expression (<doctest test.test_syntax[13]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +000090
91From compiler_complex_args():
92
93>>> def f(None=1):
94... pass
95Traceback (most recent call last):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +000096SyntaxError: cannot assign to None (<doctest test.test_syntax[14]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +000097
98
99From ast_for_arguments():
100
101>>> def f(x, y=1, z):
102... pass
103Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000104SyntaxError: non-default argument follows default argument (<doctest test.test_syntax[15]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000105
106>>> def f(x, None):
107... pass
108Traceback (most recent call last):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000109SyntaxError: cannot assign to None (<doctest test.test_syntax[16]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000110
111>>> def f(*None):
112... pass
113Traceback (most recent call last):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000114SyntaxError: cannot assign to None (<doctest test.test_syntax[17]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000115
116>>> def f(**None):
117... pass
118Traceback (most recent call last):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000119SyntaxError: cannot assign to None (<doctest test.test_syntax[18]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000120
121
122From ast_for_funcdef():
123
124>>> def None(x):
125... pass
126Traceback (most recent call last):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000127SyntaxError: cannot assign to None (<doctest test.test_syntax[19]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000128
129
130From ast_for_call():
131
132>>> def f(it, *varargs):
133... return list(it)
134>>> L = range(10)
135>>> f(x for x in L)
136[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
137>>> f(x for x in L, 1)
138Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000139SyntaxError: Generator expression must be parenthesized if not sole argument (<doctest test.test_syntax[23]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000140>>> f((x for x in L), 1)
141[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
142
143>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
144... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
145... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
146... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
147... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
148... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
149... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
150... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
151... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
152... i100, i101, i102, i103, i104, i105, i106, i107, i108,
153... i109, i110, i111, i112, i113, i114, i115, i116, i117,
154... i118, i119, i120, i121, i122, i123, i124, i125, i126,
155... i127, i128, i129, i130, i131, i132, i133, i134, i135,
156... i136, i137, i138, i139, i140, i141, i142, i143, i144,
157... i145, i146, i147, i148, i149, i150, i151, i152, i153,
158... i154, i155, i156, i157, i158, i159, i160, i161, i162,
159... i163, i164, i165, i166, i167, i168, i169, i170, i171,
160... i172, i173, i174, i175, i176, i177, i178, i179, i180,
161... i181, i182, i183, i184, i185, i186, i187, i188, i189,
162... i190, i191, i192, i193, i194, i195, i196, i197, i198,
163... i199, i200, i201, i202, i203, i204, i205, i206, i207,
164... i208, i209, i210, i211, i212, i213, i214, i215, i216,
165... i217, i218, i219, i220, i221, i222, i223, i224, i225,
166... i226, i227, i228, i229, i230, i231, i232, i233, i234,
167... i235, i236, i237, i238, i239, i240, i241, i242, i243,
168... i244, i245, i246, i247, i248, i249, i250, i251, i252,
169... i253, i254, i255)
170Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000171SyntaxError: more than 255 arguments (<doctest test.test_syntax[25]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000172
173The actual error cases counts positional arguments, keyword arguments,
174and generator expression arguments separately. This test combines the
175three.
176
177>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
178... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
179... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
180... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
181... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
182... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
183... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
184... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
185... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
186... i100, i101, i102, i103, i104, i105, i106, i107, i108,
187... i109, i110, i111, i112, i113, i114, i115, i116, i117,
188... i118, i119, i120, i121, i122, i123, i124, i125, i126,
189... i127, i128, i129, i130, i131, i132, i133, i134, i135,
190... i136, i137, i138, i139, i140, i141, i142, i143, i144,
191... i145, i146, i147, i148, i149, i150, i151, i152, i153,
192... i154, i155, i156, i157, i158, i159, i160, i161, i162,
193... i163, i164, i165, i166, i167, i168, i169, i170, i171,
194... i172, i173, i174, i175, i176, i177, i178, i179, i180,
195... i181, i182, i183, i184, i185, i186, i187, i188, i189,
196... i190, i191, i192, i193, i194, i195, i196, i197, i198,
197... i199, i200, i201, i202, i203, i204, i205, i206, i207,
198... i208, i209, i210, i211, i212, i213, i214, i215, i216,
199... i217, i218, i219, i220, i221, i222, i223, i224, i225,
200... i226, i227, i228, i229, i230, i231, i232, i233, i234,
201... i235, i236, i237, i238, i239, i240, i241, i242, i243,
202... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
203... i252=1, i253=1, i254=1, i255=1)
204Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000205SyntaxError: more than 255 arguments (<doctest test.test_syntax[26]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000206
207>>> f(lambda x: x[0] = 3)
208Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000209SyntaxError: lambda cannot contain assignment (<doctest test.test_syntax[27]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000210
211The grammar accepts any test (basically, any expression) in the
212keyword slot of a call site. Test a few different options.
213
214>>> f(x()=2)
215Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000216SyntaxError: keyword can't be an expression (<doctest test.test_syntax[28]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000217>>> f(a or b=1)
218Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000219SyntaxError: keyword can't be an expression (<doctest test.test_syntax[29]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000220>>> f(x.y=1)
221Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000222SyntaxError: keyword can't be an expression (<doctest test.test_syntax[30]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000223
224
225From ast_for_expr_stmt():
226
227>>> (x for x in x) += 1
228Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000229SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_syntax[31]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000230>>> None += 1
231Traceback (most recent call last):
Benjamin Peterson2c98faa2008-11-08 18:38:54 +0000232SyntaxError: cannot assign to None (<doctest test.test_syntax[32]>, line 1)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000233>>> f() += 1
234Traceback (most recent call last):
Neal Norwitz373f0a72006-05-15 07:04:36 +0000235SyntaxError: illegal expression for augmented assignment (<doctest test.test_syntax[33]>, line 1)
Neal Norwitz4f096d92006-08-21 19:47:08 +0000236
237
238Test continue in finally in weird combinations.
239
240continue in for loop under finally shouuld be ok.
241
242 >>> def test():
243 ... try:
244 ... pass
245 ... finally:
246 ... for abc in range(10):
247 ... continue
248 ... print abc
249 >>> test()
250 9
251
252Start simple, a continue in a finally should not be allowed.
253
254 >>> def test():
255 ... for abc in range(10):
256 ... try:
257 ... pass
258 ... finally:
259 ... continue
260 Traceback (most recent call last):
261 ...
262 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[36]>, line 6)
263
264This is essentially a continue in a finally which should not be allowed.
265
266 >>> def test():
267 ... for abc in range(10):
268 ... try:
269 ... pass
270 ... finally:
271 ... try:
272 ... continue
273 ... except:
274 ... pass
275 Traceback (most recent call last):
276 ...
277 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[37]>, line 7)
278
279 >>> def foo():
280 ... try:
281 ... pass
282 ... finally:
283 ... continue
284 Traceback (most recent call last):
285 ...
286 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[38]>, line 5)
287
288 >>> def foo():
289 ... for a in ():
290 ... try:
291 ... pass
292 ... finally:
293 ... continue
294 Traceback (most recent call last):
295 ...
296 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[39]>, line 6)
297
298 >>> def foo():
299 ... for a in ():
300 ... try:
301 ... pass
302 ... finally:
303 ... try:
304 ... continue
305 ... finally:
306 ... pass
307 Traceback (most recent call last):
308 ...
309 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[40]>, line 7)
310
311 >>> def foo():
312 ... for a in ():
313 ... try: pass
314 ... finally:
315 ... try:
316 ... pass
317 ... except:
318 ... continue
319 Traceback (most recent call last):
320 ...
321 SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[41]>, line 8)
322
Jeremy Hylton82271f12006-10-04 02:24:52 +0000323There is one test for a break that is not in a loop. The compiler
324uses a single data structure to keep track of try-finally and loops,
325so we need to be sure that a break is actually inside a loop. If it
326isn't, there should be a syntax error.
327
328 >>> try:
329 ... print 1
330 ... break
331 ... print 2
332 ... finally:
333 ... print 3
334 Traceback (most recent call last):
335 ...
336 SyntaxError: 'break' outside loop (<doctest test.test_syntax[42]>, line 3)
Neal Norwitz21997af2006-10-28 21:19:07 +0000337
338This should probably raise a better error than a SystemError (or none at all).
339In 2.5 there was a missing exception and an assert was triggered in a debug
340build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
341
342 >>> while 1:
343 ... while 2:
344 ... while 3:
345 ... while 4:
346 ... while 5:
347 ... while 6:
348 ... while 8:
349 ... while 9:
350 ... while 10:
351 ... while 11:
352 ... while 12:
353 ... while 13:
354 ... while 14:
355 ... while 15:
356 ... while 16:
357 ... while 17:
358 ... while 18:
359 ... while 19:
360 ... while 20:
361 ... while 21:
362 ... while 22:
363 ... break
364 Traceback (most recent call last):
365 ...
366 SystemError: too many statically nested blocks
367
Collin Winter77c67bd2007-03-16 04:11:30 +0000368This tests assignment-context; there was a bug in Python 2.5 where compiling
369a complex 'if' (one with 'elif') would fail to notice an invalid suite,
370leading to spurious errors.
371
372 >>> if 1:
373 ... x() = 1
374 ... elif 1:
375 ... pass
376 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000377 ...
Collin Winter77c67bd2007-03-16 04:11:30 +0000378 SyntaxError: can't assign to function call (<doctest test.test_syntax[44]>, line 2)
379
380 >>> if 1:
381 ... pass
382 ... elif 1:
383 ... x() = 1
384 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000385 ...
Collin Winter77c67bd2007-03-16 04:11:30 +0000386 SyntaxError: can't assign to function call (<doctest test.test_syntax[45]>, line 4)
387
388 >>> if 1:
389 ... x() = 1
390 ... elif 1:
391 ... pass
392 ... else:
393 ... pass
394 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000395 ...
Collin Winter77c67bd2007-03-16 04:11:30 +0000396 SyntaxError: can't assign to function call (<doctest test.test_syntax[46]>, line 2)
397
398 >>> if 1:
399 ... pass
400 ... elif 1:
401 ... x() = 1
402 ... else:
403 ... pass
404 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000405 ...
Collin Winter77c67bd2007-03-16 04:11:30 +0000406 SyntaxError: can't assign to function call (<doctest test.test_syntax[47]>, line 4)
407
408 >>> if 1:
409 ... pass
410 ... elif 1:
411 ... pass
412 ... else:
413 ... x() = 1
414 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000415 ...
Collin Winter77c67bd2007-03-16 04:11:30 +0000416 SyntaxError: can't assign to function call (<doctest test.test_syntax[48]>, line 6)
417
Benjamin Peterson175e4d92008-07-01 19:34:52 +0000418>>> f(a=23, a=234)
419Traceback (most recent call last):
420 ...
421SyntaxError: keyword argument repeated (<doctest test.test_syntax[49]>, line 1)
422
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000423"""
424
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000425import re
426import unittest
Jeremy Hylton42d90162003-07-15 20:24:27 +0000427import warnings
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000428
Barry Warsaw04f357c2002-07-23 19:04:11 +0000429from test import test_support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000430
431class SyntaxTestCase(unittest.TestCase):
432
433 def _check_error(self, code, errtext,
Martin v. Löwis77736712006-05-04 05:51:03 +0000434 filename="<testcase>", mode="exec", subclass=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000435 """Check that compiling code raises SyntaxError with errtext.
436
437 errtest is a regular expression that must be present in the
Martin v. Löwis77736712006-05-04 05:51:03 +0000438 test of the exception raised. If subclass is specified it
439 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000440 """
441 try:
442 compile(code, filename, mode)
443 except SyntaxError, err:
Martin v. Löwis77736712006-05-04 05:51:03 +0000444 if subclass and not isinstance(err, subclass):
445 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000446 mo = re.search(errtext, str(err))
447 if mo is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000448 self.fail("SyntaxError did not contain '%r'" % (errtext,))
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000449 else:
450 self.fail("compile() did not raise SyntaxError")
451
452 def test_assign_call(self):
453 self._check_error("f() = 1", "assign")
454
455 def test_assign_del(self):
456 self._check_error("del f()", "delete")
457
Jeremy Hylton42d90162003-07-15 20:24:27 +0000458 def test_global_err_then_warn(self):
459 # Bug tickler: The SyntaxError raised for one global statement
460 # shouldn't be clobbered by a SyntaxWarning issued for a later one.
461 source = re.sub('(?m)^ *:', '', """\
462 :def error(a):
463 : global a # SyntaxError
464 :def warning():
465 : b = 1
466 : global b # SyntaxWarning
467 :""")
468 warnings.filterwarnings(action='ignore', category=SyntaxWarning)
469 self._check_error(source, "global")
470 warnings.filters.pop(0)
471
Neal Norwitzfcf44352005-11-27 20:37:43 +0000472 def test_break_outside_loop(self):
473 self._check_error("break", "outside loop")
474
475 def test_delete_deref(self):
476 source = re.sub('(?m)^ *:', '', """\
477 :def foo(x):
478 : def bar():
479 : print x
480 : del x
481 :""")
482 self._check_error(source, "nested scope")
483
Martin v. Löwis77736712006-05-04 05:51:03 +0000484 def test_unexpected_indent(self):
485 self._check_error("foo()\n bar()\n", "unexpected indent",
486 subclass=IndentationError)
487
488 def test_no_indent(self):
489 self._check_error("if 1:\nfoo()", "expected an indented block",
490 subclass=IndentationError)
491
492 def test_bad_outdent(self):
493 self._check_error("if 1:\n foo()\n bar()",
494 "unindent does not match .* level",
495 subclass=IndentationError)
496
Neal Norwitz5ef92242006-05-19 06:43:50 +0000497 def test_kwargs_last(self):
498 self._check_error("int(base=10, '2')", "non-keyword arg")
499
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000500def test_main():
501 test_support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000502 from test import test_syntax
503 test_support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000504
505if __name__ == "__main__":
506 test_main()