mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
[3.11] gh-110378: Close invalid generators in contextmanager and asynccontextmanager (GH-110499) (#110589)
contextmanager and asynccontextmanager context managers now close an invalid
underlying generator object that yields more then one value.
(cherry picked from commit 96fed66a65
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
parent
d099defc63
commit
2b3a418279
3 changed files with 37 additions and 7 deletions
|
@ -156,9 +156,24 @@ class ContextManagerTestCase(unittest.TestCase):
|
|||
yield
|
||||
ctx = whoo()
|
||||
ctx.__enter__()
|
||||
self.assertRaises(
|
||||
RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None
|
||||
)
|
||||
with self.assertRaises(RuntimeError):
|
||||
ctx.__exit__(TypeError, TypeError("foo"), None)
|
||||
if support.check_impl_detail(cpython=True):
|
||||
# The "gen" attribute is an implementation detail.
|
||||
self.assertFalse(ctx.gen.gi_suspended)
|
||||
|
||||
def test_contextmanager_trap_second_yield(self):
|
||||
@contextmanager
|
||||
def whoo():
|
||||
yield
|
||||
yield
|
||||
ctx = whoo()
|
||||
ctx.__enter__()
|
||||
with self.assertRaises(RuntimeError):
|
||||
ctx.__exit__(None, None, None)
|
||||
if support.check_impl_detail(cpython=True):
|
||||
# The "gen" attribute is an implementation detail.
|
||||
self.assertFalse(ctx.gen.gi_suspended)
|
||||
|
||||
def test_contextmanager_except(self):
|
||||
state = []
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue