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