0001 from test.test_support import verify, verbose, TestFailed, run_unittest 0002 import sys 0003 import dis 0004 import StringIO 0005 0006 # Minimal tests for dis module 0007 0008 import unittest 0009 0010 def _f(a): 0011 print a 0012 return 1 0013 0014 dis_f = """\ 0015 %-4d 0 LOAD_FAST 0 (a) 0016 3 PRINT_ITEM 0017 4 PRINT_NEWLINE 0018 0019 %-4d 5 LOAD_CONST 1 (1) 0020 8 RETURN_VALUE 0021 """%(_f.func_code.co_firstlineno + 1, 0022 _f.func_code.co_firstlineno + 2) 0023 0024 0025 def bug708901(): 0026 for res in range(1, 0027 10): 0028 pass 0029 0030 dis_bug708901 = """\ 0031 %-4d 0 SETUP_LOOP 23 (to 26) 0032 3 LOAD_GLOBAL 0 (range) 0033 6 LOAD_CONST 1 (1) 0034 0035 %-4d 9 LOAD_CONST 2 (10) 0036 12 CALL_FUNCTION 2 0037 15 GET_ITER 0038 >> 16 FOR_ITER 6 (to 25) 0039 19 STORE_FAST 0 (res) 0040 0041 %-4d 22 JUMP_ABSOLUTE 16 0042 >> 25 POP_BLOCK 0043 >> 26 LOAD_CONST 0 (None) 0044 29 RETURN_VALUE 0045 """%(bug708901.func_code.co_firstlineno + 1, 0046 bug708901.func_code.co_firstlineno + 2, 0047 bug708901.func_code.co_firstlineno + 3) 0048 0049 class DisTests(unittest.TestCase): 0050 def do_disassembly_test(self, func, expected): 0051 s = StringIO.StringIO() 0052 save_stdout = sys.stdout 0053 sys.stdout = s 0054 dis.dis(func) 0055 sys.stdout = save_stdout 0056 got = s.getvalue() 0057 # Trim trailing blanks (if any). 0058 lines = got.split('\n') 0059 lines = [line.rstrip() for line in lines] 0060 expected = expected.split("\n") 0061 import difflib 0062 if expected != lines: 0063 self.fail( 0064 "events did not match expectation:\n" + 0065 "\n".join(difflib.ndiff(expected, 0066 lines))) 0067 0068 def test_opmap(self): 0069 self.assertEqual(dis.opmap["STOP_CODE"], 0) 0070 self.assertEqual(dis.opmap["LOAD_CONST"] in dis.hasconst, True) 0071 self.assertEqual(dis.opmap["STORE_NAME"] in dis.hasname, True) 0072 0073 def test_opname(self): 0074 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") 0075 0076 def test_boundaries(self): 0077 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) 0078 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT) 0079 0080 def test_dis(self): 0081 self.do_disassembly_test(_f, dis_f) 0082 0083 def test_bug_708901(self): 0084 self.do_disassembly_test(bug708901, dis_bug708901) 0085 0086 def test_main(): 0087 run_unittest(DisTests) 0088 0089 0090 if __name__ == "__main__": 0091 test_main() 0092
Generated by PyXR 0.9.4