diff options
author | Gregory P. Smith <greg@krypto.org> | 2016-06-14 09:19:20 -0700 |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2016-06-14 09:19:20 -0700 |
commit | ba2ecd68414b9c53d00560579f5bc13459bc0449 (patch) | |
tree | 0f23b73c117c222dbb5b3781afb3f7dc3462a9f9 /Lib/test/test_contextlib.py | |
parent | c72828ba3377a2139f36b8fdc9b0cbd5dd798712 (diff) | |
download | cpython-git-ba2ecd68414b9c53d00560579f5bc13459bc0449.tar.gz |
Issue #27123: When an exception is raised within the context being
managed by a contextlib.ExitStack() and one of the exit stack
generators catches and raises it in a chain, do not re-raise the
original exception when exiting, let the new chained one through.
This avoids the PEP 479 bug described in issue25782.
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r-- | Lib/test/test_contextlib.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 30a6377bed..a246c43187 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -762,6 +762,34 @@ class TestExitStack(unittest.TestCase): 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: |