Short Version
I have a section of code I'm debugging that checks the value of __debug__
and executes some code if it is True.
if __debug__:
<stuff happens>
The problem is that "stuff" never happens, even though __debug__
appears to be True.
Long Version / Details
To check this, I am printing out the values of several variables, most notably __debug__
, to a file as the function executes, using the following pattern. (I am using os.open
because open
is already defined in this module.)
try:
myfile = os.open("test.txt", os.O_RDWR|os.O_CREAT|os.O_APPEND)
# work + some print statements to check the value of __DEBUG__
finally:
os.close(myfile)
The piece of code I'm most confused by looks like this:
os.write(myfile, "LINE %s | LDAP FUNCTION __DEBUG__: %s \n" %(sys._getframe(0).f_lineno, __debug__))
os.write(myfile, "LINE %s | LDAP FUNCTION __DEBUG__: %s \n" %(sys._getframe(0).f_lineno, type(__debug__)))
os.write(myfile, "LINE %s | LDAP FUNCTION __DEBUG__: %s \n" %(sys._getframe(0).f_lineno, bool(__debug__)))
if __debug__:
os.write(myfile, "LINE %s | LDAP FUNCTION __DEBUG__: %s \n" %(sys._getframe(0).f_lineno, __debug__))
if bool(__debug__):
os.write(myfile, "LINE %s | LDAP FUNCTION __DEBUG__: %s \n" %(sys._getframe(0).f_lineno, __debug__))
if True:
os.write(myfile, "LINE %s | LDAP FUNCTION __DEBUG__: %s \n" %(sys._getframe(0).f_lineno, __debug__))
if __debug__:
os.write(myfile, "LINE %s | LDAP FUNCTION __DEBUG__: %s \n" %(sys._getframe(0).f_lineno, __debug__))
And the output file looks like this:
LINE 82 | LDAP FUNCTION __DEBUG__: True
LINE 83 | LDAP FUNCTION __DEBUG__: <type 'bool'>
LINE 84 | LDAP FUNCTION __DEBUG__: True
LINE 88 | LDAP FUNCTION __DEBUG__: True
LINE 90 | LDAP FUNCTION __DEBUG__: True
The first 3 statements (lines 82-84) are every way I could think of checking if __debug__
is "truthy", and all 3 imply that __debug__
is True. Similarly, casting __debug__
as a boolean and then evaluating if
(line 88) works as expected too. Line 90 is a silly sanity check.
Is there anything I'm missing in the way __debug__
works that may be causing this?
Note: I found this while I was working through an error I am getting in the _ldap_function_call
function in the python-ldap
module. I only get this error when using IIS - everything works fine with Django's development server.