summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_contextlib.py
diff options
context:
space:
mode:
authorPhillip J. Eby <pje@telecommunity.com>2006-03-25 00:28:24 (GMT)
committerPhillip J. Eby <pje@telecommunity.com>2006-03-25 00:28:24 (GMT)
commit6edd2586082f62a5ac61af5acab77b63919faa47 (patch)
tree183890c03fa62ea02b86afbcca1273427d03f72e /Lib/test/test_contextlib.py
parentbee071221419795d707a15286e08424d141a6ca6 (diff)
downloadcpython-6edd2586082f62a5ac61af5acab77b63919faa47.zip
cpython-6edd2586082f62a5ac61af5acab77b63919faa47.tar.gz
cpython-6edd2586082f62a5ac61af5acab77b63919faa47.tar.bz2
Fix a problem with @contextmanager not detecting a broken generator
that yields after a throw(). Make @contextmanager not reraise exceptions, but return a false value in that case instead. Add test cases for both behaviors.
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r--Lib/test/test_contextlib.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index f8db88c..7d7f8d2 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -45,6 +45,28 @@ class ContextManagerTestCase(unittest.TestCase):
self.fail("Expected ZeroDivisionError")
self.assertEqual(state, [1, 42, 999])
+ def test_contextmanager_no_reraise(self):
+ @contextmanager
+ def whee():
+ yield
+ ctx = whee().__context__()
+ ctx.__enter__()
+ # Calling __exit__ should not result in an exception
+ self.failIf(ctx.__exit__(TypeError, TypeError("foo"), None))
+
+ def test_contextmanager_trap_yield_after_throw(self):
+ @contextmanager
+ def whoo():
+ try:
+ yield
+ except:
+ yield
+ ctx = whoo().__context__()
+ ctx.__enter__()
+ self.assertRaises(
+ RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None
+ )
+
def test_contextmanager_except(self):
state = []
@contextmanager