diff -r 9649acf7d472 Lib/contextlib.py --- a/Lib/contextlib.py Sat Jun 11 18:02:46 2016 -0700 +++ b/Lib/contextlib.py Sat Jun 11 18:58:22 2016 -0700 @@ -82,6 +82,9 @@ # raised inside the "with" statement from being suppressed. return exc is not value except RuntimeError as exc: + # Don't re-raise the passed in exception. (issue27112) + if exc is value: + return False # Likewise, avoid suppressing if a StopIteration exception # was passed to throw() and later wrapped into a RuntimeError # (see PEP 479). diff -r 9649acf7d472 Lib/test/test_contextlib.py --- a/Lib/test/test_contextlib.py Sat Jun 11 18:02:46 2016 -0700 +++ b/Lib/test/test_contextlib.py Sat Jun 11 18:58:22 2016 -0700 @@ -762,6 +762,34 @@ stack.push(cm) self.assertIs(stack._exit_callbacks[-1], cm) + def test_dont_reraise_RuntimeError(self): + """https://bugs.python.org/issue27122""" + class UniqueException(Exception): pass + + @contextmanager + def second(): + try: + yield 1 + except Exception as exc: + raise UniqueException("new exception") from exc + + @contextmanager + def first(): + try: + yield 1 + except Exception as exc: + raise exc + + # The RuntimeError should be caught by second()'s exception + # handler which chain raised a new UniqueException. + with self.assertRaises(UniqueException) as err_ctx: + with ExitStack() as es_ctx: + es_ctx.enter_context(second()) + es_ctx.enter_context(first()) + raise RuntimeError("please no infinite loop.") + + self.assertEqual(err_ctx.exception.args[0], "new exception") + class TestRedirectStream: