diff -r 55d3289c50f9 Lib/test/test_dis.py --- a/Lib/test/test_dis.py Tue Mar 15 04:16:42 2011 -0400 +++ b/Lib/test/test_dis.py Tue Mar 15 15:17:01 2011 -0400 @@ -6,6 +6,29 @@ import dis import io +class _C: + def __init__(self, x): + self.x = x == 1 + +dis_c_instance_method = """\ + %-4d 0 LOAD_FAST 1 (x) + 3 LOAD_CONST 1 (1) + 6 COMPARE_OP 2 (==) + 9 LOAD_FAST 0 (self) + 12 STORE_ATTR 0 (x) + 15 LOAD_CONST 0 (None) + 18 RETURN_VALUE +""" % (_C.__init__.__code__.co_firstlineno + 1,) + +dis_c_instance_method_bytes = """\ + 0 LOAD_FAST 1 (1) + 3 LOAD_CONST 1 (1) + 6 COMPARE_OP 2 (==) + 9 LOAD_FAST 0 (0) + 12 STORE_ATTR 0 (0) + 15 LOAD_CONST 0 (0) + 18 RETURN_VALUE +""" def _f(a): print(a) @@ -23,6 +46,16 @@ _f.__code__.co_firstlineno + 2) +dis_f_co_code = """\ + 0 LOAD_GLOBAL 0 (0) + 3 LOAD_FAST 0 (0) + 6 CALL_FUNCTION 1 + 9 POP_TOP + 10 LOAD_CONST 1 (1) + 13 RETURN_VALUE +""" + + def bug708901(): for res in range(1, 10): @@ -138,17 +171,33 @@ """ class DisTests(unittest.TestCase): + def get_disassemble_as_string(self, func, lasti=-1): + s = io.StringIO() + save_stdout = sys.stdout + sys.stdout = s + try: + dis.disassemble(func, lasti) + finally: + sys.stdout = save_stdout + got = s.getvalue() + # Trim trailing blanks (if any). + lines = got.splitlines() + lines = [line.rstrip() for line in lines] + return '\n'.join(lines) + def do_disassembly_test(self, func, expected): s = io.StringIO() save_stdout = sys.stdout sys.stdout = s - dis.dis(func) - sys.stdout = save_stdout + try: + dis.dis(func) + finally: + sys.stdout = save_stdout got = s.getvalue() # Trim trailing blanks (if any). - lines = got.split('\n') + lines = got.splitlines() lines = [line.rstrip() for line in lines] - expected = expected.split("\n") + expected = expected.splitlines() import difflib if expected != lines: self.fail( @@ -211,6 +260,46 @@ self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str) self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str) + def test_disassemble_bytes(self): + self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code) + + def test_disassemble_method(self): + self.do_disassembly_test(_C(1).__init__, dis_c_instance_method) + + def test_disassemble_method_bytes(self): + method_bytecode = _C(1).__init__.__code__.co_code + self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes) + + def test_dis_none(self): + self.assertRaises(RuntimeError, dis.dis, None) + + def test_dis_object(self): + self.assertRaises(TypeError, dis.dis, object()) + + def test_dis_traceback(self): + not_defined = object() + tb = None + old = getattr(sys, 'last_traceback', not_defined) + + def cleanup(): + if old != not_defined: + sys.last_traceback = old + else: + del sys.last_traceback + + try: + 1/0 + except Exception as e: + tb = e.__traceback__ + sys.last_traceback = tb + self.addCleanup(cleanup) + + tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti) + self.do_disassembly_test(None, tb_dis) + + def test_dis_object(self): + self.assertRaises(TypeError, dis.dis, object()) + code_info_code_info = """\ Name: code_info Filename: (.*) @@ -363,6 +452,13 @@ dis.show_code(x) self.assertRegex(output.getvalue(), expected+"\n") + def test_code_info_object(self): + self.assertRaises(TypeError, dis.code_info, object()) + + def test_pretty_flags_no_flags(self): + self.assertEqual(dis.pretty_flags(0), '0x0') + + def test_main(): run_unittest(DisTests, CodeInfoTests)