[issue8379] if __debug__: has nonobvious behaviour when evaluating .pyo without -O

Andy Friesen report at bugs.python.org
Mon Apr 12 23:34:07 CEST 2010


New submission from Andy Friesen <andy at imvu.com>:

In certain circumstances, "if __debug__" seems to be evaluating its "else" clause even when -O is not specified.  This can cause very  surprising behavior.

a.zip contains a single file named a/__init__.pyo.  It is the compiled form of the following code:

    if __debug__:
        print 'DEBUG'
    else:
        print 'RELEASE'
        if __debug__ == True:
            raise Exception("this is impossible")

pythonbug.py evaluates this script with the following:

    import sys
    sys.path = ['a.zip']
    import a

When using Windows Python 2.6.2 and 2.6.5, running pythonbug.py produces this output:

    RELEASE
    Traceback (most recent call last):
      File "pythonbug.py", line 3, in <module>
        import a
      File "__init__.py", line 8, in <module>
        raise Exception("this is impossible")
    Exception: this is impossible

When -O is passed, the exception is not raised.

My best guess is that the Python bytecode is optimizing the "if __debug__" test away in the produced .pyo, but does not actually affect the value of __debug__ or check more complex expressions involving __debug__.

----------
files: pythonbug.zip
messages: 102975
nosy: afriesen
severity: normal
status: open
title: if __debug__: has nonobvious behaviour when evaluating .pyo without -O
type: behavior
versions: Python 2.6
Added file: http://bugs.python.org/file16901/pythonbug.zip

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8379>
_______________________________________


More information about the Python-bugs-list mailing list