>>> @contextlib.contextmanager ... def bun(): ... print('top bun') ... yield ... print('bottom bun') ... >>> with bun(): ... raise ValueError('no meat!') ... top bun Traceback (most recent call last): File "", line 2, in ValueError: no meat!