1 | """This module tests SyntaxErrors.
|
---|
2 |
|
---|
3 | Here's an example of the sort of thing that is tested.
|
---|
4 |
|
---|
5 | >>> def f(x):
|
---|
6 | ... global x
|
---|
7 | Traceback (most recent call last):
|
---|
8 | SyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1)
|
---|
9 |
|
---|
10 | The tests are all raise SyntaxErrors. They were created by checking
|
---|
11 | each C call that raises SyntaxError. There are several modules that
|
---|
12 | raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
|
---|
13 | symtable.c.
|
---|
14 |
|
---|
15 | The parser itself outlaws a lot of invalid syntax. None of these
|
---|
16 | errors are tested here at the moment. We should add some tests; since
|
---|
17 | there are infinitely many programs with invalid syntax, we would need
|
---|
18 | to be judicious in selecting some.
|
---|
19 |
|
---|
20 | The compiler generates a synthetic module name for code executed by
|
---|
21 | doctest. 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
|
---|
23 | order of tests in this module means renumbering all the errors after
|
---|
24 | it. (Maybe we should enable the ellipsis option for these tests.)
|
---|
25 |
|
---|
26 | In ast.c, syntax errors are raised by calling ast_error().
|
---|
27 |
|
---|
28 | Errors from set_context():
|
---|
29 |
|
---|
30 | >>> obj.None = 1
|
---|
31 | Traceback (most recent call last):
|
---|
32 | File "<doctest test.test_syntax[1]>", line 1
|
---|
33 | SyntaxError: cannot assign to None
|
---|
34 |
|
---|
35 | >>> None = 1
|
---|
36 | Traceback (most recent call last):
|
---|
37 | File "<doctest test.test_syntax[2]>", line 1
|
---|
38 | SyntaxError: cannot assign to None
|
---|
39 |
|
---|
40 | It's a syntax error to assign to the empty tuple. Why isn't it an
|
---|
41 | error to assign to the empty list? It will always raise some error at
|
---|
42 | runtime.
|
---|
43 |
|
---|
44 | >>> () = 1
|
---|
45 | Traceback (most recent call last):
|
---|
46 | File "<doctest test.test_syntax[3]>", line 1
|
---|
47 | SyntaxError: can't assign to ()
|
---|
48 |
|
---|
49 | >>> f() = 1
|
---|
50 | Traceback (most recent call last):
|
---|
51 | File "<doctest test.test_syntax[4]>", line 1
|
---|
52 | SyntaxError: can't assign to function call
|
---|
53 |
|
---|
54 | >>> del f()
|
---|
55 | Traceback (most recent call last):
|
---|
56 | File "<doctest test.test_syntax[5]>", line 1
|
---|
57 | SyntaxError: can't delete function call
|
---|
58 |
|
---|
59 | >>> a + 1 = 2
|
---|
60 | Traceback (most recent call last):
|
---|
61 | File "<doctest test.test_syntax[6]>", line 1
|
---|
62 | SyntaxError: can't assign to operator
|
---|
63 |
|
---|
64 | >>> (x for x in x) = 1
|
---|
65 | Traceback (most recent call last):
|
---|
66 | File "<doctest test.test_syntax[7]>", line 1
|
---|
67 | SyntaxError: can't assign to generator expression
|
---|
68 |
|
---|
69 | >>> 1 = 1
|
---|
70 | Traceback (most recent call last):
|
---|
71 | File "<doctest test.test_syntax[8]>", line 1
|
---|
72 | SyntaxError: can't assign to literal
|
---|
73 |
|
---|
74 | >>> "abc" = 1
|
---|
75 | Traceback (most recent call last):
|
---|
76 | File "<doctest test.test_syntax[8]>", line 1
|
---|
77 | SyntaxError: can't assign to literal
|
---|
78 |
|
---|
79 | >>> `1` = 1
|
---|
80 | Traceback (most recent call last):
|
---|
81 | File "<doctest test.test_syntax[10]>", line 1
|
---|
82 | SyntaxError: can't assign to repr
|
---|
83 |
|
---|
84 | If the left-hand side of an assignment is a list or tuple, an illegal
|
---|
85 | expression inside that contain should still cause a syntax error.
|
---|
86 | This test just checks a couple of cases rather than enumerating all of
|
---|
87 | them.
|
---|
88 |
|
---|
89 | >>> (a, "b", c) = (1, 2, 3)
|
---|
90 | Traceback (most recent call last):
|
---|
91 | File "<doctest test.test_syntax[11]>", line 1
|
---|
92 | SyntaxError: can't assign to literal
|
---|
93 |
|
---|
94 | >>> [a, b, c + 1] = [1, 2, 3]
|
---|
95 | Traceback (most recent call last):
|
---|
96 | File "<doctest test.test_syntax[12]>", line 1
|
---|
97 | SyntaxError: can't assign to operator
|
---|
98 |
|
---|
99 | >>> a if 1 else b = 1
|
---|
100 | Traceback (most recent call last):
|
---|
101 | File "<doctest test.test_syntax[13]>", line 1
|
---|
102 | SyntaxError: can't assign to conditional expression
|
---|
103 |
|
---|
104 | From compiler_complex_args():
|
---|
105 |
|
---|
106 | >>> def f(None=1):
|
---|
107 | ... pass
|
---|
108 | Traceback (most recent call last):
|
---|
109 | File "<doctest test.test_syntax[14]>", line 1
|
---|
110 | SyntaxError: cannot assign to None
|
---|
111 |
|
---|
112 |
|
---|
113 | From ast_for_arguments():
|
---|
114 |
|
---|
115 | >>> def f(x, y=1, z):
|
---|
116 | ... pass
|
---|
117 | Traceback (most recent call last):
|
---|
118 | File "<doctest test.test_syntax[15]>", line 1
|
---|
119 | SyntaxError: non-default argument follows default argument
|
---|
120 |
|
---|
121 | >>> def f(x, None):
|
---|
122 | ... pass
|
---|
123 | Traceback (most recent call last):
|
---|
124 | File "<doctest test.test_syntax[16]>", line 1
|
---|
125 | SyntaxError: cannot assign to None
|
---|
126 |
|
---|
127 | >>> def f(*None):
|
---|
128 | ... pass
|
---|
129 | Traceback (most recent call last):
|
---|
130 | File "<doctest test.test_syntax[17]>", line 1
|
---|
131 | SyntaxError: cannot assign to None
|
---|
132 |
|
---|
133 | >>> def f(**None):
|
---|
134 | ... pass
|
---|
135 | Traceback (most recent call last):
|
---|
136 | File "<doctest test.test_syntax[18]>", line 1
|
---|
137 | SyntaxError: cannot assign to None
|
---|
138 |
|
---|
139 |
|
---|
140 | From ast_for_funcdef():
|
---|
141 |
|
---|
142 | >>> def None(x):
|
---|
143 | ... pass
|
---|
144 | Traceback (most recent call last):
|
---|
145 | File "<doctest test.test_syntax[19]>", line 1
|
---|
146 | SyntaxError: cannot assign to None
|
---|
147 |
|
---|
148 |
|
---|
149 | From 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)
|
---|
157 | Traceback (most recent call last):
|
---|
158 | File "<doctest test.test_syntax[23]>", line 1
|
---|
159 | SyntaxError: Generator expression must be parenthesized if not sole argument
|
---|
160 | >>> 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)
|
---|
190 | Traceback (most recent call last):
|
---|
191 | File "<doctest test.test_syntax[25]>", line 1
|
---|
192 | SyntaxError: more than 255 arguments
|
---|
193 |
|
---|
194 | The actual error cases counts positional arguments, keyword arguments,
|
---|
195 | and generator expression arguments separately. This test combines the
|
---|
196 | three.
|
---|
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)
|
---|
225 | Traceback (most recent call last):
|
---|
226 | File "<doctest test.test_syntax[26]>", line 1
|
---|
227 | SyntaxError: more than 255 arguments
|
---|
228 |
|
---|
229 | >>> f(lambda x: x[0] = 3)
|
---|
230 | Traceback (most recent call last):
|
---|
231 | File "<doctest test.test_syntax[27]>", line 1
|
---|
232 | SyntaxError: lambda cannot contain assignment
|
---|
233 |
|
---|
234 | The grammar accepts any test (basically, any expression) in the
|
---|
235 | keyword slot of a call site. Test a few different options.
|
---|
236 |
|
---|
237 | >>> f(x()=2)
|
---|
238 | Traceback (most recent call last):
|
---|
239 | File "<doctest test.test_syntax[28]>", line 1
|
---|
240 | SyntaxError: keyword can't be an expression
|
---|
241 | >>> f(a or b=1)
|
---|
242 | Traceback (most recent call last):
|
---|
243 | File "<doctest test.test_syntax[29]>", line 1
|
---|
244 | SyntaxError: keyword can't be an expression
|
---|
245 | >>> f(x.y=1)
|
---|
246 | Traceback (most recent call last):
|
---|
247 | File "<doctest test.test_syntax[30]>", line 1
|
---|
248 | SyntaxError: keyword can't be an expression
|
---|
249 |
|
---|
250 |
|
---|
251 | More set_context():
|
---|
252 |
|
---|
253 | >>> (x for x in x) += 1
|
---|
254 | Traceback (most recent call last):
|
---|
255 | File "<doctest test.test_syntax[31]>", line 1
|
---|
256 | SyntaxError: can't assign to generator expression
|
---|
257 | >>> None += 1
|
---|
258 | Traceback (most recent call last):
|
---|
259 | File "<doctest test.test_syntax[32]>", line 1
|
---|
260 | SyntaxError: cannot assign to None
|
---|
261 | >>> f() += 1
|
---|
262 | Traceback (most recent call last):
|
---|
263 | File "<doctest test.test_syntax[33]>", line 1
|
---|
264 | SyntaxError: can't assign to function call
|
---|
265 |
|
---|
266 |
|
---|
267 | Test continue in finally in weird combinations.
|
---|
268 |
|
---|
269 | continue in for loop under finally should be ok.
|
---|
270 |
|
---|
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 |
|
---|
281 | Start 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 | ...
|
---|
291 | File "<doctest test.test_syntax[36]>", line 6
|
---|
292 | SyntaxError: 'continue' not supported inside 'finally' clause
|
---|
293 |
|
---|
294 | This 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 | ...
|
---|
307 | File "<doctest test.test_syntax[37]>", line 6
|
---|
308 | SyntaxError: 'continue' not supported inside 'finally' clause
|
---|
309 |
|
---|
310 | >>> def foo():
|
---|
311 | ... try:
|
---|
312 | ... pass
|
---|
313 | ... finally:
|
---|
314 | ... continue
|
---|
315 | Traceback (most recent call last):
|
---|
316 | ...
|
---|
317 | File "<doctest test.test_syntax[38]>", line 5
|
---|
318 | SyntaxError: 'continue' not supported inside 'finally' clause
|
---|
319 |
|
---|
320 | >>> def foo():
|
---|
321 | ... for a in ():
|
---|
322 | ... try:
|
---|
323 | ... pass
|
---|
324 | ... finally:
|
---|
325 | ... continue
|
---|
326 | Traceback (most recent call last):
|
---|
327 | ...
|
---|
328 | File "<doctest test.test_syntax[39]>", line 6
|
---|
329 | SyntaxError: 'continue' not supported inside 'finally' clause
|
---|
330 |
|
---|
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 | ...
|
---|
342 | File "<doctest test.test_syntax[40]>", line 7
|
---|
343 | SyntaxError: 'continue' not supported inside 'finally' clause
|
---|
344 |
|
---|
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 | ...
|
---|
355 | File "<doctest test.test_syntax[41]>", line 8
|
---|
356 | SyntaxError: 'continue' not supported inside 'finally' clause
|
---|
357 |
|
---|
358 | There is one test for a break that is not in a loop. The compiler
|
---|
359 | uses a single data structure to keep track of try-finally and loops,
|
---|
360 | so we need to be sure that a break is actually inside a loop. If it
|
---|
361 | isn'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 | ...
|
---|
371 | File "<doctest test.test_syntax[42]>", line 3
|
---|
372 | SyntaxError: 'break' outside loop
|
---|
373 |
|
---|
374 | This should probably raise a better error than a SystemError (or none at all).
|
---|
375 | In 2.5 there was a missing exception and an assert was triggered in a debug
|
---|
376 | build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
|
---|
377 |
|
---|
378 | >>> while 1:
|
---|
379 | ... while 2:
|
---|
380 | ... while 3:
|
---|
381 | ... while 4:
|
---|
382 | ... while 5:
|
---|
383 | ... while 6:
|
---|
384 | ... while 8:
|
---|
385 | ... while 9:
|
---|
386 | ... while 10:
|
---|
387 | ... while 11:
|
---|
388 | ... while 12:
|
---|
389 | ... while 13:
|
---|
390 | ... while 14:
|
---|
391 | ... while 15:
|
---|
392 | ... while 16:
|
---|
393 | ... while 17:
|
---|
394 | ... while 18:
|
---|
395 | ... while 19:
|
---|
396 | ... while 20:
|
---|
397 | ... while 21:
|
---|
398 | ... while 22:
|
---|
399 | ... break
|
---|
400 | Traceback (most recent call last):
|
---|
401 | ...
|
---|
402 | SystemError: too many statically nested blocks
|
---|
403 |
|
---|
404 | This tests assignment-context; there was a bug in Python 2.5 where compiling
|
---|
405 | a complex 'if' (one with 'elif') would fail to notice an invalid suite,
|
---|
406 | leading to spurious errors.
|
---|
407 |
|
---|
408 | >>> if 1:
|
---|
409 | ... x() = 1
|
---|
410 | ... elif 1:
|
---|
411 | ... pass
|
---|
412 | Traceback (most recent call last):
|
---|
413 | ...
|
---|
414 | File "<doctest test.test_syntax[44]>", line 2
|
---|
415 | SyntaxError: can't assign to function call
|
---|
416 |
|
---|
417 | >>> if 1:
|
---|
418 | ... pass
|
---|
419 | ... elif 1:
|
---|
420 | ... x() = 1
|
---|
421 | Traceback (most recent call last):
|
---|
422 | ...
|
---|
423 | File "<doctest test.test_syntax[45]>", line 4
|
---|
424 | SyntaxError: can't assign to function call
|
---|
425 |
|
---|
426 | >>> if 1:
|
---|
427 | ... x() = 1
|
---|
428 | ... elif 1:
|
---|
429 | ... pass
|
---|
430 | ... else:
|
---|
431 | ... pass
|
---|
432 | Traceback (most recent call last):
|
---|
433 | ...
|
---|
434 | File "<doctest test.test_syntax[46]>", line 2
|
---|
435 | SyntaxError: can't assign to function call
|
---|
436 |
|
---|
437 | >>> if 1:
|
---|
438 | ... pass
|
---|
439 | ... elif 1:
|
---|
440 | ... x() = 1
|
---|
441 | ... else:
|
---|
442 | ... pass
|
---|
443 | Traceback (most recent call last):
|
---|
444 | ...
|
---|
445 | File "<doctest test.test_syntax[47]>", line 4
|
---|
446 | SyntaxError: can't assign to function call
|
---|
447 |
|
---|
448 | >>> if 1:
|
---|
449 | ... pass
|
---|
450 | ... elif 1:
|
---|
451 | ... pass
|
---|
452 | ... else:
|
---|
453 | ... x() = 1
|
---|
454 | Traceback (most recent call last):
|
---|
455 | ...
|
---|
456 | File "<doctest test.test_syntax[48]>", line 6
|
---|
457 | SyntaxError: can't assign to function call
|
---|
458 |
|
---|
459 | >>> f(a=23, a=234)
|
---|
460 | Traceback (most recent call last):
|
---|
461 | ...
|
---|
462 | File "<doctest test.test_syntax[49]>", line 1
|
---|
463 | SyntaxError: keyword argument repeated
|
---|
464 |
|
---|
465 | >>> del ()
|
---|
466 | Traceback (most recent call last):
|
---|
467 | ...
|
---|
468 | File "<doctest test.test_syntax[50]>", line 1
|
---|
469 | SyntaxError: can't delete ()
|
---|
470 |
|
---|
471 | >>> {1, 2, 3} = 42
|
---|
472 | Traceback (most recent call last):
|
---|
473 | ...
|
---|
474 | File "<doctest test.test_syntax[50]>", line 1
|
---|
475 | SyntaxError: can't assign to literal
|
---|
476 |
|
---|
477 | Corner-case that used to crash:
|
---|
478 |
|
---|
479 | >>> def f(*xx, **__debug__): pass
|
---|
480 | Traceback (most recent call last):
|
---|
481 | SyntaxError: cannot assign to __debug__
|
---|
482 |
|
---|
483 | """
|
---|
484 |
|
---|
485 | import re
|
---|
486 | import unittest
|
---|
487 | import warnings
|
---|
488 |
|
---|
489 | from test import test_support
|
---|
490 |
|
---|
491 | class SyntaxTestCase(unittest.TestCase):
|
---|
492 |
|
---|
493 | def _check_error(self, code, errtext,
|
---|
494 | filename="<testcase>", mode="exec", subclass=None):
|
---|
495 | """Check that compiling code raises SyntaxError with errtext.
|
---|
496 |
|
---|
497 | errtest is a regular expression that must be present in the
|
---|
498 | test of the exception raised. If subclass is specified it
|
---|
499 | is the expected subclass of SyntaxError (e.g. IndentationError).
|
---|
500 | """
|
---|
501 | try:
|
---|
502 | compile(code, filename, mode)
|
---|
503 | except SyntaxError, err:
|
---|
504 | if subclass and not isinstance(err, subclass):
|
---|
505 | self.fail("SyntaxError is not a %s" % subclass.__name__)
|
---|
506 | mo = re.search(errtext, str(err))
|
---|
507 | if mo is None:
|
---|
508 | self.fail("%s did not contain '%r'" % (err, errtext,))
|
---|
509 | else:
|
---|
510 | self.fail("compile() did not raise SyntaxError")
|
---|
511 |
|
---|
512 | def test_paren_arg_with_default(self):
|
---|
513 | self._check_error("def f((x)=23): pass",
|
---|
514 | "parenthesized arg with default")
|
---|
515 |
|
---|
516 | def test_assign_call(self):
|
---|
517 | self._check_error("f() = 1", "assign")
|
---|
518 |
|
---|
519 | def test_assign_del(self):
|
---|
520 | self._check_error("del f()", "delete")
|
---|
521 |
|
---|
522 | def test_global_err_then_warn(self):
|
---|
523 | # Bug tickler: The SyntaxError raised for one global statement
|
---|
524 | # shouldn't be clobbered by a SyntaxWarning issued for a later one.
|
---|
525 | source = re.sub('(?m)^ *:', '', """\
|
---|
526 | :def error(a):
|
---|
527 | : global a # SyntaxError
|
---|
528 | :def warning():
|
---|
529 | : b = 1
|
---|
530 | : global b # SyntaxWarning
|
---|
531 | :""")
|
---|
532 | warnings.filterwarnings(action='ignore', category=SyntaxWarning)
|
---|
533 | self._check_error(source, "global")
|
---|
534 | warnings.filters.pop(0)
|
---|
535 |
|
---|
536 | def test_break_outside_loop(self):
|
---|
537 | self._check_error("break", "outside loop")
|
---|
538 |
|
---|
539 | def test_delete_deref(self):
|
---|
540 | source = re.sub('(?m)^ *:', '', """\
|
---|
541 | :def foo(x):
|
---|
542 | : def bar():
|
---|
543 | : print x
|
---|
544 | : del x
|
---|
545 | :""")
|
---|
546 | self._check_error(source, "nested scope")
|
---|
547 |
|
---|
548 | def test_unexpected_indent(self):
|
---|
549 | self._check_error("foo()\n bar()\n", "unexpected indent",
|
---|
550 | subclass=IndentationError)
|
---|
551 |
|
---|
552 | def test_no_indent(self):
|
---|
553 | self._check_error("if 1:\nfoo()", "expected an indented block",
|
---|
554 | subclass=IndentationError)
|
---|
555 |
|
---|
556 | def test_bad_outdent(self):
|
---|
557 | self._check_error("if 1:\n foo()\n bar()",
|
---|
558 | "unindent does not match .* level",
|
---|
559 | subclass=IndentationError)
|
---|
560 |
|
---|
561 | def test_kwargs_last(self):
|
---|
562 | self._check_error("int(base=10, '2')", "non-keyword arg")
|
---|
563 |
|
---|
564 | def test_main():
|
---|
565 | test_support.run_unittest(SyntaxTestCase)
|
---|
566 | from test import test_syntax
|
---|
567 | with test_support.check_py3k_warnings(("backquote not supported",
|
---|
568 | SyntaxWarning)):
|
---|
569 | test_support.run_doctest(test_syntax, verbosity=True)
|
---|
570 |
|
---|
571 | if __name__ == "__main__":
|
---|
572 | test_main()
|
---|