mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
PEP 479: Change StopIteration handling inside generators.
Closes issue #22906.
This commit is contained in:
parent
bd60e8dece
commit
8170e8c0d1
14 changed files with 103 additions and 15 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue