Reg Python Byte code <warning: arcane knowledge>

Scott David Daniels Scott.Daniels at Acm.Org
Mon Aug 22 15:03:01 EDT 2005


Warning: this post contains arcane knowledge.

Steve Holden wrote:
> praba kar wrote:
>>       Python 2.3 creates byte code with *.pyc
>> extention.  But Python 2.4 creates bytes code with
>> *.pyo.  Is there any difference between *.pyc
>> and *.pyo?.
>>
> Yes. The .pyo files are optimized by removing certain features that 
> aren't essential to execution (things like doc strings).

The -O option defines __debug__ to be False, and (as a side-effect)
ignores assert statements in the code used.  The -OO option does
everything the -O option does, while also removing things like doc
strings.  Both -O and -OO produce .pyo files, so having made things
with -OO, even using -O you will not see the doc strings in those
modules.

Here is a weird python program called opdemo.py to show these effects:

Call it opdemo.py
==================================
import sys

def function():
     '''doc strings kept'''
     try:
         assert 4 == 5
         print '%s: assert skipped (__debug__ = %s)' % (
                  __name__, __debug__)
     except AssertError:
         print '%s: assert checked (__debug__ = %s)' % (
                  __name__, __debug__)

docstring = function.__doc__ or 'No doc strings'
print sys.version
print '%s: __debug__ = %s; %s' % (__name__, __debug__, docstring)
print __file__
function()

if __name__ == '__main__':
     print '============='
     import opdemo
     print opdemo.__name__, opdemo.docstring, opdemo.__file__
     opdemo.function()
==================================

Now that you have this, you can run:
     python opdemo.py
and then
     python opdemo.py
To see how you can tell if you are running post-compiled.

If you run
     python -OO opdemo.py
and then
     python -O opdemo.py
You will see it looks like -O also removes docstrings.
However if you delete opdemo.py and run
     python -O opdemo.py
and then
     python -O opdemo.py
You will see the docstrings are in fact there for -O.
Of course, for all of this you will have to think a lot about
exactly what code is being executed.  A help to figuring this out
is to remember that if you import a module you get a new and
different copy of the module from the main program.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list