]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_compiler.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_compiler.py
CommitLineData
4710c53d 1import test.test_support\r
2compiler = test.test_support.import_module('compiler', deprecated=True)\r
3from compiler.ast import flatten\r
4import os, sys, time, unittest\r
5from random import random\r
6from StringIO import StringIO\r
7\r
8# How much time in seconds can pass before we print a 'Still working' message.\r
9_PRINT_WORKING_MSG_INTERVAL = 5 * 60\r
10\r
11class TrivialContext(object):\r
12 def __enter__(self):\r
13 return self\r
14 def __exit__(self, *exc_info):\r
15 pass\r
16\r
17class CompilerTest(unittest.TestCase):\r
18\r
19 def testCompileLibrary(self):\r
20 # A simple but large test. Compile all the code in the\r
21 # standard library and its test suite. This doesn't verify\r
22 # that any of the code is correct, merely the compiler is able\r
23 # to generate some kind of code for it.\r
24\r
25 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL\r
26 # warning: if 'os' or 'test_support' are moved in some other dir,\r
27 # they should be changed here.\r
28 libdir = os.path.dirname(os.__file__)\r
29 testdir = os.path.dirname(test.test_support.__file__)\r
30\r
31 for dir in [libdir, testdir]:\r
32 for basename in os.listdir(dir):\r
33 # Print still working message since this test can be really slow\r
34 if next_time <= time.time():\r
35 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL\r
36 print >>sys.__stdout__, \\r
37 ' testCompileLibrary still working, be patient...'\r
38 sys.__stdout__.flush()\r
39\r
40 if not basename.endswith(".py"):\r
41 continue\r
42 if not TEST_ALL and random() < 0.98:\r
43 continue\r
44 path = os.path.join(dir, basename)\r
45 if test.test_support.verbose:\r
46 print "compiling", path\r
47 f = open(path, "U")\r
48 buf = f.read()\r
49 f.close()\r
50 if "badsyntax" in basename or "bad_coding" in basename:\r
51 self.assertRaises(SyntaxError, compiler.compile,\r
52 buf, basename, "exec")\r
53 else:\r
54 try:\r
55 compiler.compile(buf, basename, "exec")\r
56 except Exception, e:\r
57 args = list(e.args)\r
58 args.append("in file %s]" % basename)\r
59 #args[0] += "[in file %s]" % basename\r
60 e.args = tuple(args)\r
61 raise\r
62\r
63 def testNewClassSyntax(self):\r
64 compiler.compile("class foo():pass\n\n","<string>","exec")\r
65\r
66 def testYieldExpr(self):\r
67 compiler.compile("def g(): yield\n\n", "<string>", "exec")\r
68\r
69 def testKeywordAfterStarargs(self):\r
70 def f(*args, **kwargs):\r
71 self.assertEqual((args, kwargs), ((2,3), {'x': 1, 'y': 4}))\r
72 c = compiler.compile('f(x=1, *(2, 3), y=4)', '<string>', 'exec')\r
73 exec c in {'f': f}\r
74\r
75 self.assertRaises(SyntaxError, compiler.parse, "foo(a=1, b)")\r
76 self.assertRaises(SyntaxError, compiler.parse, "foo(1, *args, 3)")\r
77\r
78 def testTryExceptFinally(self):\r
79 # Test that except and finally clauses in one try stmt are recognized\r
80 c = compiler.compile("try:\n 1//0\nexcept:\n e = 1\nfinally:\n f = 1",\r
81 "<string>", "exec")\r
82 dct = {}\r
83 exec c in dct\r
84 self.assertEqual(dct.get('e'), 1)\r
85 self.assertEqual(dct.get('f'), 1)\r
86\r
87 def testDefaultArgs(self):\r
88 self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")\r
89\r
90 def testDocstrings(self):\r
91 c = compiler.compile('"doc"', '<string>', 'exec')\r
92 self.assertIn('__doc__', c.co_names)\r
93 c = compiler.compile('def f():\n "doc"', '<string>', 'exec')\r
94 g = {}\r
95 exec c in g\r
96 self.assertEqual(g['f'].__doc__, "doc")\r
97\r
98 def testLineNo(self):\r
99 # Test that all nodes except Module have a correct lineno attribute.\r
100 filename = __file__\r
101 if filename.endswith((".pyc", ".pyo")):\r
102 filename = filename[:-1]\r
103 tree = compiler.parseFile(filename)\r
104 self.check_lineno(tree)\r
105\r
106 def check_lineno(self, node):\r
107 try:\r
108 self._check_lineno(node)\r
109 except AssertionError:\r
110 print node.__class__, node.lineno\r
111 raise\r
112\r
113 def _check_lineno(self, node):\r
114 if not node.__class__ in NOLINENO:\r
115 self.assertIsInstance(node.lineno, int,\r
116 "lineno=%s on %s" % (node.lineno, node.__class__))\r
117 self.assertTrue(node.lineno > 0,\r
118 "lineno=%s on %s" % (node.lineno, node.__class__))\r
119 for child in node.getChildNodes():\r
120 self.check_lineno(child)\r
121\r
122 def testFlatten(self):\r
123 self.assertEqual(flatten([1, [2]]), [1, 2])\r
124 self.assertEqual(flatten((1, (2,))), [1, 2])\r
125\r
126 def testNestedScope(self):\r
127 c = compiler.compile('def g():\n'\r
128 ' a = 1\n'\r
129 ' def f(): return a + 2\n'\r
130 ' return f()\n'\r
131 'result = g()',\r
132 '<string>',\r
133 'exec')\r
134 dct = {}\r
135 exec c in dct\r
136 self.assertEqual(dct.get('result'), 3)\r
137\r
138 def testGenExp(self):\r
139 c = compiler.compile('list((i,j) for i in range(3) if i < 3'\r
140 ' for j in range(4) if j > 2)',\r
141 '<string>',\r
142 'eval')\r
143 self.assertEqual(eval(c), [(0, 3), (1, 3), (2, 3)])\r
144\r
145 def testSetLiteral(self):\r
146 c = compiler.compile('{1, 2, 3}', '<string>', 'eval')\r
147 self.assertEqual(eval(c), {1,2,3})\r
148 c = compiler.compile('{1, 2, 3,}', '<string>', 'eval')\r
149 self.assertEqual(eval(c), {1,2,3})\r
150\r
151 def testDictLiteral(self):\r
152 c = compiler.compile('{1:2, 2:3, 3:4}', '<string>', 'eval')\r
153 self.assertEqual(eval(c), {1:2, 2:3, 3:4})\r
154 c = compiler.compile('{1:2, 2:3, 3:4,}', '<string>', 'eval')\r
155 self.assertEqual(eval(c), {1:2, 2:3, 3:4})\r
156\r
157 def testSetComp(self):\r
158 c = compiler.compile('{x for x in range(1, 4)}', '<string>', 'eval')\r
159 self.assertEqual(eval(c), {1, 2, 3})\r
160 c = compiler.compile('{x * y for x in range(3) if x != 0'\r
161 ' for y in range(4) if y != 0}',\r
162 '<string>',\r
163 'eval')\r
164 self.assertEqual(eval(c), {1, 2, 3, 4, 6})\r
165\r
166 def testDictComp(self):\r
167 c = compiler.compile('{x:x+1 for x in range(1, 4)}', '<string>', 'eval')\r
168 self.assertEqual(eval(c), {1:2, 2:3, 3:4})\r
169 c = compiler.compile('{(x, y) : y for x in range(2) if x != 0'\r
170 ' for y in range(3) if y != 0}',\r
171 '<string>',\r
172 'eval')\r
173 self.assertEqual(eval(c), {(1, 2): 2, (1, 1): 1})\r
174\r
175 def testWith(self):\r
176 # SF bug 1638243\r
177 c = compiler.compile('from __future__ import with_statement\n'\r
178 'def f():\n'\r
179 ' with TrivialContext():\n'\r
180 ' return 1\n'\r
181 'result = f()',\r
182 '<string>',\r
183 'exec' )\r
184 dct = {'TrivialContext': TrivialContext}\r
185 exec c in dct\r
186 self.assertEqual(dct.get('result'), 1)\r
187\r
188 def testWithAss(self):\r
189 c = compiler.compile('from __future__ import with_statement\n'\r
190 'def f():\n'\r
191 ' with TrivialContext() as tc:\n'\r
192 ' return 1\n'\r
193 'result = f()',\r
194 '<string>',\r
195 'exec' )\r
196 dct = {'TrivialContext': TrivialContext}\r
197 exec c in dct\r
198 self.assertEqual(dct.get('result'), 1)\r
199\r
200 def testWithMult(self):\r
201 events = []\r
202 class Ctx:\r
203 def __init__(self, n):\r
204 self.n = n\r
205 def __enter__(self):\r
206 events.append(self.n)\r
207 def __exit__(self, *args):\r
208 pass\r
209 c = compiler.compile('from __future__ import with_statement\n'\r
210 'def f():\n'\r
211 ' with Ctx(1) as tc, Ctx(2) as tc2:\n'\r
212 ' return 1\n'\r
213 'result = f()',\r
214 '<string>',\r
215 'exec' )\r
216 dct = {'Ctx': Ctx}\r
217 exec c in dct\r
218 self.assertEqual(dct.get('result'), 1)\r
219 self.assertEqual(events, [1, 2])\r
220\r
221 def testGlobal(self):\r
222 code = compiler.compile('global x\nx=1', '<string>', 'exec')\r
223 d1 = {'__builtins__': {}}\r
224 d2 = {}\r
225 exec code in d1, d2\r
226 # x should be in the globals dict\r
227 self.assertEqual(d1.get('x'), 1)\r
228\r
229 def testPrintFunction(self):\r
230 c = compiler.compile('from __future__ import print_function\n'\r
231 'print("a", "b", sep="**", end="++", '\r
232 'file=output)',\r
233 '<string>',\r
234 'exec' )\r
235 dct = {'output': StringIO()}\r
236 exec c in dct\r
237 self.assertEqual(dct['output'].getvalue(), 'a**b++')\r
238\r
239 def _testErrEnc(self, src, text, offset):\r
240 try:\r
241 compile(src, "", "exec")\r
242 except SyntaxError, e:\r
243 self.assertEqual(e.offset, offset)\r
244 self.assertEqual(e.text, text)\r
245\r
246 def testSourceCodeEncodingsError(self):\r
247 # Test SyntaxError with encoding definition\r
248 sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n"\r
249 ascii = "print '12345678', '\n"\r
250 encdef = "#! -*- coding: ShiftJIS -*-\n"\r
251\r
252 # ascii source without encdef\r
253 self._testErrEnc(ascii, ascii, 19)\r
254\r
255 # ascii source with encdef\r
256 self._testErrEnc(encdef+ascii, ascii, 19)\r
257\r
258 # non-ascii source with encdef\r
259 self._testErrEnc(encdef+sjis, sjis, 19)\r
260\r
261 # ShiftJIS source without encdef\r
262 self._testErrEnc(sjis, sjis, 19)\r
263\r
264\r
265NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)\r
266\r
267###############################################################################\r
268# code below is just used to trigger some possible errors, for the benefit of\r
269# testLineNo\r
270###############################################################################\r
271\r
272class Toto:\r
273 """docstring"""\r
274 pass\r
275\r
276a, b = 2, 3\r
277[c, d] = 5, 6\r
278l = [(x, y) for x, y in zip(range(5), range(5,10))]\r
279l[0]\r
280l[3:4]\r
281d = {'a': 2}\r
282d = {}\r
283d = {x: y for x, y in zip(range(5), range(5,10))}\r
284s = {x for x in range(10)}\r
285s = {1}\r
286t = ()\r
287t = (1, 2)\r
288l = []\r
289l = [1, 2]\r
290if l:\r
291 pass\r
292else:\r
293 a, b = b, a\r
294\r
295try:\r
296 print yo\r
297except:\r
298 yo = 3\r
299else:\r
300 yo += 3\r
301\r
302try:\r
303 a += b\r
304finally:\r
305 b = 0\r
306\r
307from math import *\r
308\r
309###############################################################################\r
310\r
311def test_main():\r
312 global TEST_ALL\r
313 TEST_ALL = test.test_support.is_resource_enabled("cpu")\r
314 test.test_support.run_unittest(CompilerTest)\r
315\r
316if __name__ == "__main__":\r
317 test_main()\r