Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 1 | import compiler |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 2 | from compiler.ast import flatten |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3 | import os, sys, time, unittest |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 4 | import test.test_support |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 5 | from random import random |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 6 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | # How much time in seconds can pass before we print a 'Still working' message. |
| 8 | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
| 9 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 10 | class TrivialContext(object): |
| 11 | def __enter__(self): |
| 12 | return self |
| 13 | def __exit__(self, *exc_info): |
| 14 | pass |
| 15 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 16 | class 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 Peters | 2841af4 | 2006-01-07 23:20:46 +0000 | [diff] [blame] | 23 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 24 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 25 | libdir = os.path.dirname(unittest.__file__) |
| 26 | testdir = os.path.dirname(test.test_support.__file__) |
| 27 | |
Guido van Rossum | f59677a | 2006-11-22 04:55:53 +0000 | [diff] [blame] | 28 | for dir in [libdir, testdir]: |
| 29 | for basename in os.listdir(dir): |
| 30 | # 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 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 33 | print(' testCompileLibrary still working, be patient...', file=sys.__stdout__) |
Guido van Rossum | f59677a | 2006-11-22 04:55:53 +0000 | [diff] [blame] | 34 | sys.__stdout__.flush() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | f59677a | 2006-11-22 04:55:53 +0000 | [diff] [blame] | 36 | if not basename.endswith(".py"): |
| 37 | continue |
| 38 | if not TEST_ALL and random() < 0.98: |
| 39 | continue |
| 40 | path = os.path.join(dir, basename) |
| 41 | if test.test_support.verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 42 | print("compiling", path) |
Guido van Rossum | f59677a | 2006-11-22 04:55:53 +0000 | [diff] [blame] | 43 | f = open(path, "U") |
| 44 | buf = f.read() |
| 45 | f.close() |
| 46 | if "badsyntax" in basename or "bad_coding" in basename: |
| 47 | self.assertRaises(SyntaxError, compiler.compile, |
| 48 | buf, basename, "exec") |
| 49 | else: |
| 50 | try: |
| 51 | compiler.compile(buf, basename, "exec") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 52 | except Exception as e: |
Guido van Rossum | f59677a | 2006-11-22 04:55:53 +0000 | [diff] [blame] | 53 | args = list(e.args) |
| 54 | args[0] += "[in file %s]" % basename |
| 55 | e.args = tuple(args) |
| 56 | raise |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 57 | |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 58 | def testNewClassSyntax(self): |
| 59 | compiler.compile("class foo():pass\n\n","<string>","exec") |
Tim Peters | e890682 | 2005-04-20 17:45:13 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 61 | def testYieldExpr(self): |
| 62 | compiler.compile("def g(): yield\n\n", "<string>", "exec") |
| 63 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | def testTryExceptFinally(self): |
| 65 | # Test that except and finally clauses in one try stmt are recognized |
| 66 | c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1", |
| 67 | "<string>", "exec") |
| 68 | dct = {} |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 69 | exec(c, dct) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 70 | self.assertEquals(dct.get('e'), 1) |
| 71 | self.assertEquals(dct.get('f'), 1) |
| 72 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 73 | def testDefaultArgs(self): |
| 74 | self.assertRaises(SyntaxError, compiler.parse, "def foo(a=1, b): pass") |
| 75 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | def testDocstrings(self): |
| 77 | c = compiler.compile('"doc"', '<string>', 'exec') |
| 78 | self.assert_('__doc__' in c.co_names) |
| 79 | c = compiler.compile('def f():\n "doc"', '<string>', 'exec') |
| 80 | g = {} |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 81 | exec(c, g) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | self.assertEquals(g['f'].__doc__, "doc") |
| 83 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 84 | def testLineNo(self): |
| 85 | # Test that all nodes except Module have a correct lineno attribute. |
| 86 | filename = __file__ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 87 | if filename.endswith((".pyc", ".pyo")): |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 88 | filename = filename[:-1] |
| 89 | tree = compiler.parseFile(filename) |
| 90 | self.check_lineno(tree) |
| 91 | |
| 92 | def check_lineno(self, node): |
| 93 | try: |
| 94 | self._check_lineno(node) |
| 95 | except AssertionError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 96 | print(node.__class__, node.lineno) |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 97 | raise |
| 98 | |
| 99 | def _check_lineno(self, node): |
| 100 | if not node.__class__ in NOLINENO: |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 101 | self.assert_(isinstance(node.lineno, int), |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 102 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 103 | self.assert_(node.lineno > 0, |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 104 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
| 105 | for child in node.getChildNodes(): |
| 106 | self.check_lineno(child) |
| 107 | |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 108 | def testFlatten(self): |
| 109 | self.assertEquals(flatten([1, [2]]), [1, 2]) |
| 110 | self.assertEquals(flatten((1, (2,))), [1, 2]) |
| 111 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 112 | def testNestedScope(self): |
| 113 | c = compiler.compile('def g():\n' |
| 114 | ' a = 1\n' |
| 115 | ' def f(): return a + 2\n' |
| 116 | ' return f()\n' |
| 117 | 'result = g()', |
| 118 | '<string>', |
| 119 | 'exec') |
| 120 | dct = {} |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 121 | exec(c, dct) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 122 | self.assertEquals(dct.get('result'), 3) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 123 | c = compiler.compile('def g(a):\n' |
| 124 | ' def f(): return a + 2\n' |
| 125 | ' return f()\n' |
| 126 | 'result = g(1)', |
| 127 | '<string>', |
| 128 | 'exec') |
| 129 | dct = {} |
| 130 | exec(c, dct) |
| 131 | self.assertEquals(dct.get('result'), 3) |
| 132 | c = compiler.compile('def g((a, b)):\n' |
| 133 | ' def f(): return a + b\n' |
| 134 | ' return f()\n' |
| 135 | 'result = g((1, 2))', |
| 136 | '<string>', |
| 137 | 'exec') |
| 138 | dct = {} |
| 139 | exec(c, dct) |
| 140 | self.assertEquals(dct.get('result'), 3) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 141 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 142 | def testGenExp(self): |
| 143 | c = compiler.compile('list((i,j) for i in range(3) if i < 3' |
| 144 | ' for j in range(4) if j > 2)', |
| 145 | '<string>', |
| 146 | 'eval') |
| 147 | self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)]) |
| 148 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 149 | def testFuncAnnotations(self): |
| 150 | testdata = [ |
| 151 | ('def f(a: 1): pass', {'a': 1}), |
| 152 | ('''def f(a, (b:1, c:2, d), e:3=4, f=5, |
| 153 | *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass |
| 154 | ''', {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, |
| 155 | 'k': 11, 'return': 12}), |
| 156 | ] |
| 157 | for sourcecode, expected in testdata: |
| 158 | # avoid IndentationError: unexpected indent from trailing lines |
| 159 | sourcecode = sourcecode.rstrip()+'\n' |
| 160 | c = compiler.compile(sourcecode, '<string>', 'exec') |
| 161 | dct = {} |
| 162 | exec(c, dct) |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame^] | 163 | self.assertEquals(dct['f'].__annotations__, expected) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 164 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 165 | def testWith(self): |
| 166 | # SF bug 1638243 |
| 167 | c = compiler.compile('from __future__ import with_statement\n' |
| 168 | 'def f():\n' |
| 169 | ' with TrivialContext():\n' |
| 170 | ' return 1\n' |
| 171 | 'result = f()', |
| 172 | '<string>', |
| 173 | 'exec' ) |
| 174 | dct = {'TrivialContext': TrivialContext} |
| 175 | exec(c, dct) |
| 176 | self.assertEquals(dct.get('result'), 1) |
| 177 | |
| 178 | def testWithAss(self): |
| 179 | c = compiler.compile('from __future__ import with_statement\n' |
| 180 | 'def f():\n' |
| 181 | ' with TrivialContext() as tc:\n' |
| 182 | ' return 1\n' |
| 183 | 'result = f()', |
| 184 | '<string>', |
| 185 | 'exec' ) |
| 186 | dct = {'TrivialContext': TrivialContext} |
| 187 | exec(c, dct) |
| 188 | self.assertEquals(dct.get('result'), 1) |
| 189 | |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 190 | def testBytesLiteral(self): |
| 191 | c = compiler.compile("b'foo'", '<string>', 'eval') |
| 192 | b = eval(c) |
| 193 | |
| 194 | c = compiler.compile('def f(b=b"foo"):\n' |
| 195 | ' b[0] += 1\n' |
| 196 | ' return b\n' |
| 197 | 'f(); f(); result = f()\n', |
| 198 | '<string>', |
| 199 | 'exec') |
| 200 | dct = {} |
| 201 | exec(c, dct) |
| 202 | self.assertEquals(dct.get('result'), b"ioo") |
| 203 | |
| 204 | c = compiler.compile('def f():\n' |
| 205 | ' b = b"foo"\n' |
| 206 | ' b[0] += 1\n' |
| 207 | ' return b\n' |
| 208 | 'f(); f(); result = f()\n', |
| 209 | '<string>', |
| 210 | 'exec') |
| 211 | dct = {} |
| 212 | exec(c, dct) |
| 213 | self.assertEquals(dct.get('result'), b"goo") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 214 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 215 | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) |
| 216 | |
| 217 | ############################################################################### |
| 218 | # code below is just used to trigger some possible errors, for the benefit of |
| 219 | # testLineNo |
| 220 | ############################################################################### |
| 221 | |
| 222 | class Toto: |
| 223 | """docstring""" |
| 224 | pass |
| 225 | |
| 226 | a, b = 2, 3 |
| 227 | [c, d] = 5, 6 |
| 228 | l = [(x, y) for x, y in zip(range(5), range(5,10))] |
| 229 | l[0] |
| 230 | l[3:4] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 231 | d = {'a': 2} |
| 232 | d = {} |
| 233 | t = () |
| 234 | t = (1, 2) |
| 235 | l = [] |
| 236 | l = [1, 2] |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 237 | if l: |
| 238 | pass |
| 239 | else: |
| 240 | a, b = b, a |
| 241 | |
| 242 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 243 | print(yo) |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 244 | except: |
| 245 | yo = 3 |
| 246 | else: |
| 247 | yo += 3 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 248 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 249 | try: |
| 250 | a += b |
| 251 | finally: |
| 252 | b = 0 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 253 | |
Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 254 | from math import * |
| 255 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 256 | ############################################################################### |
| 257 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 258 | def test_main(all=False): |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 259 | global TEST_ALL |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 260 | TEST_ALL = all or test.test_support.is_resource_enabled("compiler") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 261 | test.test_support.run_unittest(CompilerTest) |
| 262 | |
| 263 | if __name__ == "__main__": |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 264 | import sys |
| 265 | test_main('all' in sys.argv) |