| 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 |  | 
| Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 36 | def testNewClassSyntax(self): | 
|  | 37 | compiler.compile("class foo():pass\n\n","<string>","exec") | 
| Tim Peters | e890682 | 2005-04-20 17:45:13 +0000 | [diff] [blame^] | 38 |  | 
| Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 39 | def testLineNo(self): | 
|  | 40 | # Test that all nodes except Module have a correct lineno attribute. | 
|  | 41 | filename = __file__ | 
|  | 42 | if filename.endswith(".pyc") or filename.endswith(".pyo"): | 
|  | 43 | filename = filename[:-1] | 
|  | 44 | tree = compiler.parseFile(filename) | 
|  | 45 | self.check_lineno(tree) | 
|  | 46 |  | 
|  | 47 | def check_lineno(self, node): | 
|  | 48 | try: | 
|  | 49 | self._check_lineno(node) | 
|  | 50 | except AssertionError: | 
|  | 51 | print node.__class__, node.lineno | 
|  | 52 | raise | 
|  | 53 |  | 
|  | 54 | def _check_lineno(self, node): | 
|  | 55 | if not node.__class__ in NOLINENO: | 
| Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 56 | self.assert_(isinstance(node.lineno, int), | 
| Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 57 | "lineno=%s on %s" % (node.lineno, node.__class__)) | 
| Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 58 | self.assert_(node.lineno > 0, | 
| Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 59 | "lineno=%s on %s" % (node.lineno, node.__class__)) | 
|  | 60 | for child in node.getChildNodes(): | 
|  | 61 | self.check_lineno(child) | 
|  | 62 |  | 
|  | 63 | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) | 
|  | 64 |  | 
|  | 65 | ############################################################################### | 
|  | 66 | # code below is just used to trigger some possible errors, for the benefit of | 
|  | 67 | # testLineNo | 
|  | 68 | ############################################################################### | 
|  | 69 |  | 
|  | 70 | class Toto: | 
|  | 71 | """docstring""" | 
|  | 72 | pass | 
|  | 73 |  | 
|  | 74 | a, b = 2, 3 | 
|  | 75 | [c, d] = 5, 6 | 
|  | 76 | l = [(x, y) for x, y in zip(range(5), range(5,10))] | 
|  | 77 | l[0] | 
|  | 78 | l[3:4] | 
|  | 79 | if l: | 
|  | 80 | pass | 
|  | 81 | else: | 
|  | 82 | a, b = b, a | 
|  | 83 |  | 
|  | 84 | try: | 
|  | 85 | print yo | 
|  | 86 | except: | 
|  | 87 | yo = 3 | 
|  | 88 | else: | 
|  | 89 | yo += 3 | 
| Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 90 |  | 
| Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 91 | try: | 
|  | 92 | a += b | 
|  | 93 | finally: | 
|  | 94 | b = 0 | 
| Tim Peters | 0e9980f | 2004-09-12 03:49:31 +0000 | [diff] [blame] | 95 |  | 
| Michael W. Hudson | e0b855f | 2004-11-08 16:46:02 +0000 | [diff] [blame] | 96 | from math import * | 
|  | 97 |  | 
| Jeremy Hylton | 566d934 | 2004-09-07 15:28:01 +0000 | [diff] [blame] | 98 | ############################################################################### | 
|  | 99 |  | 
| Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 100 | def test_main(): | 
| Raymond Hettinger | ed20ad8 | 2004-09-04 20:09:13 +0000 | [diff] [blame] | 101 | global TEST_ALL | 
|  | 102 | TEST_ALL = test.test_support.is_resource_enabled("compiler") | 
| Jeremy Hylton | 4336eda | 2004-08-07 19:25:33 +0000 | [diff] [blame] | 103 | test.test_support.run_unittest(CompilerTest) | 
|  | 104 |  | 
|  | 105 | if __name__ == "__main__": | 
|  | 106 | test_main() |