Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | import compiler |
2 | import os | |
3 | import test.test_support | |
4 | import unittest | |
5 | from random import random | |
6 | ||
7 | class 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]: | |
19 | for basename in os.listdir(dir): | |
20 | if not basename.endswith(".py"): | |
21 | continue | |
22 | if not TEST_ALL and random() < 0.98: | |
23 | continue | |
24 | path = os.path.join(dir, basename) | |
25 | if test.test_support.verbose: | |
26 | print "compiling", path | |
27 | f = open(path, "U") | |
28 | buf = f.read() | |
29 | f.close() | |
30 | if "badsyntax" in basename: | |
31 | self.assertRaises(SyntaxError, compiler.compile, | |
32 | buf, basename, "exec") | |
33 | else: | |
34 | compiler.compile(buf, basename, "exec") | |
35 | ||
36 | 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: | |
53 | self.assert_(isinstance(node.lineno, int), | |
54 | "lineno=%s on %s" % (node.lineno, node.__class__)) | |
55 | self.assert_(node.lineno > 0, | |
56 | "lineno=%s on %s" % (node.lineno, node.__class__)) | |
57 | for child in node.getChildNodes(): | |
58 | self.check_lineno(child) | |
59 | ||
60 | NOLINENO = (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 | ||
67 | class Toto: | |
68 | """docstring""" | |
69 | pass | |
70 | ||
71 | a, b = 2, 3 | |
72 | [c, d] = 5, 6 | |
73 | l = [(x, y) for x, y in zip(range(5), range(5,10))] | |
74 | l[0] | |
75 | l[3:4] | |
76 | if l: | |
77 | pass | |
78 | else: | |
79 | a, b = b, a | |
80 | ||
81 | try: | |
82 | print yo | |
83 | except: | |
84 | yo = 3 | |
85 | else: | |
86 | yo += 3 | |
87 | ||
88 | try: | |
89 | a += b | |
90 | finally: | |
91 | b = 0 | |
92 | ||
93 | from math import * | |
94 | ||
95 | ############################################################################### | |
96 | ||
97 | def test_main(): | |
98 | global TEST_ALL | |
99 | TEST_ALL = test.test_support.is_resource_enabled("compiler") | |
100 | test.test_support.run_unittest(CompilerTest) | |
101 | ||
102 | if __name__ == "__main__": | |
103 | test_main() |