Skip to content

Commit 2eeac26

Browse files
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'.
1 parent c51d8c9 commit 2eeac26

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

Lib/test/test_coroutines.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,7 @@ async def __aenter__(self):
12691269
def __aexit__(self, *e):
12701270
return 444
12711271

1272+
# Exit with exception
12721273
async def foo():
12731274
async with CM():
12741275
1/0
@@ -1296,19 +1297,58 @@ async def __aenter__(self):
12961297
def __aexit__(self, *e):
12971298
return 456
12981299

1300+
# Normal exit
12991301
async def foo():
13001302
nonlocal CNT
13011303
async with CM():
13021304
CNT += 1
1305+
with self.assertRaisesRegex(
1306+
TypeError,
1307+
"'async with' received an object from __aexit__ "
1308+
"that does not implement __await__: int"):
1309+
run_async(foo())
1310+
self.assertEqual(CNT, 1)
13031311

1312+
# Exit with 'break'
1313+
async def foo():
1314+
nonlocal CNT
1315+
for i in range(2):
1316+
async with CM():
1317+
CNT += 1
1318+
break
1319+
with self.assertRaisesRegex(
1320+
TypeError,
1321+
"'async with' received an object from __aexit__ "
1322+
"that does not implement __await__: int"):
1323+
run_async(foo())
1324+
self.assertEqual(CNT, 2)
13041325

1326+
# Exit with 'continue'
1327+
async def foo():
1328+
nonlocal CNT
1329+
for i in range(2):
1330+
async with CM():
1331+
CNT += 1
1332+
continue
13051333
with self.assertRaisesRegex(
13061334
TypeError,
13071335
"'async with' received an object from __aexit__ "
13081336
"that does not implement __await__: int"):
13091337
run_async(foo())
1338+
self.assertEqual(CNT, 3)
13101339

1311-
self.assertEqual(CNT, 1)
1340+
# Exit with 'return'
1341+
async def foo():
1342+
nonlocal CNT
1343+
async with CM():
1344+
CNT += 1
1345+
return
1346+
with self.assertRaisesRegex(
1347+
TypeError,
1348+
"'async with' received an object from __aexit__ "
1349+
"that does not implement __await__: int"):
1350+
run_async(foo())
1351+
self.assertEqual(CNT, 4)
13121352

13131353

13141354
def test_with_9(self):

0 commit comments

Comments
 (0)