Skip to content

Commit 1487cd1

Browse files
bpo-29922: Add more tests for error messages in 'async with'. (pythonGH-6370)
Different paths are executed for normal exit and for leaving the 'async with' block with 'break', 'continue' or 'return'. (cherry picked from commit 2eeac26) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 4fd6c27 commit 1487cd1

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
@@ -1243,6 +1243,7 @@ async def __aenter__(self):
12431243
def __aexit__(self, *e):
12441244
return 444
12451245

1246+
# Exit with exception
12461247
async def foo():
12471248
async with CM():
12481249
1/0
@@ -1270,19 +1271,58 @@ async def __aenter__(self):
12701271
def __aexit__(self, *e):
12711272
return 456
12721273

1274+
# Normal exit
12731275
async def foo():
12741276
nonlocal CNT
12751277
async with CM():
12761278
CNT += 1
1279+
with self.assertRaisesRegex(
1280+
TypeError,
1281+
"'async with' received an object from __aexit__ "
1282+
"that does not implement __await__: int"):
1283+
run_async(foo())
1284+
self.assertEqual(CNT, 1)
12771285

1286+
# Exit with 'break'
1287+
async def foo():
1288+
nonlocal CNT
1289+
for i in range(2):
1290+
async with CM():
1291+
CNT += 1
1292+
break
1293+
with self.assertRaisesRegex(
1294+
TypeError,
1295+
"'async with' received an object from __aexit__ "
1296+
"that does not implement __await__: int"):
1297+
run_async(foo())
1298+
self.assertEqual(CNT, 2)
12781299

1300+
# Exit with 'continue'
1301+
async def foo():
1302+
nonlocal CNT
1303+
for i in range(2):
1304+
async with CM():
1305+
CNT += 1
1306+
continue
12791307
with self.assertRaisesRegex(
12801308
TypeError,
12811309
"'async with' received an object from __aexit__ "
12821310
"that does not implement __await__: int"):
12831311
run_async(foo())
1312+
self.assertEqual(CNT, 3)
12841313

1285-
self.assertEqual(CNT, 1)
1314+
# Exit with 'return'
1315+
async def foo():
1316+
nonlocal CNT
1317+
async with CM():
1318+
CNT += 1
1319+
return
1320+
with self.assertRaisesRegex(
1321+
TypeError,
1322+
"'async with' received an object from __aexit__ "
1323+
"that does not implement __await__: int"):
1324+
run_async(foo())
1325+
self.assertEqual(CNT, 4)
12861326

12871327

12881328
def test_with_9(self):

0 commit comments

Comments
 (0)