diff options
author | svelankar <siddharth.velankar@gmail.com> | 2017-04-11 09:11:13 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2017-04-11 09:11:13 (GMT) |
commit | 00c75e9a45ff0366c185e9e8a2e23af5a35481b0 (patch) | |
tree | 15be21a9abc45df371cfccdf11cb25f3e82a1594 /Lib/contextlib.py | |
parent | 6fab78e9027f9ebd6414995580781b480433e595 (diff) | |
download | cpython-00c75e9a45ff0366c185e9e8a2e23af5a35481b0.zip cpython-00c75e9a45ff0366c185e9e8a2e23af5a35481b0.tar.gz cpython-00c75e9a45ff0366c185e9e8a2e23af5a35481b0.tar.bz2 |
bpo-29692: contextlib.contextmanager may incorrectly unchain RuntimeError (GH-949)
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.
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r-- | Lib/contextlib.py | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py index e91cf46..5e47054 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -88,7 +88,7 @@ class _GeneratorContextManager(ContextDecorator, AbstractContextManager): try: next(self.gen) except StopIteration: - return + return False else: raise RuntimeError("generator didn't stop") else: @@ -110,7 +110,7 @@ class _GeneratorContextManager(ContextDecorator, AbstractContextManager): # 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: @@ -121,10 +121,10 @@ class _GeneratorContextManager(ContextDecorator, AbstractContextManager): # 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): |