(original) (raw)

diff -r 4f7845be9e23 Lib/dis.py --- a/Lib/dis.py Mon Aug 19 23:31:18 2013 +0200 +++ b/Lib/dis.py Wed Aug 21 14:42:10 2013 +0300 @@ -402,6 +402,13 @@ self.line_offset = 0 self.original_object = x + @classmethod + def from_tb(cls, tb): + """ Construct a Bytecode from the given traceback """ + while tb.tb_next: + tb = tb.tb_next + return cls(tb.tb_frame.f_code) + def __iter__(self): co = self.codeobj return _get_instructions_bytes(co.co_code, co.co_varnames, co.co_names, diff -r 4f7845be9e23 Lib/test/test_dis.py --- a/Lib/test/test_dis.py Mon Aug 19 23:31:18 2013 +0200 +++ b/Lib/test/test_dis.py Wed Aug 21 14:42:10 2013 +0300 @@ -696,6 +696,19 @@ self.assertBytecodeExactlyMatches(jumpy, expected_opinfo_jumpy, line_offset=expected_jumpy_offset) + def test_from_tb(self): + def get_tb(): + try: + 1 / 0 + except Exception as e: + tb = e.__traceback__ + return tb + + b = dis.Bytecode.from_tb(get_tb()) + for instr in b: + self.assertIsInstance(instr, dis.Instruction) + + class BytecodeTests(unittest.TestCase): def test_instantiation(self): # Test with function, method, code string and code object