fork download
  1. import types
  2.  
  3.  
  4. def inv(fn):
  5. import types
  6. import dis
  7.  
  8. t=fn.__code__
  9. c=list(t.co_code)
  10.  
  11. idxs=[]
  12. i=0
  13. n=len(c)
  14. lc=dis.opmap['LOAD_CONST']
  15.  
  16. while i<n:
  17. opcode=ord(c[i])
  18. if opcode==lc and c[i+1]!='\0':
  19. idxs+=[i+1]
  20. i+=1
  21. if opcode>=dis.HAVE_ARGUMENT:
  22. i+=2
  23.  
  24. for i in range(len(idxs)/2):
  25. c[idxs[i]], c[idxs[-i-1]] = c[idxs[-i-1]], c[idxs[i]]
  26. outcode=types.CodeType( t.co_argcount, t.co_nlocals, t.co_stacksize, t.co_flags, ''.join(c), t.co_consts, t.co_names, t.co_varnames, t.co_filename, t.co_name, t.co_firstlineno, t.co_lnotab)
  27. return types.FunctionType(outcode, globals(), fn.__name__[::-1])
  28.  
  29. @inv
  30. def f():
  31. print 'Line1'
  32. print 'Line2'
  33. print 'Line3'
  34. print 'Line4'
  35.  
  36. f()
  37.  
Success #stdin #stdout 0.01s 7896KB
stdin
Standard input is empty
stdout
Line4
Line3
Line2
Line1