Topic: programmatically decompile bytecode

I know how to get the bytecode of a function like this:
from byteplay import Code

codeObject = compile("""
y = 1
print y
print z""", '<Summink>', 'exec')

code = Code.from_code(codeObject)
byteCode2 = code.code
pprint(byteCode2)

Which gives the output like this:

[(SetLineno, 2),
 (LOAD_CONST, 1),
 (STORE_NAME, 'y'),
 (SetLineno, 3),
 (LOAD_NAME, 'y'),
 (PRINT_ITEM, None),
 (PRINT_NEWLINE, None),
 (SetLineno, 4),
 (LOAD_NAME, 'z'),
 (PRINT_ITEM, None),
 (PRINT_NEWLINE, None),
 (LOAD_CONST, None),
 (RETURN_VALUE, None)]

I want to revert back, I have a list of bytecodes but would like it to be converted into readable python code.
Something like this:

print byteCode2.a_method_to_produce_a_code_string()

which then would produce the following output:

"""
y = 1
print y
print z"""

Does anybody have an idea?

Last edited by me97esn (November 22, 2011 22:36)

Thumbs up