[Python-checkins] cpython (merge 3.5 -> default): Issue #27123: When an exception is raised within the context being

gregory.p.smith python-checkins at python.org
Tue Jun 14 12:24:56 EDT 2016


https://hg.python.org/cpython/rev/9fadeee05880
changeset: 102039:9fadeee05880
parent: 102037:5bee11956448
parent: 102038:9ee36b74b432
user: Gregory P. Smith <greg at krypto.org>
date: Tue Jun 14 09:24:31 2016 -0700
summary:
 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.
files:
 Lib/contextlib.py | 3 ++
 Lib/test/test_contextlib.py | 28 +++++++++++++++++++++++++
 Misc/NEWS | 6 +++++
 3 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -105,6 +105,9 @@
 # raised inside the "with" statement from being suppressed.
 return exc is not value
 except RuntimeError as exc:
+ # Don't re-raise the passed in exception. (issue27112)
+ if exc is value:
+ return False
 # Likewise, avoid suppressing if a StopIteration exception
 # was passed to throw() and later wrapped into a RuntimeError
 # (see PEP 479).
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -795,6 +795,34 @@
 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:
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,12 @@
 Library
 -------
 
+- 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.
+
 - Issue #27278: Fix os.urandom() implementation using getrandom() on Linux.
 Truncate size to INT_MAX and loop until we collected enough random bytes,
 instead of casting a directly Py_ssize_t to int.
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /