| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  |    Test cases for codeop.py | 
					
						
							|  |  |  |    Nick Mathewson | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  | from test.test_support import run_unittest, is_jython | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-13 22:07:59 +00:00
										 |  |  | from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  | if is_jython: | 
					
						
							|  |  |  |     import sys | 
					
						
							|  |  |  |     import cStringIO | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def unify_callables(d): | 
					
						
							|  |  |  |         for n,v in d.items(): | 
					
						
							|  |  |  |             if callable(v): | 
					
						
							|  |  |  |                 d[n] = callable | 
					
						
							|  |  |  |         return d | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  | class CodeopTests(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def assertValid(self, str, symbol='single'): | 
					
						
							|  |  |  |         '''succeed iff str is a valid piece of code''' | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         if is_jython: | 
					
						
							|  |  |  |             code = compile_command(str, "<input>", symbol) | 
					
						
							|  |  |  |             self.assert_(code) | 
					
						
							|  |  |  |             if symbol == "single": | 
					
						
							|  |  |  |                 d,r = {},{} | 
					
						
							| 
									
										
										
										
											2003-05-17 12:51:10 +00:00
										 |  |  |                 saved_stdout = sys.stdout | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |                 sys.stdout = cStringIO.StringIO() | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     exec code in d | 
					
						
							|  |  |  |                     exec compile(str,"<input>","single") in r | 
					
						
							|  |  |  |                 finally: | 
					
						
							| 
									
										
										
										
											2003-05-17 12:51:10 +00:00
										 |  |  |                     sys.stdout = saved_stdout | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |             elif symbol == 'eval': | 
					
						
							|  |  |  |                 ctx = {'a': 2} | 
					
						
							|  |  |  |                 d = { 'value': eval(code,ctx) } | 
					
						
							|  |  |  |                 r = { 'value': eval(str,ctx) } | 
					
						
							|  |  |  |             self.assertEquals(unify_callables(r),unify_callables(d)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) | 
					
						
							|  |  |  |             self.assertEquals( compile_command(str, "<input>", symbol), expected) | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def assertIncomplete(self, str, symbol='single'): | 
					
						
							|  |  |  |         '''succeed iff str is the start of a valid piece of code''' | 
					
						
							|  |  |  |         self.assertEquals( compile_command(str, symbol=symbol), None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def assertInvalid(self, str, symbol='single', is_syntax=1): | 
					
						
							|  |  |  |         '''succeed iff str is the start of an invalid piece of code''' | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             compile_command(str,symbol=symbol) | 
					
						
							|  |  |  |             self.fail("No exception thrown for invalid code") | 
					
						
							|  |  |  |         except SyntaxError: | 
					
						
							|  |  |  |             self.assert_(is_syntax) | 
					
						
							|  |  |  |         except OverflowError: | 
					
						
							|  |  |  |             self.assert_(not is_syntax) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_valid(self): | 
					
						
							|  |  |  |         av = self.assertValid | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # special case | 
					
						
							|  |  |  |         if not is_jython: | 
					
						
							|  |  |  |             self.assertEquals(compile_command(""), | 
					
						
							|  |  |  |                             compile("pass", "<input>", 'single', | 
					
						
							|  |  |  |                                     PyCF_DONT_IMPLY_DEDENT)) | 
					
						
							|  |  |  |             self.assertEquals(compile_command("\n"), | 
					
						
							| 
									
										
										
										
											2003-06-15 23:26:30 +00:00
										 |  |  |                             compile("pass", "<input>", 'single', | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |                                     PyCF_DONT_IMPLY_DEDENT)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             av("") | 
					
						
							|  |  |  |             av("\n") | 
					
						
							| 
									
										
										
										
											2003-06-15 23:26:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         av("a = 1") | 
					
						
							|  |  |  |         av("\na = 1") | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         av("a = 1\n") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         av("a = 1\n\n") | 
					
						
							|  |  |  |         av("\n\na = 1\n\n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         av("def x():\n  pass\n") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         av("if 1:\n pass\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         av("\n\nif 1: pass\n") | 
					
						
							|  |  |  |         av("\n\nif 1: pass\n\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         av("def x():\n\n pass\n") | 
					
						
							|  |  |  |         av("def x():\n  pass\n  \n") | 
					
						
							|  |  |  |         av("def x():\n  pass\n \n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         av("pass\n") | 
					
						
							|  |  |  |         av("3**3\n") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         av("if 9==3:\n   pass\nelse:\n   pass\n") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         av("if 1:\n pass\n if 1:\n  pass\n else:\n  pass\n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         av("#a\n#b\na = 3\n") | 
					
						
							|  |  |  |         av("#a\n\n   \na=3\n") | 
					
						
							|  |  |  |         av("a=3\n\n") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         av("a = 9+ \\\n3") | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         av("3**3","eval") | 
					
						
							|  |  |  |         av("(lambda z: \n z**3)","eval") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         av("9+ \\\n3","eval") | 
					
						
							|  |  |  |         av("9+ \\\n3\n","eval") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         av("\n\na**3","eval") | 
					
						
							|  |  |  |         av("\n \na**3","eval") | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         av("#a\n#b\na**3","eval") | 
					
						
							| 
									
										
										
										
											2001-08-09 21:40:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |     def test_incomplete(self): | 
					
						
							|  |  |  |         ai = self.assertIncomplete | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         ai("(a **") | 
					
						
							|  |  |  |         ai("(a,b,") | 
					
						
							|  |  |  |         ai("(a,b,(") | 
					
						
							|  |  |  |         ai("(a,b,(") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         ai("a = (") | 
					
						
							|  |  |  |         ai("a = {") | 
					
						
							|  |  |  |         ai("b + {") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ai("if 9==3:\n   pass\nelse:") | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         ai("if 9==3:\n   pass\nelse:\n") | 
					
						
							|  |  |  |         ai("if 9==3:\n   pass\nelse:\n   pass") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         ai("if 1:") | 
					
						
							|  |  |  |         ai("if 1:\n") | 
					
						
							|  |  |  |         ai("if 1:\n pass\n if 1:\n  pass\n else:") | 
					
						
							| 
									
										
										
										
											2003-06-15 23:26:30 +00:00
										 |  |  |         ai("if 1:\n pass\n if 1:\n  pass\n else:\n") | 
					
						
							|  |  |  |         ai("if 1:\n pass\n if 1:\n  pass\n else:\n  pass") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         ai("def x():") | 
					
						
							|  |  |  |         ai("def x():\n") | 
					
						
							|  |  |  |         ai("def x():\n\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ai("def x():\n  pass") | 
					
						
							|  |  |  |         ai("def x():\n  pass\n ") | 
					
						
							|  |  |  |         ai("def x():\n  pass\n  ") | 
					
						
							|  |  |  |         ai("\n\ndef x():\n  pass") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         ai("a = 9+ \\") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         ai("a = 'a\\") | 
					
						
							|  |  |  |         ai("a = '''xy") | 
					
						
							| 
									
										
										
										
											2001-08-09 21:40:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         ai("","eval") | 
					
						
							|  |  |  |         ai("\n","eval") | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         ai("(","eval") | 
					
						
							|  |  |  |         ai("(\n\n\n","eval") | 
					
						
							|  |  |  |         ai("(9+","eval") | 
					
						
							|  |  |  |         ai("9+ \\","eval") | 
					
						
							|  |  |  |         ai("lambda z: \\","eval") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_invalid(self): | 
					
						
							|  |  |  |         ai = self.assertInvalid | 
					
						
							|  |  |  |         ai("a b") | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ai("a @") | 
					
						
							|  |  |  |         ai("a b @") | 
					
						
							|  |  |  |         ai("a ** @") | 
					
						
							| 
									
										
										
										
											2003-06-15 23:26:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         ai("a = ") | 
					
						
							|  |  |  |         ai("a = 9 +") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-17 02:39:52 +00:00
										 |  |  |         ai("def x():\n\npass\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ai("\n\n if 1: pass\n\npass") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ai("a = 9+ \\\n") | 
					
						
							|  |  |  |         ai("a = 'a\\ ") | 
					
						
							|  |  |  |         ai("a = 'a\\\n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-30 12:30:08 +00:00
										 |  |  |         ai("a = 1","eval") | 
					
						
							|  |  |  |         ai("a = (","eval") | 
					
						
							|  |  |  |         ai("]","eval") | 
					
						
							|  |  |  |         ai("())","eval") | 
					
						
							|  |  |  |         ai("[}","eval") | 
					
						
							|  |  |  |         ai("9+","eval") | 
					
						
							|  |  |  |         ai("lambda z:","eval") | 
					
						
							|  |  |  |         ai("a b","eval") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_filename(self): | 
					
						
							|  |  |  |         self.assertEquals(compile_command("a = 1\n", "abc").co_filename, | 
					
						
							|  |  |  |                           compile("a = 1\n", "abc", 'single').co_filename) | 
					
						
							|  |  |  |         self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename, | 
					
						
							|  |  |  |                              compile("a = 1\n", "def", 'single').co_filename) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-20 21:33:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							|  |  |  |     run_unittest(CodeopTests) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |