title: Conditional __debug__ statement not executed though __debug__ is True
body: <p><strong>Short Version</strong></p>
  
  <p>I have a section of code I'm debugging that checks the value of <code>__debug__</code> and executes some code if it is True.</p>
  
  <pre><code>if __debug__:
    &lt;stuff happens&gt;
  </code></pre>
  
  <p>The problem is that "stuff" never happens, even though <code>__debug__</code> appears to be True.</p>
  
  <p><strong>Long Version / Details</strong></p>
  
  <p>To check this, I am printing out the values of several variables, most notably <code>__debug__</code>, to a file as the function executes, using the following pattern.  (I am using <code>os.open</code> because <code>open</code> is already defined in this module.)</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The piece of code I'm most confused by looks like this:</p>
  
  <pre><code>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__))
  </code></pre>
  
  <p>And the output file looks like this:</p>
  
  <pre><code>LINE 82 | LDAP FUNCTION __DEBUG__: True 
  LINE 83 | LDAP FUNCTION __DEBUG__: &lt;type 'bool'&gt; 
  LINE 84 | LDAP FUNCTION __DEBUG__: True 
  LINE 88 | LDAP FUNCTION __DEBUG__: True 
  LINE 90 | LDAP FUNCTION __DEBUG__: True 
  </code></pre>
  
  <p>The first 3 statements (lines 82-84) are every way I could think of checking if <code>__debug__</code> is "truthy", and all 3 imply that <code>__debug__</code> is True.  Similarly, casting <code>__debug__</code> as a boolean and then evaluating <code>if</code> (line 88) works as expected too.  Line 90 is a silly sanity check.</p>
  
  <p>Is there anything I'm missing in the way <code>__debug__</code> works that may be causing this?</p>
  
  <p><em>Note</em>: I found this while I was working through an error I am getting in the <code>_ldap_function_call</code> function in the <code>python-ldap</code> module.  I only get this error when using IIS - everything works fine with Django's development server.</p>