diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-09 15:44:30 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-09 15:44:30 (GMT) |
commit | 8170e8c0d12cb9414f3a3d3ca81a447b4afc5f26 (patch) | |
tree | d6353155110b49bf7953fd0d7d3f808512e8feb1 /Lib/contextlib.py | |
parent | bd60e8dece89440ebdc80a19b2217d5ba2515124 (diff) | |
download | cpython-8170e8c0d12cb9414f3a3d3ca81a447b4afc5f26.zip cpython-8170e8c0d12cb9414f3a3d3ca81a447b4afc5f26.tar.gz cpython-8170e8c0d12cb9414f3a3d3ca81a447b4afc5f26.tar.bz2 |
PEP 479: Change StopIteration handling inside generators.
Closes issue #22906.
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r-- | Lib/contextlib.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 2fbc90c..379c251 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -77,10 +77,17 @@ class _GeneratorContextManager(ContextDecorator): self.gen.throw(type, value, traceback) raise RuntimeError("generator didn't stop after throw()") except StopIteration as exc: - # Suppress the exception *unless* it's the same exception that + # Suppress StopIteration *unless* it's the same exception that # was passed to throw(). This prevents a StopIteration - # raised inside the "with" statement from being suppressed + # raised inside the "with" statement from being suppressed. return exc is not value + except RuntimeError as exc: + # 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: + return False + raise except: # only re-raise if it's *not* the exception that was # passed to throw(), because __exit__() must not raise |