0001 import compiler 0002 import os 0003 import test.test_support 0004 import unittest 0005 from random import random 0006 0007 class CompilerTest(unittest.TestCase): 0008 0009 def testCompileLibrary(self): 0010 # A simple but large test. Compile all the code in the 0011 # standard library and its test suite. This doesn't verify 0012 # that any of the code is correct, merely the compiler is able 0013 # to generate some kind of code for it. 0014 0015 libdir = os.path.dirname(unittest.__file__) 0016 testdir = os.path.dirname(test.test_support.__file__) 0017 0018 for dir in [libdir, testdir]: 0019 for basename in os.listdir(dir): 0020 if not basename.endswith(".py"): 0021 continue 0022 if not TEST_ALL and random() < 0.98: 0023 continue 0024 path = os.path.join(dir, basename) 0025 if test.test_support.verbose: 0026 print "compiling", path 0027 f = open(path, "U") 0028 buf = f.read() 0029 f.close() 0030 if "badsyntax" in basename: 0031 self.assertRaises(SyntaxError, compiler.compile, 0032 buf, basename, "exec") 0033 else: 0034 compiler.compile(buf, basename, "exec") 0035 0036 def testLineNo(self): 0037 # Test that all nodes except Module have a correct lineno attribute. 0038 filename = __file__ 0039 if filename.endswith(".pyc") or filename.endswith(".pyo"): 0040 filename = filename[:-1] 0041 tree = compiler.parseFile(filename) 0042 self.check_lineno(tree) 0043 0044 def check_lineno(self, node): 0045 try: 0046 self._check_lineno(node) 0047 except AssertionError: 0048 print node.__class__, node.lineno 0049 raise 0050 0051 def _check_lineno(self, node): 0052 if not node.__class__ in NOLINENO: 0053 self.assert_(isinstance(node.lineno, int), 0054 "lineno=%s on %s" % (node.lineno, node.__class__)) 0055 self.assert_(node.lineno > 0, 0056 "lineno=%s on %s" % (node.lineno, node.__class__)) 0057 for child in node.getChildNodes(): 0058 self.check_lineno(child) 0059 0060 NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) 0061 0062 ############################################################################### 0063 # code below is just used to trigger some possible errors, for the benefit of 0064 # testLineNo 0065 ############################################################################### 0066 0067 class Toto: 0068 """docstring""" 0069 pass 0070 0071 a, b = 2, 3 0072 [c, d] = 5, 6 0073 l = [(x, y) for x, y in zip(range(5), range(5,10))] 0074 l[0] 0075 l[3:4] 0076 if l: 0077 pass 0078 else: 0079 a, b = b, a 0080 0081 try: 0082 print yo 0083 except: 0084 yo = 3 0085 else: 0086 yo += 3 0087 0088 try: 0089 a += b 0090 finally: 0091 b = 0 0092 0093 ############################################################################### 0094 0095 def test_main(): 0096 global TEST_ALL 0097 TEST_ALL = test.test_support.is_resource_enabled("compiler") 0098 test.test_support.run_unittest(CompilerTest) 0099 0100 if __name__ == "__main__": 0101 test_main() 0102
Generated by PyXR 0.9.4