]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_syntax.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_syntax.py
CommitLineData
4710c53d 1"""This module tests SyntaxErrors.\r
2\r
3Here's an example of the sort of thing that is tested.\r
4\r
5>>> def f(x):\r
6... global x\r
7Traceback (most recent call last):\r
8SyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1)\r
9\r
10The tests are all raise SyntaxErrors. They were created by checking\r
11each C call that raises SyntaxError. There are several modules that\r
12raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and\r
13symtable.c.\r
14\r
15The parser itself outlaws a lot of invalid syntax. None of these\r
16errors are tested here at the moment. We should add some tests; since\r
17there are infinitely many programs with invalid syntax, we would need\r
18to be judicious in selecting some.\r
19\r
20The compiler generates a synthetic module name for code executed by\r
21doctest. Since all the code comes from the same module, a suffix like\r
22[1] is appended to the module name, As a consequence, changing the\r
23order of tests in this module means renumbering all the errors after\r
24it. (Maybe we should enable the ellipsis option for these tests.)\r
25\r
26In ast.c, syntax errors are raised by calling ast_error().\r
27\r
28Errors from set_context():\r
29\r
30>>> obj.None = 1\r
31Traceback (most recent call last):\r
32 File "<doctest test.test_syntax[1]>", line 1\r
33SyntaxError: cannot assign to None\r
34\r
35>>> None = 1\r
36Traceback (most recent call last):\r
37 File "<doctest test.test_syntax[2]>", line 1\r
38SyntaxError: cannot assign to None\r
39\r
40It's a syntax error to assign to the empty tuple. Why isn't it an\r
41error to assign to the empty list? It will always raise some error at\r
42runtime.\r
43\r
44>>> () = 1\r
45Traceback (most recent call last):\r
46 File "<doctest test.test_syntax[3]>", line 1\r
47SyntaxError: can't assign to ()\r
48\r
49>>> f() = 1\r
50Traceback (most recent call last):\r
51 File "<doctest test.test_syntax[4]>", line 1\r
52SyntaxError: can't assign to function call\r
53\r
54>>> del f()\r
55Traceback (most recent call last):\r
56 File "<doctest test.test_syntax[5]>", line 1\r
57SyntaxError: can't delete function call\r
58\r
59>>> a + 1 = 2\r
60Traceback (most recent call last):\r
61 File "<doctest test.test_syntax[6]>", line 1\r
62SyntaxError: can't assign to operator\r
63\r
64>>> (x for x in x) = 1\r
65Traceback (most recent call last):\r
66 File "<doctest test.test_syntax[7]>", line 1\r
67SyntaxError: can't assign to generator expression\r
68\r
69>>> 1 = 1\r
70Traceback (most recent call last):\r
71 File "<doctest test.test_syntax[8]>", line 1\r
72SyntaxError: can't assign to literal\r
73\r
74>>> "abc" = 1\r
75Traceback (most recent call last):\r
76 File "<doctest test.test_syntax[8]>", line 1\r
77SyntaxError: can't assign to literal\r
78\r
79>>> `1` = 1\r
80Traceback (most recent call last):\r
81 File "<doctest test.test_syntax[10]>", line 1\r
82SyntaxError: can't assign to repr\r
83\r
84If the left-hand side of an assignment is a list or tuple, an illegal\r
85expression inside that contain should still cause a syntax error.\r
86This test just checks a couple of cases rather than enumerating all of\r
87them.\r
88\r
89>>> (a, "b", c) = (1, 2, 3)\r
90Traceback (most recent call last):\r
91 File "<doctest test.test_syntax[11]>", line 1\r
92SyntaxError: can't assign to literal\r
93\r
94>>> [a, b, c + 1] = [1, 2, 3]\r
95Traceback (most recent call last):\r
96 File "<doctest test.test_syntax[12]>", line 1\r
97SyntaxError: can't assign to operator\r
98\r
99>>> a if 1 else b = 1\r
100Traceback (most recent call last):\r
101 File "<doctest test.test_syntax[13]>", line 1\r
102SyntaxError: can't assign to conditional expression\r
103\r
104From compiler_complex_args():\r
105\r
106>>> def f(None=1):\r
107... pass\r
108Traceback (most recent call last):\r
109 File "<doctest test.test_syntax[14]>", line 1\r
110SyntaxError: cannot assign to None\r
111\r
112\r
113From ast_for_arguments():\r
114\r
115>>> def f(x, y=1, z):\r
116... pass\r
117Traceback (most recent call last):\r
118 File "<doctest test.test_syntax[15]>", line 1\r
119SyntaxError: non-default argument follows default argument\r
120\r
121>>> def f(x, None):\r
122... pass\r
123Traceback (most recent call last):\r
124 File "<doctest test.test_syntax[16]>", line 1\r
125SyntaxError: cannot assign to None\r
126\r
127>>> def f(*None):\r
128... pass\r
129Traceback (most recent call last):\r
130 File "<doctest test.test_syntax[17]>", line 1\r
131SyntaxError: cannot assign to None\r
132\r
133>>> def f(**None):\r
134... pass\r
135Traceback (most recent call last):\r
136 File "<doctest test.test_syntax[18]>", line 1\r
137SyntaxError: cannot assign to None\r
138\r
139\r
140From ast_for_funcdef():\r
141\r
142>>> def None(x):\r
143... pass\r
144Traceback (most recent call last):\r
145 File "<doctest test.test_syntax[19]>", line 1\r
146SyntaxError: cannot assign to None\r
147\r
148\r
149From ast_for_call():\r
150\r
151>>> def f(it, *varargs):\r
152... return list(it)\r
153>>> L = range(10)\r
154>>> f(x for x in L)\r
155[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\r
156>>> f(x for x in L, 1)\r
157Traceback (most recent call last):\r
158 File "<doctest test.test_syntax[23]>", line 1\r
159SyntaxError: Generator expression must be parenthesized if not sole argument\r
160>>> f((x for x in L), 1)\r
161[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\r
162\r
163>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,\r
164... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,\r
165... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,\r
166... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,\r
167... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,\r
168... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,\r
169... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,\r
170... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,\r
171... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,\r
172... i100, i101, i102, i103, i104, i105, i106, i107, i108,\r
173... i109, i110, i111, i112, i113, i114, i115, i116, i117,\r
174... i118, i119, i120, i121, i122, i123, i124, i125, i126,\r
175... i127, i128, i129, i130, i131, i132, i133, i134, i135,\r
176... i136, i137, i138, i139, i140, i141, i142, i143, i144,\r
177... i145, i146, i147, i148, i149, i150, i151, i152, i153,\r
178... i154, i155, i156, i157, i158, i159, i160, i161, i162,\r
179... i163, i164, i165, i166, i167, i168, i169, i170, i171,\r
180... i172, i173, i174, i175, i176, i177, i178, i179, i180,\r
181... i181, i182, i183, i184, i185, i186, i187, i188, i189,\r
182... i190, i191, i192, i193, i194, i195, i196, i197, i198,\r
183... i199, i200, i201, i202, i203, i204, i205, i206, i207,\r
184... i208, i209, i210, i211, i212, i213, i214, i215, i216,\r
185... i217, i218, i219, i220, i221, i222, i223, i224, i225,\r
186... i226, i227, i228, i229, i230, i231, i232, i233, i234,\r
187... i235, i236, i237, i238, i239, i240, i241, i242, i243,\r
188... i244, i245, i246, i247, i248, i249, i250, i251, i252,\r
189... i253, i254, i255)\r
190Traceback (most recent call last):\r
191 File "<doctest test.test_syntax[25]>", line 1\r
192SyntaxError: more than 255 arguments\r
193\r
194The actual error cases counts positional arguments, keyword arguments,\r
195and generator expression arguments separately. This test combines the\r
196three.\r
197\r
198>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,\r
199... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,\r
200... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,\r
201... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,\r
202... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,\r
203... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,\r
204... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,\r
205... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,\r
206... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,\r
207... i100, i101, i102, i103, i104, i105, i106, i107, i108,\r
208... i109, i110, i111, i112, i113, i114, i115, i116, i117,\r
209... i118, i119, i120, i121, i122, i123, i124, i125, i126,\r
210... i127, i128, i129, i130, i131, i132, i133, i134, i135,\r
211... i136, i137, i138, i139, i140, i141, i142, i143, i144,\r
212... i145, i146, i147, i148, i149, i150, i151, i152, i153,\r
213... i154, i155, i156, i157, i158, i159, i160, i161, i162,\r
214... i163, i164, i165, i166, i167, i168, i169, i170, i171,\r
215... i172, i173, i174, i175, i176, i177, i178, i179, i180,\r
216... i181, i182, i183, i184, i185, i186, i187, i188, i189,\r
217... i190, i191, i192, i193, i194, i195, i196, i197, i198,\r
218... i199, i200, i201, i202, i203, i204, i205, i206, i207,\r
219... i208, i209, i210, i211, i212, i213, i214, i215, i216,\r
220... i217, i218, i219, i220, i221, i222, i223, i224, i225,\r
221... i226, i227, i228, i229, i230, i231, i232, i233, i234,\r
222... i235, i236, i237, i238, i239, i240, i241, i242, i243,\r
223... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,\r
224... i252=1, i253=1, i254=1, i255=1)\r
225Traceback (most recent call last):\r
226 File "<doctest test.test_syntax[26]>", line 1\r
227SyntaxError: more than 255 arguments\r
228\r
229>>> f(lambda x: x[0] = 3)\r
230Traceback (most recent call last):\r
231 File "<doctest test.test_syntax[27]>", line 1\r
232SyntaxError: lambda cannot contain assignment\r
233\r
234The grammar accepts any test (basically, any expression) in the\r
235keyword slot of a call site. Test a few different options.\r
236\r
237>>> f(x()=2)\r
238Traceback (most recent call last):\r
239 File "<doctest test.test_syntax[28]>", line 1\r
240SyntaxError: keyword can't be an expression\r
241>>> f(a or b=1)\r
242Traceback (most recent call last):\r
243 File "<doctest test.test_syntax[29]>", line 1\r
244SyntaxError: keyword can't be an expression\r
245>>> f(x.y=1)\r
246Traceback (most recent call last):\r
247 File "<doctest test.test_syntax[30]>", line 1\r
248SyntaxError: keyword can't be an expression\r
249\r
250\r
251More set_context():\r
252\r
253>>> (x for x in x) += 1\r
254Traceback (most recent call last):\r
255 File "<doctest test.test_syntax[31]>", line 1\r
256SyntaxError: can't assign to generator expression\r
257>>> None += 1\r
258Traceback (most recent call last):\r
259 File "<doctest test.test_syntax[32]>", line 1\r
260SyntaxError: cannot assign to None\r
261>>> f() += 1\r
262Traceback (most recent call last):\r
263 File "<doctest test.test_syntax[33]>", line 1\r
264SyntaxError: can't assign to function call\r
265\r
266\r
267Test continue in finally in weird combinations.\r
268\r
269continue in for loop under finally should be ok.\r
270\r
271 >>> def test():\r
272 ... try:\r
273 ... pass\r
274 ... finally:\r
275 ... for abc in range(10):\r
276 ... continue\r
277 ... print abc\r
278 >>> test()\r
279 9\r
280\r
281Start simple, a continue in a finally should not be allowed.\r
282\r
283 >>> def test():\r
284 ... for abc in range(10):\r
285 ... try:\r
286 ... pass\r
287 ... finally:\r
288 ... continue\r
289 Traceback (most recent call last):\r
290 ...\r
291 File "<doctest test.test_syntax[36]>", line 6\r
292 SyntaxError: 'continue' not supported inside 'finally' clause\r
293\r
294This is essentially a continue in a finally which should not be allowed.\r
295\r
296 >>> def test():\r
297 ... for abc in range(10):\r
298 ... try:\r
299 ... pass\r
300 ... finally:\r
301 ... try:\r
302 ... continue\r
303 ... except:\r
304 ... pass\r
305 Traceback (most recent call last):\r
306 ...\r
307 File "<doctest test.test_syntax[37]>", line 6\r
308 SyntaxError: 'continue' not supported inside 'finally' clause\r
309\r
310 >>> def foo():\r
311 ... try:\r
312 ... pass\r
313 ... finally:\r
314 ... continue\r
315 Traceback (most recent call last):\r
316 ...\r
317 File "<doctest test.test_syntax[38]>", line 5\r
318 SyntaxError: 'continue' not supported inside 'finally' clause\r
319\r
320 >>> def foo():\r
321 ... for a in ():\r
322 ... try:\r
323 ... pass\r
324 ... finally:\r
325 ... continue\r
326 Traceback (most recent call last):\r
327 ...\r
328 File "<doctest test.test_syntax[39]>", line 6\r
329 SyntaxError: 'continue' not supported inside 'finally' clause\r
330\r
331 >>> def foo():\r
332 ... for a in ():\r
333 ... try:\r
334 ... pass\r
335 ... finally:\r
336 ... try:\r
337 ... continue\r
338 ... finally:\r
339 ... pass\r
340 Traceback (most recent call last):\r
341 ...\r
342 File "<doctest test.test_syntax[40]>", line 7\r
343 SyntaxError: 'continue' not supported inside 'finally' clause\r
344\r
345 >>> def foo():\r
346 ... for a in ():\r
347 ... try: pass\r
348 ... finally:\r
349 ... try:\r
350 ... pass\r
351 ... except:\r
352 ... continue\r
353 Traceback (most recent call last):\r
354 ...\r
355 File "<doctest test.test_syntax[41]>", line 8\r
356 SyntaxError: 'continue' not supported inside 'finally' clause\r
357\r
358There is one test for a break that is not in a loop. The compiler\r
359uses a single data structure to keep track of try-finally and loops,\r
360so we need to be sure that a break is actually inside a loop. If it\r
361isn't, there should be a syntax error.\r
362\r
363 >>> try:\r
364 ... print 1\r
365 ... break\r
366 ... print 2\r
367 ... finally:\r
368 ... print 3\r
369 Traceback (most recent call last):\r
370 ...\r
371 File "<doctest test.test_syntax[42]>", line 3\r
372 SyntaxError: 'break' outside loop\r
373\r
374This should probably raise a better error than a SystemError (or none at all).\r
375In 2.5 there was a missing exception and an assert was triggered in a debug\r
376build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514\r
377\r
378 >>> while 1:\r
379 ... while 2:\r
380 ... while 3:\r
381 ... while 4:\r
382 ... while 5:\r
383 ... while 6:\r
384 ... while 8:\r
385 ... while 9:\r
386 ... while 10:\r
387 ... while 11:\r
388 ... while 12:\r
389 ... while 13:\r
390 ... while 14:\r
391 ... while 15:\r
392 ... while 16:\r
393 ... while 17:\r
394 ... while 18:\r
395 ... while 19:\r
396 ... while 20:\r
397 ... while 21:\r
398 ... while 22:\r
399 ... break\r
400 Traceback (most recent call last):\r
401 ...\r
402 SystemError: too many statically nested blocks\r
403\r
404This tests assignment-context; there was a bug in Python 2.5 where compiling\r
405a complex 'if' (one with 'elif') would fail to notice an invalid suite,\r
406leading to spurious errors.\r
407\r
408 >>> if 1:\r
409 ... x() = 1\r
410 ... elif 1:\r
411 ... pass\r
412 Traceback (most recent call last):\r
413 ...\r
414 File "<doctest test.test_syntax[44]>", line 2\r
415 SyntaxError: can't assign to function call\r
416\r
417 >>> if 1:\r
418 ... pass\r
419 ... elif 1:\r
420 ... x() = 1\r
421 Traceback (most recent call last):\r
422 ...\r
423 File "<doctest test.test_syntax[45]>", line 4\r
424 SyntaxError: can't assign to function call\r
425\r
426 >>> if 1:\r
427 ... x() = 1\r
428 ... elif 1:\r
429 ... pass\r
430 ... else:\r
431 ... pass\r
432 Traceback (most recent call last):\r
433 ...\r
434 File "<doctest test.test_syntax[46]>", line 2\r
435 SyntaxError: can't assign to function call\r
436\r
437 >>> if 1:\r
438 ... pass\r
439 ... elif 1:\r
440 ... x() = 1\r
441 ... else:\r
442 ... pass\r
443 Traceback (most recent call last):\r
444 ...\r
445 File "<doctest test.test_syntax[47]>", line 4\r
446 SyntaxError: can't assign to function call\r
447\r
448 >>> if 1:\r
449 ... pass\r
450 ... elif 1:\r
451 ... pass\r
452 ... else:\r
453 ... x() = 1\r
454 Traceback (most recent call last):\r
455 ...\r
456 File "<doctest test.test_syntax[48]>", line 6\r
457 SyntaxError: can't assign to function call\r
458\r
459>>> f(a=23, a=234)\r
460Traceback (most recent call last):\r
461 ...\r
462 File "<doctest test.test_syntax[49]>", line 1\r
463SyntaxError: keyword argument repeated\r
464\r
465>>> del ()\r
466Traceback (most recent call last):\r
467 ...\r
468 File "<doctest test.test_syntax[50]>", line 1\r
469SyntaxError: can't delete ()\r
470\r
471>>> {1, 2, 3} = 42\r
472Traceback (most recent call last):\r
473 ...\r
474 File "<doctest test.test_syntax[50]>", line 1\r
475SyntaxError: can't assign to literal\r
476\r
477Corner-case that used to crash:\r
478\r
479 >>> def f(*xx, **__debug__): pass\r
480 Traceback (most recent call last):\r
481 SyntaxError: cannot assign to __debug__\r
482\r
483"""\r
484\r
485import re\r
486import unittest\r
487import warnings\r
488\r
489from test import test_support\r
490\r
491class SyntaxTestCase(unittest.TestCase):\r
492\r
493 def _check_error(self, code, errtext,\r
494 filename="<testcase>", mode="exec", subclass=None):\r
495 """Check that compiling code raises SyntaxError with errtext.\r
496\r
497 errtest is a regular expression that must be present in the\r
498 test of the exception raised. If subclass is specified it\r
499 is the expected subclass of SyntaxError (e.g. IndentationError).\r
500 """\r
501 try:\r
502 compile(code, filename, mode)\r
503 except SyntaxError, err:\r
504 if subclass and not isinstance(err, subclass):\r
505 self.fail("SyntaxError is not a %s" % subclass.__name__)\r
506 mo = re.search(errtext, str(err))\r
507 if mo is None:\r
508 self.fail("%s did not contain '%r'" % (err, errtext,))\r
509 else:\r
510 self.fail("compile() did not raise SyntaxError")\r
511\r
512 def test_paren_arg_with_default(self):\r
513 self._check_error("def f((x)=23): pass",\r
514 "parenthesized arg with default")\r
515\r
516 def test_assign_call(self):\r
517 self._check_error("f() = 1", "assign")\r
518\r
519 def test_assign_del(self):\r
520 self._check_error("del f()", "delete")\r
521\r
522 def test_global_err_then_warn(self):\r
523 # Bug tickler: The SyntaxError raised for one global statement\r
524 # shouldn't be clobbered by a SyntaxWarning issued for a later one.\r
525 source = re.sub('(?m)^ *:', '', """\\r
526 :def error(a):\r
527 : global a # SyntaxError\r
528 :def warning():\r
529 : b = 1\r
530 : global b # SyntaxWarning\r
531 :""")\r
532 warnings.filterwarnings(action='ignore', category=SyntaxWarning)\r
533 self._check_error(source, "global")\r
534 warnings.filters.pop(0)\r
535\r
536 def test_break_outside_loop(self):\r
537 self._check_error("break", "outside loop")\r
538\r
539 def test_delete_deref(self):\r
540 source = re.sub('(?m)^ *:', '', """\\r
541 :def foo(x):\r
542 : def bar():\r
543 : print x\r
544 : del x\r
545 :""")\r
546 self._check_error(source, "nested scope")\r
547\r
548 def test_unexpected_indent(self):\r
549 self._check_error("foo()\n bar()\n", "unexpected indent",\r
550 subclass=IndentationError)\r
551\r
552 def test_no_indent(self):\r
553 self._check_error("if 1:\nfoo()", "expected an indented block",\r
554 subclass=IndentationError)\r
555\r
556 def test_bad_outdent(self):\r
557 self._check_error("if 1:\n foo()\n bar()",\r
558 "unindent does not match .* level",\r
559 subclass=IndentationError)\r
560\r
561 def test_kwargs_last(self):\r
562 self._check_error("int(base=10, '2')", "non-keyword arg")\r
563\r
564def test_main():\r
565 test_support.run_unittest(SyntaxTestCase)\r
566 from test import test_syntax\r
567 with test_support.check_py3k_warnings(("backquote not supported",\r
568 SyntaxWarning)):\r
569 test_support.run_doctest(test_syntax, verbosity=True)\r
570\r
571if __name__ == "__main__":\r
572 test_main()\r