[3.12] gh-110378: Close invalid generators in contextmanager and asynccontextmanager (GH-110499) (#110588)

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>
This commit is contained in:
Miss Islington (bot) 2023-10-10 11:12:28 +02:00 committed by GitHub
parent 36886726a2
commit 2fc80814bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 7 deletions

View file

@ -204,6 +204,9 @@ class AsyncContextManagerTestCase(unittest.TestCase):
await ctx.__aenter__()
with self.assertRaises(RuntimeError):
await ctx.__aexit__(TypeError, TypeError('foo'), None)
if support.check_impl_detail(cpython=True):
# The "gen" attribute is an implementation detail.
self.assertFalse(ctx.gen.ag_suspended)
@_async_test
async def test_contextmanager_trap_no_yield(self):
@ -225,6 +228,9 @@ class AsyncContextManagerTestCase(unittest.TestCase):
await ctx.__aenter__()
with self.assertRaises(RuntimeError):
await ctx.__aexit__(None, None, None)
if support.check_impl_detail(cpython=True):
# The "gen" attribute is an implementation detail.
self.assertFalse(ctx.gen.ag_suspended)
@_async_test
async def test_contextmanager_non_normalised(self):