File tree Expand file tree Collapse file tree 1 file changed +41
-1
lines changed Expand file tree Collapse file tree 1 file changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -1269,6 +1269,7 @@ async def __aenter__(self):
1269
1269
def __aexit__ (self , * e ):
1270
1270
return 444
1271
1271
1272
+ # Exit with exception
1272
1273
async def foo ():
1273
1274
async with CM ():
1274
1275
1 / 0
@@ -1296,19 +1297,58 @@ async def __aenter__(self):
1296
1297
def __aexit__ (self , * e ):
1297
1298
return 456
1298
1299
1300
+ # Normal exit
1299
1301
async def foo ():
1300
1302
nonlocal CNT
1301
1303
async with CM ():
1302
1304
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 )
1303
1311
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 )
1304
1325
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
1305
1333
with self .assertRaisesRegex (
1306
1334
TypeError ,
1307
1335
"'async with' received an object from __aexit__ "
1308
1336
"that does not implement __await__: int" ):
1309
1337
run_async (foo ())
1338
+ self .assertEqual (CNT , 3 )
1310
1339
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 )
1312
1352
1313
1353
1314
1354
def test_with_9 (self ):
You can’t perform that action at this time.
0 commit comments