summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_contextlib.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r--Lib/test/test_contextlib.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 30a6377..516403e 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -762,6 +762,40 @@ 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
+ class UniqueRuntimeError(RuntimeError): 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 UniqueRuntimeError 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 UniqueRuntimeError("please no infinite loop.")
+
+ exc = err_ctx.exception
+ self.assertIsInstance(exc, UniqueException)
+ self.assertIsInstance(exc.__context__, UniqueRuntimeError)
+ self.assertIsNone(exc.__context__.__context__)
+ self.assertIsNone(exc.__context__.__cause__)
+ self.assertIs(exc.__cause__, exc.__context__)
+
class TestRedirectStream: