def get_instructions(code):
"""
Iterator parsing the bytecode into easy-usable minimal emulation of
Python 3.4 `dis.Instruction` instances.
"""
# shortcuts
HAVE_ARGUMENT = dis.HAVE_ARGUMENT
EXTENDED_ARG = dis.EXTENDED_ARG
class Instruction:
# Minimal emulation of Python 3.4 dis.Instruction
def __init__(self, opcode, oparg):
self.opname = dis.opname[opcode]
self.arg = oparg
# opcode, argval, argrepr, offset, is_jump_target and
# starts_line are not used by our code, so we leave them away
# here.
code = code.co_code
extended_arg = 0
i = 0
n = len(code)
while i < n:
c = code[i]
i = i + 1
op = _cOrd(c)
if op >= HAVE_ARGUMENT:
oparg = _cOrd(code[i]) + _cOrd(code[i + 1]) * 256 + extended_arg
extended_arg = 0
i += 2
if op == EXTENDED_ARG:
extended_arg = oparg*65536
else:
oparg = None
yield Instruction(op, oparg)
#FIXME: Leverage this rather than magic numbers below.
评论列表
文章目录