title: python stdout flush and tee
body: <p>The following code ends with broken pipe when piped into tee, but behave correctly when not piped :</p>
  
  <pre><code>#!/usr/bin/python
  import sys
  def testfun():
      while 1:
          try :
              s = sys.stdin.readline()
          except(KeyboardInterrupt) :
              print('Ctrl-C pressed')
              sys.stdout.flush()
              return
          print s
  
  if __name__ == "__main__":
      testfun()
      sys.exit()
  </code></pre>
  
  <p>Expected output :</p>
  
  <pre><code>./bug.py 
  Ctrl-C pressed
  </code></pre>
  
  <p>What is observed when piped into tee  is either a broken pipe or no output at all, ie nothing on tee stdout, and nothing in bug.log :</p>
  
  <pre><code>./bug.py | tee bug.log
  Traceback (most recent call last):
    File "./bug.py", line 14, in &lt;module&gt;
      sys.stdout.flush()
  IOError: [Errno 32] Broken pipe
  </code></pre>
  
  <p>What can be the reason for this ?</p>