def code_all_variables_dynamic(fun):
co = fun.func_code
len_co_names = len(co.co_names)
new_co_names = co.co_names + co.co_varnames
new_co_flags = co.co_flags & ~0x02
new_code = ''
for end, op, arg in bytecode(fun):
if dis.opname[op] == 'STORE_FAST':
new_arg = arg + len_co_names
new_code += chr(dis.opmap['STORE_NAME']) + \
chr(new_arg % 256) + \
chr(new_arg // 256)
elif dis.opname[op] == 'LOAD_FAST':
new_arg = arg + len_co_names
new_code += chr(dis.opmap['LOAD_NAME']) + \
chr(new_arg % 256) + \
chr(new_arg // 256)
else:
if arg is None:
new_code += chr(op)
else:
new_code += chr(op) + chr(arg % 256) + chr(arg // 256)
func_co = new.code(co.co_argcount, co.co_nlocals, co.co_stacksize,
new_co_flags, new_code, co.co_consts, new_co_names,
co.co_varnames, co.co_filename, co.co_name,
co.co_firstlineno, co.co_lnotab,
co.co_freevars, co.co_cellvars)
return func_co
# This is how make statement is implemented:
#
# make <callable> <name> <tuple>:
# <block>
#
# @make(<callable>, <tuple>)
# def <name>():
# <block>
评论列表
文章目录