blob: a59d6aa7bacedff017c9d1360bbaf800ec6a5069 [file] [log] [blame]
Jeremy Hylton4336eda2004-08-07 19:25:33 +00001import compiler
Neil Schemenauerf3694702005-06-02 05:55:20 +00002from compiler.ast import flatten
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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...'
29
Tim Peters2a5f6562004-08-08 16:37:37 +000030 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000031 continue
Guido van Rossum5bde08d2006-03-02 04:24:01 +000032 if not TEST_ALL and random() < 0.98:
Raymond Hettingered20ad82004-09-04 20:09:13 +000033 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000034 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000035 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000036 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000037 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000038 buf = f.read()
39 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000040 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000041 self.assertRaises(SyntaxError, compiler.compile,
42 buf, basename, "exec")
43 else:
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000044 try:
45 compiler.compile(buf, basename, "exec")
46 except Exception, e:
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000047 args = list(e.args)
48 args[0] += "[in file %s]" % basename
49 e.args = tuple(args)
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000050 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000051
Brett Cannonf4189912005-04-09 02:30:16 +000052 def testNewClassSyntax(self):
53 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000054
Guido van Rossum5bde08d2006-03-02 04:24:01 +000055 def testYieldExpr(self):
56 compiler.compile("def g(): yield\n\n", "<string>", "exec")
57
Jeremy Hylton566d9342004-09-07 15:28:01 +000058 def testLineNo(self):
59 # Test that all nodes except Module have a correct lineno attribute.
60 filename = __file__
61 if filename.endswith(".pyc") or filename.endswith(".pyo"):
62 filename = filename[:-1]
63 tree = compiler.parseFile(filename)
64 self.check_lineno(tree)
65
66 def check_lineno(self, node):
67 try:
68 self._check_lineno(node)
69 except AssertionError:
70 print node.__class__, node.lineno
71 raise
72
73 def _check_lineno(self, node):
74 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +000075 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +000076 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +000077 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +000078 "lineno=%s on %s" % (node.lineno, node.__class__))
79 for child in node.getChildNodes():
80 self.check_lineno(child)
81
Neil Schemenauerf3694702005-06-02 05:55:20 +000082 def testFlatten(self):
83 self.assertEquals(flatten([1, [2]]), [1, 2])
84 self.assertEquals(flatten((1, (2,))), [1, 2])
85
Jeremy Hylton566d9342004-09-07 15:28:01 +000086NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
87
88###############################################################################
89# code below is just used to trigger some possible errors, for the benefit of
90# testLineNo
91###############################################################################
92
93class Toto:
94 """docstring"""
95 pass
96
97a, b = 2, 3
98[c, d] = 5, 6
99l = [(x, y) for x, y in zip(range(5), range(5,10))]
100l[0]
101l[3:4]
102if l:
103 pass
104else:
105 a, b = b, a
106
107try:
108 print yo
109except:
110 yo = 3
111else:
112 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +0000113
Jeremy Hylton566d9342004-09-07 15:28:01 +0000114try:
115 a += b
116finally:
117 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000118
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000119from math import *
120
Jeremy Hylton566d9342004-09-07 15:28:01 +0000121###############################################################################
122
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000123def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000124 global TEST_ALL
125 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000126 test.test_support.run_unittest(CompilerTest)
127
128if __name__ == "__main__":
129 test_main()