blob: 6394b53a0cf8262ac558f0831e4dde77a132b80c [file] [log] [blame]
Brett Cannon6192df12008-05-10 02:58:26 +00001import test.test_support
2compiler = test.test_support.import_module('compiler', deprecated=True)
Neil Schemenauerf3694702005-06-02 05:55:20 +00003from compiler.ast import flatten
Neal Norwitz07c60712006-04-13 06:34:59 +00004import os, sys, time, unittest
Raymond Hettingered20ad82004-09-04 20:09:13 +00005from random import random
Eric Smith14cae962008-03-19 03:13:34 +00006from StringIO import StringIO
Jeremy Hylton4336eda2004-08-07 19:25:33 +00007
Neal Norwitz07c60712006-04-13 06:34:59 +00008# How much time in seconds can pass before we print a 'Still working' message.
9_PRINT_WORKING_MSG_INTERVAL = 5 * 60
10
Georg Brandlab496842007-01-27 17:43:02 +000011class TrivialContext(object):
12 def __enter__(self):
13 return self
14 def __exit__(self, *exc_info):
15 pass
16
Jeremy Hylton4336eda2004-08-07 19:25:33 +000017class CompilerTest(unittest.TestCase):
18
19 def testCompileLibrary(self):
20 # A simple but large test. Compile all the code in the
21 # standard library and its test suite. This doesn't verify
22 # that any of the code is correct, merely the compiler is able
23 # to generate some kind of code for it.
Tim Peters2841af42006-01-07 23:20:46 +000024
Neal Norwitz07c60712006-04-13 06:34:59 +000025 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Ezio Melotti7999dea2010-04-02 21:43:10 +000026 # warning: if 'os' or 'test_support' are moved in some other dir,
27 # they should be changed here.
28 libdir = os.path.dirname(os.__file__)
Jeremy Hylton4336eda2004-08-07 19:25:33 +000029 testdir = os.path.dirname(test.test_support.__file__)
30
31 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000032 for basename in os.listdir(dir):
Neal Norwitz07c60712006-04-13 06:34:59 +000033 # Print still working message since this test can be really slow
34 if next_time <= time.time():
35 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
36 print >>sys.__stdout__, \
37 ' testCompileLibrary still working, be patient...'
Neal Norwitzdd28d1c2006-04-28 04:34:43 +000038 sys.__stdout__.flush()
Neal Norwitz07c60712006-04-13 06:34:59 +000039
Tim Peters2a5f6562004-08-08 16:37:37 +000040 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000041 continue
Guido van Rossum5bde08d2006-03-02 04:24:01 +000042 if not TEST_ALL and random() < 0.98:
Raymond Hettingered20ad82004-09-04 20:09:13 +000043 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000044 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000045 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000046 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000047 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000048 buf = f.read()
49 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000050 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000051 self.assertRaises(SyntaxError, compiler.compile,
52 buf, basename, "exec")
53 else:
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000054 try:
55 compiler.compile(buf, basename, "exec")
56 except Exception, e:
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000057 args = list(e.args)
Christian Heimesc5f05e42008-02-23 17:40:11 +000058 args.append("in file %s]" % basename)
59 #args[0] += "[in file %s]" % basename
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000060 e.args = tuple(args)
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000061 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000062
Brett Cannonf4189912005-04-09 02:30:16 +000063 def testNewClassSyntax(self):
64 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000065
Guido van Rossum5bde08d2006-03-02 04:24:01 +000066 def testYieldExpr(self):
67 compiler.compile("def g(): yield\n\n", "<string>", "exec")
68
Amaury Forgeot d'Arc67f24f12008-08-20 00:08:47 +000069 def testKeywordAfterStarargs(self):
70 def f(*args, **kwargs):
71 self.assertEqual((args, kwargs), ((2,3), {'x': 1, 'y': 4}))
72 c = compiler.compile('f(x=1, *(2, 3), y=4)', '<string>', 'exec')
73 exec c in {'f': f}
74
75 self.assertRaises(SyntaxError, compiler.parse, "foo(a=1, b)")
76 self.assertRaises(SyntaxError, compiler.parse, "foo(1, *args, 3)")
77
Georg Brandlf57c54d2006-06-22 14:46:46 +000078 def testTryExceptFinally(self):
79 # Test that except and finally clauses in one try stmt are recognized
Ezio Melottidde5b942010-02-03 05:37:26 +000080 c = compiler.compile("try:\n 1//0\nexcept:\n e = 1\nfinally:\n f = 1",
Georg Brandlf57c54d2006-06-22 14:46:46 +000081 "<string>", "exec")
82 dct = {}
83 exec c in dct
84 self.assertEquals(dct.get('e'), 1)
85 self.assertEquals(dct.get('f'), 1)
86
Georg Brandl1bb62302006-05-03 18:18:32 +000087 def testDefaultArgs(self):
88 self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
89
Georg Brandledd9b0d2006-07-29 09:33:26 +000090 def testDocstrings(self):
91 c = compiler.compile('"doc"', '<string>', 'exec')
Ezio Melottiaa980582010-01-23 23:04:36 +000092 self.assertIn('__doc__', c.co_names)
Georg Brandledd9b0d2006-07-29 09:33:26 +000093 c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
94 g = {}
95 exec c in g
96 self.assertEquals(g['f'].__doc__, "doc")
97
Jeremy Hylton566d9342004-09-07 15:28:01 +000098 def testLineNo(self):
99 # Test that all nodes except Module have a correct lineno attribute.
100 filename = __file__
Georg Brandlb2afe852006-06-09 20:43:48 +0000101 if filename.endswith((".pyc", ".pyo")):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000102 filename = filename[:-1]
103 tree = compiler.parseFile(filename)
104 self.check_lineno(tree)
105
106 def check_lineno(self, node):
107 try:
108 self._check_lineno(node)
109 except AssertionError:
110 print node.__class__, node.lineno
111 raise
112
113 def _check_lineno(self, node):
114 if not node.__class__ in NOLINENO:
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000115 self.assertIsInstance(node.lineno, int,
Jeremy Hylton566d9342004-09-07 15:28:01 +0000116 "lineno=%s on %s" % (node.lineno, node.__class__))
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000117 self.assertTrue(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +0000118 "lineno=%s on %s" % (node.lineno, node.__class__))
119 for child in node.getChildNodes():
120 self.check_lineno(child)
121
Neil Schemenauerf3694702005-06-02 05:55:20 +0000122 def testFlatten(self):
123 self.assertEquals(flatten([1, [2]]), [1, 2])
124 self.assertEquals(flatten((1, (2,))), [1, 2])
125
Neil Schemenauer06ded092006-08-04 16:20:30 +0000126 def testNestedScope(self):
127 c = compiler.compile('def g():\n'
128 ' a = 1\n'
129 ' def f(): return a + 2\n'
130 ' return f()\n'
131 'result = g()',
132 '<string>',
133 'exec')
134 dct = {}
135 exec c in dct
136 self.assertEquals(dct.get('result'), 3)
137
Neil Schemenauer4c6b0d52006-08-16 23:38:05 +0000138 def testGenExp(self):
139 c = compiler.compile('list((i,j) for i in range(3) if i < 3'
140 ' for j in range(4) if j > 2)',
141 '<string>',
142 'eval')
143 self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
144
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000145 def testSetLiteral(self):
146 c = compiler.compile('{1, 2, 3}', '<string>', 'eval')
147 self.assertEquals(eval(c), {1,2,3})
148 c = compiler.compile('{1, 2, 3,}', '<string>', 'eval')
149 self.assertEquals(eval(c), {1,2,3})
150
151 def testDictLiteral(self):
152 c = compiler.compile('{1:2, 2:3, 3:4}', '<string>', 'eval')
153 self.assertEquals(eval(c), {1:2, 2:3, 3:4})
154 c = compiler.compile('{1:2, 2:3, 3:4,}', '<string>', 'eval')
155 self.assertEquals(eval(c), {1:2, 2:3, 3:4})
156
157 def testSetComp(self):
158 c = compiler.compile('{x for x in range(1, 4)}', '<string>', 'eval')
159 self.assertEquals(eval(c), {1, 2, 3})
160 c = compiler.compile('{x * y for x in range(3) if x != 0'
161 ' for y in range(4) if y != 0}',
162 '<string>',
163 'eval')
164 self.assertEquals(eval(c), {1, 2, 3, 4, 6})
165
166 def testDictComp(self):
167 c = compiler.compile('{x:x+1 for x in range(1, 4)}', '<string>', 'eval')
168 self.assertEquals(eval(c), {1:2, 2:3, 3:4})
169 c = compiler.compile('{(x, y) : y for x in range(2) if x != 0'
170 ' for y in range(3) if y != 0}',
171 '<string>',
172 'eval')
173 self.assertEquals(eval(c), {(1, 2): 2, (1, 1): 1})
174
Georg Brandlab496842007-01-27 17:43:02 +0000175 def testWith(self):
176 # SF bug 1638243
177 c = compiler.compile('from __future__ import with_statement\n'
178 'def f():\n'
179 ' with TrivialContext():\n'
180 ' return 1\n'
181 'result = f()',
182 '<string>',
183 'exec' )
184 dct = {'TrivialContext': TrivialContext}
185 exec c in dct
186 self.assertEquals(dct.get('result'), 1)
187
188 def testWithAss(self):
189 c = compiler.compile('from __future__ import with_statement\n'
190 'def f():\n'
191 ' with TrivialContext() as tc:\n'
192 ' return 1\n'
193 'result = f()',
194 '<string>',
195 'exec' )
196 dct = {'TrivialContext': TrivialContext}
197 exec c in dct
198 self.assertEquals(dct.get('result'), 1)
199
Georg Brandl944f6842009-05-25 21:02:56 +0000200 def testWithMult(self):
201 events = []
202 class Ctx:
203 def __init__(self, n):
204 self.n = n
205 def __enter__(self):
206 events.append(self.n)
207 def __exit__(self, *args):
208 pass
209 c = compiler.compile('from __future__ import with_statement\n'
210 'def f():\n'
211 ' with Ctx(1) as tc, Ctx(2) as tc2:\n'
212 ' return 1\n'
213 'result = f()',
214 '<string>',
215 'exec' )
216 dct = {'Ctx': Ctx}
217 exec c in dct
218 self.assertEquals(dct.get('result'), 1)
219 self.assertEquals(events, [1, 2])
220
Neil Schemenauerf8741ea2009-02-07 18:35:16 +0000221 def testGlobal(self):
222 code = compiler.compile('global x\nx=1', '<string>', 'exec')
223 d1 = {'__builtins__': {}}
224 d2 = {}
225 exec code in d1, d2
226 # x should be in the globals dict
227 self.assertEquals(d1.get('x'), 1)
Tim Petersb1ccc4d2006-08-04 22:00:35 +0000228
Eric Smith14cae962008-03-19 03:13:34 +0000229 def testPrintFunction(self):
230 c = compiler.compile('from __future__ import print_function\n'
231 'print("a", "b", sep="**", end="++", '
232 'file=output)',
233 '<string>',
234 'exec' )
235 dct = {'output': StringIO()}
236 exec c in dct
237 self.assertEquals(dct['output'].getvalue(), 'a**b++')
238
Martin v. Löwisa5136192007-09-04 14:19:28 +0000239 def _testErrEnc(self, src, text, offset):
240 try:
241 compile(src, "", "exec")
242 except SyntaxError, e:
243 self.assertEquals(e.offset, offset)
244 self.assertEquals(e.text, text)
245
246 def testSourceCodeEncodingsError(self):
247 # Test SyntaxError with encoding definition
248 sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n"
249 ascii = "print '12345678', '\n"
250 encdef = "#! -*- coding: ShiftJIS -*-\n"
251
252 # ascii source without encdef
253 self._testErrEnc(ascii, ascii, 19)
254
255 # ascii source with encdef
256 self._testErrEnc(encdef+ascii, ascii, 19)
257
258 # non-ascii source with encdef
259 self._testErrEnc(encdef+sjis, sjis, 19)
260
261 # ShiftJIS source without encdef
262 self._testErrEnc(sjis, sjis, 19)
263
264
Jeremy Hylton566d9342004-09-07 15:28:01 +0000265NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
266
267###############################################################################
268# code below is just used to trigger some possible errors, for the benefit of
269# testLineNo
270###############################################################################
271
272class Toto:
273 """docstring"""
274 pass
275
276a, b = 2, 3
277[c, d] = 5, 6
278l = [(x, y) for x, y in zip(range(5), range(5,10))]
279l[0]
280l[3:4]
Georg Brandlf57c54d2006-06-22 14:46:46 +0000281d = {'a': 2}
282d = {}
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000283d = {x: y for x, y in zip(range(5), range(5,10))}
284s = {x for x in range(10)}
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000285s = {1}
Georg Brandlf57c54d2006-06-22 14:46:46 +0000286t = ()
287t = (1, 2)
288l = []
289l = [1, 2]
Jeremy Hylton566d9342004-09-07 15:28:01 +0000290if l:
291 pass
292else:
293 a, b = b, a
294
295try:
296 print yo
297except:
298 yo = 3
299else:
300 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +0000301
Jeremy Hylton566d9342004-09-07 15:28:01 +0000302try:
303 a += b
304finally:
305 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000306
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000307from math import *
308
Jeremy Hylton566d9342004-09-07 15:28:01 +0000309###############################################################################
310
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000311def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000312 global TEST_ALL
313 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000314 test.test_support.run_unittest(CompilerTest)
315
316if __name__ == "__main__":
317 test_main()