blob: 7d5ec7c5b81e2d0154f435cbf1ea4aee3204985d [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
Georg Brandlab496842007-01-27 17:43:02 +000010class TrivialContext(object):
11 def __enter__(self):
12 return self
13 def __exit__(self, *exc_info):
14 pass
15
Jeremy Hylton4336eda2004-08-07 19:25:33 +000016class CompilerTest(unittest.TestCase):
17
18 def testCompileLibrary(self):
19 # A simple but large test. Compile all the code in the
20 # standard library and its test suite. This doesn't verify
21 # that any of the code is correct, merely the compiler is able
22 # to generate some kind of code for it.
Tim Peters2841af42006-01-07 23:20:46 +000023
Neal Norwitz07c60712006-04-13 06:34:59 +000024 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Jeremy Hylton4336eda2004-08-07 19:25:33 +000025 libdir = os.path.dirname(unittest.__file__)
26 testdir = os.path.dirname(test.test_support.__file__)
27
28 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000029 for basename in os.listdir(dir):
Neal Norwitz07c60712006-04-13 06:34:59 +000030 # Print still working message since this test can be really slow
31 if next_time <= time.time():
32 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
33 print >>sys.__stdout__, \
34 ' testCompileLibrary still working, be patient...'
Neal Norwitzdd28d1c2006-04-28 04:34:43 +000035 sys.__stdout__.flush()
Neal Norwitz07c60712006-04-13 06:34:59 +000036
Tim Peters2a5f6562004-08-08 16:37:37 +000037 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000038 continue
Guido van Rossum5bde08d2006-03-02 04:24:01 +000039 if not TEST_ALL and random() < 0.98:
Raymond Hettingered20ad82004-09-04 20:09:13 +000040 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000041 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000042 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000043 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000044 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000045 buf = f.read()
46 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000047 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000048 self.assertRaises(SyntaxError, compiler.compile,
49 buf, basename, "exec")
50 else:
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000051 try:
52 compiler.compile(buf, basename, "exec")
53 except Exception, e:
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000054 args = list(e.args)
Christian Heimesc5f05e42008-02-23 17:40:11 +000055 args.append("in file %s]" % basename)
56 #args[0] += "[in file %s]" % basename
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000057 e.args = tuple(args)
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000058 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000059
Brett Cannonf4189912005-04-09 02:30:16 +000060 def testNewClassSyntax(self):
61 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000062
Guido van Rossum5bde08d2006-03-02 04:24:01 +000063 def testYieldExpr(self):
64 compiler.compile("def g(): yield\n\n", "<string>", "exec")
65
Georg Brandlf57c54d2006-06-22 14:46:46 +000066 def testTryExceptFinally(self):
67 # Test that except and finally clauses in one try stmt are recognized
68 c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
69 "<string>", "exec")
70 dct = {}
71 exec c in dct
72 self.assertEquals(dct.get('e'), 1)
73 self.assertEquals(dct.get('f'), 1)
74
Georg Brandl1bb62302006-05-03 18:18:32 +000075 def testDefaultArgs(self):
76 self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
77
Georg Brandledd9b0d2006-07-29 09:33:26 +000078 def testDocstrings(self):
79 c = compiler.compile('"doc"', '<string>', 'exec')
80 self.assert_('__doc__' in c.co_names)
81 c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
82 g = {}
83 exec c in g
84 self.assertEquals(g['f'].__doc__, "doc")
85
Jeremy Hylton566d9342004-09-07 15:28:01 +000086 def testLineNo(self):
87 # Test that all nodes except Module have a correct lineno attribute.
88 filename = __file__
Georg Brandlb2afe852006-06-09 20:43:48 +000089 if filename.endswith((".pyc", ".pyo")):
Jeremy Hylton566d9342004-09-07 15:28:01 +000090 filename = filename[:-1]
91 tree = compiler.parseFile(filename)
92 self.check_lineno(tree)
93
94 def check_lineno(self, node):
95 try:
96 self._check_lineno(node)
97 except AssertionError:
98 print node.__class__, node.lineno
99 raise
100
101 def _check_lineno(self, node):
102 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +0000103 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +0000104 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +0000105 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +0000106 "lineno=%s on %s" % (node.lineno, node.__class__))
107 for child in node.getChildNodes():
108 self.check_lineno(child)
109
Neil Schemenauerf3694702005-06-02 05:55:20 +0000110 def testFlatten(self):
111 self.assertEquals(flatten([1, [2]]), [1, 2])
112 self.assertEquals(flatten((1, (2,))), [1, 2])
113
Neil Schemenauer06ded092006-08-04 16:20:30 +0000114 def testNestedScope(self):
115 c = compiler.compile('def g():\n'
116 ' a = 1\n'
117 ' def f(): return a + 2\n'
118 ' return f()\n'
119 'result = g()',
120 '<string>',
121 'exec')
122 dct = {}
123 exec c in dct
124 self.assertEquals(dct.get('result'), 3)
125
Neil Schemenauer4c6b0d52006-08-16 23:38:05 +0000126 def testGenExp(self):
127 c = compiler.compile('list((i,j) for i in range(3) if i < 3'
128 ' for j in range(4) if j > 2)',
129 '<string>',
130 'eval')
131 self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
132
Georg Brandlab496842007-01-27 17:43:02 +0000133 def testWith(self):
134 # SF bug 1638243
135 c = compiler.compile('from __future__ import with_statement\n'
136 'def f():\n'
137 ' with TrivialContext():\n'
138 ' return 1\n'
139 'result = f()',
140 '<string>',
141 'exec' )
142 dct = {'TrivialContext': TrivialContext}
143 exec c in dct
144 self.assertEquals(dct.get('result'), 1)
145
146 def testWithAss(self):
147 c = compiler.compile('from __future__ import with_statement\n'
148 'def f():\n'
149 ' with TrivialContext() as tc:\n'
150 ' return 1\n'
151 'result = f()',
152 '<string>',
153 'exec' )
154 dct = {'TrivialContext': TrivialContext}
155 exec c in dct
156 self.assertEquals(dct.get('result'), 1)
157
Tim Petersb1ccc4d2006-08-04 22:00:35 +0000158
Martin v. Löwisa5136192007-09-04 14:19:28 +0000159 def _testErrEnc(self, src, text, offset):
160 try:
161 compile(src, "", "exec")
162 except SyntaxError, e:
163 self.assertEquals(e.offset, offset)
164 self.assertEquals(e.text, text)
165
166 def testSourceCodeEncodingsError(self):
167 # Test SyntaxError with encoding definition
168 sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n"
169 ascii = "print '12345678', '\n"
170 encdef = "#! -*- coding: ShiftJIS -*-\n"
171
172 # ascii source without encdef
173 self._testErrEnc(ascii, ascii, 19)
174
175 # ascii source with encdef
176 self._testErrEnc(encdef+ascii, ascii, 19)
177
178 # non-ascii source with encdef
179 self._testErrEnc(encdef+sjis, sjis, 19)
180
181 # ShiftJIS source without encdef
182 self._testErrEnc(sjis, sjis, 19)
183
184
Jeremy Hylton566d9342004-09-07 15:28:01 +0000185NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
186
187###############################################################################
188# code below is just used to trigger some possible errors, for the benefit of
189# testLineNo
190###############################################################################
191
192class Toto:
193 """docstring"""
194 pass
195
196a, b = 2, 3
197[c, d] = 5, 6
198l = [(x, y) for x, y in zip(range(5), range(5,10))]
199l[0]
200l[3:4]
Georg Brandlf57c54d2006-06-22 14:46:46 +0000201d = {'a': 2}
202d = {}
203t = ()
204t = (1, 2)
205l = []
206l = [1, 2]
Jeremy Hylton566d9342004-09-07 15:28:01 +0000207if l:
208 pass
209else:
210 a, b = b, a
211
212try:
213 print yo
214except:
215 yo = 3
216else:
217 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +0000218
Jeremy Hylton566d9342004-09-07 15:28:01 +0000219try:
220 a += b
221finally:
222 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000223
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000224from math import *
225
Jeremy Hylton566d9342004-09-07 15:28:01 +0000226###############################################################################
227
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000228def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000229 global TEST_ALL
230 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000231 test.test_support.run_unittest(CompilerTest)
232
233if __name__ == "__main__":
234 test_main()