@@ -1255,8 +1255,9 @@ async def __anext__(self):
1255
1255
1256
1256
buffer = []
1257
1257
async def test1 ():
1258
- async for i1 , i2 in AsyncIter ():
1259
- buffer .append (i1 + i2 )
1258
+ with self .assertWarnsRegex (PendingDeprecationWarning , "legacy" ):
1259
+ async for i1 , i2 in AsyncIter ():
1260
+ buffer .append (i1 + i2 )
1260
1261
1261
1262
yielded , _ = run_async (test1 ())
1262
1263
# Make sure that __aiter__ was called only once
@@ -1268,12 +1269,13 @@ async def test1():
1268
1269
buffer = []
1269
1270
async def test2 ():
1270
1271
nonlocal buffer
1271
- async for i in AsyncIter ():
1272
- buffer .append (i [0 ])
1273
- if i [0 ] == 20 :
1274
- break
1275
- else :
1276
- buffer .append ('what?' )
1272
+ with self .assertWarnsRegex (PendingDeprecationWarning , "legacy" ):
1273
+ async for i in AsyncIter ():
1274
+ buffer .append (i [0 ])
1275
+ if i [0 ] == 20 :
1276
+ break
1277
+ else :
1278
+ buffer .append ('what?' )
1277
1279
buffer .append ('end' )
1278
1280
1279
1281
yielded , _ = run_async (test2 ())
@@ -1286,12 +1288,13 @@ async def test2():
1286
1288
buffer = []
1287
1289
async def test3 ():
1288
1290
nonlocal buffer
1289
- async for i in AsyncIter ():
1290
- if i [0 ] > 20 :
1291
- continue
1292
- buffer .append (i [0 ])
1293
- else :
1294
- buffer .append ('what?' )
1291
+ with self .assertWarnsRegex (PendingDeprecationWarning , "legacy" ):
1292
+ async for i in AsyncIter ():
1293
+ if i [0 ] > 20 :
1294
+ continue
1295
+ buffer .append (i [0 ])
1296
+ else :
1297
+ buffer .append ('what?' )
1295
1298
buffer .append ('end' )
1296
1299
1297
1300
yielded , _ = run_async (test3 ())
@@ -1338,7 +1341,7 @@ async def foo():
1338
1341
1339
1342
def test_for_4 (self ):
1340
1343
class I :
1341
- async def __aiter__ (self ):
1344
+ def __aiter__ (self ):
1342
1345
return self
1343
1346
1344
1347
def __anext__ (self ):
@@ -1368,8 +1371,9 @@ def __anext__(self):
1368
1371
return 123
1369
1372
1370
1373
async def foo ():
1371
- async for i in I ():
1372
- print ('never going to happen' )
1374
+ with self .assertWarnsRegex (PendingDeprecationWarning , "legacy" ):
1375
+ async for i in I ():
1376
+ print ('never going to happen' )
1373
1377
1374
1378
with self .assertRaisesRegex (
1375
1379
TypeError ,
@@ -1393,7 +1397,7 @@ class Iterable:
1393
1397
def __init__ (self ):
1394
1398
self .i = 0
1395
1399
1396
- async def __aiter__ (self ):
1400
+ def __aiter__ (self ):
1397
1401
return self
1398
1402
1399
1403
async def __anext__ (self ):
@@ -1417,7 +1421,11 @@ async def main():
1417
1421
I += 1
1418
1422
I += 1000
1419
1423
1420
- run_async (main ())
1424
+ with warnings .catch_warnings ():
1425
+ warnings .simplefilter ("error" )
1426
+ # Test that __aiter__ that returns an asyncronous iterator
1427
+ # directly does not throw any warnings.
1428
+ run_async (main ())
1421
1429
self .assertEqual (I , 111011 )
1422
1430
1423
1431
self .assertEqual (sys .getrefcount (manager ), mrefs_before )
@@ -1470,15 +1478,65 @@ def test_for_7(self):
1470
1478
class AI :
1471
1479
async def __aiter__ (self ):
1472
1480
1 / 0
1481
+ async def foo ():
1482
+ nonlocal CNT
1483
+ with self .assertWarnsRegex (PendingDeprecationWarning , "legacy" ):
1484
+ async for i in AI ():
1485
+ CNT += 1
1486
+ CNT += 10
1487
+ with self .assertRaises (ZeroDivisionError ):
1488
+ run_async (foo ())
1489
+ self .assertEqual (CNT , 0 )
1490
+
1491
+ def test_for_8 (self ):
1492
+ CNT = 0
1493
+ class AI :
1494
+ def __aiter__ (self ):
1495
+ 1 / 0
1473
1496
async def foo ():
1474
1497
nonlocal CNT
1475
1498
async for i in AI ():
1476
1499
CNT += 1
1477
1500
CNT += 10
1478
1501
with self .assertRaises (ZeroDivisionError ):
1479
- run_async (foo ())
1502
+ with warnings .catch_warnings ():
1503
+ warnings .simplefilter ("error" )
1504
+ # Test that if __aiter__ raises an exception it propagates
1505
+ # without any kind of warning.
1506
+ run_async (foo ())
1480
1507
self .assertEqual (CNT , 0 )
1481
1508
1509
+ def test_for_9 (self ):
1510
+ # Test that PendingDeprecationWarning can safely be converted into
1511
+ # an exception (__aiter__ should not have a chance to raise
1512
+ # a ZeroDivisionError.)
1513
+ class AI :
1514
+ async def __aiter__ (self ):
1515
+ 1 / 0
1516
+ async def foo ():
1517
+ async for i in AI ():
1518
+ pass
1519
+
1520
+ with self .assertRaises (PendingDeprecationWarning ):
1521
+ with warnings .catch_warnings ():
1522
+ warnings .simplefilter ("error" )
1523
+ run_async (foo ())
1524
+
1525
+ def test_for_10 (self ):
1526
+ # Test that PendingDeprecationWarning can safely be converted into
1527
+ # an exception.
1528
+ class AI :
1529
+ async def __aiter__ (self ):
1530
+ pass
1531
+ async def foo ():
1532
+ async for i in AI ():
1533
+ pass
1534
+
1535
+ with self .assertRaises (PendingDeprecationWarning ):
1536
+ with warnings .catch_warnings ():
1537
+ warnings .simplefilter ("error" )
1538
+ run_async (foo ())
1539
+
1482
1540
def test_copy (self ):
1483
1541
async def func (): pass
1484
1542
coro = func ()
0 commit comments