summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorMariatta <Mariatta@users.noreply.github.com>2017-04-13 09:50:21 (GMT)
committerGitHub <noreply@github.com>2017-04-13 09:50:21 (GMT)
commit9b409ff41ceb2d7ea7e8d25a7bbf5eb7d46625f3 (patch)
tree4af736ab237728f15ea85e6e2767a19d24be6e6d /Lib/test
parentbd1173f202f5a3990063d980368e7ad1edc9b5b5 (diff)
downloadcpython-9b409ff41ceb2d7ea7e8d25a7bbf5eb7d46625f3.zip
cpython-9b409ff41ceb2d7ea7e8d25a7bbf5eb7d46625f3.tar.gz
cpython-9b409ff41ceb2d7ea7e8d25a7bbf5eb7d46625f3.tar.bz2
[3.6] bpo-29692: contextlib.contextmanager may incorrectly unchain RuntimeError (GH-949) (#1105)
contextlib._GeneratorContextManager.__exit__ includes a special case to deal with PEP 479 RuntimeErrors created when `StopIteration` is thrown into the context manager body. Previously this check was too permissive, and undid one level of chaining on *all* RuntimeError instances, not just those that wrapped a StopIteration instance. (cherry picked from commit 00c75e9a45ff0366c185e9e8a2e23af5a35481b0)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_contextlib.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index c04c804..b1a467d 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -152,6 +152,29 @@ def woohoo():
else:
self.fail('StopIteration was suppressed')
+ def test_contextmanager_do_not_unchain_non_stopiteration_exceptions(self):
+ @contextmanager
+ def test_issue29692():
+ try:
+ yield
+ except Exception as exc:
+ raise RuntimeError('issue29692:Chained') from exc
+ try:
+ with test_issue29692():
+ raise ZeroDivisionError
+ except Exception as ex:
+ self.assertIs(type(ex), RuntimeError)
+ self.assertEqual(ex.args[0], 'issue29692:Chained')
+ self.assertIsInstance(ex.__cause__, ZeroDivisionError)
+
+ try:
+ with test_issue29692():
+ raise StopIteration('issue29692:Unchained')
+ except Exception as ex:
+ self.assertIs(type(ex), StopIteration)
+ self.assertEqual(ex.args[0], 'issue29692:Unchained')
+ self.assertIsNone(ex.__cause__)
+
def _create_contextmanager_attribs(self):
def attribs(**kw):
def decorate(func):