tags:

views:

168

answers:

3

I am working in the Python Interactive Shell (ActiveState ActivePython 2.6.4 under Windows XP). I created a function that does what I want. However, I've cleared the screen so I can't go back and look at the function definition. It is also a multiline function so the up arrow to redisplay lines is of minimal value. Is there anyway to return the actual code of the function? There are "code" and "func_code" attributes displayed by dir(), but I don't know if they contain what I need.

A: 

Unless there is a way of doing it on the activestate shell, no, there is no way to retrieve the exact code you've typed on the shell. At least on Linux, using the Python Shell provided by CPython there is no special way to achieve this. Maybe using iPython.

The func_code attribute is an object representing the function bytecode, the only thing you can get from this object is bytecode itself, not the "original" code.

Bruno Gola
On Linux, `readline.write_history_file` would help -- but not on Windows.
Alex Martelli
+3  A: 

No, __code__ and func_code are references to the compiled bytecode -- you can disassemble them (see dis.dis) but not get back to the Python source code.

Alas, the source code is simply gone, not remembered anywhere...:

>>> import inspect
>>> def f():
...   print 'ciao'
... 
>>> inspect.getsource(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 694, in getsource
    lines, lnum = getsourcelines(object)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 683, in getsourcelines
    lines, lnum = findsource(object)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/inspect.py", line 531, in findsource
    raise IOError('could not get source code')
IOError: could not get source code
>>> 

If inspect can't get to it, that's a pretty indicative sign.

If you were on a platform using GNU readline (basically, any except Windows), you might exploit the fact that readline itself does remember some "history" and can write it out to a file...:

>>> readline.write_history_file('/tmp/hist.txt')

and then read that history file -- however, I know of no way to do this in Windows.

You may want to use some IDE with better memory capabilities, rather than the "raw" command interpreter, especially on a platform such as Windows.

Alex Martelli
I agree about the IDE. But the shell looks like a DOS command window and the PHB doesn't ask what I'm doing.
Count Boxer
maybe this a good feature request for idle...
Dyno Fu
ipython may help for a rich IDE at a DOS command window, see http://ipython.scipy.org/moin/ -- it's so rich I've lost track of most of its features by now;-).
Alex Martelli
@Alex, I just test readline.write_history_file('hist.txt') with readline's Windows port: http://ipython.scipy.org/dist/ . it has saved def f(): print 'ciao' into hist.txt.
sunqiang
@sunqiang, nice to hear this -- it might prove helpful to the OP!
Alex Martelli
+1  A: 

No, not really. You could write yourfunc.func_code.co_code (the actually compiled bytecode) to a file and then try using decompyle or unpyc to decompile them, but both projects are old and unmaintained, and have never supported decompiling very well.

It's infinitely easier to simply write your function to a file to start with.

Chris S