blob: d491b7809614340d32092723e0801f2fed9d8d04 [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
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
Jeremy Hylton4336eda2004-08-07 19:25:33 +000026 libdir = os.path.dirname(unittest.__file__)
27 testdir = os.path.dirname(test.test_support.__file__)
28
29 for dir in [libdir, testdir]:
Tim Peters2a5f6562004-08-08 16:37:37 +000030 for basename in os.listdir(dir):
Neal Norwitz07c60712006-04-13 06:34:59 +000031 # Print still working message since this test can be really slow
32 if next_time <= time.time():
33 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
34 print >>sys.__stdout__, \
35 ' testCompileLibrary still working, be patient...'
Neal Norwitzdd28d1c2006-04-28 04:34:43 +000036 sys.__stdout__.flush()
Neal Norwitz07c60712006-04-13 06:34:59 +000037
Tim Peters2a5f6562004-08-08 16:37:37 +000038 if not basename.endswith(".py"):
Jeremy Hylton4336eda2004-08-07 19:25:33 +000039 continue
Guido van Rossum5bde08d2006-03-02 04:24:01 +000040 if not TEST_ALL and random() < 0.98:
Raymond Hettingered20ad82004-09-04 20:09:13 +000041 continue
Tim Peters2a5f6562004-08-08 16:37:37 +000042 path = os.path.join(dir, basename)
Tim Petersb6ecc162004-08-08 16:32:54 +000043 if test.test_support.verbose:
Tim Peters2a5f6562004-08-08 16:37:37 +000044 print "compiling", path
Michael W. Hudson29589a02004-10-11 15:34:31 +000045 f = open(path, "U")
Jeremy Hylton4336eda2004-08-07 19:25:33 +000046 buf = f.read()
47 f.close()
Neal Norwitzf8950652005-10-24 00:01:37 +000048 if "badsyntax" in basename or "bad_coding" in basename:
Tim Peters0955f292004-08-08 16:43:59 +000049 self.assertRaises(SyntaxError, compiler.compile,
50 buf, basename, "exec")
51 else:
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000052 try:
53 compiler.compile(buf, basename, "exec")
54 except Exception, e:
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000055 args = list(e.args)
Christian Heimesc5f05e42008-02-23 17:40:11 +000056 args.append("in file %s]" % basename)
57 #args[0] += "[in file %s]" % basename
Martin v. Löwisd9bfeac2006-03-01 23:24:34 +000058 e.args = tuple(args)
Martin v. Löwis15bfc3b2006-03-01 21:11:49 +000059 raise
Jeremy Hylton4336eda2004-08-07 19:25:33 +000060
Brett Cannonf4189912005-04-09 02:30:16 +000061 def testNewClassSyntax(self):
62 compiler.compile("class foo():pass\n\n","<string>","exec")
Tim Peterse8906822005-04-20 17:45:13 +000063
Guido van Rossum5bde08d2006-03-02 04:24:01 +000064 def testYieldExpr(self):
65 compiler.compile("def g(): yield\n\n", "<string>", "exec")
66
Georg Brandlf57c54d2006-06-22 14:46:46 +000067 def testTryExceptFinally(self):
68 # Test that except and finally clauses in one try stmt are recognized
69 c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
70 "<string>", "exec")
71 dct = {}
72 exec c in dct
73 self.assertEquals(dct.get('e'), 1)
74 self.assertEquals(dct.get('f'), 1)
75
Georg Brandl1bb62302006-05-03 18:18:32 +000076 def testDefaultArgs(self):
77 self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass")
78
Georg Brandledd9b0d2006-07-29 09:33:26 +000079 def testDocstrings(self):
80 c = compiler.compile('"doc"', '<string>', 'exec')
81 self.assert_('__doc__' in c.co_names)
82 c = compiler.compile('def f():\n "doc"', '<string>', 'exec')
83 g = {}
84 exec c in g
85 self.assertEquals(g['f'].__doc__, "doc")
86
Jeremy Hylton566d9342004-09-07 15:28:01 +000087 def testLineNo(self):
88 # Test that all nodes except Module have a correct lineno attribute.
89 filename = __file__
Georg Brandlb2afe852006-06-09 20:43:48 +000090 if filename.endswith((".pyc", ".pyo")):
Jeremy Hylton566d9342004-09-07 15:28:01 +000091 filename = filename[:-1]
92 tree = compiler.parseFile(filename)
93 self.check_lineno(tree)
94
95 def check_lineno(self, node):
96 try:
97 self._check_lineno(node)
98 except AssertionError:
99 print node.__class__, node.lineno
100 raise
101
102 def _check_lineno(self, node):
103 if not node.__class__ in NOLINENO:
Tim Peters0e9980f2004-09-12 03:49:31 +0000104 self.assert_(isinstance(node.lineno, int),
Jeremy Hylton566d9342004-09-07 15:28:01 +0000105 "lineno=%s on %s" % (node.lineno, node.__class__))
Tim Peters0e9980f2004-09-12 03:49:31 +0000106 self.assert_(node.lineno > 0,
Jeremy Hylton566d9342004-09-07 15:28:01 +0000107 "lineno=%s on %s" % (node.lineno, node.__class__))
108 for child in node.getChildNodes():
109 self.check_lineno(child)
110
Neil Schemenauerf3694702005-06-02 05:55:20 +0000111 def testFlatten(self):
112 self.assertEquals(flatten([1, [2]]), [1, 2])
113 self.assertEquals(flatten((1, (2,))), [1, 2])
114
Neil Schemenauer06ded092006-08-04 16:20:30 +0000115 def testNestedScope(self):
116 c = compiler.compile('def g():\n'
117 ' a = 1\n'
118 ' def f(): return a + 2\n'
119 ' return f()\n'
120 'result = g()',
121 '<string>',
122 'exec')
123 dct = {}
124 exec c in dct
125 self.assertEquals(dct.get('result'), 3)
126
Neil Schemenauer4c6b0d52006-08-16 23:38:05 +0000127 def testGenExp(self):
128 c = compiler.compile('list((i,j) for i in range(3) if i < 3'
129 ' for j in range(4) if j > 2)',
130 '<string>',
131 'eval')
132 self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
133
Georg Brandlab496842007-01-27 17:43:02 +0000134 def testWith(self):
135 # SF bug 1638243
136 c = compiler.compile('from __future__ import with_statement\n'
137 'def f():\n'
138 ' with TrivialContext():\n'
139 ' return 1\n'
140 'result = f()',
141 '<string>',
142 'exec' )
143 dct = {'TrivialContext': TrivialContext}
144 exec c in dct
145 self.assertEquals(dct.get('result'), 1)
146
147 def testWithAss(self):
148 c = compiler.compile('from __future__ import with_statement\n'
149 'def f():\n'
150 ' with TrivialContext() as tc:\n'
151 ' return 1\n'
152 'result = f()',
153 '<string>',
154 'exec' )
155 dct = {'TrivialContext': TrivialContext}
156 exec c in dct
157 self.assertEquals(dct.get('result'), 1)
158
Tim Petersb1ccc4d2006-08-04 22:00:35 +0000159
Eric Smith14cae962008-03-19 03:13:34 +0000160 def testPrintFunction(self):
161 c = compiler.compile('from __future__ import print_function\n'
162 'print("a", "b", sep="**", end="++", '
163 'file=output)',
164 '<string>',
165 'exec' )
166 dct = {'output': StringIO()}
167 exec c in dct
168 self.assertEquals(dct['output'].getvalue(), 'a**b++')
169
Martin v. Löwisa5136192007-09-04 14:19:28 +0000170 def _testErrEnc(self, src, text, offset):
171 try:
172 compile(src, "", "exec")
173 except SyntaxError, e:
174 self.assertEquals(e.offset, offset)
175 self.assertEquals(e.text, text)
176
177 def testSourceCodeEncodingsError(self):
178 # Test SyntaxError with encoding definition
179 sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n"
180 ascii = "print '12345678', '\n"
181 encdef = "#! -*- coding: ShiftJIS -*-\n"
182
183 # ascii source without encdef
184 self._testErrEnc(ascii, ascii, 19)
185
186 # ascii source with encdef
187 self._testErrEnc(encdef+ascii, ascii, 19)
188
189 # non-ascii source with encdef
190 self._testErrEnc(encdef+sjis, sjis, 19)
191
192 # ShiftJIS source without encdef
193 self._testErrEnc(sjis, sjis, 19)
194
195
Jeremy Hylton566d9342004-09-07 15:28:01 +0000196NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
197
198###############################################################################
199# code below is just used to trigger some possible errors, for the benefit of
200# testLineNo
201###############################################################################
202
203class Toto:
204 """docstring"""
205 pass
206
207a, b = 2, 3
208[c, d] = 5, 6
209l = [(x, y) for x, y in zip(range(5), range(5,10))]
210l[0]
211l[3:4]
Georg Brandlf57c54d2006-06-22 14:46:46 +0000212d = {'a': 2}
213d = {}
214t = ()
215t = (1, 2)
216l = []
217l = [1, 2]
Jeremy Hylton566d9342004-09-07 15:28:01 +0000218if l:
219 pass
220else:
221 a, b = b, a
222
223try:
224 print yo
225except:
226 yo = 3
227else:
228 yo += 3
Tim Peters0e9980f2004-09-12 03:49:31 +0000229
Jeremy Hylton566d9342004-09-07 15:28:01 +0000230try:
231 a += b
232finally:
233 b = 0
Tim Peters0e9980f2004-09-12 03:49:31 +0000234
Michael W. Hudsone0b855f2004-11-08 16:46:02 +0000235from math import *
236
Jeremy Hylton566d9342004-09-07 15:28:01 +0000237###############################################################################
238
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000239def test_main():
Raymond Hettingered20ad82004-09-04 20:09:13 +0000240 global TEST_ALL
241 TEST_ALL = test.test_support.is_resource_enabled("compiler")
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000242 test.test_support.run_unittest(CompilerTest)
243
244if __name__ == "__main__":
245 test_main()