I made a simple test program to test the functionality of assigning to __debug__
using globals()["__debug__"] = value
(__debug__ = value
is a SyntaxError
). It basically tries to raise an AssertionError
and prints if the error was raised and whether or not it was expected. I did this as I ran in to issues with __debug__
changing mid-program.
print("__debug__ is", __debug__)
exp = ["(expected)", "(not expected)"]
if __debug__:
exp = exp[::-1]
try:
assert False
print("No AssertionError", exp[0])
except AssertionError:
print("AssertionError", exp[1])
exp = exp[::-1]
globals()["__debug__"] = not __debug__
print("__debug__ is", __debug__)
try:
assert False
print("No AssertionError", exp[0])
except AssertionError:
print("AssertionError", exp[1])
It produces unexpected results when run from command prompt, with and without the -O
flag.
C:\Test>python assert.py
__debug__ is True
AssertionError (expected)
__debug__ is False
AssertionError (not expected)
C:\Test>python -O assert.py
__debug__ is False
No AssertionError (expected)
__debug__ is True
No AssertionError (not expected)
It seems that __debug__
is changing, but assert
is not actually checking if it has.