title: Python curses dilemma
body: <p>I'm playing around a little with Python and curses.</p>
  
  <p>When I run</p>
  
  <pre><code>import time
  import curses
  
  def main():
      curses.initscr()
      curses.cbreak()
      for i in range(3):
          time.sleep(1)
          curses.flash()
          pass
      print( "Hello World" )
      curses.endwin()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>if I wait all the way through, <code>curses.endwin()</code> gets called so everything works out fine.
  However, if I cut it short with Ctrl-C, <code>curses.endwin()</code> never gets called so it screws up my terminal session.</p>
  
  <p>What is the proper way to handle this situation? How can I make sure that no matter how I try to end/interrupt the program (e.g. Ctrl-C, Ctrl-Z), it doesn't mess up the terminal?</p>