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.
__debug__
is aSyntaxError
; in earlier 2.x versions__debug__
wasn't a magic constant—but in 2.6, and a few versions before that (not sure how many), it is a magic constant, but you can rebind the name to something else, which leads to weirdness. – Mettlesome__debug__
in this way, or more likely, the context code does (python-ldap?). – Suspensionsys.modules[__name__].__debug__
, which is legal in 2.6 but not 2.7, and can cause symptoms exactly like what he's seeing. But you're right,globals()['__debug__']
still works in 2.7, and it might cause the same problems. Let me check. – Mettlesomeldap
anddjango-ldap
directories, and neither seems to reassign__debug__
. I also deleted all .pyc/.pyo files inldap
, but I'm still getting the same behavior. – Lawmaker