Getting source code from interpreter-defined function

POYEN OP Olivier (DCL) Olivier.POYEN at clf-dexia.com
Fri Apr 11 05:57:43 EDT 2003


Hi all,

I'm playing with inspect module, which, BTW, is great for finding useful info on saved object, written object.
With inspect, I can get source code from a function.

Is there anyway I could do the same with "alive" object, without having to wrap the whole python interpreter and create tmpfile for writing session ? (an "alive" object would be an object defined at the interpreter prompt)

That was the question ;  now on to the details:

I'm trying to do some kind of interactive console IDE for PyGtk. I want people to try fast code, binding code to live widget, and then just do a "dump" in order to create the source file. 

So, in a way, I'd like to be able to do, at prompt:

>>> def foo():
...    print "bar"

And then , latter, after playing with it, after having tweaked it *whithout* killing the python interpreter

>>> dump_alive_code(foo):
def foo():
   print "bar"

or at least:
>>> dump_inner_code(foo):
   print "bar"

Insect do already this for written code, by reading the file and retrieving source code. But for object defined in the interpreter... I can't figure that out ! 

I know, I know, why on earth would I want to get that code: just write it down on file and fire 'python myfile.py'
But as I said, I want some kind of interactive console IDE, and I don't want to shutdown python interpreter. Plus, I want to be able to quickly test/bind widget with quick function. 

One solution I thought about is to wrap the whole python interpreter, try saving the code as it is feed, and then, when user asked for a dump, produce it. Overkill ! So much work for almost nothing ! There must be a more elegant solution ! 

Other solution: given any function, even defined in interpreter, I can get function.func_code.co_code, which is byte compiled code for this function. Perhaps I could use it and decompile it. But how....? 


Well, that was long, but good.

Any pointers, any RTFM, any URL will be well received.

Thanks in advance.

OPQ
-------------- next part --------------
-----------------------------------------------------------------

Ce message est confidentiel ; son contenu ne represente en aucun

cas un engagement de la part de Dexia Credit Local ou de Dexia

CLF Banque. Toute publication, utilisation ou diffusion, meme

partielle, doit etre autorisee prealablement par l'emetteur. Si

vous n'etes pas destinataire de ce message, merci d'en avertir

immediatement l'expediteur.



This message is confidential ; its contents do not constitute a

commitment by Dexia Credit Local or Dexia CLF Banque. Any

unauthorised disclosure, use or dissemination, either whole or

partial, is prohibited. If you are not the intended recipient of

the message, please notify the sender immediately.

-----------------------------------------------------------------

Consultez notre site internet www.dexia-clf.fr

La cle d'une gestion optimisee du secteur public local

-----------------------------------------------------------------


More information about the Python-list mailing list