diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-10-10 09:12:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-10 09:12:28 (GMT) |
commit | 2fc80814bf55445fb2cc56b25ec54d8e308e3408 (patch) | |
tree | 20fd204c6a10e7c811bf4f27661c9dadea42dcf2 /Lib/test/test_contextlib.py | |
parent | 36886726a2fe9a2c8511cfb405704f39cf184d7c (diff) | |
download | cpython-2fc80814bf55445fb2cc56b25ec54d8e308e3408.zip cpython-2fc80814bf55445fb2cc56b25ec54d8e308e3408.tar.gz cpython-2fc80814bf55445fb2cc56b25ec54d8e308e3408.tar.bz2 |
[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 96fed66a65097eac2dc528ce29c9ba676bb07689)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r-- | Lib/test/test_contextlib.py | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 0f8351a..b158294 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -157,9 +157,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 = [] |