blob: 0a61462a608557df83556ae60c851d4ff406f597 [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 Peterson52b96202009-04-07 15:15:04 +000032 File "<doctest test.test_syntax[1]>", line 1
33SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +000034
35>>> None = 1
36Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000037 File "<doctest test.test_syntax[2]>", line 1
38SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +000039
40It's a syntax error to assign to the empty tuple. Why isn't it an
41error to assign to the empty list? It will always raise some error at
42runtime.
43
44>>> () = 1
45Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000046 File "<doctest test.test_syntax[3]>", line 1
47SyntaxError: can't assign to ()
Jeremy Hyltonc960f262006-01-27 15:18:39 +000048
49>>> f() = 1
50Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000051 File "<doctest test.test_syntax[4]>", line 1
52SyntaxError: can't assign to function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000053
54>>> del f()
55Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000056 File "<doctest test.test_syntax[5]>", line 1
57SyntaxError: can't delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000058
59>>> a + 1 = 2
60Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000061 File "<doctest test.test_syntax[6]>", line 1
62SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000063
64>>> (x for x in x) = 1
65Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000066 File "<doctest test.test_syntax[7]>", line 1
67SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000068
69>>> 1 = 1
70Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000071 File "<doctest test.test_syntax[8]>", line 1
72SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000073
74>>> "abc" = 1
75Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000076 File "<doctest test.test_syntax[8]>", line 1
77SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000078
79>>> `1` = 1
80Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000081 File "<doctest test.test_syntax[10]>", line 1
82SyntaxError: can't assign to repr
Jeremy Hyltonc960f262006-01-27 15:18:39 +000083
84If the left-hand side of an assignment is a list or tuple, an illegal
85expression inside that contain should still cause a syntax error.
86This test just checks a couple of cases rather than enumerating all of
87them.
88
89>>> (a, "b", c) = (1, 2, 3)
90Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000091 File "<doctest test.test_syntax[11]>", line 1
92SyntaxError: can't assign to literal
Jeremy Hyltonc960f262006-01-27 15:18:39 +000093
94>>> [a, b, c + 1] = [1, 2, 3]
95Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +000096 File "<doctest test.test_syntax[12]>", line 1
97SyntaxError: can't assign to operator
Jeremy Hyltonc960f262006-01-27 15:18:39 +000098
Neal Norwitz373f0a72006-05-15 07:04:36 +000099>>> a if 1 else b = 1
100Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000101 File "<doctest test.test_syntax[13]>", line 1
102SyntaxError: can't assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000103
104From compiler_complex_args():
105
106>>> def f(None=1):
107... pass
108Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000109 File "<doctest test.test_syntax[14]>", line 1
110SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000111
112
113From ast_for_arguments():
114
115>>> def f(x, y=1, z):
116... pass
117Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000118 File "<doctest test.test_syntax[15]>", line 1
119SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000120
121>>> def f(x, None):
122... pass
123Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000124 File "<doctest test.test_syntax[16]>", line 1
125SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000126
127>>> def f(*None):
128... pass
129Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000130 File "<doctest test.test_syntax[17]>", line 1
131SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000132
133>>> def f(**None):
134... pass
135Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000136 File "<doctest test.test_syntax[18]>", line 1
137SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000138
139
140From ast_for_funcdef():
141
142>>> def None(x):
143... pass
144Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000145 File "<doctest test.test_syntax[19]>", line 1
146SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000147
148
149From ast_for_call():
150
151>>> def f(it, *varargs):
152... return list(it)
153>>> L = range(10)
154>>> f(x for x in L)
155[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
156>>> f(x for x in L, 1)
157Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000158 File "<doctest test.test_syntax[23]>", line 1
159SyntaxError: Generator expression must be parenthesized if not sole argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000160>>> f((x for x in L), 1)
161[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
162
163>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
164... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
165... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
166... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
167... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
168... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
169... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
170... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
171... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
172... i100, i101, i102, i103, i104, i105, i106, i107, i108,
173... i109, i110, i111, i112, i113, i114, i115, i116, i117,
174... i118, i119, i120, i121, i122, i123, i124, i125, i126,
175... i127, i128, i129, i130, i131, i132, i133, i134, i135,
176... i136, i137, i138, i139, i140, i141, i142, i143, i144,
177... i145, i146, i147, i148, i149, i150, i151, i152, i153,
178... i154, i155, i156, i157, i158, i159, i160, i161, i162,
179... i163, i164, i165, i166, i167, i168, i169, i170, i171,
180... i172, i173, i174, i175, i176, i177, i178, i179, i180,
181... i181, i182, i183, i184, i185, i186, i187, i188, i189,
182... i190, i191, i192, i193, i194, i195, i196, i197, i198,
183... i199, i200, i201, i202, i203, i204, i205, i206, i207,
184... i208, i209, i210, i211, i212, i213, i214, i215, i216,
185... i217, i218, i219, i220, i221, i222, i223, i224, i225,
186... i226, i227, i228, i229, i230, i231, i232, i233, i234,
187... i235, i236, i237, i238, i239, i240, i241, i242, i243,
188... i244, i245, i246, i247, i248, i249, i250, i251, i252,
189... i253, i254, i255)
190Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000191 File "<doctest test.test_syntax[25]>", line 1
192SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000193
194The actual error cases counts positional arguments, keyword arguments,
195and generator expression arguments separately. This test combines the
196three.
197
198>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
199... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
200... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
201... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
202... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
203... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
204... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
205... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
206... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
207... i100, i101, i102, i103, i104, i105, i106, i107, i108,
208... i109, i110, i111, i112, i113, i114, i115, i116, i117,
209... i118, i119, i120, i121, i122, i123, i124, i125, i126,
210... i127, i128, i129, i130, i131, i132, i133, i134, i135,
211... i136, i137, i138, i139, i140, i141, i142, i143, i144,
212... i145, i146, i147, i148, i149, i150, i151, i152, i153,
213... i154, i155, i156, i157, i158, i159, i160, i161, i162,
214... i163, i164, i165, i166, i167, i168, i169, i170, i171,
215... i172, i173, i174, i175, i176, i177, i178, i179, i180,
216... i181, i182, i183, i184, i185, i186, i187, i188, i189,
217... i190, i191, i192, i193, i194, i195, i196, i197, i198,
218... i199, i200, i201, i202, i203, i204, i205, i206, i207,
219... i208, i209, i210, i211, i212, i213, i214, i215, i216,
220... i217, i218, i219, i220, i221, i222, i223, i224, i225,
221... i226, i227, i228, i229, i230, i231, i232, i233, i234,
222... i235, i236, i237, i238, i239, i240, i241, i242, i243,
223... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
224... i252=1, i253=1, i254=1, i255=1)
225Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000226 File "<doctest test.test_syntax[26]>", line 1
227SyntaxError: more than 255 arguments
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000228
229>>> f(lambda x: x[0] = 3)
230Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000231 File "<doctest test.test_syntax[27]>", line 1
232SyntaxError: lambda cannot contain assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000233
234The grammar accepts any test (basically, any expression) in the
235keyword slot of a call site. Test a few different options.
236
237>>> f(x()=2)
238Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000239 File "<doctest test.test_syntax[28]>", line 1
240SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000241>>> f(a or b=1)
242Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000243 File "<doctest test.test_syntax[29]>", line 1
244SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000245>>> f(x.y=1)
246Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000247 File "<doctest test.test_syntax[30]>", line 1
248SyntaxError: keyword can't be an expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000249
250
Benjamin Petersona5a57282009-06-08 23:44:13 +0000251More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000252
253>>> (x for x in x) += 1
254Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000255 File "<doctest test.test_syntax[31]>", line 1
Benjamin Petersona5a57282009-06-08 23:44:13 +0000256SyntaxError: can't assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000257>>> None += 1
258Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000259 File "<doctest test.test_syntax[32]>", line 1
260SyntaxError: cannot assign to None
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000261>>> f() += 1
262Traceback (most recent call last):
Benjamin Peterson52b96202009-04-07 15:15:04 +0000263 File "<doctest test.test_syntax[33]>", line 1
Benjamin Petersona5a57282009-06-08 23:44:13 +0000264SyntaxError: can't assign to function call
Neal Norwitz4f096d92006-08-21 19:47:08 +0000265
266
267Test continue in finally in weird combinations.
268
Ezio Melottic2077b02011-03-16 12:34:31 +0200269continue in for loop under finally should be ok.
Neal Norwitz4f096d92006-08-21 19:47:08 +0000270
271 >>> def test():
272 ... try:
273 ... pass
274 ... finally:
275 ... for abc in range(10):
276 ... continue
277 ... print abc
278 >>> test()
279 9
280
281Start simple, a continue in a finally should not be allowed.
282
283 >>> def test():
284 ... for abc in range(10):
285 ... try:
286 ... pass
287 ... finally:
288 ... continue
289 Traceback (most recent call last):
290 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000291 File "<doctest test.test_syntax[36]>", line 6
292 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000293
294This is essentially a continue in a finally which should not be allowed.
295
296 >>> def test():
297 ... for abc in range(10):
298 ... try:
299 ... pass
300 ... finally:
301 ... try:
302 ... continue
303 ... except:
304 ... pass
305 Traceback (most recent call last):
306 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000307 File "<doctest test.test_syntax[37]>", line 6
308 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000309
310 >>> def foo():
311 ... try:
312 ... pass
313 ... finally:
314 ... continue
315 Traceback (most recent call last):
316 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000317 File "<doctest test.test_syntax[38]>", line 5
318 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000319
320 >>> def foo():
321 ... for a in ():
322 ... try:
323 ... pass
324 ... finally:
325 ... continue
326 Traceback (most recent call last):
327 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000328 File "<doctest test.test_syntax[39]>", line 6
329 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000330
331 >>> def foo():
332 ... for a in ():
333 ... try:
334 ... pass
335 ... finally:
336 ... try:
337 ... continue
338 ... finally:
339 ... pass
340 Traceback (most recent call last):
341 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000342 File "<doctest test.test_syntax[40]>", line 7
343 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000344
345 >>> def foo():
346 ... for a in ():
347 ... try: pass
348 ... finally:
349 ... try:
350 ... pass
351 ... except:
352 ... continue
353 Traceback (most recent call last):
354 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000355 File "<doctest test.test_syntax[41]>", line 8
356 SyntaxError: 'continue' not supported inside 'finally' clause
Neal Norwitz4f096d92006-08-21 19:47:08 +0000357
Jeremy Hylton82271f12006-10-04 02:24:52 +0000358There is one test for a break that is not in a loop. The compiler
359uses a single data structure to keep track of try-finally and loops,
360so we need to be sure that a break is actually inside a loop. If it
361isn't, there should be a syntax error.
362
363 >>> try:
364 ... print 1
365 ... break
366 ... print 2
367 ... finally:
368 ... print 3
369 Traceback (most recent call last):
370 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000371 File "<doctest test.test_syntax[42]>", line 3
372 SyntaxError: 'break' outside loop
Neal Norwitz21997af2006-10-28 21:19:07 +0000373
Benjamin Peterson6c4fa702016-07-14 22:00:03 -0700374This raises a SyntaxError, it used to raise a SystemError.
375Context for this change can be found on issue #27514
376
Neal Norwitz21997af2006-10-28 21:19:07 +0000377In 2.5 there was a missing exception and an assert was triggered in a debug
378build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
379
380 >>> while 1:
381 ... while 2:
382 ... while 3:
383 ... while 4:
384 ... while 5:
385 ... while 6:
386 ... while 8:
387 ... while 9:
388 ... while 10:
389 ... while 11:
390 ... while 12:
391 ... while 13:
392 ... while 14:
393 ... while 15:
394 ... while 16:
395 ... while 17:
396 ... while 18:
397 ... while 19:
398 ... while 20:
399 ... while 21:
400 ... while 22:
401 ... break
402 Traceback (most recent call last):
403 ...
Benjamin Peterson6c4fa702016-07-14 22:00:03 -0700404 SyntaxError: too many statically nested blocks
Neal Norwitz21997af2006-10-28 21:19:07 +0000405
Collin Winter77c67bd2007-03-16 04:11:30 +0000406This tests assignment-context; there was a bug in Python 2.5 where compiling
407a complex 'if' (one with 'elif') would fail to notice an invalid suite,
408leading to spurious errors.
409
410 >>> if 1:
411 ... x() = 1
412 ... elif 1:
413 ... pass
414 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000415 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000416 File "<doctest test.test_syntax[44]>", line 2
417 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000418
419 >>> if 1:
420 ... pass
421 ... elif 1:
422 ... x() = 1
423 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000424 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000425 File "<doctest test.test_syntax[45]>", line 4
426 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000427
428 >>> if 1:
429 ... x() = 1
430 ... elif 1:
431 ... pass
432 ... else:
433 ... pass
434 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000435 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000436 File "<doctest test.test_syntax[46]>", line 2
437 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000438
439 >>> if 1:
440 ... pass
441 ... elif 1:
442 ... x() = 1
443 ... else:
444 ... pass
445 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000446 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000447 File "<doctest test.test_syntax[47]>", line 4
448 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000449
450 >>> if 1:
451 ... pass
452 ... elif 1:
453 ... pass
454 ... else:
455 ... x() = 1
456 Traceback (most recent call last):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000457 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000458 File "<doctest test.test_syntax[48]>", line 6
459 SyntaxError: can't assign to function call
Collin Winter77c67bd2007-03-16 04:11:30 +0000460
Benjamin Peterson175e4d92008-07-01 19:34:52 +0000461>>> f(a=23, a=234)
462Traceback (most recent call last):
463 ...
Benjamin Peterson52b96202009-04-07 15:15:04 +0000464 File "<doctest test.test_syntax[49]>", line 1
465SyntaxError: keyword argument repeated
Benjamin Peterson175e4d92008-07-01 19:34:52 +0000466
Benjamin Peterson52c4bec2009-06-13 17:08:53 +0000467>>> del ()
468Traceback (most recent call last):
469 ...
470 File "<doctest test.test_syntax[50]>", line 1
471SyntaxError: can't delete ()
472
Benjamin Peterson947ce582010-06-24 00:12:40 +0000473>>> {1, 2, 3} = 42
474Traceback (most recent call last):
475 ...
476 File "<doctest test.test_syntax[50]>", line 1
477SyntaxError: can't assign to literal
478
Amaury Forgeot d'Arcb1147f52010-08-19 21:50:08 +0000479Corner-case that used to crash:
480
481 >>> def f(*xx, **__debug__): pass
482 Traceback (most recent call last):
483 SyntaxError: cannot assign to __debug__
484
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000485"""
486
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000487import re
488import unittest
Jeremy Hylton42d90162003-07-15 20:24:27 +0000489import warnings
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000490
Barry Warsaw04f357c2002-07-23 19:04:11 +0000491from test import test_support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000492
493class SyntaxTestCase(unittest.TestCase):
494
495 def _check_error(self, code, errtext,
Martin v. Löwis77736712006-05-04 05:51:03 +0000496 filename="<testcase>", mode="exec", subclass=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000497 """Check that compiling code raises SyntaxError with errtext.
498
499 errtest is a regular expression that must be present in the
Martin v. Löwis77736712006-05-04 05:51:03 +0000500 test of the exception raised. If subclass is specified it
501 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000502 """
503 try:
504 compile(code, filename, mode)
505 except SyntaxError, err:
Martin v. Löwis77736712006-05-04 05:51:03 +0000506 if subclass and not isinstance(err, subclass):
507 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000508 mo = re.search(errtext, str(err))
509 if mo is None:
Benjamin Peterson99a50232009-11-19 22:54:57 +0000510 self.fail("%s did not contain '%r'" % (err, errtext,))
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000511 else:
512 self.fail("compile() did not raise SyntaxError")
513
Benjamin Peterson99a50232009-11-19 22:54:57 +0000514 def test_paren_arg_with_default(self):
515 self._check_error("def f((x)=23): pass",
516 "parenthesized arg with default")
517
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000518 def test_assign_call(self):
519 self._check_error("f() = 1", "assign")
520
521 def test_assign_del(self):
522 self._check_error("del f()", "delete")
523
Jeremy Hylton42d90162003-07-15 20:24:27 +0000524 def test_global_err_then_warn(self):
525 # Bug tickler: The SyntaxError raised for one global statement
526 # shouldn't be clobbered by a SyntaxWarning issued for a later one.
527 source = re.sub('(?m)^ *:', '', """\
528 :def error(a):
529 : global a # SyntaxError
530 :def warning():
531 : b = 1
532 : global b # SyntaxWarning
533 :""")
534 warnings.filterwarnings(action='ignore', category=SyntaxWarning)
535 self._check_error(source, "global")
536 warnings.filters.pop(0)
537
Neal Norwitzfcf44352005-11-27 20:37:43 +0000538 def test_break_outside_loop(self):
539 self._check_error("break", "outside loop")
540
541 def test_delete_deref(self):
542 source = re.sub('(?m)^ *:', '', """\
543 :def foo(x):
544 : def bar():
545 : print x
546 : del x
547 :""")
548 self._check_error(source, "nested scope")
549
Martin v. Löwis77736712006-05-04 05:51:03 +0000550 def test_unexpected_indent(self):
551 self._check_error("foo()\n bar()\n", "unexpected indent",
552 subclass=IndentationError)
553
554 def test_no_indent(self):
555 self._check_error("if 1:\nfoo()", "expected an indented block",
556 subclass=IndentationError)
557
558 def test_bad_outdent(self):
559 self._check_error("if 1:\n foo()\n bar()",
560 "unindent does not match .* level",
561 subclass=IndentationError)
562
Neal Norwitz5ef92242006-05-19 06:43:50 +0000563 def test_kwargs_last(self):
564 self._check_error("int(base=10, '2')", "non-keyword arg")
565
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000566def test_main():
567 test_support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000568 from test import test_syntax
Florent Xicluna07627882010-03-21 01:14:24 +0000569 with test_support.check_py3k_warnings(("backquote not supported",
570 SyntaxWarning)):
571 test_support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +0000572
573if __name__ == "__main__":
574 test_main()