title: Writing a context manager in Python that itself uses a with statement
body: <p>I'm trying to write a context manager that uses other context managers, so clients don't need to know the whole recipe, just the interface I'm presenting.  I can't do it using <code>@contextmanager</code> - the code after <code>yield</code> call doesn't get executed if you're interrupted by an exception, so I need to use a class-based manager.</p>
  
  <p>Here's a little example script:</p>
  
  <pre><code>from contextlib import contextmanager
  import pprint
  
  d = {}
  
  @contextmanager
  def simple(arg, val):
      print "enter", arg
      d[arg] = val
      yield
      print "exit", arg
      del d[arg]
  
  class compl(object):
      def __init__(self, arg, val):
          self.arg=arg
          self.val=val
  
      def __enter__(self):
          with simple("one",1):
              with simple("two",2):
                  print "enter complex", self.arg
                  d[self.arg] = self.val
  
      def __exit__(self,*args):
          print "exit complex", self.arg
          del d[self.arg]
  
  print "before"
  print d
  print ""
  
  with compl("three",3):
      print d
      print ""
  
  print "after"
  print d
  print ""
  </code></pre>
  
  <p>That outputs this:</p>
  
  <pre><code>before
  {}
  
  enter one
  enter two
  enter complex three
  exit two
  exit one
  {'three': 3}
  
  exit complex three
  after
  {}
  </code></pre>
  
  <p>I want it to output this:</p>
  
  <pre><code>before
  {}
  
  enter one
  enter two
  enter complex three
  {'one': 1, 'three': 3, 'two': 2}
  
  exit complex three
  exit two
  exit one
  after
  {}
  </code></pre>
  
  <p>Is there any way to tell a class-based context manager to wrap itself with other context managers?</p>