@@ -133,24 +133,6 @@ def async_iterate(g):
133133 break
134134 return res
135135
136- def async_iterate (g ):
137- res = []
138- while True :
139- try :
140- g .__anext__ ().__next__ ()
141- except StopAsyncIteration :
142- res .append ('STOP' )
143- break
144- except StopIteration as ex :
145- if ex .args :
146- res .append (ex .args [0 ])
147- else :
148- res .append ('EMPTY StopIteration' )
149- break
150- except Exception as ex :
151- res .append (str (type (ex )))
152- return res
153-
154136 sync_gen_result = sync_iterate (sync_gen )
155137 async_gen_result = async_iterate (async_gen )
156138 self .assertEqual (sync_gen_result , async_gen_result )
@@ -176,19 +158,22 @@ async def gen():
176158
177159 g = gen ()
178160 ai = g .__aiter__ ()
179- self .assertEqual (ai .__anext__ ().__next__ (), ('result' ,))
161+
162+ an = ai .__anext__ ()
163+ self .assertEqual (an .__next__ (), ('result' ,))
180164
181165 try :
182- ai . __anext__ () .__next__ ()
166+ an .__next__ ()
183167 except StopIteration as ex :
184168 self .assertEqual (ex .args [0 ], 123 )
185169 else :
186170 self .fail ('StopIteration was not raised' )
187171
188- self .assertEqual (ai .__anext__ ().__next__ (), ('result' ,))
172+ an = ai .__anext__ ()
173+ self .assertEqual (an .__next__ (), ('result' ,))
189174
190175 try :
191- ai . __anext__ () .__next__ ()
176+ an .__next__ ()
192177 except StopAsyncIteration as ex :
193178 self .assertFalse (ex .args )
194179 else :
@@ -212,10 +197,11 @@ async def gen():
212197
213198 g = gen ()
214199 ai = g .__aiter__ ()
215- self .assertEqual (ai .__anext__ ().__next__ (), ('result' ,))
200+ an = ai .__anext__ ()
201+ self .assertEqual (an .__next__ (), ('result' ,))
216202
217203 try :
218- ai . __anext__ () .__next__ ()
204+ an .__next__ ()
219205 except StopIteration as ex :
220206 self .assertEqual (ex .args [0 ], 123 )
221207 else :
@@ -646,17 +632,13 @@ async def run():
646632 gen = foo ()
647633 it = gen .__aiter__ ()
648634 self .assertEqual (await it .__anext__ (), 1 )
649- t = self .loop .create_task (it .__anext__ ())
650- await asyncio .sleep (0.01 )
651635 await gen .aclose ()
652- return t
653636
654- t = self .loop .run_until_complete (run ())
637+ self .loop .run_until_complete (run ())
655638 self .assertEqual (DONE , 1 )
656639
657640 # Silence ResourceWarnings
658641 fut .cancel ()
659- t .cancel ()
660642 self .loop .run_until_complete (asyncio .sleep (0.01 ))
661643
662644 def test_async_gen_asyncio_gc_aclose_09 (self ):
@@ -1053,46 +1035,18 @@ async def wait():
10531035
10541036 self .loop .run_until_complete (asyncio .sleep (0.1 ))
10551037
1056- self .loop .run_until_complete (self .loop .shutdown_asyncgens ())
1057- self .assertEqual (finalized , 2 )
1058-
10591038 # Silence warnings
10601039 t1 .cancel ()
10611040 t2 .cancel ()
1062- self .loop .run_until_complete (asyncio .sleep (0.1 ))
10631041
1064- def test_async_gen_asyncio_shutdown_02 (self ):
1065- logged = 0
1066-
1067- def logger (loop , context ):
1068- nonlocal logged
1069- self .assertIn ('asyncgen' , context )
1070- expected = 'an error occurred during closing of asynchronous'
1071- if expected in context ['message' ]:
1072- logged += 1
1073-
1074- async def waiter (timeout ):
1075- try :
1076- await asyncio .sleep (timeout )
1077- yield 1
1078- finally :
1079- 1 / 0
1080-
1081- async def wait ():
1082- async for _ in waiter (1 ):
1083- pass
1084-
1085- t = self .loop .create_task (wait ())
1086- self .loop .run_until_complete (asyncio .sleep (0.1 ))
1042+ with self .assertRaises (asyncio .CancelledError ):
1043+ self .loop .run_until_complete (t1 )
1044+ with self .assertRaises (asyncio .CancelledError ):
1045+ self .loop .run_until_complete (t2 )
10871046
1088- self .loop .set_exception_handler (logger )
10891047 self .loop .run_until_complete (self .loop .shutdown_asyncgens ())
10901048
1091- self .assertEqual (logged , 1 )
1092-
1093- # Silence warnings
1094- t .cancel ()
1095- self .loop .run_until_complete (asyncio .sleep (0.1 ))
1049+ self .assertEqual (finalized , 2 )
10961050
10971051 def test_async_gen_expression_01 (self ):
10981052 async def arange (n ):
0 commit comments