mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
bpo-29922: Add more tests for error messages in 'async with'. (GH-6370)
Different paths are executed for normal exit and for leaving
the 'async with' block with 'break', 'continue' or 'return'.
(cherry picked from commit 2eeac269dd
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
fcd4e03e08
commit
785f36c876
1 changed files with 43 additions and 3 deletions
|
@ -1269,6 +1269,7 @@ class CoroutineTest(unittest.TestCase):
|
||||||
def __aexit__(self, *e):
|
def __aexit__(self, *e):
|
||||||
return 444
|
return 444
|
||||||
|
|
||||||
|
# Exit with exception
|
||||||
async def foo():
|
async def foo():
|
||||||
async with CM():
|
async with CM():
|
||||||
1/0
|
1/0
|
||||||
|
@ -1296,20 +1297,59 @@ class CoroutineTest(unittest.TestCase):
|
||||||
def __aexit__(self, *e):
|
def __aexit__(self, *e):
|
||||||
return 456
|
return 456
|
||||||
|
|
||||||
|
# Normal exit
|
||||||
async def foo():
|
async def foo():
|
||||||
nonlocal CNT
|
nonlocal CNT
|
||||||
async with CM():
|
async with CM():
|
||||||
CNT += 1
|
CNT += 1
|
||||||
|
|
||||||
|
|
||||||
with self.assertRaisesRegex(
|
with self.assertRaisesRegex(
|
||||||
TypeError,
|
TypeError,
|
||||||
"'async with' received an object from __aexit__ "
|
"'async with' received an object from __aexit__ "
|
||||||
"that does not implement __await__: int"):
|
"that does not implement __await__: int"):
|
||||||
run_async(foo())
|
run_async(foo())
|
||||||
|
|
||||||
self.assertEqual(CNT, 1)
|
self.assertEqual(CNT, 1)
|
||||||
|
|
||||||
|
# Exit with 'break'
|
||||||
|
async def foo():
|
||||||
|
nonlocal CNT
|
||||||
|
for i in range(2):
|
||||||
|
async with CM():
|
||||||
|
CNT += 1
|
||||||
|
break
|
||||||
|
with self.assertRaisesRegex(
|
||||||
|
TypeError,
|
||||||
|
"'async with' received an object from __aexit__ "
|
||||||
|
"that does not implement __await__: int"):
|
||||||
|
run_async(foo())
|
||||||
|
self.assertEqual(CNT, 2)
|
||||||
|
|
||||||
|
# Exit with 'continue'
|
||||||
|
async def foo():
|
||||||
|
nonlocal CNT
|
||||||
|
for i in range(2):
|
||||||
|
async with CM():
|
||||||
|
CNT += 1
|
||||||
|
continue
|
||||||
|
with self.assertRaisesRegex(
|
||||||
|
TypeError,
|
||||||
|
"'async with' received an object from __aexit__ "
|
||||||
|
"that does not implement __await__: int"):
|
||||||
|
run_async(foo())
|
||||||
|
self.assertEqual(CNT, 3)
|
||||||
|
|
||||||
|
# Exit with 'return'
|
||||||
|
async def foo():
|
||||||
|
nonlocal CNT
|
||||||
|
async with CM():
|
||||||
|
CNT += 1
|
||||||
|
return
|
||||||
|
with self.assertRaisesRegex(
|
||||||
|
TypeError,
|
||||||
|
"'async with' received an object from __aexit__ "
|
||||||
|
"that does not implement __await__: int"):
|
||||||
|
run_async(foo())
|
||||||
|
self.assertEqual(CNT, 4)
|
||||||
|
|
||||||
|
|
||||||
def test_with_9(self):
|
def test_with_9(self):
|
||||||
CNT = 0
|
CNT = 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue