Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 1 | import compiler |
| 2 | import os |
| 3 | import test.test_support |
| 4 | import unittest |
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 | |
| 7 | class CompilerTest(unittest.TestCase): |
| 8 | |
| 9 | def testCompileLibrary(self): |
| 10 | # A simple but large test. Compile all the code in the |
| 11 | # standard library and its test suite. This doesn't verify |
| 12 | # that any of the code is correct, merely the compiler is able |
| 13 | # to generate some kind of code for it. |
| 14 | |
| 15 | libdir = os.path.dirname(unittest.__file__) |
| 16 | testdir = os.path.dirname(test.test_support.__file__) |
| 17 | |
| 18 | for dir in [libdir, testdir]: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 19 | for basename in os.listdir(dir): |
| 20 | if not basename.endswith(".py"): |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 21 | continue |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 22 | if not TEST_ALL and random() < 0.98: |
| 23 | continue |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 24 | path = os.path.join(dir, basename) |
Tim Peters | b6ecc16 | 2004-08-08 16:32:54 +0000 | [diff] [blame] | 25 | if test.test_support.verbose: |
Tim Peters | 2a5f656 | 2004-08-08 16:37:37 +0000 | [diff] [blame] | 26 | print "compiling", path |
Michael W. Hudson | 29589a0 | 2004-10-11 15:34:31 +0000 | [diff] [blame] | 27 | f = open(path, "U") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 28 | buf = f.read() |
| 29 | f.close() |
Tim Peters | 0955f29 | 2004-08-08 16:43:59 +0000 | [diff] [blame] | 30 | if "badsyntax" in basename: |
| 31 | self.assertRaises(SyntaxError, compiler.compile, |
| 32 | buf, basename, "exec") |
| 33 | else: |
| 34 | compiler.compile(buf, basename, "exec") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 35 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 36 | def testLineNo(self): |
| 37 | # Test that all nodes except Module have a correct lineno attribute. |
| 38 | filename = __file__ |
| 39 | if filename.endswith(".pyc") or filename.endswith(".pyo"): |
| 40 | filename = filename[:-1] |
| 41 | tree = compiler.parseFile(filename) |
| 42 | self.check_lineno(tree) |
| 43 | |
| 44 | def check_lineno(self, node): |
| 45 | try: |
| 46 | self._check_lineno(node) |
| 47 | except AssertionError: |
| 48 | print node.__class__, node.lineno |
| 49 | raise |
| 50 | |
| 51 | def _check_lineno(self, node): |
| 52 | if not node.__class__ in NOLINENO: |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 53 | self.assert_(isinstance(node.lineno, int), |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 54 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 55 | self.assert_(node.lineno > 0, |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 56 | "lineno=%s on %s" % (node.lineno, node.__class__)) |
| 57 | for child in node.getChildNodes(): |
| 58 | self.check_lineno(child) |
| 59 | |
| 60 | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) |
| 61 | |
| 62 | ############################################################################### |
| 63 | # code below is just used to trigger some possible errors, for the benefit of |
| 64 | # testLineNo |
| 65 | ############################################################################### |
| 66 | |
| 67 | class Toto: |
| 68 | """docstring""" |
| 69 | pass |
| 70 | |
| 71 | a, b = 2, 3 |
| 72 | [c, d] = 5, 6 |
| 73 | l = [(x, y) for x, y in zip(range(5), range(5,10))] |
| 74 | l[0] |
| 75 | l[3:4] |
| 76 | if l: |
| 77 | pass |
| 78 | else: |
| 79 | a, b = b, a |
| 80 | |
| 81 | try: |
| 82 | print yo |
| 83 | except: |
| 84 | yo = 3 |
| 85 | else: |
| 86 | yo += 3 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 87 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 88 | try: |
| 89 | a += b |
| 90 | finally: |
| 91 | b = 0 |
Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 92 | |
Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 93 | from math import * |
| 94 | |
Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 95 | ############################################################################### |
| 96 | |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 97 | def test_main(): |
Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 98 | global TEST_ALL |
| 99 | TEST_ALL = test.test_support.is_resource_enabled("compiler") |
Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 100 | test.test_support.run_unittest(CompilerTest) |
| 101 | |
| 102 | if __name__ == "__main__": |
| 103 | test_main() |