blob: 32302a19c22f45b755b8629e05889bdd2b03e760 [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
Brett Cannonf4189912005-04-09 02:30:16 +000036 def testNewClassSyntax(self):
37 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000038
Jeremy Hylton566d9342004-09-07 15:28:01 +000039 def testLineNo(self):
40 # Test that all nodes except Module have a correct lineno attribute.
41 filename = __file__
42 if filename.endswith(".pyc") or filename.endswith(".pyo"):
43 filename = filename[:-1]
44 tree = compiler.parseFile(filename)
45 self.check_lineno(tree)
46
47 def check_lineno(self, node):
48 try:
49 self._check_lineno(node)
50 except AssertionError:
51 print node.__class__, node.lineno
52 raise
53
54 def _check_lineno(self, node):
55 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000056 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000057 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000058 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000059 "lineno=%s on %s" % (node.lineno, node.__class__))
60 for child in node.getChildNodes():
61 self.check_lineno(child)
62
63NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
64
65###############################################################################
66# code below is just used to trigger some possible errors, for the benefit of
67# testLineNo
68###############################################################################
69
70class Toto:
71 """docstring"""
72 pass
73
74a, b = 2, 3
75[c, d] = 5, 6
76l = [(x, y) for x, y in zip(range(5), range(5,10))]
77l[0]
78l[3:4]
79if l:
80 pass
81else:
82 a, b = b, a
83
84try:
85 print yo
86except:
87 yo = 3
88else:
89 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +000090
Jeremy Hylton566d9342004-09-07 15:28:01 +000091try:
92 a += b
93finally:
94 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +000095
Michael W. Hudsone0b855f2004-11-08 16:46:02 +000096from math import *
97
Jeremy Hylton566d9342004-09-07 15:28:01 +000098###############################################################################
99
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000100def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000101 global TEST_ALL
102 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000103 test.test_support.run_unittest(CompilerTest)
104
105if __name__ == "__main__":
106 test_main()