Message102975
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__. |
|
Date |
User |
Action |
Args |
2010-04-12 21:34:07 | afriesen | set | recipients:
+ afriesen |
2010-04-12 21:34:07 | afriesen | set | messageid: <1271108047.08.0.236449263776.issue8379@psf.upfronthosting.co.za> |
2010-04-12 21:34:05 | afriesen | link | issue8379 messages |
2010-04-12 21:34:04 | afriesen | create | |
|