title: Exception not caught mixing contextmanager with a decorator
body: <p>I've been struggling for a while with this issue. In some code I'm writing I need to write a bunch of files and optionally create the directory tree if needed. My idea is as follows: catch exception IOError and if its first argument is ENOENT then create the directory structure and attempt to write the file again.</p>
  
  <p>I have written a relatively small retry function but I'd like to generalize it to "any" code that might throw an exception. It all worked until I came upon something like this:</p>
  
  <pre><code>def retry(f):
      def wrapper(*args, **kwargs):
          try:
              return f(*args, **kwargs)
          except:
              print "Gotcha here!"
      return wrapper
  
  def update(file, value):
      @contextmanager
      @retry
      def safeopen(file, mode):
          with open(file, mode) as f:
              yield f
      try:
          with safeopen(file, 'w') as f:
              f.write(value)
      except:
          print "Gotcha there!"
  
  update( 'tests/nonexisting/dummy.txt', 'Dummy line')
  </code></pre>
  
  <p>I've tightened the code to the minimum to show what fails when <code>open()</code> throws an exception. In this code the exception is caught only from the except block in <code>update()</code>, not in <code>wrapper()</code> so I always get <code>Gotcha there!</code> although I expected <code>Gotcha here</code> instead. I've tried swapping the @decorator and @contextmanager lines, no way. I have checked and made sure the wrapper gets called: it does. Just that it catches no exception from <code>f()</code>.</p>
  
  <p>What am I doing wrong?</p>