Skip to content

bpo-29922: Add more tests for error messages in 'async with'. #6370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@ async def __aenter__(self):
def __aexit__(self, *e):
return 444

# Exit with exception
async def foo():
async with CM():
1/0
Expand Down Expand Up @@ -1296,19 +1297,58 @@ async def __aenter__(self):
def __aexit__(self, *e):
return 456

# Normal exit
async def foo():
nonlocal CNT
async with CM():
CNT += 1
with self.assertRaisesRegex(
TypeError,
"'async with' received an object from __aexit__ "
"that does not implement __await__: int"):
run_async(foo())
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)

self.assertEqual(CNT, 1)
# 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):
Expand Down