blob: 5e7b15c38cdce75f0ca946a2720e82486d4f58af [file] [log] [blame]
Jeremy Hylton4336eda2004-08-07 19:25:33 +00001import compiler
Neil Schemenauerf3694702005-06-02 05:55:20 +00002from compiler.ast import flatten
Jeremy Hylton4336eda2004-08-07 19:25:33 +00003import os
4import test.test_support
5import unittest
Raymond Hettingered20ad82004-09-04 20:09:13 +00006from random import random
Jeremy Hylton4336eda2004-08-07 19:25:33 +00007
8class CompilerTest(unittest.TestCase):
9
10 def testCompileLibrary(self):
11 # A simple but large test. Compile all the code in the
12 # standard library and its test suite. This doesn't verify
13 # that any of the code is correct, merely the compiler is able
14 # to generate some kind of code for it.
Tim Peters2841af42006-01-07 23:20:46 +000015
Jeremy Hylton4336eda2004-08-07 19:25:33 +000016 libdir = os.path.dirname(unittest.__file__)
17 testdir = os.path.dirname(test.test_support.__file__)
18
19 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000020 for basename in os.listdir(dir):
21 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000022 continue
Guido van Rossum5bde08d2006-03-02 04:24:01 +000023 if not TEST_ALL and random() < 0.98:
Raymond Hettingered20ad82004-09-04 20:09:13 +000024 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000025 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000026 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000027 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000028 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000029 buf = f.read()
30 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000031 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000032 self.assertRaises(SyntaxError, compiler.compile,
33 buf, basename, "exec")
34 else:
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000035 try:
36 compiler.compile(buf, basename, "exec")
37 except Exception, e:
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000038 args = list(e.args)
39 args[0] += "[in file %s]" % basename
40 e.args = tuple(args)
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000041 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000042
Brett Cannonf4189912005-04-09 02:30:16 +000043 def testNewClassSyntax(self):
44 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000045
Guido van Rossum5bde08d2006-03-02 04:24:01 +000046 def testYieldExpr(self):
47 compiler.compile("def g(): yield\n\n", "<string>", "exec")
48
Jeremy Hylton566d9342004-09-07 15:28:01 +000049 def testLineNo(self):
50 # Test that all nodes except Module have a correct lineno attribute.
51 filename = __file__
52 if filename.endswith(".pyc") or filename.endswith(".pyo"):
53 filename = filename[:-1]
54 tree = compiler.parseFile(filename)
55 self.check_lineno(tree)
56
57 def check_lineno(self, node):
58 try:
59 self._check_lineno(node)
60 except AssertionError:
61 print node.__class__, node.lineno
62 raise
63
64 def _check_lineno(self, node):
65 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000066 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000067 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000068 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000069 "lineno=%s on %s" % (node.lineno, node.__class__))
70 for child in node.getChildNodes():
71 self.check_lineno(child)
72
Neil Schemenauerf3694702005-06-02 05:55:20 +000073 def testFlatten(self):
74 self.assertEquals(flatten([1, [2]]), [1, 2])
75 self.assertEquals(flatten((1, (2,))), [1, 2])
76
Jeremy Hylton566d9342004-09-07 15:28:01 +000077NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
78
79###############################################################################
80# code below is just used to trigger some possible errors, for the benefit of
81# testLineNo
82###############################################################################
83
84class Toto:
85 """docstring"""
86 pass
87
88a, b = 2, 3
89[c, d] = 5, 6
90l = [(x, y) for x, y in zip(range(5), range(5,10))]
91l[0]
92l[3:4]
93if l:
94 pass
95else:
96 a, b = b, a
97
98try:
99 print yo
100except:
101 yo = 3
102else:
103 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +0000104
Jeremy Hylton566d9342004-09-07 15:28:01 +0000105try:
106 a += b
107finally:
108 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000109
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000110from math import *
111
Jeremy Hylton566d9342004-09-07 15:28:01 +0000112###############################################################################
113
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000114def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000115 global TEST_ALL
116 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000117 test.test_support.run_unittest(CompilerTest)
118
119if __name__ == "__main__":
120 test_main()