@@ -1255,8 +1255,9 @@ async def __anext__(self):
12551255
12561256 buffer = []
12571257 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 )
12601261
12611262 yielded , _ = run_async (test1 ())
12621263 # Make sure that __aiter__ was called only once
@@ -1268,12 +1269,13 @@ async def test1():
12681269 buffer = []
12691270 async def test2 ():
12701271 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?' )
12771279 buffer .append ('end' )
12781280
12791281 yielded , _ = run_async (test2 ())
@@ -1286,12 +1288,13 @@ async def test2():
12861288 buffer = []
12871289 async def test3 ():
12881290 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?' )
12951298 buffer .append ('end' )
12961299
12971300 yielded , _ = run_async (test3 ())
@@ -1338,7 +1341,7 @@ async def foo():
13381341
13391342 def test_for_4 (self ):
13401343 class I :
1341- async def __aiter__ (self ):
1344+ def __aiter__ (self ):
13421345 return self
13431346
13441347 def __anext__ (self ):
@@ -1368,8 +1371,9 @@ def __anext__(self):
13681371 return 123
13691372
13701373 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' )
13731377
13741378 with self .assertRaisesRegex (
13751379 TypeError ,
@@ -1393,7 +1397,7 @@ class Iterable:
13931397 def __init__ (self ):
13941398 self .i = 0
13951399
1396- async def __aiter__ (self ):
1400+ def __aiter__ (self ):
13971401 return self
13981402
13991403 async def __anext__ (self ):
@@ -1417,7 +1421,11 @@ async def main():
14171421 I += 1
14181422 I += 1000
14191423
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 ())
14211429 self .assertEqual (I , 111011 )
14221430
14231431 self .assertEqual (sys .getrefcount (manager ), mrefs_before )
@@ -1470,15 +1478,65 @@ def test_for_7(self):
14701478 class AI :
14711479 async def __aiter__ (self ):
14721480 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
14731496 async def foo ():
14741497 nonlocal CNT
14751498 async for i in AI ():
14761499 CNT += 1
14771500 CNT += 10
14781501 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 ())
14801507 self .assertEqual (CNT , 0 )
14811508
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+
14821540 def test_copy (self ):
14831541 async def func (): pass
14841542 coro = func ()
0 commit comments