Skip to content

Instantly share code, notes, and snippets.

@JoaoFelipe
Created May 4, 2018 17:04
Show Gist options
  • Save JoaoFelipe/edd9e7800c8b32ee1f9f6830272d2173 to your computer and use it in GitHub Desktop.
Save JoaoFelipe/edd9e7800c8b32ee1f9f6830272d2173 to your computer and use it in GitHub Desktop.
Bytecode change
import uncompyle6
import types
def change_bytecode(code, sub=False):
if not isinstance(code, types.CodeType):
return code
if sub:
# 0 LOAD_CONST 1 (0)
# 2 RETURN_VALUE
co_code = b'd\x01S\x00'
co_consts = (None, 42)
co_lnotab = b''
else:
co_code = code.co_code
co_consts = tuple([
change_bytecode(x, sub=True) for x in code.co_consts
] + [42])
co_lnotab = code.co_lnotab
return types.CodeType(
code.co_argcount, code.co_kwonlyargcount, code.co_nlocals,
code.co_stacksize, code.co_flags, co_code, co_consts,
code.co_names, code.co_varnames, code.co_filename,
code.co_name, code.co_firstlineno, co_lnotab,
code.co_freevars, code.co_cellvars
)
expr='''
def resposta():
print ('Alô Mundo')
def outra_resposta():
return 0
print(resposta())
'''
code = compile(expr, '__main__', 'exec')
uncompyle6.deparse_code(3.6, change_bytecode(code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment