1

I am new here, so sorry for any mistakes. I am using spyder 3.0.0 and iPython console.

I have mistakenly overwritten my custom module file. My last hope is that I have loaded it in iPython console. Is there any way to get the source code "back" from the console?
The console lists all the names (of my overwritten module) after calling dir (modulename), it also creates objects and hint arguments for objects' methods.
I have tried (after importing inspect) print(inspect.getsource(MyClass)), but it throws

File "C:\Users\witek\Anaconda3\lib\inspect.py", line 613, in getfile
raise TypeError('{!r} is a built-in class'.format(object))

TypeError: <module '__main__'> is a built-in class

If I call print(inspect.getsource(MyModule)) it returns the source code of the file that I saved under my module name.

I will really appreciate any help! :)

  • 1
    is this what youre looking for? https://stackoverflow.com/questions/2055110/is-there-a-way-to-view-the-source-of-a-module-from-within-the-python-console – Stanley Aug 26 '18 at 20:35
  • You’re looking for a decompiler that you can apply to the _code objects_ in your surviving session. – Davis Herring Aug 26 '18 at 21:35
  • what if you use `inspect.getsource` on a function inside the module? – xdze2 Aug 26 '18 at 21:41
  • 2
    @Stanley thanks, but solutions posted there didn't work for my case (source code file gone) – Witek Jaworski Aug 26 '18 at 21:43
  • @xdze2 `File "C:\Users\witek\Anaconda3\lib\inspect.py", line 804, in findsource if pat.match(lines[lnum]): break IndexError: list index out of range` – Witek Jaworski Aug 26 '18 at 21:45
  • @DavisHerring and that's what I did, just in time, thank you! Luckily .pyc file survived and i used uncompyle6 getting it all back. What a relief!! :) I wonder what if I couldn't find .pyc file in __pycache__ , would compileall.compile_dir work in that case... – Witek Jaworski Aug 26 '18 at 21:50
  • @WitekJaworski: You can’t compile files that aren’t there, but it is possible (if more difficult) to decompile the _loaded_ bytecode in memory. – Davis Herring Aug 27 '18 at 00:57

0 Answers0