mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-110378: Close invalid generators in contextmanager and asynccontextmanager (GH-110499)
contextmanager and asynccontextmanager context managers now close an invalid underlying generator object that yields more then one value.
This commit is contained in:
parent
def7ea5cec
commit
96fed66a65
4 changed files with 43 additions and 7 deletions
|
@ -149,7 +149,10 @@ class _GeneratorContextManager(
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("generator didn't stop")
|
try:
|
||||||
|
raise RuntimeError("generator didn't stop")
|
||||||
|
finally:
|
||||||
|
self.gen.close()
|
||||||
else:
|
else:
|
||||||
if value is None:
|
if value is None:
|
||||||
# Need to force instantiation so we can reliably
|
# Need to force instantiation so we can reliably
|
||||||
|
@ -191,7 +194,10 @@ class _GeneratorContextManager(
|
||||||
raise
|
raise
|
||||||
exc.__traceback__ = traceback
|
exc.__traceback__ = traceback
|
||||||
return False
|
return False
|
||||||
raise RuntimeError("generator didn't stop after throw()")
|
try:
|
||||||
|
raise RuntimeError("generator didn't stop after throw()")
|
||||||
|
finally:
|
||||||
|
self.gen.close()
|
||||||
|
|
||||||
class _AsyncGeneratorContextManager(
|
class _AsyncGeneratorContextManager(
|
||||||
_GeneratorContextManagerBase,
|
_GeneratorContextManagerBase,
|
||||||
|
@ -216,7 +222,10 @@ class _AsyncGeneratorContextManager(
|
||||||
except StopAsyncIteration:
|
except StopAsyncIteration:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("generator didn't stop")
|
try:
|
||||||
|
raise RuntimeError("generator didn't stop")
|
||||||
|
finally:
|
||||||
|
await self.gen.aclose()
|
||||||
else:
|
else:
|
||||||
if value is None:
|
if value is None:
|
||||||
# Need to force instantiation so we can reliably
|
# Need to force instantiation so we can reliably
|
||||||
|
@ -258,7 +267,10 @@ class _AsyncGeneratorContextManager(
|
||||||
raise
|
raise
|
||||||
exc.__traceback__ = traceback
|
exc.__traceback__ = traceback
|
||||||
return False
|
return False
|
||||||
raise RuntimeError("generator didn't stop after athrow()")
|
try:
|
||||||
|
raise RuntimeError("generator didn't stop after athrow()")
|
||||||
|
finally:
|
||||||
|
await self.gen.aclose()
|
||||||
|
|
||||||
|
|
||||||
def contextmanager(func):
|
def contextmanager(func):
|
||||||
|
|
|
@ -167,9 +167,24 @@ class ContextManagerTestCase(unittest.TestCase):
|
||||||
yield
|
yield
|
||||||
ctx = whoo()
|
ctx = whoo()
|
||||||
ctx.__enter__()
|
ctx.__enter__()
|
||||||
self.assertRaises(
|
with self.assertRaises(RuntimeError):
|
||||||
RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None
|
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):
|
def test_contextmanager_except(self):
|
||||||
state = []
|
state = []
|
||||||
|
|
|
@ -199,6 +199,9 @@ class AsyncContextManagerTestCase(unittest.IsolatedAsyncioTestCase):
|
||||||
await ctx.__aenter__()
|
await ctx.__aenter__()
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
await ctx.__aexit__(TypeError, TypeError('foo'), None)
|
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 def test_contextmanager_trap_no_yield(self):
|
async def test_contextmanager_trap_no_yield(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
@ -218,6 +221,9 @@ class AsyncContextManagerTestCase(unittest.IsolatedAsyncioTestCase):
|
||||||
await ctx.__aenter__()
|
await ctx.__aenter__()
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
await ctx.__aexit__(None, None, None)
|
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 def test_contextmanager_non_normalised(self):
|
async def test_contextmanager_non_normalised(self):
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
:func:`~contextlib.contextmanager` and
|
||||||
|
:func:`~contextlib.asynccontextmanager` context managers now close an invalid
|
||||||
|
underlying generator object that yields more then one value.
|
Loading…
Add table
Add a link
Reference in a new issue