blob: 17f181ee41c2396861a299183ebfef408381bdc0 [file] [log] [blame]
Jeremy Hylton4336eda2004-08-07 19:25:33 +00001import compiler
Neil Schemenauerf3694702005-06-02 05:55:20 +00002from compiler.ast import flatten
Neal Norwitz07c60712006-04-13 06:34:59 +00003import os, sys, time, unittest
Jeremy Hylton4336eda2004-08-07 19:25:33 +00004import test.test_support
Raymond Hettingered20ad82004-09-04 20:09:13 +00005from random import random
Jeremy Hylton4336eda2004-08-07 19:25:33 +00006
Neal Norwitz07c60712006-04-13 06:34:59 +00007# How much time in seconds can pass before we print a 'Still working' message.
8_PRINT_WORKING_MSG_INTERVAL = 5 * 60
9
Jeremy Hylton4336eda2004-08-07 19:25:33 +000010class CompilerTest(unittest.TestCase):
11
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.
Tim Peters2841af42006-01-07 23:20:46 +000017
Neal Norwitz07c60712006-04-13 06:34:59 +000018 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Jeremy Hylton4336eda2004-08-07 19:25:33 +000019 libdir = os.path.dirname(unittest.__file__)
20 testdir = os.path.dirname(test.test_support.__file__)
21
22 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000023 for basename in os.listdir(dir):
Neal Norwitz07c60712006-04-13 06:34:59 +000024 # 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...'
Neal Norwitzdd28d1c2006-04-28 04:34:43 +000029 sys.__stdout__.flush()
Neal Norwitz07c60712006-04-13 06:34:59 +000030
Tim Peters2a5f6562004-08-08 16:37:37 +000031 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000032 continue
Guido van Rossum5bde08d2006-03-02 04:24:01 +000033 if not TEST_ALL and random() < 0.98:
Raymond Hettingered20ad82004-09-04 20:09:13 +000034 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000035 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000036 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000037 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000038 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000039 buf = f.read()
40 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000041 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000042 self.assertRaises(SyntaxError, compiler.compile,
43 buf, basename, "exec")
44 else:
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000045 try:
46 compiler.compile(buf, basename, "exec")
47 except Exception, e:
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000048 args = list(e.args)
49 args[0] += "[in file %s]" % basename
50 e.args = tuple(args)
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000051 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000052
Brett Cannonf4189912005-04-09 02:30:16 +000053 def testNewClassSyntax(self):
54 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000055
Guido van Rossum5bde08d2006-03-02 04:24:01 +000056 def testYieldExpr(self):
57 compiler.compile("def g(): yield\n\n", "<string>", "exec")
58
Georg Brandlf57c54d2006-06-22 14:46:46 +000059 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)
67
Georg Brandl1bb62302006-05-03 18:18:32 +000068 def testDefaultArgs(self):
69 self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
70
Jeremy Hylton566d9342004-09-07 15:28:01 +000071 def testLineNo(self):
72 # Test that all nodes except Module have a correct lineno attribute.
73 filename = __file__
Georg Brandlb2afe852006-06-09 20:43:48 +000074 if filename.endswith((".pyc", ".pyo")):
Jeremy Hylton566d9342004-09-07 15:28:01 +000075 filename = filename[:-1]
76 tree = compiler.parseFile(filename)
77 self.check_lineno(tree)
78
79 def check_lineno(self, node):
80 try:
81 self._check_lineno(node)
82 except AssertionError:
83 print node.__class__, node.lineno
84 raise
85
86 def _check_lineno(self, node):
87 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000088 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000089 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000090 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000091 "lineno=%s on %s" % (node.lineno, node.__class__))
92 for child in node.getChildNodes():
93 self.check_lineno(child)
94
Neil Schemenauerf3694702005-06-02 05:55:20 +000095 def testFlatten(self):
96 self.assertEquals(flatten([1, [2]]), [1, 2])
97 self.assertEquals(flatten((1, (2,))), [1, 2])
98
Jeremy Hylton566d9342004-09-07 15:28:01 +000099NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
100
101###############################################################################
102# code below is just used to trigger some possible errors, for the benefit of
103# testLineNo
104###############################################################################
105
106class Toto:
107 """docstring"""
108 pass
109
110a, b = 2, 3
111[c, d] = 5, 6
112l = [(x, y) for x, y in zip(range(5), range(5,10))]
113l[0]
114l[3:4]
Georg Brandlf57c54d2006-06-22 14:46:46 +0000115d = {'a': 2}
116d = {}
117t = ()
118t = (1, 2)
119l = []
120l = [1, 2]
Jeremy Hylton566d9342004-09-07 15:28:01 +0000121if l:
122 pass
123else:
124 a, b = b, a
125
126try:
127 print yo
128except:
129 yo = 3
130else:
131 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +0000132
Jeremy Hylton566d9342004-09-07 15:28:01 +0000133try:
134 a += b
135finally:
136 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000137
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000138from math import *
139
Jeremy Hylton566d9342004-09-07 15:28:01 +0000140###############################################################################
141
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000142def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000143 global TEST_ALL
144 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000145 test.test_support.run_unittest(CompilerTest)
146
147if __name__ == "__main__":
148 test_main()