Brett Cannon | 6192df1 | 2008-05-10 02:58:26 +0000 | [diff] [blame] | 1 | import test.test_support |
| 2 | compiler = test.test_support.import_module('compiler', deprecated=True) |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 3 | from compiler.ast import flatten |
Neal Norwitz | 07c6071 | 2006-04-13 06:34:59 +0000 | [diff] [blame] | 4 | import os, sys, time, unittest |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 5 | from random import random |
Eric Smith | 14cae96 | 2008-03-19 03:13:34 +0000 | [diff] [blame] | 6 | from StringIO import StringIO |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 7 | |
Neal Norwitz | 07c6071 | 2006-04-13 06:34:59 +0000 | [diff] [blame] | 8 | # How much time in seconds can pass before we print a 'Still working' message. |
| 9 | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
| 10 | |
Georg Brandl | ab49684 | 2007-01-27 17:43:02 +0000 | [diff] [blame] | 11 | class TrivialContext(object): |
| 12 | def __enter__(self): |
| 13 | return self |
| 14 | def __exit__(self, *exc_info): |
| 15 | pass |
| 16 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 17 | class 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 Peters | 2841af4 | 2006-01-07 23:20:46 +0000 | [diff] [blame] | 24 | |
Neal Norwitz | 07c6071 | 2006-04-13 06:34:59 +0000 | [diff] [blame] | 25 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 26 | libdir = os.path.dirname(unittest.__file__) |
| 27 | testdir = os.path.dirname(test.test_support.__file__) |
| 28 | |
| 29 | for dir in [libdir, testdir]: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 30 | for basename in os.listdir(dir): |
Neal Norwitz | 07c6071 | 2006-04-13 06:34:59 +0000 | [diff] [blame] | 31 | # 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 Norwitz | dd28d1c | 2006-04-28 04:34:43 +0000 | [diff] [blame] | 36 | sys.__stdout__.flush() |
Neal Norwitz | 07c6071 | 2006-04-13 06:34:59 +0000 | [diff] [blame] | 37 | |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 38 | if not basename.endswith(".py"): |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 39 | continue |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 40 | if not TEST_ALL and random() < 0.98: |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 41 | continue |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 42 | path = os.path.join(dir, basename) |
Tim Peters | b6ecc16 | 2004-08-08 16:32:54 +0000 | [diff] [blame] | 43 | if test.test_support.verbose: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 44 | print "compiling", path |
Michael W. Hudson | 29589a0 | 2004-10-11 15:34:31 +0000 | [diff] [blame] | 45 | f = open(path, "U") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 46 | buf = f.read() |
| 47 | f.close() |
Neal Norwitz | f895065 | 2005-10-24 00:01:37 +0000 | [diff] [blame] | 48 | if "badsyntax" in basename or "bad_coding" in basename: |
Tim Peters | 0955f29 | 2004-08-08 16:43:59 +0000 | [diff] [blame] | 49 | self.assertRaises(SyntaxError, compiler.compile, |
| 50 | buf, basename, "exec") |
| 51 | else: |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 52 | try: |
| 53 | compiler.compile(buf, basename, "exec") |
| 54 | except Exception, e: |
Martin v. Löwis | d9bfeac | 2006-03-01 23:24:34 +0000 | [diff] [blame] | 55 | args = list(e.args) |
Christian Heimes | c5f05e4 | 2008-02-23 17:40:11 +0000 | [diff] [blame] | 56 | args.append("in file %s]" % basename) |
| 57 | #args[0] += "[in file %s]" % basename |
Martin v. Löwis | d9bfeac | 2006-03-01 23:24:34 +0000 | [diff] [blame] | 58 | e.args = tuple(args) |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 59 | raise |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 60 | |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 61 | def testNewClassSyntax(self): |
| 62 | compiler.compile("class foo():pass\n\n","<string>","exec") |
Tim Peters | e890682 | 2005-04-20 17:45:13 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 64 | def testYieldExpr(self): |
| 65 | compiler.compile("def g(): yield\n\n", "<string>", "exec") |
| 66 | |
Amaury Forgeot d'Arc | 67f24f1 | 2008-08-20 00:08:47 +0000 | [diff] [blame] | 67 | def testKeywordAfterStarargs(self): |
| 68 | def f(*args, **kwargs): |
| 69 | self.assertEqual((args, kwargs), ((2,3), {'x': 1, 'y': 4})) |
| 70 | c = compiler.compile('f(x=1, *(2, 3), y=4)', '<string>', 'exec') |
| 71 | exec c in {'f': f} |
| 72 | |
| 73 | self.assertRaises(SyntaxError, compiler.parse, "foo(a=1, b)") |
| 74 | self.assertRaises(SyntaxError, compiler.parse, "foo(1, *args, 3)") |
| 75 | |
Georg Brandl | f57c54d | 2006-06-22 14:46:46 +0000 | [diff] [blame] | 76 | def testTryExceptFinally(self): |
| 77 | # Test that except and finally clauses in one try stmt are recognized |
Senthil Kumaran | ce8e33a | 2010-01-08 19:04:16 +0000 | [diff] [blame] | 78 | c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1", |
Georg Brandl | f57c54d | 2006-06-22 14:46:46 +0000 | [diff] [blame] | 79 | "<string>", "exec") |
| 80 | dct = {} |
| 81 | exec c in dct |
| 82 | self.assertEquals(dct.get('e'), 1) |
| 83 | self.assertEquals(dct.get('f'), 1) |
| 84 | |
Georg Brandl | 1bb6230 | 2006-05-03 18:18:32 +0000 | [diff] [blame] | 85 | def testDefaultArgs(self): |
| 86 | self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass") |
| 87 | |
Georg Brandl | edd9b0d | 2006-07-29 09:33:26 +0000 | [diff] [blame] | 88 | def testDocstrings(self): |
| 89 | c = compiler.compile('"doc"', '<string>', 'exec') |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 90 | self.assertTrue('__doc__' in c.co_names) |
Georg Brandl | edd9b0d | 2006-07-29 09:33:26 +0000 | [diff] [blame] | 91 | c = compiler.compile('def f():\n "doc"', '<string>', 'exec') |
| 92 | g = {} |
| 93 | exec c in g |
| 94 | self.assertEquals(g['f'].__doc__, "doc") |
| 95 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 96 | def testLineNo(self): |
| 97 | # Test that all nodes except Module have a correct lineno attribute. |
| 98 | filename = __file__ |
Georg Brandl | b2afe85 | 2006-06-09 20:43:48 +0000 | [diff] [blame] | 99 | if filename.endswith((".pyc", ".pyo")): |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 100 | filename = filename[:-1] |
| 101 | tree = compiler.parseFile(filename) |
| 102 | self.check_lineno(tree) |
| 103 | |
| 104 | def check_lineno(self, node): |
| 105 | try: |
| 106 | self._check_lineno(node) |
| 107 | except AssertionError: |
| 108 | print node.__class__, node.lineno |
| 109 | raise |
| 110 | |
| 111 | def _check_lineno(self, node): |
| 112 | if not node.__class__ in NOLINENO: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 113 | self.assertTrue(isinstance(node.lineno, int), |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 114 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 115 | self.assertTrue(node.lineno > 0, |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 116 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
| 117 | for child in node.getChildNodes(): |
| 118 | self.check_lineno(child) |
| 119 | |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 120 | def testFlatten(self): |
| 121 | self.assertEquals(flatten([1, [2]]), [1, 2]) |
| 122 | self.assertEquals(flatten((1, (2,))), [1, 2]) |
| 123 | |
Neil Schemenauer | 06ded09 | 2006-08-04 16:20:30 +0000 | [diff] [blame] | 124 | def testNestedScope(self): |
| 125 | c = compiler.compile('def g():\n' |
| 126 | ' a = 1\n' |
| 127 | ' def f(): return a + 2\n' |
| 128 | ' return f()\n' |
| 129 | 'result = g()', |
| 130 | '<string>', |
| 131 | 'exec') |
| 132 | dct = {} |
| 133 | exec c in dct |
| 134 | self.assertEquals(dct.get('result'), 3) |
| 135 | |
Neil Schemenauer | 4c6b0d5 | 2006-08-16 23:38:05 +0000 | [diff] [blame] | 136 | def testGenExp(self): |
| 137 | c = compiler.compile('list((i,j) for i in range(3) if i < 3' |
| 138 | ' for j in range(4) if j > 2)', |
| 139 | '<string>', |
| 140 | 'eval') |
| 141 | self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)]) |
| 142 | |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame^] | 143 | def testSetLiteral(self): |
| 144 | c = compiler.compile('{1, 2, 3}', '<string>', 'eval') |
| 145 | self.assertEquals(eval(c), {1,2,3}) |
| 146 | c = compiler.compile('{1, 2, 3,}', '<string>', 'eval') |
| 147 | self.assertEquals(eval(c), {1,2,3}) |
| 148 | |
| 149 | def testDictLiteral(self): |
| 150 | c = compiler.compile('{1:2, 2:3, 3:4}', '<string>', 'eval') |
| 151 | self.assertEquals(eval(c), {1:2, 2:3, 3:4}) |
| 152 | c = compiler.compile('{1:2, 2:3, 3:4,}', '<string>', 'eval') |
| 153 | self.assertEquals(eval(c), {1:2, 2:3, 3:4}) |
| 154 | |
| 155 | def testSetComp(self): |
| 156 | c = compiler.compile('{x for x in range(1, 4)}', '<string>', 'eval') |
| 157 | self.assertEquals(eval(c), {1, 2, 3}) |
| 158 | c = compiler.compile('{x * y for x in range(3) if x != 0' |
| 159 | ' for y in range(4) if y != 0}', |
| 160 | '<string>', |
| 161 | 'eval') |
| 162 | self.assertEquals(eval(c), {1, 2, 3, 4, 6}) |
| 163 | |
| 164 | def testDictComp(self): |
| 165 | c = compiler.compile('{x:x+1 for x in range(1, 4)}', '<string>', 'eval') |
| 166 | self.assertEquals(eval(c), {1:2, 2:3, 3:4}) |
| 167 | c = compiler.compile('{(x, y) : y for x in range(2) if x != 0' |
| 168 | ' for y in range(3) if y != 0}', |
| 169 | '<string>', |
| 170 | 'eval') |
| 171 | self.assertEquals(eval(c), {(1, 2): 2, (1, 1): 1}) |
| 172 | |
Georg Brandl | ab49684 | 2007-01-27 17:43:02 +0000 | [diff] [blame] | 173 | def testWith(self): |
| 174 | # SF bug 1638243 |
| 175 | c = compiler.compile('from __future__ import with_statement\n' |
| 176 | 'def f():\n' |
| 177 | ' with TrivialContext():\n' |
| 178 | ' return 1\n' |
| 179 | 'result = f()', |
| 180 | '<string>', |
| 181 | 'exec' ) |
| 182 | dct = {'TrivialContext': TrivialContext} |
| 183 | exec c in dct |
| 184 | self.assertEquals(dct.get('result'), 1) |
| 185 | |
| 186 | def testWithAss(self): |
| 187 | c = compiler.compile('from __future__ import with_statement\n' |
| 188 | 'def f():\n' |
| 189 | ' with TrivialContext() as tc:\n' |
| 190 | ' return 1\n' |
| 191 | 'result = f()', |
| 192 | '<string>', |
| 193 | 'exec' ) |
| 194 | dct = {'TrivialContext': TrivialContext} |
| 195 | exec c in dct |
| 196 | self.assertEquals(dct.get('result'), 1) |
| 197 | |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 198 | def testWithMult(self): |
| 199 | events = [] |
| 200 | class Ctx: |
| 201 | def __init__(self, n): |
| 202 | self.n = n |
| 203 | def __enter__(self): |
| 204 | events.append(self.n) |
| 205 | def __exit__(self, *args): |
| 206 | pass |
| 207 | c = compiler.compile('from __future__ import with_statement\n' |
| 208 | 'def f():\n' |
| 209 | ' with Ctx(1) as tc, Ctx(2) as tc2:\n' |
| 210 | ' return 1\n' |
| 211 | 'result = f()', |
| 212 | '<string>', |
| 213 | 'exec' ) |
| 214 | dct = {'Ctx': Ctx} |
| 215 | exec c in dct |
| 216 | self.assertEquals(dct.get('result'), 1) |
| 217 | self.assertEquals(events, [1, 2]) |
| 218 | |
Neil Schemenauer | f8741ea | 2009-02-07 18:35:16 +0000 | [diff] [blame] | 219 | def testGlobal(self): |
| 220 | code = compiler.compile('global x\nx=1', '<string>', 'exec') |
| 221 | d1 = {'__builtins__': {}} |
| 222 | d2 = {} |
| 223 | exec code in d1, d2 |
| 224 | # x should be in the globals dict |
| 225 | self.assertEquals(d1.get('x'), 1) |
Tim Peters | b1ccc4d | 2006-08-04 22:00:35 +0000 | [diff] [blame] | 226 | |
Eric Smith | 14cae96 | 2008-03-19 03:13:34 +0000 | [diff] [blame] | 227 | def testPrintFunction(self): |
| 228 | c = compiler.compile('from __future__ import print_function\n' |
| 229 | 'print("a", "b", sep="**", end="++", ' |
| 230 | 'file=output)', |
| 231 | '<string>', |
| 232 | 'exec' ) |
| 233 | dct = {'output': StringIO()} |
| 234 | exec c in dct |
| 235 | self.assertEquals(dct['output'].getvalue(), 'a**b++') |
| 236 | |
Martin v. Löwis | a513619 | 2007-09-04 14:19:28 +0000 | [diff] [blame] | 237 | def _testErrEnc(self, src, text, offset): |
| 238 | try: |
| 239 | compile(src, "", "exec") |
| 240 | except SyntaxError, e: |
| 241 | self.assertEquals(e.offset, offset) |
| 242 | self.assertEquals(e.text, text) |
| 243 | |
| 244 | def testSourceCodeEncodingsError(self): |
| 245 | # Test SyntaxError with encoding definition |
| 246 | sjis = "print '\x83\x70\x83\x43\x83\x5c\x83\x93', '\n" |
| 247 | ascii = "print '12345678', '\n" |
| 248 | encdef = "#! -*- coding: ShiftJIS -*-\n" |
| 249 | |
| 250 | # ascii source without encdef |
| 251 | self._testErrEnc(ascii, ascii, 19) |
| 252 | |
| 253 | # ascii source with encdef |
| 254 | self._testErrEnc(encdef+ascii, ascii, 19) |
| 255 | |
| 256 | # non-ascii source with encdef |
| 257 | self._testErrEnc(encdef+sjis, sjis, 19) |
| 258 | |
| 259 | # ShiftJIS source without encdef |
| 260 | self._testErrEnc(sjis, sjis, 19) |
| 261 | |
| 262 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 263 | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) |
| 264 | |
| 265 | ############################################################################### |
| 266 | # code below is just used to trigger some possible errors, for the benefit of |
| 267 | # testLineNo |
| 268 | ############################################################################### |
| 269 | |
| 270 | class Toto: |
| 271 | """docstring""" |
| 272 | pass |
| 273 | |
| 274 | a, b = 2, 3 |
| 275 | [c, d] = 5, 6 |
| 276 | l = [(x, y) for x, y in zip(range(5), range(5,10))] |
| 277 | l[0] |
| 278 | l[3:4] |
Georg Brandl | f57c54d | 2006-06-22 14:46:46 +0000 | [diff] [blame] | 279 | d = {'a': 2} |
| 280 | d = {} |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame^] | 281 | d = {x: y for x, y in zip(range(5), range(5,10))} |
| 282 | s = {x for x in range(10)} |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 283 | s = {1} |
Georg Brandl | f57c54d | 2006-06-22 14:46:46 +0000 | [diff] [blame] | 284 | t = () |
| 285 | t = (1, 2) |
| 286 | l = [] |
| 287 | l = [1, 2] |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 288 | if l: |
| 289 | pass |
| 290 | else: |
| 291 | a, b = b, a |
| 292 | |
| 293 | try: |
| 294 | print yo |
| 295 | except: |
| 296 | yo = 3 |
| 297 | else: |
| 298 | yo += 3 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 299 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 300 | try: |
| 301 | a += b |
| 302 | finally: |
| 303 | b = 0 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 304 | |
Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 305 | from math import * |
| 306 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 307 | ############################################################################### |
| 308 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 309 | def test_main(): |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 310 | global TEST_ALL |
| 311 | TEST_ALL = test.test_support.is_resource_enabled("compiler") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 312 | test.test_support.run_unittest(CompilerTest) |
| 313 | |
| 314 | if __name__ == "__main__": |
| 315 | test_main() |