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...' |
| 29 | |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 30 | if not basename.endswith(".py"): |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 31 | continue |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 32 | if not TEST_ALL and random() < 0.98: |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 33 | continue |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 34 | path = os.path.join(dir, basename) |
Tim Peters | b6ecc16 | 2004-08-08 16:32:54 +0000 | [diff] [blame] | 35 | if test.test_support.verbose: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 36 | print "compiling", path |
Michael W. Hudson | 29589a0 | 2004-10-11 15:34:31 +0000 | [diff] [blame] | 37 | f = open(path, "U") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 38 | buf = f.read() |
| 39 | f.close() |
Neal Norwitz | f895065 | 2005-10-24 00:01:37 +0000 | [diff] [blame] | 40 | if "badsyntax" in basename or "bad_coding" in basename: |
Tim Peters | 0955f29 | 2004-08-08 16:43:59 +0000 | [diff] [blame] | 41 | self.assertRaises(SyntaxError, compiler.compile, |
| 42 | buf, basename, "exec") |
| 43 | else: |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 44 | try: |
| 45 | compiler.compile(buf, basename, "exec") |
| 46 | except Exception, e: |
Martin v. Löwis | d9bfeac | 2006-03-01 23:24:34 +0000 | [diff] [blame] | 47 | args = list(e.args) |
| 48 | args[0] += "[in file %s]" % basename |
| 49 | e.args = tuple(args) |
Martin v. Löwis | 15bfc3b | 2006-03-01 21:11:49 +0000 | [diff] [blame] | 50 | raise |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 51 | |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 52 | def testNewClassSyntax(self): |
| 53 | compiler.compile("class foo():pass\n\n","<string>","exec") |
Tim Peters | e890682 | 2005-04-20 17:45:13 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 5bde08d | 2006-03-02 04:24:01 +0000 | [diff] [blame] | 55 | def testYieldExpr(self): |
| 56 | compiler.compile("def g(): yield\n\n", "<string>", "exec") |
| 57 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 58 | def testLineNo(self): |
| 59 | # Test that all nodes except Module have a correct lineno attribute. |
| 60 | filename = __file__ |
| 61 | if filename.endswith(".pyc") or filename.endswith(".pyo"): |
| 62 | filename = filename[:-1] |
| 63 | tree = compiler.parseFile(filename) |
| 64 | self.check_lineno(tree) |
| 65 | |
| 66 | def check_lineno(self, node): |
| 67 | try: |
| 68 | self._check_lineno(node) |
| 69 | except AssertionError: |
| 70 | print node.__class__, node.lineno |
| 71 | raise |
| 72 | |
| 73 | def _check_lineno(self, node): |
| 74 | if not node.__class__ in NOLINENO: |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 75 | self.assert_(isinstance(node.lineno, int), |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 76 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 77 | self.assert_(node.lineno > 0, |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 78 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
| 79 | for child in node.getChildNodes(): |
| 80 | self.check_lineno(child) |
| 81 | |
Neil Schemenauer | f369470 | 2005-06-02 05:55:20 +0000 | [diff] [blame] | 82 | def testFlatten(self): |
| 83 | self.assertEquals(flatten([1, [2]]), [1, 2]) |
| 84 | self.assertEquals(flatten((1, (2,))), [1, 2]) |
| 85 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 86 | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) |
| 87 | |
| 88 | ############################################################################### |
| 89 | # code below is just used to trigger some possible errors, for the benefit of |
| 90 | # testLineNo |
| 91 | ############################################################################### |
| 92 | |
| 93 | class Toto: |
| 94 | """docstring""" |
| 95 | pass |
| 96 | |
| 97 | a, b = 2, 3 |
| 98 | [c, d] = 5, 6 |
| 99 | l = [(x, y) for x, y in zip(range(5), range(5,10))] |
| 100 | l[0] |
| 101 | l[3:4] |
| 102 | if l: |
| 103 | pass |
| 104 | else: |
| 105 | a, b = b, a |
| 106 | |
| 107 | try: |
| 108 | print yo |
| 109 | except: |
| 110 | yo = 3 |
| 111 | else: |
| 112 | yo += 3 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 113 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 114 | try: |
| 115 | a += b |
| 116 | finally: |
| 117 | b = 0 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 118 | |
Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 119 | from math import * |
| 120 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 121 | ############################################################################### |
| 122 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 123 | def test_main(): |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 124 | global TEST_ALL |
| 125 | TEST_ALL = test.test_support.is_resource_enabled("compiler") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 126 | test.test_support.run_unittest(CompilerTest) |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | test_main() |