[Python-Dev] __pycode__ extension

Phillip J. Eby pje at telecommunity.com
Wed Nov 17 17:01:56 CET 2004

At 02:24 PM 11/17/04 +0200, Stelios Xanthakis wrote:
>       #!/usr/bin/python -P
>       def save_world():
>           F = open ('MYSYSTEM.py')
>           F.write ('#!/usr/bin/python -P\n')
>           for i in globals():
>               if hasattr (eval(a), '__pycode__'):
>                   F.write (eval(a).__pycode__)

There are bugs in this code.  First, the wrong variable is used for eval(), 
and second, it shouldn't use eval.  If there's a global named 'F', for 
example, the code above will not save it, because it will see the local 'F' 
instead.  Loop over globals().values(), and do not use eval.

>The __pycode__ attribute is not marshaled.
>The logic behind this is that if we edit a function/class
>and save it in a .pyc file, the next time the .py module
>is executed it will overwrite the .pyc file and our changes
>are lost.
>So for 'import'ed code, __pycode__=None

What good is that?  Likewise, I don't see the point of having this only 
enabled when a command-line option is given.

If the idea is just to allow saving code from interactive mode, why not 
just modify the interactive mode to do this?

More information about the Python-Dev mailing list