summaryrefslogtreecommitdiffstats
path: root/Lib/contextlib.py
diff options
context:
space:
mode:
authorMariatta <Mariatta@users.noreply.github.com>2017-04-13 10:14:53 (GMT)
committerGitHub <noreply@github.com>2017-04-13 10:14:53 (GMT)
commit4d015a40a7b9c3c1b8cfbe81453187d700a43163 (patch)
treecafa37d8b357968486792e40e9625163e0c45aea /Lib/contextlib.py
parentc0f4240fac397e1cdd1ee202fc1ce6eb23037d06 (diff)
downloadcpython-4d015a40a7b9c3c1b8cfbe81453187d700a43163.zip
cpython-4d015a40a7b9c3c1b8cfbe81453187d700a43163.tar.gz
cpython-4d015a40a7b9c3c1b8cfbe81453187d700a43163.tar.bz2
[3.5] bpo-29692: contextlib.contextmanager may incorrectly unchain RuntimeError (GH-949) (#1107)
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/contextlib.py')
-rw-r--r--Lib/contextlib.py12
1 files changed, 6 insertions, 6 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index c018895..5371a9f 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -65,7 +65,7 @@ class _GeneratorContextManager(ContextDecorator):
try:
next(self.gen)
except StopIteration:
- return
+ return False
else:
raise RuntimeError("generator didn't stop")
else:
@@ -87,7 +87,7 @@ class _GeneratorContextManager(ContextDecorator):
# Likewise, avoid suppressing if a StopIteration exception
# was passed to throw() and later wrapped into a RuntimeError
# (see PEP 479).
- if exc.__cause__ is value:
+ if type is StopIteration and exc.__cause__ is value:
return False
raise
except:
@@ -98,10 +98,10 @@ class _GeneratorContextManager(ContextDecorator):
# fixes the impedance mismatch between the throw() protocol
# and the __exit__() protocol.
#
- if sys.exc_info()[1] is not value:
- raise
- else:
- raise RuntimeError("generator didn't stop after throw()")
+ if sys.exc_info()[1] is value:
+ return False
+ raise
+ raise RuntimeError("generator didn't stop after throw()")
def contextmanager(func):