8

This might be a silly question, but, given the output of, say..

>>> from dis import dis
>>> def myfunc(x):
...     print x ** 2
... 
>>> dis(myfunc)
  2           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (2)
              6 BINARY_POWER        
              7 PRINT_ITEM          
              8 PRINT_NEWLINE       
              9 LOAD_CONST               0 (None)
             12 RETURN_VALUE        

..or a .pyc file - is it possible to reassembling this into a valid piece of Python source code? I.e where reassemble(dis(myfunc)) == "def reassembled_function(x):\n print x ** 2"

Not for any particular practical reason, I'm just curious if this is possible, or has been attempted..

Related

Community
  • 1
  • 1
dbr
  • 165,801
  • 69
  • 278
  • 343

1 Answers1

5

http://sourceforge.net/projects/decompyle/

Sam Blackburn
  • 288
  • 2
  • 9
  • 1
    This works, but it's quite badly out of date and will have problems with some newer language features (and will probably plain refuse to work on Python 3). – Max Shawabkeh Jan 24 '10 at 01:00
  • looks like there is a fork that is still active at https://github.com/Mysterie/uncompyle2 and works for python bytecode from 2.5-2.7 – Mike McKerns May 14 '14 at 00:12