summaryrefslogtreecommitdiff
path: root/Lib/test/test_contextlib.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2013-10-01 23:24:56 +1000
committerNick Coghlan <ncoghlan@gmail.com>2013-10-01 23:24:56 +1000
commit1a33b2f35b9195b440b492afa87dcf83ba2ecca4 (patch)
treee286962b93a945bbf1ef81e351bdfca6b856b90f /Lib/test/test_contextlib.py
parent2ff2190b6293966f76633d810dfbe2d232ff5973 (diff)
downloadcpython-git-1a33b2f35b9195b440b492afa87dcf83ba2ecca4.tar.gz
Close #19092: ExitStack now reraises exceptions from __exit__
Report and patch by Hrvoje Nikšić
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r--Lib/test/test_contextlib.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index e52ed91a58..9e45f70f85 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -573,6 +573,43 @@ class TestExitStack(unittest.TestCase):
self.assertIsInstance(inner_exc, ValueError)
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
+ def test_exit_exception_non_suppressing(self):
+ # http://bugs.python.org/issue19092
+ def raise_exc(exc):
+ raise exc
+
+ def suppress_exc(*exc_details):
+ return True
+
+ try:
+ with ExitStack() as stack:
+ stack.callback(lambda: None)
+ stack.callback(raise_exc, IndexError)
+ except Exception as exc:
+ self.assertIsInstance(exc, IndexError)
+ else:
+ self.fail("Expected IndexError, but no exception was raised")
+
+ try:
+ with ExitStack() as stack:
+ stack.callback(raise_exc, KeyError)
+ stack.push(suppress_exc)
+ stack.callback(raise_exc, IndexError)
+ except Exception as exc:
+ self.assertIsInstance(exc, KeyError)
+ else:
+ self.fail("Expected KeyError, but no exception was raised")
+
+ def test_body_exception_suppress(self):
+ def suppress_exc(*exc_details):
+ return True
+ try:
+ with ExitStack() as stack:
+ stack.push(suppress_exc)
+ 1/0
+ except IndexError as exc:
+ self.fail("Expected no exception, got IndexError")
+
def test_exit_exception_chaining_suppress(self):
with ExitStack() as stack:
stack.push(lambda *exc: True)