Created
May 4, 2018 17:04
-
-
Save JoaoFelipe/edd9e7800c8b32ee1f9f6830272d2173 to your computer and use it in GitHub Desktop.
Bytecode change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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