Debian Codegraph @

Lib/test/test_asyncgen.py

raw
1 import inspect
2 import types
3 import unittest
4 import contextlib
5
6 from test.support.import_helper import import_module
7 from test.support import gc_collect, requires_working_socket
8 asyncio = import_module("asyncio")
9
10
11 requires_working_socket(module=True)
12
13 _no_default = object()
14
15
16 class AwaitException(Exception):
17 pass
18
19
20 @types.coroutine
21 def awaitable(*, throw=False):
22 if throw:
23 yield ('throw',)
24 else:
25 yield ('result',)
26
27
28 def run_until_complete(coro):
29 exc = False
30 while True:
31 try:
32 if exc:
33 exc = False
34 fut = coro.throw(AwaitException)
35 else:
36 fut = coro.send(None)
37 except StopIteration as ex:
38 return ex.args[0]
39
40 if fut == ('throw',):
41 exc = True
42
43
44 def to_list(gen):
45 async def iterate():
46 res = []
47 async for i in gen:
48 res.append(i)
49 return res
50
51 return run_until_complete(iterate())
52
53
54 def py_anext(iterator, default=_no_default):
55 """Pure-Python implementation of anext() for testing purposes.
56
57 Closely matches the builtin anext() C implementation.
58 Can be used to compare the built-in implementation of the inner
59 coroutines machinery to C-implementation of __anext__() and send()
60 or throw() on the returned generator.
61 """
62
63 try:
64 __anext__ = type(iterator).__anext__
65 except AttributeError:
66 raise TypeError(f'{iterator!r} is not an async iterator')
67
68 if default is _no_default:
69 return __anext__(iterator)
70
71 async def anext_impl():
72 try:
73 # The C code is way more low-level than this, as it implements
74 # all methods of the iterator protocol. In this implementation
75 # we're relying on higher-level coroutine concepts, but that's
76 # exactly what we want -- crosstest pure-Python high-level
77 # implementation and low-level C anext() iterators.
78 return await __anext__(iterator)
79 except StopAsyncIteration:
80 return default
81
82 return anext_impl()
83
84
85 class AsyncGenSyntaxTest(unittest.TestCase):
86
87 def test_async_gen_syntax_01(self):
88 code = '''async def foo():
89 await abc
90 yield from 123
91 '''
92
93 with self.assertRaisesRegex(SyntaxError, 'yield from.*inside async'):
94 exec(code, {}, {})
95
96 def test_async_gen_syntax_02(self):
97 code = '''async def foo():
98 yield from 123
99 '''
100
101 with self.assertRaisesRegex(SyntaxError, 'yield from.*inside async'):
102 exec(code, {}, {})
103
104 def test_async_gen_syntax_03(self):
105 code = '''async def foo():
106 await abc
107 yield
108 return 123
109 '''
110
111 with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'):
112 exec(code, {}, {})
113
114 def test_async_gen_syntax_04(self):
115 code = '''async def foo():
116 yield
117 return 123
118 '''
119
120 with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'):
121 exec(code, {}, {})
122
123 def test_async_gen_syntax_05(self):
124 code = '''async def foo():
125 if 0:
126 yield
127 return 12
128 '''
129
130 with self.assertRaisesRegex(SyntaxError, 'return.*value.*async gen'):
131 exec(code, {}, {})
132
133
134 class AsyncGenTest(unittest.TestCase):
135
136 def compare_generators(self, sync_gen, async_gen):
137 def sync_iterate(g):
138 res = []
139 while True:
140 try:
141 res.append(g.__next__())
142 except StopIteration:
143 res.append('STOP')
144 break
145 except Exception as ex:
146 res.append(str(type(ex)))
147 return res
148
149 def async_iterate(g):
150 res = []
151 while True:
152 an = g.__anext__()
153 try:
154 while True:
155 try:
156 an.__next__()
157 except StopIteration as ex:
158 if ex.args:
159 res.append(ex.args[0])
160 break
161 else:
162 res.append('EMPTY StopIteration')
163 break
164 except StopAsyncIteration:
165 raise
166 except Exception as ex:
167 res.append(str(type(ex)))
168 break
169 except StopAsyncIteration:
170 res.append('STOP')
171 break
172 return res
173
174 sync_gen_result = sync_iterate(sync_gen)
175 async_gen_result = async_iterate(async_gen)
176 self.assertEqual(sync_gen_result, async_gen_result)
177 return async_gen_result
178
179 def test_async_gen_iteration_01(self):
180 async def gen():
181 await awaitable()
182 a = yield 123
183 self.assertIs(a, None)
184 await awaitable()
185 yield 456
186 await awaitable()
187 yield 789
188
189 self.assertEqual(to_list(gen()), [123, 456, 789])
190
191 def test_async_gen_iteration_02(self):
192 async def gen():
193 await awaitable()
194 yield 123
195 await awaitable()
196
197 g = gen()
198 ai = g.__aiter__()
199
200 an = ai.__anext__()
201 self.assertEqual(an.__next__(), ('result',))
202
203 try:
204 an.__next__()
205 except StopIteration as ex:
206 self.assertEqual(ex.args[0], 123)
207 else:
208 self.fail('StopIteration was not raised')
209
210 an = ai.__anext__()
211 self.assertEqual(an.__next__(), ('result',))
212
213 try:
214 an.__next__()
215 except StopAsyncIteration as ex:
216 self.assertFalse(ex.args)
217 else:
218 self.fail('StopAsyncIteration was not raised')
219
220 def test_async_gen_exception_03(self):
221 async def gen():
222 await awaitable()
223 yield 123
224 await awaitable(throw=True)
225 yield 456
226
227 with self.assertRaises(AwaitException):
228 to_list(gen())
229
230 def test_async_gen_exception_04(self):
231 async def gen():
232 await awaitable()
233 yield 123
234 1 / 0
235
236 g = gen()
237 ai = g.__aiter__()
238 an = ai.__anext__()
239 self.assertEqual(an.__next__(), ('result',))
240
241 try:
242 an.__next__()
243 except StopIteration as ex:
244 self.assertEqual(ex.args[0], 123)
245 else:
246 self.fail('StopIteration was not raised')
247
248 with self.assertRaises(ZeroDivisionError):
249 ai.__anext__().__next__()
250
251 def test_async_gen_exception_05(self):
252 async def gen():
253 yield 123
254 raise StopAsyncIteration
255
256 with self.assertRaisesRegex(RuntimeError,
257 'async generator.*StopAsyncIteration'):
258 to_list(gen())
259
260 def test_async_gen_exception_06(self):
261 async def gen():
262 yield 123
263 raise StopIteration
264
265 with self.assertRaisesRegex(RuntimeError,
266 'async generator.*StopIteration'):
267 to_list(gen())
268
269 def test_async_gen_exception_07(self):
270 def sync_gen():
271 try:
272 yield 1
273 1 / 0
274 finally:
275 yield 2
276 yield 3
277
278 yield 100
279
280 async def async_gen():
281 try:
282 yield 1
283 1 / 0
284 finally:
285 yield 2
286 yield 3
287
288 yield 100
289
290 self.compare_generators(sync_gen(), async_gen())
291
292 def test_async_gen_exception_08(self):
293 def sync_gen():
294 try:
295 yield 1
296 finally:
297 yield 2
298 1 / 0
299 yield 3
300
301 yield 100
302
303 async def async_gen():
304 try:
305 yield 1
306 await awaitable()
307 finally:
308 await awaitable()
309 yield 2
310 1 / 0
311 yield 3
312
313 yield 100
314
315 self.compare_generators(sync_gen(), async_gen())
316
317 def test_async_gen_exception_09(self):
318 def sync_gen():
319 try:
320 yield 1
321 1 / 0
322 finally:
323 yield 2
324 yield 3
325
326 yield 100
327
328 async def async_gen():
329 try:
330 await awaitable()
331 yield 1
332 1 / 0
333 finally:
334 yield 2
335 await awaitable()
336 yield 3
337
338 yield 100
339
340 self.compare_generators(sync_gen(), async_gen())
341
342 def test_async_gen_exception_10(self):
343 async def gen():
344 yield 123
345 with self.assertRaisesRegex(TypeError,
346 "non-None value .* async generator"):
347 gen().__anext__().send(100)
348
349 def test_async_gen_exception_11(self):
350 def sync_gen():
351 yield 10
352 yield 20
353
354 def sync_gen_wrapper():
355 yield 1
356 sg = sync_gen()
357 sg.send(None)
358 try:
359 sg.throw(GeneratorExit())
360 except GeneratorExit:
361 yield 2
362 yield 3
363
364 async def async_gen():
365 yield 10
366 yield 20
367
368 async def async_gen_wrapper():
369 yield 1
370 asg = async_gen()
371 await asg.asend(None)
372 try:
373 await asg.athrow(GeneratorExit())
374 except GeneratorExit:
375 yield 2
376 yield 3
377
378 self.compare_generators(sync_gen_wrapper(), async_gen_wrapper())
379
380 def test_async_gen_exception_12(self):
381 async def gen():
382 with self.assertWarnsRegex(RuntimeWarning,
383 f"coroutine method 'asend' of '{gen.__qualname__}' "
384 f"was never awaited"):
385 await anext(me)
386 yield 123
387
388 me = gen()
389 ai = me.__aiter__()
390 an = ai.__anext__()
391
392 with self.assertRaisesRegex(RuntimeError,
393 r'anext\(\): asynchronous generator is already running'):
394 an.__next__()
395
396 with self.assertRaisesRegex(RuntimeError,
397 r"cannot reuse already awaited __anext__\(\)/asend\(\)"):
398 an.send(None)
399
400 def test_async_gen_asend_throw_concurrent_with_send(self):
401 import types
402
403 @types.coroutine
404 def _async_yield(v):
405 return (yield v)
406
407 class MyExc(Exception):
408 pass
409
410 async def agenfn():
411 while True:
412 try:
413 await _async_yield(None)
414 except MyExc:
415 pass
416 return
417 yield
418
419
420 agen = agenfn()
421 gen = agen.asend(None)
422 gen.send(None)
423 gen2 = agen.asend(None)
424
425 with self.assertRaisesRegex(RuntimeError,
426 r'anext\(\): asynchronous generator is already running'):
427 gen2.throw(MyExc)
428
429 with self.assertRaisesRegex(RuntimeError,
430 r"cannot reuse already awaited __anext__\(\)/asend\(\)"):
431 gen2.send(None)
432
433 def test_async_gen_athrow_throw_concurrent_with_send(self):
434 import types
435
436 @types.coroutine
437 def _async_yield(v):
438 return (yield v)
439
440 class MyExc(Exception):
441 pass
442
443 async def agenfn():
444 while True:
445 try:
446 await _async_yield(None)
447 except MyExc:
448 pass
449 return
450 yield
451
452
453 agen = agenfn()
454 gen = agen.asend(None)
455 gen.send(None)
456 gen2 = agen.athrow(MyExc)
457
458 with self.assertRaisesRegex(RuntimeError,
459 r'athrow\(\): asynchronous generator is already running'):
460 gen2.throw(MyExc)
461
462 with self.assertRaisesRegex(RuntimeError,
463 r"cannot reuse already awaited aclose\(\)/athrow\(\)"):
464 gen2.send(None)
465
466 def test_async_gen_asend_throw_concurrent_with_throw(self):
467 import types
468
469 @types.coroutine
470 def _async_yield(v):
471 return (yield v)
472
473 class MyExc(Exception):
474 pass
475
476 async def agenfn():
477 try:
478 yield
479 except MyExc:
480 pass
481 while True:
482 try:
483 await _async_yield(None)
484 except MyExc:
485 pass
486
487
488 agen = agenfn()
489 with self.assertRaises(StopIteration):
490 agen.asend(None).send(None)
491
492 gen = agen.athrow(MyExc)
493 gen.throw(MyExc)
494 gen2 = agen.asend(MyExc)
495
496 with self.assertRaisesRegex(RuntimeError,
497 r'anext\(\): asynchronous generator is already running'):
498 gen2.throw(MyExc)
499
500 with self.assertRaisesRegex(RuntimeError,
501 r"cannot reuse already awaited __anext__\(\)/asend\(\)"):
502 gen2.send(None)
503
504 def test_async_gen_athrow_throw_concurrent_with_throw(self):
505 import types
506
507 @types.coroutine
508 def _async_yield(v):
509 return (yield v)
510
511 class MyExc(Exception):
512 pass
513
514 async def agenfn():
515 try:
516 yield
517 except MyExc:
518 pass
519 while True:
520 try:
521 await _async_yield(None)
522 except MyExc:
523 pass
524
525 agen = agenfn()
526 with self.assertRaises(StopIteration):
527 agen.asend(None).send(None)
528
529 gen = agen.athrow(MyExc)
530 gen.throw(MyExc)
531 gen2 = agen.athrow(None)
532
533 with self.assertRaisesRegex(RuntimeError,
534 r'athrow\(\): asynchronous generator is already running'):
535 gen2.throw(MyExc)
536
537 with self.assertRaisesRegex(RuntimeError,
538 r"cannot reuse already awaited aclose\(\)/athrow\(\)"):
539 gen2.send(None)
540
541 def test_async_gen_3_arg_deprecation_warning(self):
542 async def gen():
543 yield 123
544
545 with self.assertWarns(DeprecationWarning):
546 x = gen().athrow(GeneratorExit, GeneratorExit(), None)
547 with self.assertRaises(GeneratorExit):
548 x.send(None)
549 del x
550 gc_collect()
551
552 def test_async_gen_api_01(self):
553 async def gen():
554 yield 123
555
556 g = gen()
557
558 self.assertEqual(g.__name__, 'gen')
559 g.__name__ = '123'
560 self.assertEqual(g.__name__, '123')
561
562 self.assertIn('.gen', g.__qualname__)
563 g.__qualname__ = '123'
564 self.assertEqual(g.__qualname__, '123')
565
566 self.assertIsNone(g.ag_await)
567 self.assertIsInstance(g.ag_frame, types.FrameType)
568 self.assertFalse(g.ag_running)
569 self.assertIsInstance(g.ag_code, types.CodeType)
570 aclose = g.aclose()
571 self.assertTrue(inspect.isawaitable(aclose))
572 aclose.close()
573
574 def test_async_gen_asend_close_runtime_error(self):
575 import types
576
577 @types.coroutine
578 def _async_yield(v):
579 return (yield v)
580
581 async def agenfn():
582 try:
583 await _async_yield(None)
584 except GeneratorExit:
585 await _async_yield(None)
586 return
587 yield
588
589 agen = agenfn()
590 gen = agen.asend(None)
591 gen.send(None)
592 with self.assertRaisesRegex(RuntimeError, "coroutine ignored GeneratorExit"):
593 gen.close()
594
595 def test_async_gen_athrow_close_runtime_error(self):
596 import types
597
598 @types.coroutine
599 def _async_yield(v):
600 return (yield v)
601
602 class MyExc(Exception):
603 pass
604
605 async def agenfn():
606 try:
607 yield
608 except MyExc:
609 try:
610 await _async_yield(None)
611 except GeneratorExit:
612 await _async_yield(None)
613
614 agen = agenfn()
615 with self.assertRaises(StopIteration):
616 agen.asend(None).send(None)
617 gen = agen.athrow(MyExc)
618 gen.send(None)
619 with self.assertRaisesRegex(RuntimeError, "coroutine ignored GeneratorExit"):
620 gen.close()
621
622
623 class AsyncGenAsyncioTest(unittest.TestCase):
624
625 def setUp(self):
626 self.loop = asyncio.new_event_loop()
627 asyncio.set_event_loop(None)
628
629 def tearDown(self):
630 self.loop.close()
631 self.loop = None
632 asyncio.events._set_event_loop_policy(None)
633
634 def check_async_iterator_anext(self, ait_class):
635 with self.subTest(anext="pure-Python"):
636 self._check_async_iterator_anext(ait_class, py_anext)
637 with self.subTest(anext="builtin"):
638 self._check_async_iterator_anext(ait_class, anext)
639
640 def _check_async_iterator_anext(self, ait_class, anext):
641 g = ait_class()
642 async def consume():
643 results = []
644 results.append(await anext(g))
645 results.append(await anext(g))
646 results.append(await anext(g, 'buckle my shoe'))
647 return results
648 res = self.loop.run_until_complete(consume())
649 self.assertEqual(res, [1, 2, 'buckle my shoe'])
650 with self.assertRaises(StopAsyncIteration):
651 self.loop.run_until_complete(consume())
652
653 async def test_2():
654 g1 = ait_class()
655 self.assertEqual(await anext(g1), 1)
656 self.assertEqual(await anext(g1), 2)
657 with self.assertRaises(StopAsyncIteration):
658 await anext(g1)
659 with self.assertRaises(StopAsyncIteration):
660 await anext(g1)
661
662 g2 = ait_class()
663 self.assertEqual(await anext(g2, "default"), 1)
664 self.assertEqual(await anext(g2, "default"), 2)
665 self.assertEqual(await anext(g2, "default"), "default")
666 self.assertEqual(await anext(g2, "default"), "default")
667
668 return "completed"
669
670 result = self.loop.run_until_complete(test_2())
671 self.assertEqual(result, "completed")
672
673 def test_send():
674 p = ait_class()
675 obj = anext(p, "completed")
676 with self.assertRaises(StopIteration):
677 with contextlib.closing(obj.__await__()) as g:
678 g.send(None)
679
680 test_send()
681
682 async def test_throw():
683 p = ait_class()
684 obj = anext(p, "completed")
685 self.assertRaises(SyntaxError, obj.throw, SyntaxError)
686 return "completed"
687
688 result = self.loop.run_until_complete(test_throw())
689 self.assertEqual(result, "completed")
690
691 def test_async_generator_anext(self):
692 async def agen():
693 yield 1
694 yield 2
695 self.check_async_iterator_anext(agen)
696
697 def test_python_async_iterator_anext(self):
698 class MyAsyncIter:
699 """Asynchronously yield 1, then 2."""
700 def __init__(self):
701 self.yielded = 0
702 def __aiter__(self):
703 return self
704 async def __anext__(self):
705 if self.yielded >= 2:
706 raise StopAsyncIteration()
707 else:
708 self.yielded += 1
709 return self.yielded
710 self.check_async_iterator_anext(MyAsyncIter)
711
712 def test_python_async_iterator_types_coroutine_anext(self):
713 import types
714 class MyAsyncIterWithTypesCoro:
715 """Asynchronously yield 1, then 2."""
716 def __init__(self):
717 self.yielded = 0
718 def __aiter__(self):
719 return self
720 @types.coroutine
721 def __anext__(self):
722 if False:
723 yield "this is a generator-based coroutine"
724 if self.yielded >= 2:
725 raise StopAsyncIteration()
726 else:
727 self.yielded += 1
728 return self.yielded
729 self.check_async_iterator_anext(MyAsyncIterWithTypesCoro)
730
731 def test_async_gen_aiter(self):
732 async def gen():
733 yield 1
734 yield 2
735 g = gen()
736 async def consume():
737 return [i async for i in aiter(g)]
738 res = self.loop.run_until_complete(consume())
739 self.assertEqual(res, [1, 2])
740
741 def test_async_gen_aiter_class(self):
742 results = []
743 class Gen:
744 async def __aiter__(self):
745 yield 1
746 yield 2
747 g = Gen()
748 async def consume():
749 ait = aiter(g)
750 while True:
751 try:
752 results.append(await anext(ait))
753 except StopAsyncIteration:
754 break
755 self.loop.run_until_complete(consume())
756 self.assertEqual(results, [1, 2])
757
758 def test_aiter_idempotent(self):
759 async def gen():
760 yield 1
761 applied_once = aiter(gen())
762 applied_twice = aiter(applied_once)
763 self.assertIs(applied_once, applied_twice)
764
765 def test_anext_bad_args(self):
766 async def gen():
767 yield 1
768 async def call_with_too_few_args():
769 await anext()
770 async def call_with_too_many_args():
771 await anext(gen(), 1, 3)
772 async def call_with_wrong_type_args():
773 await anext(1, gen())
774 async def call_with_kwarg():
775 await anext(aiterator=gen())
776 with self.assertRaises(TypeError):
777 self.loop.run_until_complete(call_with_too_few_args())
778 with self.assertRaises(TypeError):
779 self.loop.run_until_complete(call_with_too_many_args())
780 with self.assertRaises(TypeError):
781 self.loop.run_until_complete(call_with_wrong_type_args())
782 with self.assertRaises(TypeError):
783 self.loop.run_until_complete(call_with_kwarg())
784
785 def test_anext_bad_await(self):
786 async def bad_awaitable():
787 class BadAwaitable:
788 def __await__(self):
789 return 42
790 class MyAsyncIter:
791 def __aiter__(self):
792 return self
793 def __anext__(self):
794 return BadAwaitable()
795 regex = r"__await__.*iterator"
796 awaitable = anext(MyAsyncIter(), "default")
797 with self.assertRaisesRegex(TypeError, regex):
798 await awaitable
799 awaitable = anext(MyAsyncIter())
800 with self.assertRaisesRegex(TypeError, regex):
801 await awaitable
802 return "completed"
803 result = self.loop.run_until_complete(bad_awaitable())
804 self.assertEqual(result, "completed")
805
806 async def check_anext_returning_iterator(self, aiter_class):
807 awaitable = anext(aiter_class(), "default")
808 with self.assertRaises(TypeError):
809 await awaitable
810 awaitable = anext(aiter_class())
811 with self.assertRaises(TypeError):
812 await awaitable
813 return "completed"
814
815 def test_anext_return_iterator(self):
816 class WithIterAnext:
817 def __aiter__(self):
818 return self
819 def __anext__(self):
820 return iter("abc")
821 result = self.loop.run_until_complete(self.check_anext_returning_iterator(WithIterAnext))
822 self.assertEqual(result, "completed")
823
824 def test_anext_return_generator(self):
825 class WithGenAnext:
826 def __aiter__(self):
827 return self
828 def __anext__(self):
829 yield
830 result = self.loop.run_until_complete(self.check_anext_returning_iterator(WithGenAnext))
831 self.assertEqual(result, "completed")
832
833 def test_anext_await_raises(self):
834 class RaisingAwaitable:
835 def __await__(self):
836 raise ZeroDivisionError()
837 yield
838 class WithRaisingAwaitableAnext:
839 def __aiter__(self):
840 return self
841 def __anext__(self):
842 return RaisingAwaitable()
843 async def do_test():
844 awaitable = anext(WithRaisingAwaitableAnext())
845 with self.assertRaises(ZeroDivisionError):
846 await awaitable
847 awaitable = anext(WithRaisingAwaitableAnext(), "default")
848 with self.assertRaises(ZeroDivisionError):
849 await awaitable
850 return "completed"
851 result = self.loop.run_until_complete(do_test())
852 self.assertEqual(result, "completed")
853
854 def test_anext_iter(self):
855 @types.coroutine
856 def _async_yield(v):
857 return (yield v)
858
859 class MyError(Exception):
860 pass
861
862 async def agenfn():
863 try:
864 await _async_yield(1)
865 except MyError:
866 await _async_yield(2)
867 return
868 yield
869
870 def test1(anext):
871 agen = agenfn()
872 with contextlib.closing(anext(agen, "default").__await__()) as g:
873 self.assertEqual(g.send(None), 1)
874 self.assertEqual(g.throw(MyError()), 2)
875 try:
876 g.send(None)
877 except StopIteration as e:
878 err = e
879 else:
880 self.fail('StopIteration was not raised')
881 self.assertEqual(err.value, "default")
882
883 def test2(anext):
884 agen = agenfn()
885 with contextlib.closing(anext(agen, "default").__await__()) as g:
886 self.assertEqual(g.send(None), 1)
887 self.assertEqual(g.throw(MyError()), 2)
888 with self.assertRaises(MyError):
889 g.throw(MyError())
890
891 def test3(anext):
892 agen = agenfn()
893 with contextlib.closing(anext(agen, "default").__await__()) as g:
894 self.assertEqual(g.send(None), 1)
895 g.close()
896 with self.assertRaisesRegex(RuntimeError, 'cannot reuse'):
897 self.assertEqual(g.send(None), 1)
898
899 def test4(anext):
900 @types.coroutine
901 def _async_yield(v):
902 yield v * 10
903 return (yield (v * 10 + 1))
904
905 async def agenfn():
906 try:
907 await _async_yield(1)
908 except MyError:
909 await _async_yield(2)
910 return
911 yield
912
913 agen = agenfn()
914 with contextlib.closing(anext(agen, "default").__await__()) as g:
915 self.assertEqual(g.send(None), 10)
916 self.assertEqual(g.throw(MyError()), 20)
917 with self.assertRaisesRegex(MyError, 'val'):
918 g.throw(MyError('val'))
919
920 def test5(anext):
921 @types.coroutine
922 def _async_yield(v):
923 yield v * 10
924 return (yield (v * 10 + 1))
925
926 async def agenfn():
927 try:
928 await _async_yield(1)
929 except MyError:
930 return
931 yield 'aaa'
932
933 agen = agenfn()
934 with contextlib.closing(anext(agen, "default").__await__()) as g:
935 self.assertEqual(g.send(None), 10)
936 with self.assertRaisesRegex(StopIteration, 'default'):
937 g.throw(MyError())
938
939 def test6(anext):
940 @types.coroutine
941 def _async_yield(v):
942 yield v * 10
943 return (yield (v * 10 + 1))
944
945 async def agenfn():
946 await _async_yield(1)
947 yield 'aaa'
948
949 agen = agenfn()
950 with contextlib.closing(anext(agen, "default").__await__()) as g:
951 with self.assertRaises(MyError):
952 g.throw(MyError())
953
954 def run_test(test):
955 with self.subTest('pure-Python anext()'):
956 test(py_anext)
957 with self.subTest('builtin anext()'):
958 test(anext)
959
960 run_test(test1)
961 run_test(test2)
962 run_test(test3)
963 run_test(test4)
964 run_test(test5)
965 run_test(test6)
966
967 def test_aiter_bad_args(self):
968 async def gen():
969 yield 1
970 async def call_with_too_few_args():
971 await aiter()
972 async def call_with_too_many_args():
973 await aiter(gen(), 1)
974 async def call_with_wrong_type_arg():
975 await aiter(1)
976 with self.assertRaises(TypeError):
977 self.loop.run_until_complete(call_with_too_few_args())
978 with self.assertRaises(TypeError):
979 self.loop.run_until_complete(call_with_too_many_args())
980 with self.assertRaises(TypeError):
981 self.loop.run_until_complete(call_with_wrong_type_arg())
982
983 async def to_list(self, gen):
984 res = []
985 async for i in gen:
986 res.append(i)
987 return res
988
989 def test_async_gen_asyncio_01(self):
990 async def gen():
991 yield 1
992 await asyncio.sleep(0.01)
993 yield 2
994 await asyncio.sleep(0.01)
995 return
996 yield 3
997
998 res = self.loop.run_until_complete(self.to_list(gen()))
999 self.assertEqual(res, [1, 2])
1000
1001 def test_async_gen_asyncio_02(self):
1002 async def gen():
1003 yield 1
1004 await asyncio.sleep(0.01)
1005 yield 2
1006 1 / 0
1007 yield 3
1008
1009 with self.assertRaises(ZeroDivisionError):
1010 self.loop.run_until_complete(self.to_list(gen()))
1011
1012 def test_async_gen_asyncio_03(self):
1013 loop = self.loop
1014
1015 class Gen:
1016 async def __aiter__(self):
1017 yield 1
1018 await asyncio.sleep(0.01)
1019 yield 2
1020
1021 res = loop.run_until_complete(self.to_list(Gen()))
1022 self.assertEqual(res, [1, 2])
1023
1024 def test_async_gen_asyncio_anext_04(self):
1025 async def foo():
1026 yield 1
1027 await asyncio.sleep(0.01)
1028 try:
1029 yield 2
1030 yield 3
1031 except ZeroDivisionError:
1032 yield 1000
1033 await asyncio.sleep(0.01)
1034 yield 4
1035
1036 async def run1():
1037 it = foo().__aiter__()
1038
1039 self.assertEqual(await it.__anext__(), 1)
1040 self.assertEqual(await it.__anext__(), 2)
1041 self.assertEqual(await it.__anext__(), 3)
1042 self.assertEqual(await it.__anext__(), 4)
1043 with self.assertRaises(StopAsyncIteration):
1044 await it.__anext__()
1045 with self.assertRaises(StopAsyncIteration):
1046 await it.__anext__()
1047
1048 async def run2():
1049 it = foo().__aiter__()
1050
1051 self.assertEqual(await it.__anext__(), 1)
1052 self.assertEqual(await it.__anext__(), 2)
1053 try:
1054 it.__anext__().throw(ZeroDivisionError)
1055 except StopIteration as ex:
1056 self.assertEqual(ex.args[0], 1000)
1057 else:
1058 self.fail('StopIteration was not raised')
1059 self.assertEqual(await it.__anext__(), 4)
1060 with self.assertRaises(StopAsyncIteration):
1061 await it.__anext__()
1062
1063 self.loop.run_until_complete(run1())
1064 self.loop.run_until_complete(run2())
1065
1066 def test_async_gen_asyncio_anext_05(self):
1067 async def foo():
1068 v = yield 1
1069 v = yield v
1070 yield v * 100
1071
1072 async def run():
1073 it = foo().__aiter__()
1074
1075 try:
1076 it.__anext__().send(None)
1077 except StopIteration as ex:
1078 self.assertEqual(ex.args[0], 1)
1079 else:
1080 self.fail('StopIteration was not raised')
1081
1082 try:
1083 it.__anext__().send(10)
1084 except StopIteration as ex:
1085 self.assertEqual(ex.args[0], 10)
1086 else:
1087 self.fail('StopIteration was not raised')
1088
1089 try:
1090 it.__anext__().send(12)
1091 except StopIteration as ex:
1092 self.assertEqual(ex.args[0], 1200)
1093 else:
1094 self.fail('StopIteration was not raised')
1095
1096 with self.assertRaises(StopAsyncIteration):
1097 await it.__anext__()
1098
1099 self.loop.run_until_complete(run())
1100
1101 def test_async_gen_asyncio_anext_06(self):
1102 DONE = 0
1103
1104 # test synchronous generators
1105 def foo():
1106 try:
1107 yield
1108 except:
1109 pass
1110 g = foo()
1111 g.send(None)
1112 with self.assertRaises(StopIteration):
1113 g.send(None)
1114
1115 # now with asynchronous generators
1116
1117 async def gen():
1118 nonlocal DONE
1119 try:
1120 yield
1121 except:
1122 pass
1123 DONE = 1
1124
1125 async def run():
1126 nonlocal DONE
1127 g = gen()
1128 await g.asend(None)
1129 with self.assertRaises(StopAsyncIteration):
1130 await g.asend(None)
1131 DONE += 10
1132
1133 self.loop.run_until_complete(run())
1134 self.assertEqual(DONE, 11)
1135
1136 def test_async_gen_asyncio_anext_tuple(self):
1137 async def foo():
1138 try:
1139 yield (1,)
1140 except ZeroDivisionError:
1141 yield (2,)
1142
1143 async def run():
1144 it = foo().__aiter__()
1145
1146 self.assertEqual(await it.__anext__(), (1,))
1147 with self.assertRaises(StopIteration) as cm:
1148 it.__anext__().throw(ZeroDivisionError)
1149 self.assertEqual(cm.exception.args[0], (2,))
1150 with self.assertRaises(StopAsyncIteration):
1151 await it.__anext__()
1152
1153 self.loop.run_until_complete(run())
1154
1155 def test_async_gen_asyncio_anext_tuple_no_exceptions(self):
1156 # StopAsyncIteration exceptions should be cleared.
1157 # See: https://github.com/python/cpython/issues/128078.
1158
1159 async def foo():
1160 if False:
1161 yield (1, 2)
1162
1163 async def run():
1164 it = foo().__aiter__()
1165 with self.assertRaises(StopAsyncIteration):
1166 await it.__anext__()
1167 res = await anext(it, ('a', 'b'))
1168 self.assertTupleEqual(res, ('a', 'b'))
1169
1170 self.loop.run_until_complete(run())
1171
1172 def test_sync_anext_raises_exception(self):
1173 # See: https://github.com/python/cpython/issues/131670
1174 msg = 'custom'
1175 for exc_type in [
1176 StopAsyncIteration,
1177 StopIteration,
1178 ValueError,
1179 Exception,
1180 ]:
1181 exc = exc_type(msg)
1182 with self.subTest(exc=exc):
1183 class A:
1184 def __anext__(self):
1185 raise exc
1186
1187 with self.assertRaisesRegex(exc_type, msg):
1188 anext(A())
1189 with self.assertRaisesRegex(exc_type, msg):
1190 anext(A(), 1)
1191
1192 def test_async_gen_asyncio_anext_stopiteration(self):
1193 async def foo():
1194 try:
1195 yield StopIteration(1)
1196 except ZeroDivisionError:
1197 yield StopIteration(3)
1198
1199 async def run():
1200 it = foo().__aiter__()
1201
1202 v = await it.__anext__()
1203 self.assertIsInstance(v, StopIteration)
1204 self.assertEqual(v.value, 1)
1205 with self.assertRaises(StopIteration) as cm:
1206 it.__anext__().throw(ZeroDivisionError)
1207 v = cm.exception.args[0]
1208 self.assertIsInstance(v, StopIteration)
1209 self.assertEqual(v.value, 3)
1210 with self.assertRaises(StopAsyncIteration):
1211 await it.__anext__()
1212
1213 self.loop.run_until_complete(run())
1214
1215 def test_async_gen_asyncio_aclose_06(self):
1216 async def foo():
1217 try:
1218 yield 1
1219 1 / 0
1220 finally:
1221 await asyncio.sleep(0.01)
1222 yield 12
1223
1224 async def run():
1225 gen = foo()
1226 it = gen.__aiter__()
1227 await it.__anext__()
1228 await gen.aclose()
1229
1230 with self.assertRaisesRegex(
1231 RuntimeError,
1232 "async generator ignored GeneratorExit"):
1233 self.loop.run_until_complete(run())
1234
1235 def test_async_gen_asyncio_aclose_07(self):
1236 DONE = 0
1237
1238 async def foo():
1239 nonlocal DONE
1240 try:
1241 yield 1
1242 1 / 0
1243 finally:
1244 await asyncio.sleep(0.01)
1245 await asyncio.sleep(0.01)
1246 DONE += 1
1247 DONE += 1000
1248
1249 async def run():
1250 gen = foo()
1251 it = gen.__aiter__()
1252 await it.__anext__()
1253 await gen.aclose()
1254
1255 self.loop.run_until_complete(run())
1256 self.assertEqual(DONE, 1)
1257
1258 def test_async_gen_asyncio_aclose_08(self):
1259 DONE = 0
1260
1261 fut = asyncio.Future(loop=self.loop)
1262
1263 async def foo():
1264 nonlocal DONE
1265 try:
1266 yield 1
1267 await fut
1268 DONE += 1000
1269 yield 2
1270 finally:
1271 await asyncio.sleep(0.01)
1272 await asyncio.sleep(0.01)
1273 DONE += 1
1274 DONE += 1000
1275
1276 async def run():
1277 gen = foo()
1278 it = gen.__aiter__()
1279 self.assertEqual(await it.__anext__(), 1)
1280 await gen.aclose()
1281
1282 self.loop.run_until_complete(run())
1283 self.assertEqual(DONE, 1)
1284
1285 # Silence ResourceWarnings
1286 fut.cancel()
1287 self.loop.run_until_complete(asyncio.sleep(0.01))
1288
1289 def test_async_gen_asyncio_gc_aclose_09(self):
1290 DONE = 0
1291
1292 async def gen():
1293 nonlocal DONE
1294 try:
1295 while True:
1296 yield 1
1297 finally:
1298 await asyncio.sleep(0)
1299 DONE = 1
1300
1301 async def run():
1302 g = gen()
1303 await g.__anext__()
1304 await g.__anext__()
1305 del g
1306 gc_collect() # For PyPy or other GCs.
1307
1308 # Starts running the aclose task
1309 await asyncio.sleep(0)
1310 # For asyncio.sleep(0) in finally block
1311 await asyncio.sleep(0)
1312
1313 self.loop.run_until_complete(run())
1314 self.assertEqual(DONE, 1)
1315
1316 def test_async_gen_asyncio_aclose_10(self):
1317 DONE = 0
1318
1319 # test synchronous generators
1320 def foo():
1321 try:
1322 yield
1323 except:
1324 pass
1325 g = foo()
1326 g.send(None)
1327 g.close()
1328
1329 # now with asynchronous generators
1330
1331 async def gen():
1332 nonlocal DONE
1333 try:
1334 yield
1335 except:
1336 pass
1337 DONE = 1
1338
1339 async def run():
1340 nonlocal DONE
1341 g = gen()
1342 await g.asend(None)
1343 await g.aclose()
1344 DONE += 10
1345
1346 self.loop.run_until_complete(run())
1347 self.assertEqual(DONE, 11)
1348
1349 def test_async_gen_asyncio_aclose_11(self):
1350 DONE = 0
1351
1352 # test synchronous generators
1353 def foo():
1354 try:
1355 yield
1356 except:
1357 pass
1358 yield
1359 g = foo()
1360 g.send(None)
1361 with self.assertRaisesRegex(RuntimeError, 'ignored GeneratorExit'):
1362 g.close()
1363
1364 # now with asynchronous generators
1365
1366 async def gen():
1367 nonlocal DONE
1368 try:
1369 yield
1370 except:
1371 pass
1372 yield
1373 DONE += 1
1374
1375 async def run():
1376 nonlocal DONE
1377 g = gen()
1378 await g.asend(None)
1379 with self.assertRaisesRegex(RuntimeError, 'ignored GeneratorExit'):
1380 await g.aclose()
1381 DONE += 10
1382
1383 self.loop.run_until_complete(run())
1384 self.assertEqual(DONE, 10)
1385
1386 def test_async_gen_asyncio_aclose_12(self):
1387 DONE = 0
1388
1389 async def target():
1390 await asyncio.sleep(0.01)
1391 1 / 0
1392
1393 async def foo():
1394 nonlocal DONE
1395 task = asyncio.create_task(target())
1396 try:
1397 yield 1
1398 finally:
1399 try:
1400 await task
1401 except ZeroDivisionError:
1402 DONE = 1
1403
1404 async def run():
1405 gen = foo()
1406 it = gen.__aiter__()
1407 await it.__anext__()
1408 await gen.aclose()
1409
1410 self.loop.run_until_complete(run())
1411 self.assertEqual(DONE, 1)
1412
1413 def test_async_gen_asyncio_asend_01(self):
1414 DONE = 0
1415
1416 # Sanity check:
1417 def sgen():
1418 v = yield 1
1419 yield v * 2
1420 sg = sgen()
1421 v = sg.send(None)
1422 self.assertEqual(v, 1)
1423 v = sg.send(100)
1424 self.assertEqual(v, 200)
1425
1426 async def gen():
1427 nonlocal DONE
1428 try:
1429 await asyncio.sleep(0.01)
1430 v = yield 1
1431 await asyncio.sleep(0.01)
1432 yield v * 2
1433 await asyncio.sleep(0.01)
1434 return
1435 finally:
1436 await asyncio.sleep(0.01)
1437 await asyncio.sleep(0.01)
1438 DONE = 1
1439
1440 async def run():
1441 g = gen()
1442
1443 v = await g.asend(None)
1444 self.assertEqual(v, 1)
1445
1446 v = await g.asend(100)
1447 self.assertEqual(v, 200)
1448
1449 with self.assertRaises(StopAsyncIteration):
1450 await g.asend(None)
1451
1452 self.loop.run_until_complete(run())
1453 self.assertEqual(DONE, 1)
1454
1455 def test_async_gen_asyncio_asend_02(self):
1456 DONE = 0
1457
1458 async def sleep_n_crash(delay):
1459 await asyncio.sleep(delay)
1460 1 / 0
1461
1462 async def gen():
1463 nonlocal DONE
1464 try:
1465 await asyncio.sleep(0.01)
1466 v = yield 1
1467 await sleep_n_crash(0.01)
1468 DONE += 1000
1469 yield v * 2
1470 finally:
1471 await asyncio.sleep(0.01)
1472 await asyncio.sleep(0.01)
1473 DONE = 1
1474
1475 async def run():
1476 g = gen()
1477
1478 v = await g.asend(None)
1479 self.assertEqual(v, 1)
1480
1481 await g.asend(100)
1482
1483 with self.assertRaises(ZeroDivisionError):
1484 self.loop.run_until_complete(run())
1485 self.assertEqual(DONE, 1)
1486
1487 def test_async_gen_asyncio_asend_03(self):
1488 DONE = 0
1489
1490 async def sleep_n_crash(delay):
1491 fut = asyncio.ensure_future(asyncio.sleep(delay),
1492 loop=self.loop)
1493 self.loop.call_later(delay / 2, lambda: fut.cancel())
1494 return await fut
1495
1496 async def gen():
1497 nonlocal DONE
1498 try:
1499 await asyncio.sleep(0.01)
1500 v = yield 1
1501 await sleep_n_crash(0.01)
1502 DONE += 1000
1503 yield v * 2
1504 finally:
1505 await asyncio.sleep(0.01)
1506 await asyncio.sleep(0.01)
1507 DONE = 1
1508
1509 async def run():
1510 g = gen()
1511
1512 v = await g.asend(None)
1513 self.assertEqual(v, 1)
1514
1515 await g.asend(100)
1516
1517 with self.assertRaises(asyncio.CancelledError):
1518 self.loop.run_until_complete(run())
1519 self.assertEqual(DONE, 1)
1520
1521 def test_async_gen_asyncio_athrow_01(self):
1522 DONE = 0
1523
1524 class FooEr(Exception):
1525 pass
1526
1527 # Sanity check:
1528 def sgen():
1529 try:
1530 v = yield 1
1531 except FooEr:
1532 v = 1000
1533 yield v * 2
1534 sg = sgen()
1535 v = sg.send(None)
1536 self.assertEqual(v, 1)
1537 v = sg.throw(FooEr)
1538 self.assertEqual(v, 2000)
1539 with self.assertRaises(StopIteration):
1540 sg.send(None)
1541
1542 async def gen():
1543 nonlocal DONE
1544 try:
1545 await asyncio.sleep(0.01)
1546 try:
1547 v = yield 1
1548 except FooEr:
1549 v = 1000
1550 await asyncio.sleep(0.01)
1551 yield v * 2
1552 await asyncio.sleep(0.01)
1553 # return
1554 finally:
1555 await asyncio.sleep(0.01)
1556 await asyncio.sleep(0.01)
1557 DONE = 1
1558
1559 async def run():
1560 g = gen()
1561
1562 v = await g.asend(None)
1563 self.assertEqual(v, 1)
1564
1565 v = await g.athrow(FooEr)
1566 self.assertEqual(v, 2000)
1567
1568 with self.assertRaises(StopAsyncIteration):
1569 await g.asend(None)
1570
1571 self.loop.run_until_complete(run())
1572 self.assertEqual(DONE, 1)
1573
1574 def test_async_gen_asyncio_athrow_02(self):
1575 DONE = 0
1576
1577 class FooEr(Exception):
1578 pass
1579
1580 async def sleep_n_crash(delay):
1581 fut = asyncio.ensure_future(asyncio.sleep(delay),
1582 loop=self.loop)
1583 self.loop.call_later(delay / 2, lambda: fut.cancel())
1584 return await fut
1585
1586 async def gen():
1587 nonlocal DONE
1588 try:
1589 await asyncio.sleep(0.01)
1590 try:
1591 v = yield 1
1592 except FooEr:
1593 await sleep_n_crash(0.01)
1594 yield v * 2
1595 await asyncio.sleep(0.01)
1596 # return
1597 finally:
1598 await asyncio.sleep(0.01)
1599 await asyncio.sleep(0.01)
1600 DONE = 1
1601
1602 async def run():
1603 g = gen()
1604
1605 v = await g.asend(None)
1606 self.assertEqual(v, 1)
1607
1608 try:
1609 await g.athrow(FooEr)
1610 except asyncio.CancelledError:
1611 self.assertEqual(DONE, 1)
1612 raise
1613 else:
1614 self.fail('CancelledError was not raised')
1615
1616 with self.assertRaises(asyncio.CancelledError):
1617 self.loop.run_until_complete(run())
1618 self.assertEqual(DONE, 1)
1619
1620 def test_async_gen_asyncio_athrow_03(self):
1621 DONE = 0
1622
1623 # test synchronous generators
1624 def foo():
1625 try:
1626 yield
1627 except:
1628 pass
1629 g = foo()
1630 g.send(None)
1631 with self.assertRaises(StopIteration):
1632 g.throw(ValueError)
1633
1634 # now with asynchronous generators
1635
1636 async def gen():
1637 nonlocal DONE
1638 try:
1639 yield
1640 except:
1641 pass
1642 DONE = 1
1643
1644 async def run():
1645 nonlocal DONE
1646 g = gen()
1647 await g.asend(None)
1648 with self.assertRaises(StopAsyncIteration):
1649 await g.athrow(ValueError)
1650 DONE += 10
1651
1652 self.loop.run_until_complete(run())
1653 self.assertEqual(DONE, 11)
1654
1655 def test_async_gen_asyncio_athrow_tuple(self):
1656 async def gen():
1657 try:
1658 yield 1
1659 except ZeroDivisionError:
1660 yield (2,)
1661
1662 async def run():
1663 g = gen()
1664 v = await g.asend(None)
1665 self.assertEqual(v, 1)
1666 v = await g.athrow(ZeroDivisionError)
1667 self.assertEqual(v, (2,))
1668 with self.assertRaises(StopAsyncIteration):
1669 await g.asend(None)
1670
1671 self.loop.run_until_complete(run())
1672
1673 def test_async_gen_asyncio_athrow_stopiteration(self):
1674 async def gen():
1675 try:
1676 yield 1
1677 except ZeroDivisionError:
1678 yield StopIteration(2)
1679
1680 async def run():
1681 g = gen()
1682 v = await g.asend(None)
1683 self.assertEqual(v, 1)
1684 v = await g.athrow(ZeroDivisionError)
1685 self.assertIsInstance(v, StopIteration)
1686 self.assertEqual(v.value, 2)
1687 with self.assertRaises(StopAsyncIteration):
1688 await g.asend(None)
1689
1690 self.loop.run_until_complete(run())
1691
1692 def test_async_gen_asyncio_shutdown_01(self):
1693 finalized = 0
1694
1695 async def waiter(timeout):
1696 nonlocal finalized
1697 try:
1698 await asyncio.sleep(timeout)
1699 yield 1
1700 finally:
1701 await asyncio.sleep(0)
1702 finalized += 1
1703
1704 async def wait():
1705 async for _ in waiter(1):
1706 pass
1707
1708 t1 = self.loop.create_task(wait())
1709 t2 = self.loop.create_task(wait())
1710
1711 self.loop.run_until_complete(asyncio.sleep(0.1))
1712
1713 # Silence warnings
1714 t1.cancel()
1715 t2.cancel()
1716
1717 with self.assertRaises(asyncio.CancelledError):
1718 self.loop.run_until_complete(t1)
1719 with self.assertRaises(asyncio.CancelledError):
1720 self.loop.run_until_complete(t2)
1721
1722 self.loop.run_until_complete(self.loop.shutdown_asyncgens())
1723
1724 self.assertEqual(finalized, 2)
1725
1726 def test_async_gen_asyncio_shutdown_02(self):
1727 messages = []
1728
1729 def exception_handler(loop, context):
1730 messages.append(context)
1731
1732 async def async_iterate():
1733 yield 1
1734 yield 2
1735
1736 it = async_iterate()
1737 async def main():
1738 loop = asyncio.get_running_loop()
1739 loop.set_exception_handler(exception_handler)
1740
1741 async for i in it:
1742 break
1743
1744 asyncio.run(main())
1745
1746 self.assertEqual(messages, [])
1747
1748 def test_async_gen_asyncio_shutdown_exception_01(self):
1749 messages = []
1750
1751 def exception_handler(loop, context):
1752 messages.append(context)
1753
1754 async def async_iterate():
1755 try:
1756 yield 1
1757 yield 2
1758 finally:
1759 1/0
1760
1761 it = async_iterate()
1762 async def main():
1763 loop = asyncio.get_running_loop()
1764 loop.set_exception_handler(exception_handler)
1765
1766 async for i in it:
1767 break
1768
1769 asyncio.run(main())
1770
1771 message, = messages
1772 self.assertEqual(message['asyncgen'], it)
1773 self.assertIsInstance(message['exception'], ZeroDivisionError)
1774 self.assertIn('an error occurred during closing of asynchronous generator',
1775 message['message'])
1776
1777 def test_async_gen_asyncio_shutdown_exception_02(self):
1778 messages = []
1779
1780 def exception_handler(loop, context):
1781 messages.append(context)
1782
1783 async def async_iterate():
1784 try:
1785 yield 1
1786 yield 2
1787 finally:
1788 1/0
1789
1790 async def main():
1791 loop = asyncio.get_running_loop()
1792 loop.set_exception_handler(exception_handler)
1793
1794 async for i in async_iterate():
1795 break
1796 gc_collect()
1797
1798 asyncio.run(main())
1799
1800 message, = messages
1801 self.assertIsInstance(message['exception'], ZeroDivisionError)
1802 self.assertIn('unhandled exception during asyncio.run() shutdown',
1803 message['message'])
1804 del message, messages
1805 gc_collect()
1806
1807 def test_async_gen_expression_01(self):
1808 async def arange(n):
1809 for i in range(n):
1810 await asyncio.sleep(0.01)
1811 yield i
1812
1813 def make_arange(n):
1814 # This syntax is legal starting with Python 3.7
1815 return (i * 2 async for i in arange(n))
1816
1817 async def run():
1818 return [i async for i in make_arange(10)]
1819
1820 res = self.loop.run_until_complete(run())
1821 self.assertEqual(res, [i * 2 for i in range(10)])
1822
1823 def test_async_gen_expression_02(self):
1824 async def wrap(n):
1825 await asyncio.sleep(0.01)
1826 return n
1827
1828 def make_arange(n):
1829 # This syntax is legal starting with Python 3.7
1830 return (i * 2 for i in range(n) if await wrap(i))
1831
1832 async def run():
1833 return [i async for i in make_arange(10)]
1834
1835 res = self.loop.run_until_complete(run())
1836 self.assertEqual(res, [i * 2 for i in range(1, 10)])
1837
1838 def test_async_gen_expression_incorrect(self):
1839 async def ag():
1840 yield 42
1841
1842 async def run(arg):
1843 (x async for x in arg)
1844
1845 err_msg_async = "'async for' requires an object with " \
1846 "__aiter__ method, got .*"
1847
1848 self.loop.run_until_complete(run(ag()))
1849 with self.assertRaisesRegex(TypeError, err_msg_async):
1850 self.loop.run_until_complete(run(None))
1851
1852 def test_asyncgen_nonstarted_hooks_are_cancellable(self):
1853 # See https://bugs.python.org/issue38013
1854 messages = []
1855
1856 def exception_handler(loop, context):
1857 messages.append(context)
1858
1859 async def async_iterate():
1860 yield 1
1861 yield 2
1862
1863 async def main():
1864 loop = asyncio.get_running_loop()
1865 loop.set_exception_handler(exception_handler)
1866
1867 async for i in async_iterate():
1868 break
1869
1870 asyncio.run(main())
1871
1872 self.assertEqual([], messages)
1873 gc_collect()
1874
1875 def test_async_gen_await_same_anext_coro_twice(self):
1876 async def async_iterate():
1877 yield 1
1878 yield 2
1879
1880 async def run():
1881 it = async_iterate()
1882 nxt = it.__anext__()
1883 await nxt
1884 with self.assertRaisesRegex(
1885 RuntimeError,
1886 r"cannot reuse already awaited __anext__\(\)/asend\(\)"
1887 ):
1888 await nxt
1889
1890 await it.aclose() # prevent unfinished iterator warning
1891
1892 self.loop.run_until_complete(run())
1893
1894 def test_async_gen_await_same_aclose_coro_twice(self):
1895 async def async_iterate():
1896 yield 1
1897 yield 2
1898
1899 async def run():
1900 it = async_iterate()
1901 nxt = it.aclose()
1902 await nxt
1903 with self.assertRaisesRegex(
1904 RuntimeError,
1905 r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1906 ):
1907 await nxt
1908
1909 self.loop.run_until_complete(run())
1910
1911 def test_async_gen_throw_same_aclose_coro_twice(self):
1912 async def async_iterate():
1913 yield 1
1914 yield 2
1915
1916 it = async_iterate()
1917 nxt = it.aclose()
1918 with self.assertRaises(StopIteration):
1919 nxt.throw(GeneratorExit)
1920
1921 with self.assertRaisesRegex(
1922 RuntimeError,
1923 r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1924 ):
1925 nxt.throw(GeneratorExit)
1926
1927 def test_async_gen_throw_custom_same_aclose_coro_twice(self):
1928 async def async_iterate():
1929 yield 1
1930 yield 2
1931
1932 it = async_iterate()
1933
1934 class MyException(Exception):
1935 pass
1936
1937 nxt = it.aclose()
1938 with self.assertRaises(MyException):
1939 nxt.throw(MyException)
1940
1941 with self.assertRaisesRegex(
1942 RuntimeError,
1943 r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1944 ):
1945 nxt.throw(MyException)
1946
1947 def test_async_gen_throw_custom_same_athrow_coro_twice(self):
1948 async def async_iterate():
1949 yield 1
1950 yield 2
1951
1952 it = async_iterate()
1953
1954 class MyException(Exception):
1955 pass
1956
1957 nxt = it.athrow(MyException)
1958 with self.assertRaises(MyException):
1959 nxt.throw(MyException)
1960
1961 with self.assertRaisesRegex(
1962 RuntimeError,
1963 r"cannot reuse already awaited aclose\(\)/athrow\(\)"
1964 ):
1965 nxt.throw(MyException)
1966
1967 def test_async_gen_aclose_twice_with_different_coros(self):
1968 # Regression test for https://bugs.python.org/issue39606
1969 async def async_iterate():
1970 yield 1
1971 yield 2
1972
1973 async def run():
1974 it = async_iterate()
1975 await it.aclose()
1976 await it.aclose()
1977
1978 self.loop.run_until_complete(run())
1979
1980 def test_async_gen_aclose_after_exhaustion(self):
1981 # Regression test for https://bugs.python.org/issue39606
1982 async def async_iterate():
1983 yield 1
1984 yield 2
1985
1986 async def run():
1987 it = async_iterate()
1988 async for _ in it:
1989 pass
1990 await it.aclose()
1991
1992 self.loop.run_until_complete(run())
1993
1994 def test_async_gen_aclose_compatible_with_get_stack(self):
1995 async def async_generator():
1996 yield object()
1997
1998 async def run():
1999 ag = async_generator()
2000 asyncio.create_task(ag.aclose())
2001 tasks = asyncio.all_tasks()
2002 for task in tasks:
2003 # No AttributeError raised
2004 task.get_stack()
2005
2006 self.loop.run_until_complete(run())
2007
2008
2009 class TestUnawaitedWarnings(unittest.TestCase):
2010 def test_asend(self):
2011 async def gen():
2012 yield 1
2013
2014 # gh-113753: asend objects allocated from a free-list should warn.
2015 # Ensure there is a finalized 'asend' object ready to be reused.
2016 try:
2017 g = gen()
2018 g.asend(None).send(None)
2019 except StopIteration:
2020 pass
2021
2022 msg = f"coroutine method 'asend' of '{gen.__qualname__}' was never awaited"
2023 with self.assertWarnsRegex(RuntimeWarning, msg):
2024 g = gen()
2025 g.asend(None)
2026 gc_collect()
2027
2028 def test_athrow(self):
2029 async def gen():
2030 yield 1
2031
2032 msg = f"coroutine method 'athrow' of '{gen.__qualname__}' was never awaited"
2033 with self.assertWarnsRegex(RuntimeWarning, msg):
2034 g = gen()
2035 g.athrow(RuntimeError)
2036 gc_collect()
2037
2038 def test_aclose(self):
2039 async def gen():
2040 yield 1
2041
2042 msg = f"coroutine method 'aclose' of '{gen.__qualname__}' was never awaited"
2043 with self.assertWarnsRegex(RuntimeWarning, msg):
2044 g = gen()
2045 g.aclose()
2046 gc_collect()
2047
2048 def test_aclose_throw(self):
2049 async def gen():
2050 return
2051 yield
2052
2053 class MyException(Exception):
2054 pass
2055
2056 g = gen()
2057 with self.assertRaises(MyException):
2058 g.aclose().throw(MyException)
2059
2060 del g
2061 gc_collect() # does not warn unawaited
2062
2063 def test_asend_send_already_running(self):
2064 @types.coroutine
2065 def _async_yield(v):
2066 return (yield v)
2067
2068 async def agenfn():
2069 while True:
2070 await _async_yield(1)
2071 return
2072 yield
2073
2074 agen = agenfn()
2075 gen = agen.asend(None)
2076 gen.send(None)
2077 gen2 = agen.asend(None)
2078
2079 with self.assertRaisesRegex(RuntimeError,
2080 r'anext\(\): asynchronous generator is already running'):
2081 gen2.send(None)
2082
2083 del gen2
2084 gc_collect() # does not warn unawaited
2085
2086
2087 def test_athrow_send_already_running(self):
2088 @types.coroutine
2089 def _async_yield(v):
2090 return (yield v)
2091
2092 async def agenfn():
2093 while True:
2094 await _async_yield(1)
2095 return
2096 yield
2097
2098 agen = agenfn()
2099 gen = agen.asend(None)
2100 gen.send(None)
2101 gen2 = agen.athrow(Exception)
2102
2103 with self.assertRaisesRegex(RuntimeError,
2104 r'athrow\(\): asynchronous generator is already running'):
2105 gen2.send(None)
2106
2107 del gen2
2108 gc_collect() # does not warn unawaited
2109
2110 if __name__ == "__main__":
2111 unittest.main()