Fix a bug in the ``compiler`` package that caused invalid code to be
[python/dscho.git] / Lib / test / test_compiler.py
blob81f2ea89250504071537814a719a1708d1920959
1 import compiler
2 from compiler.ast import flatten
3 import os, sys, time, unittest
4 import test.test_support
5 from random import random
7 # How much time in seconds can pass before we print a 'Still working' message.
8 _PRINT_WORKING_MSG_INTERVAL = 5 * 60
10 class CompilerTest(unittest.TestCase):
12 def testCompileLibrary(self):
13 # A simple but large test. Compile all the code in the
14 # standard library and its test suite. This doesn't verify
15 # that any of the code is correct, merely the compiler is able
16 # to generate some kind of code for it.
18 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
19 libdir = os.path.dirname(unittest.__file__)
20 testdir = os.path.dirname(test.test_support.__file__)
22 for dir in [libdir, testdir]:
23 for basename in os.listdir(dir):
24 # Print still working message since this test can be really slow
25 if next_time <= time.time():
26 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
27 print >>sys.__stdout__, \
28 ' testCompileLibrary still working, be patient...'
29 sys.__stdout__.flush()
31 if not basename.endswith(".py"):
32 continue
33 if not TEST_ALL and random() < 0.98:
34 continue
35 path = os.path.join(dir, basename)
36 if test.test_support.verbose:
37 print "compiling", path
38 f = open(path, "U")
39 buf = f.read()
40 f.close()
41 if "badsyntax" in basename or "bad_coding" in basename:
42 self.assertRaises(SyntaxError, compiler.compile,
43 buf, basename, "exec")
44 else:
45 try:
46 compiler.compile(buf, basename, "exec")
47 except Exception, e:
48 args = list(e.args)
49 args[0] += "[in file %s]" % basename
50 e.args = tuple(args)
51 raise
53 def testNewClassSyntax(self):
54 compiler.compile("class foo():pass\n\n","<string>","exec")
56 def testYieldExpr(self):
57 compiler.compile("def g(): yield\n\n", "<string>", "exec")
59 def testTryExceptFinally(self):
60 # Test that except and finally clauses in one try stmt are recognized
61 c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
62 "<string>", "exec")
63 dct = {}
64 exec c in dct
65 self.assertEquals(dct.get('e'), 1)
66 self.assertEquals(dct.get('f'), 1)
68 def testDefaultArgs(self):
69 self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
71 def testDocstrings(self):
72 c = compiler.compile('"doc"', '<string>', 'exec')
73 self.assert_('__doc__' in c.co_names)
74 c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
75 g = {}
76 exec c in g
77 self.assertEquals(g['f'].__doc__, "doc")
79 def testLineNo(self):
80 # Test that all nodes except Module have a correct lineno attribute.
81 filename = __file__
82 if filename.endswith((".pyc", ".pyo")):
83 filename = filename[:-1]
84 tree = compiler.parseFile(filename)
85 self.check_lineno(tree)
87 def check_lineno(self, node):
88 try:
89 self._check_lineno(node)
90 except AssertionError:
91 print node.__class__, node.lineno
92 raise
94 def _check_lineno(self, node):
95 if not node.__class__ in NOLINENO:
96 self.assert_(isinstance(node.lineno, int),
97 "lineno=%s on %s" % (node.lineno, node.__class__))
98 self.assert_(node.lineno > 0,
99 "lineno=%s on %s" % (node.lineno, node.__class__))
100 for child in node.getChildNodes():
101 self.check_lineno(child)
103 def testFlatten(self):
104 self.assertEquals(flatten([1, [2]]), [1, 2])
105 self.assertEquals(flatten((1, (2,))), [1, 2])
107 def testNestedScope(self):
108 c = compiler.compile('def g():\n'
109 ' a = 1\n'
110 ' def f(): return a + 2\n'
111 ' return f()\n'
112 'result = g()',
113 '<string>',
114 'exec')
115 dct = {}
116 exec c in dct
117 self.assertEquals(dct.get('result'), 3)
119 def testGenExp(self):
120 c = compiler.compile('list((i,j) for i in range(3) if i < 3'
121 ' for j in range(4) if j > 2)',
122 '<string>',
123 'eval')
124 self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
127 NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
129 ###############################################################################
130 # code below is just used to trigger some possible errors, for the benefit of
131 # testLineNo
132 ###############################################################################
134 class Toto:
135 """docstring"""
136 pass
138 a, b = 2, 3
139 [c, d] = 5, 6
140 l = [(x, y) for x, y in zip(range(5), range(5,10))]
141 l[0]
142 l[3:4]
143 d = {'a': 2}
144 d = {}
145 t = ()
146 t = (1, 2)
147 l = []
148 l = [1, 2]
149 if l:
150 pass
151 else:
152 a, b = b, a
154 try:
155 print yo
156 except:
157 yo = 3
158 else:
159 yo += 3
161 try:
162 a += b
163 finally:
164 b = 0
166 from math import *
168 ###############################################################################
170 def test_main():
171 global TEST_ALL
172 TEST_ALL = test.test_support.is_resource_enabled("compiler")
173 test.test_support.run_unittest(CompilerTest)
175 if __name__ == "__main__":
176 test_main()