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 | |
| 22 | for dir in [libdir, testdir]: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 23 | for basename in os.listdir(dir): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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...' |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 29 | sys.__stdout__.flush() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 30 | |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 31 | if not basename.endswith(".py"): |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 32 | continue |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 33 | if not TEST_ALL and random() < 0.98: |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 34 | continue |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 35 | path = os.path.join(dir, basename) |
Tim Peters | b6ecc16 | 2004-08-08 16:32:54 +0000 | [diff] [blame] | 36 | if test.test_support.verbose: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 37 | print "compiling", path |
Michael W. Hudson | 29589a0 | 2004-10-11 15:34:31 +0000 | [diff] [blame] | 38 | f = open(path, "U") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 39 | buf = f.read() |
| 40 | f.close() |
Neal Norwitz | f895065 | 2005-10-24 00:01:37 +0000 | [diff] [blame] | 41 | if "badsyntax" in basename or "bad_coding" in basename: |
Tim Peters | 0955f29 | 2004-08-08 16:43:59 +0000 | [diff] [blame] | 42 | self.assertRaises(SyntaxError, compiler.compile, |
| 43 | buf, basename, "exec") |
| 44 | else: |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 45 | try: |
| 46 | compiler.compile(buf, basename, "exec") |
| 47 | except Exception, e: |
Martin v. Löwis | d9bfeac | 2006-03-01 23:24:34 +0000 | [diff] [blame] | 48 | args = list(e.args) |
| 49 | args[0] += "[in file %s]" % basename |
| 50 | e.args = tuple(args) |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 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 = {} |
| 64 | exec c in dct |
| 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 = {} |
| 76 | exec c in g |
| 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 = {} |
| 116 | exec c in dct |
| 117 | self.assertEquals(dct.get('result'), 3) |
| 118 | |
| 119 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 120 | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) |
| 121 | |
| 122 | ############################################################################### |
| 123 | # code below is just used to trigger some possible errors, for the benefit of |
| 124 | # testLineNo |
| 125 | ############################################################################### |
| 126 | |
| 127 | class Toto: |
| 128 | """docstring""" |
| 129 | pass |
| 130 | |
| 131 | a, b = 2, 3 |
| 132 | [c, d] = 5, 6 |
| 133 | l = [(x, y) for x, y in zip(range(5), range(5,10))] |
| 134 | l[0] |
| 135 | l[3:4] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame^] | 136 | d = {'a': 2} |
| 137 | d = {} |
| 138 | t = () |
| 139 | t = (1, 2) |
| 140 | l = [] |
| 141 | l = [1, 2] |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 142 | if l: |
| 143 | pass |
| 144 | else: |
| 145 | a, b = b, a |
| 146 | |
| 147 | try: |
| 148 | print yo |
| 149 | except: |
| 150 | yo = 3 |
| 151 | else: |
| 152 | yo += 3 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 153 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 154 | try: |
| 155 | a += b |
| 156 | finally: |
| 157 | b = 0 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 158 | |
Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 159 | from math import * |
| 160 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 161 | ############################################################################### |
| 162 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 163 | def test_main(): |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 164 | global TEST_ALL |
| 165 | TEST_ALL = test.test_support.is_resource_enabled("compiler") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 166 | test.test_support.run_unittest(CompilerTest) |
| 167 | |
| 168 | if __name__ == "__main__": |
| 169 | test_main() |