mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
Synchronize test_contextlib with test_contextlib_async (GH-111000)
This commit is contained in:
parent
11312eae6e
commit
ff4e53cb74
1 changed files with 46 additions and 0 deletions
|
|
@ -173,6 +173,15 @@ def whoo():
|
||||||
# The "gen" attribute is an implementation detail.
|
# The "gen" attribute is an implementation detail.
|
||||||
self.assertFalse(ctx.gen.gi_suspended)
|
self.assertFalse(ctx.gen.gi_suspended)
|
||||||
|
|
||||||
|
def test_contextmanager_trap_no_yield(self):
|
||||||
|
@contextmanager
|
||||||
|
def whoo():
|
||||||
|
if False:
|
||||||
|
yield
|
||||||
|
ctx = whoo()
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
ctx.__enter__()
|
||||||
|
|
||||||
def test_contextmanager_trap_second_yield(self):
|
def test_contextmanager_trap_second_yield(self):
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def whoo():
|
def whoo():
|
||||||
|
|
@ -186,6 +195,19 @@ def whoo():
|
||||||
# The "gen" attribute is an implementation detail.
|
# The "gen" attribute is an implementation detail.
|
||||||
self.assertFalse(ctx.gen.gi_suspended)
|
self.assertFalse(ctx.gen.gi_suspended)
|
||||||
|
|
||||||
|
def test_contextmanager_non_normalised(self):
|
||||||
|
@contextmanager
|
||||||
|
def whoo():
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
except RuntimeError:
|
||||||
|
raise SyntaxError
|
||||||
|
|
||||||
|
ctx = whoo()
|
||||||
|
ctx.__enter__()
|
||||||
|
with self.assertRaises(SyntaxError):
|
||||||
|
ctx.__exit__(RuntimeError, None, None)
|
||||||
|
|
||||||
def test_contextmanager_except(self):
|
def test_contextmanager_except(self):
|
||||||
state = []
|
state = []
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
|
@ -265,6 +287,25 @@ def test_issue29692():
|
||||||
self.assertEqual(ex.args[0], 'issue29692:Unchained')
|
self.assertEqual(ex.args[0], 'issue29692:Unchained')
|
||||||
self.assertIsNone(ex.__cause__)
|
self.assertIsNone(ex.__cause__)
|
||||||
|
|
||||||
|
def test_contextmanager_wrap_runtimeerror(self):
|
||||||
|
@contextmanager
|
||||||
|
def woohoo():
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
except Exception as exc:
|
||||||
|
raise RuntimeError(f'caught {exc}') from exc
|
||||||
|
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
with woohoo():
|
||||||
|
1 / 0
|
||||||
|
|
||||||
|
# If the context manager wrapped StopIteration in a RuntimeError,
|
||||||
|
# we also unwrap it, because we can't tell whether the wrapping was
|
||||||
|
# done by the generator machinery or by the generator itself.
|
||||||
|
with self.assertRaises(StopIteration):
|
||||||
|
with woohoo():
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
def _create_contextmanager_attribs(self):
|
def _create_contextmanager_attribs(self):
|
||||||
def attribs(**kw):
|
def attribs(**kw):
|
||||||
def decorate(func):
|
def decorate(func):
|
||||||
|
|
@ -276,6 +317,7 @@ def decorate(func):
|
||||||
@attribs(foo='bar')
|
@attribs(foo='bar')
|
||||||
def baz(spam):
|
def baz(spam):
|
||||||
"""Whee!"""
|
"""Whee!"""
|
||||||
|
yield
|
||||||
return baz
|
return baz
|
||||||
|
|
||||||
def test_contextmanager_attribs(self):
|
def test_contextmanager_attribs(self):
|
||||||
|
|
@ -332,8 +374,11 @@ def woohoo(a, *, b):
|
||||||
|
|
||||||
def test_recursive(self):
|
def test_recursive(self):
|
||||||
depth = 0
|
depth = 0
|
||||||
|
ncols = 0
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def woohoo():
|
def woohoo():
|
||||||
|
nonlocal ncols
|
||||||
|
ncols += 1
|
||||||
nonlocal depth
|
nonlocal depth
|
||||||
before = depth
|
before = depth
|
||||||
depth += 1
|
depth += 1
|
||||||
|
|
@ -347,6 +392,7 @@ def recursive():
|
||||||
recursive()
|
recursive()
|
||||||
|
|
||||||
recursive()
|
recursive()
|
||||||
|
self.assertEqual(ncols, 10)
|
||||||
self.assertEqual(depth, 0)
|
self.assertEqual(depth, 0)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue