[Python-Dev] __pycode__ extension

Stelios Xanthakis sxanth at ceid.upatras.gr
Wed Nov 17 13:24:18 CET 2004

Hi Nick.

> One option would be an addition to coflags that is off by default - then 
> the feature could be selectively enabled by an application without 
> impacting normal operation.

100% agree. I used the '-P' command line option to activate this.
It is very important to understand that python will run in
an entirely different mode. People who are not interested
using this should have zero overhead.
Only those who are willing to pay the price will get __pycode__
-- and it's not that big.

So the real question is: will there be people who will use this?

Trying to answer that..
Insight: If this gets in, pythoneers will be tempted to
create a file MYSYSTEM.py with one function:

       #!/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__)
           F.write ('main()')
       def main():
          import code
          code.interact ('', raw_input, globals())

And that will give birth to their own personal incremental
python shell/library.

> The number one downside I can see (beyond the extra memory consumed) is 
> the bloating effect on pyc files - for this to work, the compiled 
> bytecode is going to have to contain the original source code as 
> strings.

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


> I think this is a potentially interesting idea, but one worth taking 
> through the PEP process for 2.5. This would have the benefit of clearly 
> identifying the gains provided by the feature, and also identifying 
> potential downsides to be addressed.

Definitelly. But before starting a PEP I'd like to gather
feedback and ideas, have a good patch vs 2.4, and, well,
understand python source a little better.


More information about the Python-Dev mailing list