def parse_byte_and_args(self):
code = self.f_code
opcode = code.co_code[self.f_lasti]
self.f_lasti = self.f_lasti + 1
if opcode >= dis.HAVE_ARGUMENT:
int_arg = ( code.co_code[self.f_lasti]
+ (code.co_code[self.f_lasti+1] << 8))
self.f_lasti = self.f_lasti + 2
if opcode in dis.hasconst:
arg = code.co_consts[int_arg]
elif opcode in dis.hasfree:
if int_arg < len(code.co_cellvars):
arg = code.co_cellvars[int_arg]
else:
arg = code.co_freevars[int_arg - len(code.co_cellvars)]
elif opcode in dis.hasname:
arg = code.co_names[int_arg]
elif opcode in dis.haslocal:
arg = code.co_varnames[int_arg]
elif opcode in dis.hasjrel:
arg = self.f_lasti + int_arg
else:
arg = int_arg
return dis.opname[opcode], (arg,)
return dis.opname[opcode], ()
python类hasfree()的实例源码
def parse_byte_and_args(self):
""" Parse 1 - 3 bytes of bytecode into
an instruction and optionally arguments."""
f = self.frame
opoffset = f.f_lasti
byteCode = ord(f.f_code.co_code[opoffset])
f.f_lasti += 1
byteName = dis.opname[byteCode]
arg = None
arguments = []
if byteCode >= dis.HAVE_ARGUMENT:
arg = f.f_code.co_code[f.f_lasti:f.f_lasti+2]
f.f_lasti += 2
intArg = ord(arg[0]) + (ord(arg[1]) << 8)
if byteCode in dis.hasconst:
arg = f.f_code.co_consts[intArg]
elif byteCode in dis.hasfree:
if intArg < len(f.f_code.co_cellvars):
arg = f.f_code.co_cellvars[intArg]
else:
var_idx = intArg - len(f.f_code.co_cellvars)
arg = f.f_code.co_freevars[var_idx]
elif byteCode in dis.hasname:
arg = f.f_code.co_names[intArg]
elif byteCode in dis.hasjrel:
arg = f.f_lasti + intArg
elif byteCode in dis.hasjabs:
arg = intArg
elif byteCode in dis.haslocal:
arg = f.f_code.co_varnames[intArg]
else:
arg = intArg
arguments = [arg]
return byteName, arguments, opoffset