def test_finally(self): # Disassembled from: # | try: # | pass # | finally: # | pass o = test_utils.Py2Opcodes co = self.make_code( [ o.SETUP_FINALLY, 4, 0, # dest=7 [3], o.POP_BLOCK, o.LOAD_CONST, 0, 0, o.END_FINALLY, o.LOAD_CONST, 0, 0, o.RETURN_VALUE, ], name="finally") bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[3], bytecode[0].target) self.assertEqual(bytecode[3], bytecode[1].block_target)
def test_break(self): # Disassembled from: # | while True: # | if []: # | break o = test_utils.Py37Opcodes co = self.make_code([ o.SETUP_LOOP, 10, o.BUILD_LIST, 0, o.POP_JUMP_IF_FALSE, 2, o.BREAK_LOOP, 0, o.JUMP_ABSOLUTE, 2, o.POP_BLOCK, 0, o.LOAD_CONST, 0, o.RETURN_VALUE, 0, ]) bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode, self.python_version) # LOAD_CONST == SETUP_LOOP.target self.assertEqual(bytecode[6], bytecode[0].target) # LOAD_CONST == BREAK_LOOP.block_target self.assertEqual(bytecode[6], bytecode[3].block_target) # BUILD_LIST == POP_JUMP_IF_FALSE.target self.assertEqual(bytecode[1], bytecode[2].target) # BUILD_LIST == JUMP_ABSOLUTE.target self.assertEqual(bytecode[1], bytecode[4].target)
def test_with(self): # Disassembled from: # | with None: # | pass o = test_utils.Py2Opcodes co = self.make_code( [ o.LOAD_CONST, 0, 0, o.SETUP_WITH, 5, 0, # dest=11 [5], o.POP_TOP, o.POP_BLOCK, o.LOAD_CONST, 0, 0, o.WITH_CLEANUP, o.END_FINALLY, o.LOAD_CONST, 0, 0, o.RETURN_VALUE, ], name="with") bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[5], bytecode[1].target) self.assertEqual(bytecode[5], bytecode[3].block_target)
def test_loop(self): # Disassembled from: # | while []: # | break o = test_utils.Py2Opcodes co = self.make_code([ o.SETUP_LOOP, 10, 0, # dest=13 [5], o.BUILD_LIST, 0, 0, o.POP_JUMP_IF_FALSE, 12, 0, # dest=12 [4], o.JUMP_ABSOLUTE, 3, 0, # dest=3 [1], o.POP_BLOCK, o.LOAD_CONST, 0, 0, o.RETURN_VALUE, ]) bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[5], bytecode[0].target) self.assertEqual(bytecode[4], bytecode[2].target) self.assertEqual(bytecode[1], bytecode[3].target) self.assertEqual(bytecode[5], bytecode[4].block_target)
def test_except(self): # Disassembled from: # | try: # | pass # | except: # | pass co = self.make_code( [ 0x79, 4, 0, # [ 0] 0 SETUP_EXCEPT, dest=7 [3], 0x57, # [ 1] 3 POP_BLOCK, 0x6e, 7, 0, # [ 2] 4 JUMP_FORWARD, dest=14 [11], 0x01, # [ 3] 7 POP_TOP, 0x01, # [ 4] 8 POP_TOP, 0x01, # [ 8] 9 POP_TOP, 0x6e, 1, 0, # [ 9] 10 JUMP_FORWARD, dest=14 [11], 0x58, # [10] 13 END_FINALLY, 0x64, 0, 0, # [11] 14 LOAD_CONST, arg=0, 0x53, # [12] 17 RETURN_VALUE ], name="except") bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[3], bytecode[0].target) self.assertEqual(bytecode[3], bytecode[1].block_target)
def test_finally(self): # Disassembled from: # | try: # | pass # | finally: # | pass co = self.make_code( [ 0x7a, 4, 0, # [0] 0 SETUP_FINALLY, dest=7 [3], 0x57, # [1] 3 POP_BLOCK, 0x64, 0, 0, # [2] 4 LOAD_CONST, arg=0 (None), 0x58, # [3] 7 END_FINALLY, 0x64, 0, 0, # [4] 8 LOAD_CONST, arg=0 (None), 0x53, # [5] 11 RETURN_VALUE ], name="finally") bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[3], bytecode[0].target) self.assertEqual(bytecode[3], bytecode[1].block_target)
def test_break(self): # Disassembled from: # | while True: # | if []: # | break o = test_utils.Py2Opcodes co = self.make_code([ o.SETUP_LOOP, 20, 0, # dest=23, [9], o.LOAD_GLOBAL, 0, 0, o.POP_JUMP_IF_FALSE, 22, 0, # dest=22 [8], o.BUILD_LIST, 0, 0, o.POP_JUMP_IF_FALSE, 3, 0, # dest=3 [1], o.BREAK_LOOP, o.JUMP_ABSOLUTE, 3, 0, # dest=3 [1], o.JUMP_ABSOLUTE, 3, 0, # dest=3 [1], o.POP_BLOCK, o.LOAD_CONST, 0, 0, o.RETURN_VALUE, ]) bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[9], bytecode[0].target) self.assertEqual(bytecode[9], bytecode[5].block_target) self.assertEqual(bytecode[1], bytecode[6].target) self.assertEqual(bytecode[1], bytecode[7].target)
def test_continue(self): # Disassembled from: # | while True: # | try: # | continue # | except: # | pass o = test_utils.Py2Opcodes co = self.make_code([ o.SETUP_LOOP, 27, 0, # dest=30 [14], o.LOAD_GLOBAL, 0, 0, o.POP_JUMP_IF_FALSE, 29, 0, # dest=29 [13], o.SETUP_EXCEPT, 7, 0, # dest=19 [7], o.CONTINUE_LOOP, 3, 0, # dest=3 [1], o.POP_BLOCK, o.JUMP_ABSOLUTE, 3, 0, # dest=3 [1], o.POP_TOP, o.POP_TOP, o.POP_TOP, o.JUMP_ABSOLUTE, 3, 0, # dest=3 [1], o.END_FINALLY, o.JUMP_ABSOLUTE, 3, 0, # dest=3 [1], o.POP_BLOCK, o.LOAD_CONST, 0, 0, o.RETURN_VALUE, ]) bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[14], bytecode[0].target) self.assertEqual(bytecode[13], bytecode[2].target) self.assertEqual(bytecode[7], bytecode[3].target) self.assertEqual(bytecode[1], bytecode[4].target) self.assertEqual(bytecode[1], bytecode[6].target) self.assertEqual(bytecode[1], bytecode[10].target) self.assertEqual(bytecode[1], bytecode[12].target)
def test_loop(self): # Disassembled from: # | while []: # | break co = self.make_code([ 0x78, 10, 0, # [0] 0 SETUP_LOOP, dest=13 [5], 0x67, 0, 0, # [1] 3 BUILD_LIST, arg=0, 0x72, 12, 0, # [2] 6 POP_JUMP_IF_FALSE, dest=12 [4], 0x71, 3, 0, # [3] 9 JUMP_ABSOLUTE, dest=3 [1], 0x57, # [4] 12 POP_BLOCK, 0x64, 0, 0, # [5] 13 LOAD_CONST, arg=0, 0x53, # [6] 16 RETURN_VALUE ]) bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[5], bytecode[0].target) self.assertEqual(bytecode[4], bytecode[2].target) self.assertEqual(bytecode[1], bytecode[3].target) self.assertEqual(bytecode[5], bytecode[4].block_target)
def test_with(self): # Disassembled from: # | with None: # | pass co = self.make_code( [ 0x64, 0, 0, # [0] 0 LOAD_CONST, arg=0, 0x8f, 5, 0, # [1] 3 SETUP_WITH, dest=11 [5], 0x01, # [2] 6 POP_TOP, 0x57, # [3] 7 POP_BLOCK, 0x64, 0, 0, # [4] 8 LOAD_CONST, arg=0, 0x51, # [5] 11 WITH_CLEANUP, 0x58, # [6] 12 END_FINALLY, 0x64, 0, 0, # [7] 13 LOAD_CONST, arg=0, 0x53, # [8] 16 RETURN_VALUE ], name="with") bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[5], bytecode[1].target) self.assertEqual(bytecode[5], bytecode[3].block_target)
def test_continue(self): # Disassembled from: # | while True: # | try: # | continue # | except: # | pass co = self.make_code([ 0x78, 27, 0, # [0] 0 SETUP_LOOP, dest=30 [14], 0x74, 0, 0, # [1] 3 LOAD_GLOBAL, arg=0, 0x72, 29, 0, # [2] 6 POP_JUMP_IF_FALSE, dest=29 [13], 0x79, 7, 0, # [3] 9 SETUP_EXCEPT, dest=19 [7], 0x77, 3, 0, # [4] 12 CONTINUE_LOOP, dest=3 [1], 0x57, # [5] 15 POP_BLOCK, 0x71, 3, 0, # [6] 16 JUMP_ABSOLUTE, dest=3 [1], 0x01, # [7] 19 POP_TOP, 0x01, # [8] 20 POP_TOP, 0x01, # [9] 21 POP_TOP, 0x71, 3, 0, # [10] 22 JUMP_ABSOLUTE, dest=3 [1], 0x58, # [11] 25 END_FINALLY, 0x71, 3, 0, # [12] 26 JUMP_ABSOLUTE, dest=3 [1], 0x57, # [13] 29 POP_BLOCK, 0x64, 0, 0, # [14] 30 LOAD_CONST, arg=0, 0x53, # [15] 33 RETURN_VALUE ]) bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[14], bytecode[0].target) self.assertEqual(bytecode[13], bytecode[2].target) self.assertEqual(bytecode[7], bytecode[3].target) self.assertEqual(bytecode[1], bytecode[4].target) self.assertEqual(bytecode[1], bytecode[6].target) self.assertEqual(bytecode[1], bytecode[10].target) self.assertEqual(bytecode[1], bytecode[12].target)
def test_except(self): # Disassembled from: # | try: # | pass # | except: # | pass o = test_utils.Py2Opcodes co = self.make_code( [ o.SETUP_EXCEPT, 4, 0, # dest=7 [3], o.POP_BLOCK, o.JUMP_FORWARD, 7, 0, # dest=14 [11], o.POP_TOP, o.POP_TOP, o.POP_TOP, o.JUMP_FORWARD, 1, 0, # dest=14 [11], o.END_FINALLY, o.LOAD_CONST, 0, 0, o.RETURN_VALUE, ], name="except") bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[3], bytecode[0].target) self.assertEqual(bytecode[3], bytecode[1].block_target)
def test_finally(self): # Disassembled from: # | try: # | pass # | finally: # | pass co = self.make_code([ 0x7a, 4, 0, # [0] 0 SETUP_FINALLY, dest=7 [3], 0x57, # [1] 3 POP_BLOCK, 0x64, 0, 0, # [2] 4 LOAD_CONST, arg=0 (None), 0x58, # [3] 7 END_FINALLY, 0x64, 0, 0, # [4] 8 LOAD_CONST, arg=0 (None), 0x53, # [5] 11 RETURN_VALUE ], name="finally") bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEquals(bytecode[3], bytecode[0].target) self.assertEquals(bytecode[3], bytecode[1].block_target)
def test_loop(self): # Disassembled from: # | while []: # | break co = self.make_code([ 0x78, 10, 0, # [0] 0 SETUP_LOOP, dest=13 [5], 0x67, 0, 0, # [1] 3 BUILD_LIST, arg=0, 0x72, 12, 0, # [2] 6 POP_JUMP_IF_FALSE, dest=12 [4], 0x71, 3, 0, # [3] 9 JUMP_ABSOLUTE, dest=3 [1], 0x57, # [4] 12 POP_BLOCK, 0x64, 0, 0, # [5] 13 LOAD_CONST, arg=0, 0x53, # [6] 16 RETURN_VALUE ]) bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEquals(bytecode[5], bytecode[0].target) self.assertEquals(bytecode[4], bytecode[2].target) self.assertEquals(bytecode[1], bytecode[3].target) self.assertEquals(bytecode[5], bytecode[4].block_target)
def test_with(self): # Disassembled from: # | with None: # | pass co = self.make_code([ 0x64, 0, 0, # [0] 0 LOAD_CONST, arg=0, 0x8f, 5, 0, # [1] 3 SETUP_WITH, dest=11 [5], 0x01, # [2] 6 POP_TOP, 0x57, # [3] 7 POP_BLOCK, 0x64, 0, 0, # [4] 8 LOAD_CONST, arg=0, 0x51, # [5] 11 WITH_CLEANUP, 0x58, # [6] 12 END_FINALLY, 0x64, 0, 0, # [7] 13 LOAD_CONST, arg=0, 0x53, # [8] 16 RETURN_VALUE ], name="with") bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEquals(bytecode[5], bytecode[1].target) self.assertEquals(bytecode[5], bytecode[3].block_target)
def test_break(self): # Disassembled from: # | while True: # | if []: # | break co = self.make_code([ 0x78, 20, 0, # [0] 0 SETUP_LOOP, dest=23, [9], 0x74, 0, 0, # [1] 3 LOAD_GLOBAL, arg=0, 0x72, 22, 0, # [2] 6 POP_JUMP_IF_FALSE, dest=22 [8], 0x67, 0, 0, # [3] 9 BUILD_LIST, arg=0, 0x72, 3, 0, # [4] 12 POP_JUMP_IF_FALSE, dest=3 [1], 0x50, # [5] 15 BREAK_LOOP, 0x71, 3, 0, # [6] 16 JUMP_ABSOLUTE, dest=3 [1], 0x71, 3, 0, # [7] 19 JUMP_ABSOLUTE, dest=3 [1], 0x57, # [8] 22 POP_BLOCK, 0x64, 0, 0, # [9] 23 LOAD_CONST, arg=0, 0x53, # [10] 26 RETURN_VALUE ]) bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEqual(bytecode[9], bytecode[0].target) self.assertEqual(bytecode[9], bytecode[5].block_target) self.assertEqual(bytecode[1], bytecode[6].target) self.assertEqual(bytecode[1], bytecode[7].target)
def test_continue(self): # Disassembled from: # | while True: # | try: # | continue # | except: # | pass o = test_utils.Py37Opcodes co = self.make_code([ o.SETUP_LOOP, 24, o.SETUP_EXCEPT, 6, o.CONTINUE_LOOP, 2, o.POP_BLOCK, 0, o.JUMP_ABSOLUTE, 2, o.POP_TOP, 0, o.POP_TOP, 0, o.POP_TOP, 0, o.POP_EXCEPT, 0, o.JUMP_ABSOLUTE, 2, o.END_FINALLY, 0, o.JUMP_ABSOLUTE, 2, o.POP_BLOCK, 0, o.LOAD_CONST, 0, o.RETURN_VALUE, 0, ]) bytecode = opcodes.dis(co.co_code, python_version=self.python_version) blocks.add_pop_block_targets(bytecode, self.python_version) # LOAD_CONST == SETUP_LOOP.target self.assertEqual(bytecode[13], bytecode[0].target) # POP_TOP == SETUP_EXCEPT.target self.assertEqual(bytecode[5], bytecode[1].target) # SETUP_EXCEPT == CONTINUE_LOOP.target self.assertEqual(bytecode[1], bytecode[2].target) # SETUP_EXCEPT == JUMP_ABSOLUTE.target self.assertEqual(bytecode[1], bytecode[4].target) # SETUP_EXCEPT == JUMP_ABSOLUTE.target self.assertEqual(bytecode[1], bytecode[9].target) # SETUP_EXCEPT == JUMP_ABSOLUTE.target self.assertEqual(bytecode[1], bytecode[11].target)
def test_except(self): # Disassembled from: # | try: # | pass # | except: # | pass co = self.make_code([ 0x79, 4, 0, # [ 0] 0 SETUP_EXCEPT, dest=7 [3], 0x57, # [ 1] 3 POP_BLOCK, 0x6e, 7, 0, # [ 2] 4 JUMP_FORWARD, dest=14 [11], 0x01, # [ 3] 7 POP_TOP, 0x01, # [ 4] 8 POP_TOP, 0x01, # [ 8] 9 POP_TOP, 0x6e, 1, 0, # [ 9] 10 JUMP_FORWARD, dest=14 [11], 0x58, # [10] 13 END_FINALLY, 0x64, 0, 0, # [11] 14 LOAD_CONST, arg=0, 0x53, # [12] 17 RETURN_VALUE ], name="except") bytecode = opcodes.dis(co.co_code, python_version=self.PYTHON_VERSION) blocks.add_pop_block_targets(bytecode) self.assertEquals(bytecode[3], bytecode[0].target) self.assertEquals(bytecode[3], bytecode[1].block_target)
def dis(self, code): """Return the opcodes from disassbling a code sequence.""" return opcodes.dis(''.join(chr(c) for c in code), self.PYTHON_VERSION)
def dis(self, code): """Return the opcodes from disassbling a code sequence.""" return opcodes.dis(compat.int_array_to_bytes(code), self.python_version)
def dis(self, code): """Return the opcodes from disassbling a code sequence.""" return opcodes.dis(compat.int_array_to_bytes(code), self.PYTHON_VERSION)
def dis(self, code, **kwargs): """Return the opcodes from disassembling a code sequence.""" return opcodes.dis(bytes(code), self.python_version, **kwargs)