| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  | import compiler | 
					
						
							| 
									
										
										
										
											2005-06-02 05:55:20 +00:00
										 |  |  | from compiler.ast import flatten | 
					
						
							| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  | import os | 
					
						
							|  |  |  | import test.test_support | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2004-09-04 20:09:13 +00:00
										 |  |  | from random import random | 
					
						
							| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class CompilerTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def testCompileLibrary(self): | 
					
						
							|  |  |  |         # A simple but large test.  Compile all the code in the | 
					
						
							|  |  |  |         # standard library and its test suite.  This doesn't verify | 
					
						
							|  |  |  |         # that any of the code is correct, merely the compiler is able | 
					
						
							|  |  |  |         # to generate some kind of code for it. | 
					
						
							|  |  |  |         libdir = os.path.dirname(unittest.__file__) | 
					
						
							|  |  |  |         testdir = os.path.dirname(test.test_support.__file__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for dir in [libdir, testdir]: | 
					
						
							| 
									
										
										
										
											2004-08-08 16:37:37 +00:00
										 |  |  |             for basename in os.listdir(dir): | 
					
						
							|  |  |  |                 if not basename.endswith(".py"): | 
					
						
							| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2004-09-04 20:09:13 +00:00
										 |  |  |                 if not TEST_ALL and random() < 0.98: | 
					
						
							|  |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2004-08-08 16:37:37 +00:00
										 |  |  |                 path = os.path.join(dir, basename) | 
					
						
							| 
									
										
										
										
											2004-08-08 16:32:54 +00:00
										 |  |  |                 if test.test_support.verbose: | 
					
						
							| 
									
										
										
										
											2004-08-08 16:37:37 +00:00
										 |  |  |                     print "compiling", path | 
					
						
							| 
									
										
										
										
											2004-10-11 15:34:31 +00:00
										 |  |  |                 f = open(path, "U") | 
					
						
							| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  |                 buf = f.read() | 
					
						
							|  |  |  |                 f.close() | 
					
						
							| 
									
										
										
										
											2005-10-24 00:01:37 +00:00
										 |  |  |                 if "badsyntax" in basename or "bad_coding" in basename: | 
					
						
							| 
									
										
										
										
											2004-08-08 16:43:59 +00:00
										 |  |  |                     self.assertRaises(SyntaxError, compiler.compile, | 
					
						
							|  |  |  |                                       buf, basename, "exec") | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     compiler.compile(buf, basename, "exec") | 
					
						
							| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-09 02:30:16 +00:00
										 |  |  |     def testNewClassSyntax(self): | 
					
						
							|  |  |  |         compiler.compile("class foo():pass\n\n","<string>","exec") | 
					
						
							| 
									
										
										
										
											2006-01-06 19:28:15 +00:00
										 |  |  |      | 
					
						
							|  |  |  |     def testSyntaxErrors(self): | 
					
						
							|  |  |  |         self.assertRaises(SyntaxError, compiler.compile,  | 
					
						
							|  |  |  |                           "def foo(a=1,b):pass\n\n", "<string>", "exec") | 
					
						
							| 
									
										
										
										
											2005-04-20 17:45:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-07 15:28:01 +00:00
										 |  |  |     def testLineNo(self): | 
					
						
							|  |  |  |         # Test that all nodes except Module have a correct lineno attribute. | 
					
						
							|  |  |  |         filename = __file__ | 
					
						
							|  |  |  |         if filename.endswith(".pyc") or filename.endswith(".pyo"): | 
					
						
							|  |  |  |             filename = filename[:-1] | 
					
						
							|  |  |  |         tree = compiler.parseFile(filename) | 
					
						
							|  |  |  |         self.check_lineno(tree) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def check_lineno(self, node): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self._check_lineno(node) | 
					
						
							|  |  |  |         except AssertionError: | 
					
						
							|  |  |  |             print node.__class__, node.lineno | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _check_lineno(self, node): | 
					
						
							|  |  |  |         if not node.__class__ in NOLINENO: | 
					
						
							| 
									
										
										
										
											2004-09-12 03:49:31 +00:00
										 |  |  |             self.assert_(isinstance(node.lineno, int), | 
					
						
							| 
									
										
										
										
											2004-09-07 15:28:01 +00:00
										 |  |  |                 "lineno=%s on %s" % (node.lineno, node.__class__)) | 
					
						
							| 
									
										
										
										
											2004-09-12 03:49:31 +00:00
										 |  |  |             self.assert_(node.lineno > 0, | 
					
						
							| 
									
										
										
										
											2004-09-07 15:28:01 +00:00
										 |  |  |                 "lineno=%s on %s" % (node.lineno, node.__class__)) | 
					
						
							|  |  |  |         for child in node.getChildNodes(): | 
					
						
							|  |  |  |             self.check_lineno(child) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-02 05:55:20 +00:00
										 |  |  |     def testFlatten(self): | 
					
						
							|  |  |  |         self.assertEquals(flatten([1, [2]]), [1, 2]) | 
					
						
							|  |  |  |         self.assertEquals(flatten((1, (2,))), [1, 2]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-07 15:28:01 +00:00
										 |  |  | NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ############################################################################### | 
					
						
							|  |  |  | # code below is just used to trigger some possible errors, for the benefit of | 
					
						
							|  |  |  | # testLineNo | 
					
						
							|  |  |  | ############################################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Toto: | 
					
						
							|  |  |  |     """docstring""" | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | a, b = 2, 3 | 
					
						
							|  |  |  | [c, d] = 5, 6 | 
					
						
							|  |  |  | l = [(x, y) for x, y in zip(range(5), range(5,10))] | 
					
						
							|  |  |  | l[0] | 
					
						
							|  |  |  | l[3:4] | 
					
						
							|  |  |  | if l: | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     a, b = b, a | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     print yo | 
					
						
							|  |  |  | except: | 
					
						
							|  |  |  |     yo = 3 | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     yo += 3 | 
					
						
							| 
									
										
										
										
											2004-09-12 03:49:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-07 15:28:01 +00:00
										 |  |  | try: | 
					
						
							|  |  |  |     a += b | 
					
						
							|  |  |  | finally: | 
					
						
							|  |  |  |     b = 0 | 
					
						
							| 
									
										
										
										
											2004-09-12 03:49:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-08 16:46:02 +00:00
										 |  |  | from math import * | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-07 15:28:01 +00:00
										 |  |  | ############################################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2004-09-04 20:09:13 +00:00
										 |  |  |     global TEST_ALL | 
					
						
							|  |  |  |     TEST_ALL = test.test_support.is_resource_enabled("compiler") | 
					
						
							| 
									
										
										
										
											2004-08-07 19:25:33 +00:00
										 |  |  |     test.test_support.run_unittest(CompilerTest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |