0001 """ 0002 Test cases for codeop.py 0003 Nick Mathewson 0004 """ 0005 import unittest 0006 from test.test_support import run_unittest, is_jython 0007 0008 from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT 0009 0010 if is_jython: 0011 import sys 0012 import cStringIO 0013 0014 def unify_callables(d): 0015 for n,v in d.items(): 0016 if callable(v): 0017 d[n] = callable 0018 return d 0019 0020 class CodeopTests(unittest.TestCase): 0021 0022 def assertValid(self, str, symbol='single'): 0023 '''succeed iff str is a valid piece of code''' 0024 if is_jython: 0025 code = compile_command(str, "<input>", symbol) 0026 self.assert_(code) 0027 if symbol == "single": 0028 d,r = {},{} 0029 saved_stdout = sys.stdout 0030 sys.stdout = cStringIO.StringIO() 0031 try: 0032 exec code in d 0033 exec compile(str,"<input>","single") in r 0034 finally: 0035 sys.stdout = saved_stdout 0036 elif symbol == 'eval': 0037 ctx = {'a': 2} 0038 d = { 'value': eval(code,ctx) } 0039 r = { 'value': eval(str,ctx) } 0040 self.assertEquals(unify_callables(r),unify_callables(d)) 0041 else: 0042 expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) 0043 self.assertEquals( compile_command(str, "<input>", symbol), expected) 0044 0045 def assertIncomplete(self, str, symbol='single'): 0046 '''succeed iff str is the start of a valid piece of code''' 0047 self.assertEquals( compile_command(str, symbol=symbol), None) 0048 0049 def assertInvalid(self, str, symbol='single', is_syntax=1): 0050 '''succeed iff str is the start of an invalid piece of code''' 0051 try: 0052 compile_command(str,symbol=symbol) 0053 self.fail("No exception thrown for invalid code") 0054 except SyntaxError: 0055 self.assert_(is_syntax) 0056 except OverflowError: 0057 self.assert_(not is_syntax) 0058 0059 def test_valid(self): 0060 av = self.assertValid 0061 0062 # special case 0063 if not is_jython: 0064 self.assertEquals(compile_command(""), 0065 compile("pass", "<input>", 'single', 0066 PyCF_DONT_IMPLY_DEDENT)) 0067 self.assertEquals(compile_command("\n"), 0068 compile("pass", "<input>", 'single', 0069 PyCF_DONT_IMPLY_DEDENT)) 0070 else: 0071 av("") 0072 av("\n") 0073 0074 av("a = 1") 0075 av("\na = 1") 0076 av("a = 1\n") 0077 av("a = 1\n\n") 0078 av("\n\na = 1\n\n") 0079 0080 av("def x():\n pass\n") 0081 av("if 1:\n pass\n") 0082 0083 av("\n\nif 1: pass\n") 0084 av("\n\nif 1: pass\n\n") 0085 0086 av("def x():\n\n pass\n") 0087 av("def x():\n pass\n \n") 0088 av("def x():\n pass\n \n") 0089 0090 av("pass\n") 0091 av("3**3\n") 0092 0093 av("if 9==3:\n pass\nelse:\n pass\n") 0094 av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") 0095 0096 av("#a\n#b\na = 3\n") 0097 av("#a\n\n \na=3\n") 0098 av("a=3\n\n") 0099 av("a = 9+ \\\n3") 0100 0101 av("3**3","eval") 0102 av("(lambda z: \n z**3)","eval") 0103 0104 av("9+ \\\n3","eval") 0105 av("9+ \\\n3\n","eval") 0106 0107 av("\n\na**3","eval") 0108 av("\n \na**3","eval") 0109 av("#a\n#b\na**3","eval") 0110 0111 def test_incomplete(self): 0112 ai = self.assertIncomplete 0113 0114 ai("(a **") 0115 ai("(a,b,") 0116 ai("(a,b,(") 0117 ai("(a,b,(") 0118 ai("a = (") 0119 ai("a = {") 0120 ai("b + {") 0121 0122 ai("if 9==3:\n pass\nelse:") 0123 ai("if 9==3:\n pass\nelse:\n") 0124 ai("if 9==3:\n pass\nelse:\n pass") 0125 ai("if 1:") 0126 ai("if 1:\n") 0127 ai("if 1:\n pass\n if 1:\n pass\n else:") 0128 ai("if 1:\n pass\n if 1:\n pass\n else:\n") 0129 ai("if 1:\n pass\n if 1:\n pass\n else:\n pass") 0130 0131 ai("def x():") 0132 ai("def x():\n") 0133 ai("def x():\n\n") 0134 0135 ai("def x():\n pass") 0136 ai("def x():\n pass\n ") 0137 ai("def x():\n pass\n ") 0138 ai("\n\ndef x():\n pass") 0139 0140 ai("a = 9+ \\") 0141 ai("a = 'a\\") 0142 ai("a = '''xy") 0143 0144 ai("","eval") 0145 ai("\n","eval") 0146 ai("(","eval") 0147 ai("(\n\n\n","eval") 0148 ai("(9+","eval") 0149 ai("9+ \\","eval") 0150 ai("lambda z: \\","eval") 0151 0152 def test_invalid(self): 0153 ai = self.assertInvalid 0154 ai("a b") 0155 0156 ai("a @") 0157 ai("a b @") 0158 ai("a ** @") 0159 0160 ai("a = ") 0161 ai("a = 9 +") 0162 0163 ai("def x():\n\npass\n") 0164 0165 ai("\n\n if 1: pass\n\npass") 0166 0167 ai("a = 9+ \\\n") 0168 ai("a = 'a\\ ") 0169 ai("a = 'a\\\n") 0170 0171 ai("a = 1","eval") 0172 ai("a = (","eval") 0173 ai("]","eval") 0174 ai("())","eval") 0175 ai("[}","eval") 0176 ai("9+","eval") 0177 ai("lambda z:","eval") 0178 ai("a b","eval") 0179 0180 def test_filename(self): 0181 self.assertEquals(compile_command("a = 1\n", "abc").co_filename, 0182 compile("a = 1\n", "abc", 'single').co_filename) 0183 self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename, 0184 compile("a = 1\n", "def", 'single').co_filename) 0185 0186 0187 def test_main(): 0188 run_unittest(CodeopTests) 0189 0190 0191 if __name__ == "__main__": 0192 test_main() 0193
Generated by PyXR 0.9.4