blob: 9976a47cf381f8b885a66ccc60f8ffa5098fefb9 [file] [log] [blame]
Jeremy Hylton4336eda2004-08-07 19:25:33 +00001import compiler
2import os
3import test.test_support
4import unittest
Raymond Hettingered20ad82004-09-04 20:09:13 +00005from random import random
Jeremy Hylton4336eda2004-08-07 19:25:33 +00006
7class CompilerTest(unittest.TestCase):
8
9 def testCompileLibrary(self):
10 # A simple but large test. Compile all the code in the
11 # standard library and its test suite. This doesn't verify
12 # that any of the code is correct, merely the compiler is able
13 # to generate some kind of code for it.
14
15 libdir = os.path.dirname(unittest.__file__)
16 testdir = os.path.dirname(test.test_support.__file__)
17
18 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000019 for basename in os.listdir(dir):
20 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000021 continue
Raymond Hettingered20ad82004-09-04 20:09:13 +000022 if not TEST_ALL and random() < 0.98:
23 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000024 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000025 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000026 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000027 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000028 buf = f.read()
29 f.close()
Tim Peters0955f292004-08-08 16:43:59 +000030 if "badsyntax" in basename:
31 self.assertRaises(SyntaxError, compiler.compile,
32 buf, basename, "exec")
33 else:
34 compiler.compile(buf, basename, "exec")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000035
Jeremy Hylton566d9342004-09-07 15:28:01 +000036 def testLineNo(self):
37 # Test that all nodes except Module have a correct lineno attribute.
38 filename = __file__
39 if filename.endswith(".pyc") or filename.endswith(".pyo"):
40 filename = filename[:-1]
41 tree = compiler.parseFile(filename)
42 self.check_lineno(tree)
43
44 def check_lineno(self, node):
45 try:
46 self._check_lineno(node)
47 except AssertionError:
48 print node.__class__, node.lineno
49 raise
50
51 def _check_lineno(self, node):
52 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000053 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000054 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000055 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000056 "lineno=%s on %s" % (node.lineno, node.__class__))
57 for child in node.getChildNodes():
58 self.check_lineno(child)
59
60NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
61
62###############################################################################
63# code below is just used to trigger some possible errors, for the benefit of
64# testLineNo
65###############################################################################
66
67class Toto:
68 """docstring"""
69 pass
70
71a, b = 2, 3
72[c, d] = 5, 6
73l = [(x, y) for x, y in zip(range(5), range(5,10))]
74l[0]
75l[3:4]
76if l:
77 pass
78else:
79 a, b = b, a
80
81try:
82 print yo
83except:
84 yo = 3
85else:
86 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +000087
Jeremy Hylton566d9342004-09-07 15:28:01 +000088try:
89 a += b
90finally:
91 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +000092
Michael W. Hudsone0b855f2004-11-08 16:46:02 +000093from math import *
94
Jeremy Hylton566d9342004-09-07 15:28:01 +000095###############################################################################
96
Jeremy Hylton4336eda2004-08-07 19:25:33 +000097def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +000098 global TEST_ALL
99 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000100 test.test_support.run_unittest(CompilerTest)
101
102if __name__ == "__main__":
103 test_main()