Debian Codegraph @

Lib/test/test_coroutines.py

raw
1 import contextlib
2 import copy
3 import inspect
4 import pickle
5 import sys
6 import types
7 import traceback
8 import unittest
9 import warnings
10 from test import support
11 from test.support import import_helper
12 from test.support import warnings_helper
13 from test.support.script_helper import assert_python_ok
14 try:
15 import _testcapi
16 except ImportError:
17 _testcapi = None
18
19
20 class AsyncYieldFrom:
21 def __init__(self, obj):
22 self.obj = obj
23
24 def __await__(self):
25 yield from self.obj
26
27
28 class AsyncYield:
29 def __init__(self, value):
30 self.value = value
31
32 def __await__(self):
33 yield self.value
34
35
36 async def asynciter(iterable):
37 """Convert an iterable to an asynchronous iterator."""
38 for x in iterable:
39 yield x
40
41
42 def run_async(coro):
43 assert coro.__class__ in {types.GeneratorType, types.CoroutineType}
44
45 buffer = []
46 result = None
47 while True:
48 try:
49 buffer.append(coro.send(None))
50 except StopIteration as ex:
51 result = ex.args[0] if ex.args else None
52 break
53 return buffer, result
54
55
56 def run_async__await__(coro):
57 assert coro.__class__ is types.CoroutineType
58 aw = coro.__await__()
59 buffer = []
60 result = None
61 i = 0
62 while True:
63 try:
64 if i % 2:
65 buffer.append(next(aw))
66 else:
67 buffer.append(aw.send(None))
68 i += 1
69 except StopIteration as ex:
70 result = ex.args[0] if ex.args else None
71 break
72 return buffer, result
73
74
75 @contextlib.contextmanager
76 def silence_coro_gc():
77 with warnings.catch_warnings():
78 warnings.simplefilter("ignore")
79 yield
80 support.gc_collect()
81
82
83 class AsyncBadSyntaxTest(unittest.TestCase):
84
85 def test_badsyntax_1(self):
86 samples = [
87 """def foo():
88 await something()
89 """,
90
91 """await something()""",
92
93 """async def foo():
94 yield from []
95 """,
96
97 """async def foo():
98 await await fut
99 """,
100
101 """async def foo(a=await something()):
102 pass
103 """,
104
105 """async def foo(a:await something()):
106 pass
107 """,
108
109 """async def foo():
110 def bar():
111 [i async for i in els]
112 """,
113
114 """async def foo():
115 def bar():
116 [await i for i in els]
117 """,
118
119 """async def foo():
120 def bar():
121 [i for i in els
122 async for b in els]
123 """,
124
125 """async def foo():
126 def bar():
127 [i for i in els
128 for c in b
129 async for b in els]
130 """,
131
132 """async def foo():
133 def bar():
134 [i for i in els
135 async for b in els
136 for c in b]
137 """,
138
139 """async def foo():
140 def bar():
141 [[async for i in b] for b in els]
142 """,
143
144 """async def foo():
145 def bar():
146 [i for i in els
147 for b in await els]
148 """,
149
150 """async def foo():
151 def bar():
152 [i for i in els
153 for b in els
154 if await b]
155 """,
156
157 """async def foo():
158 def bar():
159 [i for i in await els]
160 """,
161
162 """async def foo():
163 def bar():
164 [i for i in els if await i]
165 """,
166
167 """def bar():
168 [i async for i in els]
169 """,
170
171 """def bar():
172 {i: i async for i in els}
173 """,
174
175 """def bar():
176 {i async for i in els}
177 """,
178
179 """def bar():
180 [await i for i in els]
181 """,
182
183 """def bar():
184 [i for i in els
185 async for b in els]
186 """,
187
188 """def bar():
189 [i for i in els
190 for c in b
191 async for b in els]
192 """,
193
194 """def bar():
195 [i for i in els
196 async for b in els
197 for c in b]
198 """,
199
200 """def bar():
201 [i for i in els
202 for b in await els]
203 """,
204
205 """def bar():
206 [i for i in els
207 for b in els
208 if await b]
209 """,
210
211 """def bar():
212 [i for i in await els]
213 """,
214
215 """def bar():
216 [i for i in els if await i]
217 """,
218
219 """def bar():
220 [[i async for i in a] for a in elts]
221 """,
222
223 """[[i async for i in a] for a in elts]
224 """,
225
226 """async def foo():
227 await
228 """,
229
230 """async def foo():
231 def bar(): pass
232 await = 1
233 """,
234
235 """async def foo():
236
237 def bar(): pass
238 await = 1
239 """,
240
241 """async def foo():
242 def bar(): pass
243 if 1:
244 await = 1
245 """,
246
247 """def foo():
248 async def bar(): pass
249 if 1:
250 await a
251 """,
252
253 """def foo():
254 async def bar(): pass
255 await a
256 """,
257
258 """def foo():
259 def baz(): pass
260 async def bar(): pass
261 await a
262 """,
263
264 """def foo():
265 def baz(): pass
266 # 456
267 async def bar(): pass
268 # 123
269 await a
270 """,
271
272 """async def foo():
273 def baz(): pass
274 # 456
275 async def bar(): pass
276 # 123
277 await = 2
278 """,
279
280 """def foo():
281
282 def baz(): pass
283
284 async def bar(): pass
285
286 await a
287 """,
288
289 """async def foo():
290
291 def baz(): pass
292
293 async def bar(): pass
294
295 await = 2
296 """,
297
298 """async def foo():
299 def async(): pass
300 """,
301
302 """async def foo():
303 def await(): pass
304 """,
305
306 """async def foo():
307 def bar():
308 await
309 """,
310
311 """async def foo():
312 return lambda async: await
313 """,
314
315 """async def foo():
316 return lambda a: await
317 """,
318
319 """await a()""",
320
321 """async def foo(a=await b):
322 pass
323 """,
324
325 """async def foo(a:await b):
326 pass
327 """,
328
329 """def baz():
330 async def foo(a=await b):
331 pass
332 """,
333
334 """async def foo(async):
335 pass
336 """,
337
338 """async def foo():
339 def bar():
340 def baz():
341 async = 1
342 """,
343
344 """async def foo():
345 def bar():
346 def baz():
347 pass
348 async = 1
349 """,
350
351 """def foo():
352 async def bar():
353
354 async def baz():
355 pass
356
357 def baz():
358 42
359
360 async = 1
361 """,
362
363 """async def foo():
364 def bar():
365 def baz():
366 pass\nawait foo()
367 """,
368
369 """def foo():
370 def bar():
371 async def baz():
372 pass\nawait foo()
373 """,
374
375 """async def foo(await):
376 pass
377 """,
378
379 """def foo():
380
381 async def bar(): pass
382
383 await a
384 """,
385
386 """def foo():
387 async def bar():
388 pass\nawait a
389 """,
390 """def foo():
391 async for i in arange(2):
392 pass
393 """,
394 """def foo():
395 async with resource:
396 pass
397 """,
398 """async with resource:
399 pass
400 """,
401 """async for i in arange(2):
402 pass
403 """,
404 ]
405
406 for code in samples:
407 with self.subTest(code=code), self.assertRaises(SyntaxError):
408 compile(code, "<test>", "exec")
409
410 def test_badsyntax_2(self):
411 samples = [
412 """def foo():
413 await = 1
414 """,
415
416 """class Bar:
417 def async(): pass
418 """,
419
420 """class Bar:
421 async = 1
422 """,
423
424 """class async:
425 pass
426 """,
427
428 """class await:
429 pass
430 """,
431
432 """import math as await""",
433
434 """def async():
435 pass""",
436
437 """def foo(*, await=1):
438 pass"""
439
440 """async = 1""",
441
442 """print(await=1)"""
443 ]
444
445 for code in samples:
446 with self.subTest(code=code), self.assertRaises(SyntaxError):
447 compile(code, "<test>", "exec")
448
449 def test_badsyntax_3(self):
450 with self.assertRaises(SyntaxError):
451 compile("async = 1", "<test>", "exec")
452
453 def test_badsyntax_4(self):
454 samples = [
455 '''def foo(await):
456 async def foo(): pass
457 async def foo():
458 pass
459 return await + 1
460 ''',
461
462 '''def foo(await):
463 async def foo(): pass
464 async def foo(): pass
465 return await + 1
466 ''',
467
468 '''def foo(await):
469
470 async def foo(): pass
471
472 async def foo(): pass
473
474 return await + 1
475 ''',
476
477 '''def foo(await):
478 """spam"""
479 async def foo(): \
480 pass
481 # 123
482 async def foo(): pass
483 # 456
484 return await + 1
485 ''',
486
487 '''def foo(await):
488 def foo(): pass
489 def foo(): pass
490 async def bar(): return await_
491 await_ = await
492 try:
493 bar().send(None)
494 except StopIteration as ex:
495 return ex.args[0] + 1
496 '''
497 ]
498
499 for code in samples:
500 with self.subTest(code=code), self.assertRaises(SyntaxError):
501 compile(code, "<test>", "exec")
502
503
504 class TokenizerRegrTest(unittest.TestCase):
505
506 def test_oneline_defs(self):
507 buf = []
508 for i in range(500):
509 buf.append('def i{i}(): return {i}'.format(i=i))
510 buf = '\n'.join(buf)
511
512 # Test that 500 consequent, one-line defs is OK
513 ns = {}
514 exec(buf, ns, ns)
515 self.assertEqual(ns['i499'](), 499)
516
517 # Test that 500 consequent, one-line defs *and*
518 # one 'async def' following them is OK
519 buf += '\nasync def foo():\n return'
520 ns = {}
521 exec(buf, ns, ns)
522 self.assertEqual(ns['i499'](), 499)
523 self.assertTrue(inspect.iscoroutinefunction(ns['foo']))
524
525
526 class CoroutineTest(unittest.TestCase):
527
528 def test_gen_1(self):
529 def gen(): yield
530 self.assertNotHasAttr(gen, '__await__')
531
532 def test_func_1(self):
533 async def foo():
534 return 10
535
536 f = foo()
537 self.assertIsInstance(f, types.CoroutineType)
538 self.assertTrue(bool(foo.__code__.co_flags & inspect.CO_COROUTINE))
539 self.assertFalse(bool(foo.__code__.co_flags & inspect.CO_GENERATOR))
540 self.assertTrue(bool(f.cr_code.co_flags & inspect.CO_COROUTINE))
541 self.assertFalse(bool(f.cr_code.co_flags & inspect.CO_GENERATOR))
542 self.assertEqual(run_async(f), ([], 10))
543
544 self.assertEqual(run_async__await__(foo()), ([], 10))
545
546 def bar(): pass
547 self.assertFalse(bool(bar.__code__.co_flags & inspect.CO_COROUTINE))
548
549 def test_func_2(self):
550 async def foo():
551 raise StopIteration
552
553 with self.assertRaisesRegex(
554 RuntimeError, "coroutine raised StopIteration"):
555
556 run_async(foo())
557
558 def test_func_3(self):
559 async def foo():
560 raise StopIteration
561
562 coro = foo()
563 self.assertRegex(repr(coro), '^<coroutine object.* at 0x.*>$')
564 coro.close()
565
566 def test_func_4(self):
567 async def foo():
568 raise StopIteration
569 coro = foo()
570
571 check = lambda: self.assertRaisesRegex(
572 TypeError, "'coroutine' object is not iterable")
573
574 with check():
575 list(coro)
576
577 with check():
578 tuple(coro)
579
580 with check():
581 sum(coro)
582
583 with check():
584 iter(coro)
585
586 with check():
587 for i in coro:
588 pass
589
590 with check():
591 [i for i in coro]
592
593 coro.close()
594
595 def test_func_5(self):
596 @types.coroutine
597 def bar():
598 yield 1
599
600 async def foo():
601 await bar()
602
603 check = lambda: self.assertRaisesRegex(
604 TypeError, "'coroutine' object is not iterable")
605
606 coro = foo()
607 with check():
608 for el in coro:
609 pass
610 coro.close()
611
612 # the following should pass without an error
613 for el in bar():
614 self.assertEqual(el, 1)
615 self.assertEqual([el for el in bar()], [1])
616 self.assertEqual(tuple(bar()), (1,))
617 self.assertEqual(next(iter(bar())), 1)
618
619 def test_func_6(self):
620 @types.coroutine
621 def bar():
622 yield 1
623 yield 2
624
625 async def foo():
626 await bar()
627
628 f = foo()
629 self.assertEqual(f.send(None), 1)
630 self.assertEqual(f.send(None), 2)
631 with self.assertRaises(StopIteration):
632 f.send(None)
633
634 def test_func_7(self):
635 async def bar():
636 return 10
637 coro = bar()
638
639 def foo():
640 yield from coro
641
642 with self.assertRaisesRegex(
643 TypeError,
644 "cannot 'yield from' a coroutine object in "
645 "a non-coroutine generator"):
646 list(foo())
647
648 coro.close()
649
650 def test_func_8(self):
651 @types.coroutine
652 def bar():
653 return (yield from coro)
654
655 async def foo():
656 return 'spam'
657
658 coro = foo()
659 self.assertEqual(run_async(bar()), ([], 'spam'))
660 coro.close()
661
662 def test_func_9(self):
663 async def foo():
664 pass
665
666 with self.assertWarnsRegex(
667 RuntimeWarning,
668 r"coroutine '.*test_func_9.*foo' was never awaited"):
669
670 foo()
671 support.gc_collect()
672
673 with self.assertWarnsRegex(
674 RuntimeWarning,
675 r"coroutine '.*test_func_9.*foo' was never awaited"):
676
677 with self.assertRaises(TypeError):
678 # See bpo-32703.
679 for _ in foo():
680 pass
681
682 support.gc_collect()
683
684 def test_func_10(self):
685 N = 0
686
687 @types.coroutine
688 def gen():
689 nonlocal N
690 try:
691 a = yield
692 yield (a ** 2)
693 except ZeroDivisionError:
694 N += 100
695 raise
696 finally:
697 N += 1
698
699 async def foo():
700 await gen()
701
702 coro = foo()
703 aw = coro.__await__()
704 self.assertIs(aw, iter(aw))
705 next(aw)
706 self.assertEqual(aw.send(10), 100)
707
708 self.assertEqual(N, 0)
709 aw.close()
710 self.assertEqual(N, 1)
711
712 coro = foo()
713 aw = coro.__await__()
714 next(aw)
715 with self.assertRaises(ZeroDivisionError):
716 aw.throw(ZeroDivisionError())
717 self.assertEqual(N, 102)
718
719 coro = foo()
720 aw = coro.__await__()
721 next(aw)
722 with self.assertRaises(ZeroDivisionError):
723 with self.assertWarns(DeprecationWarning):
724 aw.throw(ZeroDivisionError, ZeroDivisionError(), None)
725
726 def test_func_11(self):
727 async def func(): pass
728 coro = func()
729 # Test that PyCoro_Type and _PyCoroWrapper_Type types were properly
730 # initialized
731 self.assertIn('__await__', dir(coro))
732 self.assertIn('__iter__', dir(coro.__await__()))
733 self.assertIn('coroutine_wrapper', repr(coro.__await__()))
734 coro.close() # avoid RuntimeWarning
735
736 def test_func_12(self):
737 async def g():
738 me.send(None)
739 await foo
740 me = g()
741 with self.assertRaisesRegex(ValueError,
742 "coroutine already executing"):
743 me.send(None)
744
745 def test_func_13(self):
746 async def g():
747 pass
748
749 coro = g()
750 with self.assertRaisesRegex(
751 TypeError,
752 "can't send non-None value to a just-started coroutine"):
753 coro.send('spam')
754
755 coro.close()
756
757 def test_func_14(self):
758 @types.coroutine
759 def gen():
760 yield
761 async def coro():
762 try:
763 await gen()
764 except GeneratorExit:
765 await gen()
766 c = coro()
767 c.send(None)
768 with self.assertRaisesRegex(RuntimeError,
769 "coroutine ignored GeneratorExit"):
770 c.close()
771
772 def test_func_15(self):
773 # See http://bugs.python.org/issue25887 for details
774
775 async def spammer():
776 return 'spam'
777 async def reader(coro):
778 return await coro
779
780 spammer_coro = spammer()
781
782 with self.assertRaisesRegex(StopIteration, 'spam'):
783 reader(spammer_coro).send(None)
784
785 with self.assertRaisesRegex(RuntimeError,
786 'cannot reuse already awaited coroutine'):
787 reader(spammer_coro).send(None)
788
789 def test_func_16(self):
790 # See http://bugs.python.org/issue25887 for details
791
792 @types.coroutine
793 def nop():
794 yield
795 async def send():
796 await nop()
797 return 'spam'
798 async def read(coro):
799 await nop()
800 return await coro
801
802 spammer = send()
803
804 reader = read(spammer)
805 reader.send(None)
806 reader.send(None)
807 with self.assertRaisesRegex(Exception, 'ham'):
808 reader.throw(Exception('ham'))
809
810 reader = read(spammer)
811 reader.send(None)
812 with self.assertRaisesRegex(RuntimeError,
813 'cannot reuse already awaited coroutine'):
814 reader.send(None)
815
816 with self.assertRaisesRegex(RuntimeError,
817 'cannot reuse already awaited coroutine'):
818 reader.throw(Exception('wat'))
819
820 def test_func_17(self):
821 # See http://bugs.python.org/issue25887 for details
822
823 async def coroutine():
824 return 'spam'
825
826 coro = coroutine()
827 with self.assertRaisesRegex(StopIteration, 'spam'):
828 coro.send(None)
829
830 with self.assertRaisesRegex(RuntimeError,
831 'cannot reuse already awaited coroutine'):
832 coro.send(None)
833
834 with self.assertRaisesRegex(RuntimeError,
835 'cannot reuse already awaited coroutine'):
836 coro.throw(Exception('wat'))
837
838 # Closing a coroutine shouldn't raise any exception even if it's
839 # already closed/exhausted (similar to generators)
840 coro.close()
841 coro.close()
842
843 def test_func_18(self):
844 # See http://bugs.python.org/issue25887 for details
845
846 async def coroutine():
847 return 'spam'
848
849 coro = coroutine()
850 await_iter = coro.__await__()
851 it = iter(await_iter)
852
853 with self.assertRaisesRegex(StopIteration, 'spam'):
854 it.send(None)
855
856 with self.assertRaisesRegex(RuntimeError,
857 'cannot reuse already awaited coroutine'):
858 it.send(None)
859
860 with self.assertRaisesRegex(RuntimeError,
861 'cannot reuse already awaited coroutine'):
862 # Although the iterator protocol requires iterators to
863 # raise another StopIteration here, we don't want to do
864 # that. In this particular case, the iterator will raise
865 # a RuntimeError, so that 'yield from' and 'await'
866 # expressions will trigger the error, instead of silently
867 # ignoring the call.
868 next(it)
869
870 with self.assertRaisesRegex(RuntimeError,
871 'cannot reuse already awaited coroutine'):
872 it.throw(Exception('wat'))
873
874 with self.assertRaisesRegex(RuntimeError,
875 'cannot reuse already awaited coroutine'):
876 it.throw(Exception('wat'))
877
878 # Closing a coroutine shouldn't raise any exception even if it's
879 # already closed/exhausted (similar to generators)
880 it.close()
881 it.close()
882
883 def test_func_19(self):
884 CHK = 0
885
886 @types.coroutine
887 def foo():
888 nonlocal CHK
889 yield
890 try:
891 yield
892 except GeneratorExit:
893 CHK += 1
894
895 async def coroutine():
896 await foo()
897
898 coro = coroutine()
899
900 coro.send(None)
901 coro.send(None)
902
903 self.assertEqual(CHK, 0)
904 coro.close()
905 self.assertEqual(CHK, 1)
906
907 for _ in range(3):
908 # Closing a coroutine shouldn't raise any exception even if it's
909 # already closed/exhausted (similar to generators)
910 coro.close()
911 self.assertEqual(CHK, 1)
912
913 def test_coro_wrapper_send_tuple(self):
914 async def foo():
915 return (10,)
916
917 result = run_async__await__(foo())
918 self.assertEqual(result, ([], (10,)))
919
920 def test_coro_wrapper_send_stop_iterator(self):
921 async def foo():
922 return StopIteration(10)
923
924 result = run_async__await__(foo())
925 self.assertIsInstance(result[1], StopIteration)
926 self.assertEqual(result[1].value, 10)
927
928 def test_cr_await(self):
929 @types.coroutine
930 def a():
931 self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_RUNNING)
932 self.assertIsNone(coro_b.cr_await)
933 yield
934 self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_RUNNING)
935 self.assertIsNone(coro_b.cr_await)
936
937 async def c():
938 await a()
939
940 async def b():
941 self.assertIsNone(coro_b.cr_await)
942 await c()
943 self.assertIsNone(coro_b.cr_await)
944
945 coro_b = b()
946 self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_CREATED)
947 self.assertIsNone(coro_b.cr_await)
948
949 coro_b.send(None)
950 self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_SUSPENDED)
951 self.assertEqual(coro_b.cr_await.cr_await.gi_code.co_name, 'a')
952
953 with self.assertRaises(StopIteration):
954 coro_b.send(None) # complete coroutine
955 self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_CLOSED)
956 self.assertIsNone(coro_b.cr_await)
957
958 def test_corotype_1(self):
959 ct = types.CoroutineType
960 if not support.MISSING_C_DOCSTRINGS:
961 self.assertIn('into coroutine', ct.send.__doc__)
962 self.assertIn('inside coroutine', ct.close.__doc__)
963 self.assertIn('in coroutine', ct.throw.__doc__)
964 self.assertIn('of the coroutine', ct.__dict__['__name__'].__doc__)
965 self.assertIn('of the coroutine', ct.__dict__['__qualname__'].__doc__)
966 self.assertEqual(ct.__name__, 'coroutine')
967
968 async def f(): pass
969 c = f()
970 self.assertIn('coroutine object', repr(c))
971 c.close()
972
973 def test_await_1(self):
974
975 async def foo():
976 await 1
977 with self.assertRaisesRegex(TypeError, "'int' object can.t be awaited"):
978 run_async(foo())
979
980 def test_await_2(self):
981 async def foo():
982 await []
983 with self.assertRaisesRegex(TypeError, "'list' object can.t be awaited"):
984 run_async(foo())
985
986 def test_await_3(self):
987 async def foo():
988 await AsyncYieldFrom([1, 2, 3])
989
990 self.assertEqual(run_async(foo()), ([1, 2, 3], None))
991 self.assertEqual(run_async__await__(foo()), ([1, 2, 3], None))
992
993 def test_await_4(self):
994 async def bar():
995 return 42
996
997 async def foo():
998 return await bar()
999
1000 self.assertEqual(run_async(foo()), ([], 42))
1001
1002 def test_await_5(self):
1003 class Awaitable:
1004 def __await__(self):
1005 return
1006
1007 async def foo():
1008 return (await Awaitable())
1009
1010 with self.assertRaisesRegex(
1011 TypeError, "__await__.*returned non-iterator of type"):
1012
1013 run_async(foo())
1014
1015 def test_await_6(self):
1016 class Awaitable:
1017 def __await__(self):
1018 return iter([52])
1019
1020 async def foo():
1021 return (await Awaitable())
1022
1023 self.assertEqual(run_async(foo()), ([52], None))
1024
1025 def test_await_7(self):
1026 class Awaitable:
1027 def __await__(self):
1028 yield 42
1029 return 100
1030
1031 async def foo():
1032 return (await Awaitable())
1033
1034 self.assertEqual(run_async(foo()), ([42], 100))
1035
1036 def test_await_8(self):
1037 class Awaitable:
1038 pass
1039
1040 async def foo(): return await Awaitable()
1041
1042 with self.assertRaisesRegex(
1043 TypeError, "'Awaitable' object can't be awaited"):
1044
1045 run_async(foo())
1046
1047 def test_await_9(self):
1048 def wrap():
1049 return bar
1050
1051 async def bar():
1052 return 42
1053
1054 async def foo():
1055 db = {'b': lambda: wrap}
1056
1057 class DB:
1058 b = wrap
1059
1060 return (await bar() + await wrap()() + await db['b']()()() +
1061 await bar() * 1000 + await DB.b()())
1062
1063 async def foo2():
1064 return -await bar()
1065
1066 self.assertEqual(run_async(foo()), ([], 42168))
1067 self.assertEqual(run_async(foo2()), ([], -42))
1068
1069 def test_await_10(self):
1070 async def baz():
1071 return 42
1072
1073 async def bar():
1074 return baz()
1075
1076 async def foo():
1077 return await (await bar())
1078
1079 self.assertEqual(run_async(foo()), ([], 42))
1080
1081 def test_await_11(self):
1082 def ident(val):
1083 return val
1084
1085 async def bar():
1086 return 'spam'
1087
1088 async def foo():
1089 return ident(val=await bar())
1090
1091 async def foo2():
1092 return await bar(), 'ham'
1093
1094 self.assertEqual(run_async(foo2()), ([], ('spam', 'ham')))
1095
1096 def test_await_12(self):
1097 async def coro():
1098 return 'spam'
1099 c = coro()
1100
1101 class Awaitable:
1102 def __await__(self):
1103 return c
1104
1105 async def foo():
1106 return await Awaitable()
1107
1108 with self.assertRaisesRegex(
1109 TypeError, r"__await__\(\) returned a coroutine"):
1110 run_async(foo())
1111
1112 c.close()
1113
1114 def test_await_13(self):
1115 class Awaitable:
1116 def __await__(self):
1117 return self
1118
1119 async def foo():
1120 return await Awaitable()
1121
1122 with self.assertRaisesRegex(
1123 TypeError, "__await__.*returned non-iterator of type"):
1124
1125 run_async(foo())
1126
1127 def test_await_14(self):
1128 class Wrapper:
1129 # Forces the interpreter to use CoroutineType.__await__
1130 def __init__(self, coro):
1131 assert coro.__class__ is types.CoroutineType
1132 self.coro = coro
1133 def __await__(self):
1134 return self.coro.__await__()
1135
1136 class FutureLike:
1137 def __await__(self):
1138 return (yield)
1139
1140 class Marker(Exception):
1141 pass
1142
1143 async def coro1():
1144 try:
1145 return await FutureLike()
1146 except ZeroDivisionError:
1147 raise Marker
1148 async def coro2():
1149 return await Wrapper(coro1())
1150
1151 c = coro2()
1152 c.send(None)
1153 with self.assertRaisesRegex(StopIteration, 'spam'):
1154 c.send('spam')
1155
1156 c = coro2()
1157 c.send(None)
1158 with self.assertRaises(Marker):
1159 c.throw(ZeroDivisionError)
1160
1161 def test_await_15(self):
1162 @types.coroutine
1163 def nop():
1164 yield
1165
1166 async def coroutine():
1167 await nop()
1168
1169 async def waiter(coro):
1170 await coro
1171
1172 coro = coroutine()
1173 coro.send(None)
1174
1175 with self.assertRaisesRegex(RuntimeError,
1176 "coroutine is being awaited already"):
1177 waiter(coro).send(None)
1178
1179 def test_await_16(self):
1180 # See https://bugs.python.org/issue29600 for details.
1181
1182 async def f():
1183 return ValueError()
1184
1185 async def g():
1186 try:
1187 raise KeyError
1188 except KeyError:
1189 return await f()
1190
1191 _, result = run_async(g())
1192 self.assertIsNone(result.__context__)
1193
1194 def test_await_17(self):
1195 # See https://github.com/python/cpython/issues/131666 for details.
1196 class A:
1197 async def __anext__(self):
1198 raise StopAsyncIteration
1199 def __aiter__(self):
1200 return self
1201
1202 with contextlib.closing(anext(A(), "a").__await__()) as anext_awaitable:
1203 self.assertRaises(TypeError, anext_awaitable.close, 1)
1204
1205 def test_with_1(self):
1206 class Manager:
1207 def __init__(self, name):
1208 self.name = name
1209
1210 async def __aenter__(self):
1211 await AsyncYieldFrom(['enter-1-' + self.name,
1212 'enter-2-' + self.name])
1213 return self
1214
1215 async def __aexit__(self, *args):
1216 await AsyncYieldFrom(['exit-1-' + self.name,
1217 'exit-2-' + self.name])
1218
1219 if self.name == 'B':
1220 return True
1221
1222
1223 async def foo():
1224 async with Manager("A") as a, Manager("B") as b:
1225 await AsyncYieldFrom([('managers', a.name, b.name)])
1226 1/0
1227
1228 f = foo()
1229 result, _ = run_async(f)
1230
1231 self.assertEqual(
1232 result, ['enter-1-A', 'enter-2-A', 'enter-1-B', 'enter-2-B',
1233 ('managers', 'A', 'B'),
1234 'exit-1-B', 'exit-2-B', 'exit-1-A', 'exit-2-A']
1235 )
1236
1237 async def foo():
1238 async with Manager("A") as a, Manager("C") as c:
1239 await AsyncYieldFrom([('managers', a.name, c.name)])
1240 1/0
1241
1242 with self.assertRaises(ZeroDivisionError):
1243 run_async(foo())
1244
1245 def test_with_2(self):
1246 class CM:
1247 def __aenter__(self):
1248 pass
1249
1250 body_executed = None
1251 async def foo():
1252 nonlocal body_executed
1253 body_executed = False
1254 async with CM():
1255 body_executed = True
1256
1257 with self.assertRaisesRegex(TypeError, 'asynchronous context manager.*__aexit__'):
1258 run_async(foo())
1259 self.assertIs(body_executed, False)
1260
1261 def test_with_3(self):
1262 class CM:
1263 def __aexit__(self):
1264 pass
1265
1266 body_executed = None
1267 async def foo():
1268 nonlocal body_executed
1269 body_executed = False
1270 async with CM():
1271 body_executed = True
1272
1273 with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
1274 run_async(foo())
1275 self.assertIs(body_executed, False)
1276
1277 def test_with_4(self):
1278 class CM:
1279 pass
1280
1281 body_executed = None
1282 async def foo():
1283 nonlocal body_executed
1284 body_executed = False
1285 async with CM():
1286 body_executed = True
1287
1288 with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
1289 run_async(foo())
1290 self.assertIs(body_executed, False)
1291
1292 def test_with_5(self):
1293 # While this test doesn't make a lot of sense,
1294 # it's a regression test for an early bug with opcodes
1295 # generation
1296
1297 class CM:
1298 async def __aenter__(self):
1299 return self
1300
1301 async def __aexit__(self, *exc):
1302 pass
1303
1304 async def func():
1305 async with CM():
1306 self.assertEqual((1, ), 1)
1307
1308 with self.assertRaises(AssertionError):
1309 run_async(func())
1310
1311 def test_with_6(self):
1312 class CM:
1313 def __aenter__(self):
1314 return 123
1315
1316 def __aexit__(self, *e):
1317 return 456
1318
1319 async def foo():
1320 async with CM():
1321 pass
1322
1323 with self.assertRaisesRegex(
1324 TypeError,
1325 "'async with' received an object from __aenter__ "
1326 "that does not implement __await__: int"):
1327 # it's important that __aexit__ wasn't called
1328 run_async(foo())
1329
1330 def test_with_7(self):
1331 class CM:
1332 async def __aenter__(self):
1333 return self
1334
1335 def __aexit__(self, *e):
1336 return 444
1337
1338 # Exit with exception
1339 async def foo():
1340 async with CM():
1341 1/0
1342
1343 try:
1344 run_async(foo())
1345 except TypeError as exc:
1346 self.assertRegex(
1347 exc.args[0],
1348 "'async with' received an object from __aexit__ "
1349 "that does not implement __await__: int")
1350 self.assertTrue(exc.__context__ is not None)
1351 self.assertTrue(isinstance(exc.__context__, ZeroDivisionError))
1352 else:
1353 self.fail('invalid asynchronous context manager did not fail')
1354
1355
1356 def test_with_8(self):
1357 CNT = 0
1358
1359 class CM:
1360 async def __aenter__(self):
1361 return self
1362
1363 def __aexit__(self, *e):
1364 return 456
1365
1366 # Normal exit
1367 async def foo():
1368 nonlocal CNT
1369 async with CM():
1370 CNT += 1
1371 with self.assertRaisesRegex(
1372 TypeError,
1373 "'async with' received an object from __aexit__ "
1374 "that does not implement __await__: int"):
1375 run_async(foo())
1376 self.assertEqual(CNT, 1)
1377
1378 # Exit with 'break'
1379 async def foo():
1380 nonlocal CNT
1381 for i in range(2):
1382 async with CM():
1383 CNT += 1
1384 break
1385 with self.assertRaisesRegex(
1386 TypeError,
1387 "'async with' received an object from __aexit__ "
1388 "that does not implement __await__: int"):
1389 run_async(foo())
1390 self.assertEqual(CNT, 2)
1391
1392 # Exit with 'continue'
1393 async def foo():
1394 nonlocal CNT
1395 for i in range(2):
1396 async with CM():
1397 CNT += 1
1398 continue
1399 with self.assertRaisesRegex(
1400 TypeError,
1401 "'async with' received an object from __aexit__ "
1402 "that does not implement __await__: int"):
1403 run_async(foo())
1404 self.assertEqual(CNT, 3)
1405
1406 # Exit with 'return'
1407 async def foo():
1408 nonlocal CNT
1409 async with CM():
1410 CNT += 1
1411 return
1412 with self.assertRaisesRegex(
1413 TypeError,
1414 "'async with' received an object from __aexit__ "
1415 "that does not implement __await__: int"):
1416 run_async(foo())
1417 self.assertEqual(CNT, 4)
1418
1419
1420 def test_with_9(self):
1421 CNT = 0
1422
1423 class CM:
1424 async def __aenter__(self):
1425 return self
1426
1427 async def __aexit__(self, *e):
1428 1/0
1429
1430 async def foo():
1431 nonlocal CNT
1432 async with CM():
1433 CNT += 1
1434
1435 with self.assertRaises(ZeroDivisionError):
1436 run_async(foo())
1437
1438 self.assertEqual(CNT, 1)
1439
1440 def test_with_10(self):
1441 CNT = 0
1442
1443 class CM:
1444 async def __aenter__(self):
1445 return self
1446
1447 async def __aexit__(self, *e):
1448 1/0
1449
1450 async def foo():
1451 nonlocal CNT
1452 async with CM():
1453 async with CM():
1454 raise RuntimeError
1455
1456 try:
1457 run_async(foo())
1458 except ZeroDivisionError as exc:
1459 self.assertTrue(exc.__context__ is not None)
1460 self.assertTrue(isinstance(exc.__context__, ZeroDivisionError))
1461 self.assertTrue(isinstance(exc.__context__.__context__,
1462 RuntimeError))
1463 else:
1464 self.fail('exception from __aexit__ did not propagate')
1465
1466 def test_with_11(self):
1467 CNT = 0
1468
1469 class CM:
1470 async def __aenter__(self):
1471 raise NotImplementedError
1472
1473 async def __aexit__(self, *e):
1474 1/0
1475
1476 async def foo():
1477 nonlocal CNT
1478 async with CM():
1479 raise RuntimeError
1480
1481 try:
1482 run_async(foo())
1483 except NotImplementedError as exc:
1484 self.assertTrue(exc.__context__ is None)
1485 else:
1486 self.fail('exception from __aenter__ did not propagate')
1487
1488 def test_with_12(self):
1489 CNT = 0
1490
1491 class CM:
1492 async def __aenter__(self):
1493 return self
1494
1495 async def __aexit__(self, *e):
1496 return True
1497
1498 async def foo():
1499 nonlocal CNT
1500 async with CM() as cm:
1501 self.assertIs(cm.__class__, CM)
1502 raise RuntimeError
1503
1504 run_async(foo())
1505
1506 def test_with_13(self):
1507 CNT = 0
1508
1509 class CM:
1510 async def __aenter__(self):
1511 1/0
1512
1513 async def __aexit__(self, *e):
1514 return True
1515
1516 async def foo():
1517 nonlocal CNT
1518 CNT += 1
1519 async with CM():
1520 CNT += 1000
1521 CNT += 10000
1522
1523 with self.assertRaises(ZeroDivisionError):
1524 run_async(foo())
1525 self.assertEqual(CNT, 1)
1526
1527 def test_for_1(self):
1528 aiter_calls = 0
1529
1530 class AsyncIter:
1531 def __init__(self):
1532 self.i = 0
1533
1534 def __aiter__(self):
1535 nonlocal aiter_calls
1536 aiter_calls += 1
1537 return self
1538
1539 async def __anext__(self):
1540 self.i += 1
1541
1542 if not (self.i % 10):
1543 await AsyncYield(self.i * 10)
1544
1545 if self.i > 100:
1546 raise StopAsyncIteration
1547
1548 return self.i, self.i
1549
1550
1551 buffer = []
1552 async def test1():
1553 async for i1, i2 in AsyncIter():
1554 buffer.append(i1 + i2)
1555
1556 yielded, _ = run_async(test1())
1557 # Make sure that __aiter__ was called only once
1558 self.assertEqual(aiter_calls, 1)
1559 self.assertEqual(yielded, [i * 100 for i in range(1, 11)])
1560 self.assertEqual(buffer, [i*2 for i in range(1, 101)])
1561
1562
1563 buffer = []
1564 async def test2():
1565 nonlocal buffer
1566 async for i in AsyncIter():
1567 buffer.append(i[0])
1568 if i[0] == 20:
1569 break
1570 else:
1571 buffer.append('what?')
1572 buffer.append('end')
1573
1574 yielded, _ = run_async(test2())
1575 # Make sure that __aiter__ was called only once
1576 self.assertEqual(aiter_calls, 2)
1577 self.assertEqual(yielded, [100, 200])
1578 self.assertEqual(buffer, [i for i in range(1, 21)] + ['end'])
1579
1580
1581 buffer = []
1582 async def test3():
1583 nonlocal buffer
1584 async for i in AsyncIter():
1585 if i[0] > 20:
1586 continue
1587 buffer.append(i[0])
1588 else:
1589 buffer.append('what?')
1590 buffer.append('end')
1591
1592 yielded, _ = run_async(test3())
1593 # Make sure that __aiter__ was called only once
1594 self.assertEqual(aiter_calls, 3)
1595 self.assertEqual(yielded, [i * 100 for i in range(1, 11)])
1596 self.assertEqual(buffer, [i for i in range(1, 21)] +
1597 ['what?', 'end'])
1598
1599 def test_for_2(self):
1600 tup = (1, 2, 3)
1601 refs_before = sys.getrefcount(tup)
1602
1603 async def foo():
1604 async for i in tup:
1605 print('never going to happen')
1606
1607 with self.assertRaisesRegex(
1608 TypeError, "async for' requires an object.*__aiter__.*tuple"):
1609
1610 run_async(foo())
1611
1612 self.assertEqual(sys.getrefcount(tup), refs_before)
1613
1614 def test_for_3(self):
1615 class I:
1616 def __aiter__(self):
1617 return self
1618
1619 aiter = I()
1620 refs_before = sys.getrefcount(aiter)
1621
1622 async def foo():
1623 async for i in aiter:
1624 print('never going to happen')
1625
1626 with self.assertRaisesRegex(
1627 TypeError,
1628 r"that does not implement __anext__"):
1629
1630 run_async(foo())
1631
1632 self.assertEqual(sys.getrefcount(aiter), refs_before)
1633
1634 def test_for_4(self):
1635 class I:
1636 def __aiter__(self):
1637 return self
1638
1639 def __anext__(self):
1640 return ()
1641
1642 aiter = I()
1643 refs_before = sys.getrefcount(aiter)
1644
1645 async def foo():
1646 async for i in aiter:
1647 print('never going to happen')
1648
1649 with self.assertRaisesRegex(
1650 TypeError,
1651 "async for' received an invalid object.*__anext__.*tuple"):
1652
1653 run_async(foo())
1654
1655 self.assertEqual(sys.getrefcount(aiter), refs_before)
1656
1657 def test_for_6(self):
1658 I = 0
1659
1660 class Manager:
1661 async def __aenter__(self):
1662 nonlocal I
1663 I += 10000
1664
1665 async def __aexit__(self, *args):
1666 nonlocal I
1667 I += 100000
1668
1669 class Iterable:
1670 def __init__(self):
1671 self.i = 0
1672
1673 def __aiter__(self):
1674 return self
1675
1676 async def __anext__(self):
1677 if self.i > 10:
1678 raise StopAsyncIteration
1679 self.i += 1
1680 return self.i
1681
1682 ##############
1683
1684 manager = Manager()
1685 iterable = Iterable()
1686 mrefs_before = sys.getrefcount(manager)
1687 irefs_before = sys.getrefcount(iterable)
1688
1689 async def main():
1690 nonlocal I
1691
1692 async with manager:
1693 async for i in iterable:
1694 I += 1
1695 I += 1000
1696
1697 with warnings.catch_warnings():
1698 warnings.simplefilter("error")
1699 # Test that __aiter__ that returns an asynchronous iterator
1700 # directly does not throw any warnings.
1701 run_async(main())
1702 self.assertEqual(I, 111011)
1703
1704 self.assertEqual(sys.getrefcount(manager), mrefs_before)
1705 self.assertEqual(sys.getrefcount(iterable), irefs_before)
1706
1707 ##############
1708
1709 async def main():
1710 nonlocal I
1711
1712 async with Manager():
1713 async for i in Iterable():
1714 I += 1
1715 I += 1000
1716
1717 async with Manager():
1718 async for i in Iterable():
1719 I += 1
1720 I += 1000
1721
1722 run_async(main())
1723 self.assertEqual(I, 333033)
1724
1725 ##############
1726
1727 async def main():
1728 nonlocal I
1729
1730 async with Manager():
1731 I += 100
1732 async for i in Iterable():
1733 I += 1
1734 else:
1735 I += 10000000
1736 I += 1000
1737
1738 async with Manager():
1739 I += 100
1740 async for i in Iterable():
1741 I += 1
1742 else:
1743 I += 10000000
1744 I += 1000
1745
1746 run_async(main())
1747 self.assertEqual(I, 20555255)
1748
1749 def test_for_7(self):
1750 CNT = 0
1751 class AI:
1752 def __aiter__(self):
1753 1/0
1754 async def foo():
1755 nonlocal CNT
1756 async for i in AI():
1757 CNT += 1
1758 CNT += 10
1759 with self.assertRaises(ZeroDivisionError):
1760 run_async(foo())
1761 self.assertEqual(CNT, 0)
1762
1763 def test_for_8(self):
1764 CNT = 0
1765 class AI:
1766 def __aiter__(self):
1767 1/0
1768 async def foo():
1769 nonlocal CNT
1770 async for i in AI():
1771 CNT += 1
1772 CNT += 10
1773 with self.assertRaises(ZeroDivisionError):
1774 with warnings.catch_warnings():
1775 warnings.simplefilter("error")
1776 # Test that if __aiter__ raises an exception it propagates
1777 # without any kind of warning.
1778 run_async(foo())
1779 self.assertEqual(CNT, 0)
1780
1781 def test_for_11(self):
1782 class F:
1783 def __aiter__(self):
1784 return self
1785 def __anext__(self):
1786 return self
1787 def __await__(self):
1788 1 / 0
1789
1790 async def main():
1791 async for _ in F():
1792 pass
1793
1794 with self.assertRaisesRegex(TypeError,
1795 'an invalid object from __anext__') as c:
1796 main().send(None)
1797
1798 err = c.exception
1799 self.assertIsInstance(err.__cause__, ZeroDivisionError)
1800
1801 def test_for_tuple(self):
1802 class Done(Exception): pass
1803
1804 class AIter(tuple):
1805 i = 0
1806 def __aiter__(self):
1807 return self
1808 async def __anext__(self):
1809 if self.i >= len(self):
1810 raise StopAsyncIteration
1811 self.i += 1
1812 return self[self.i - 1]
1813
1814 result = []
1815 async def foo():
1816 async for i in AIter([42]):
1817 result.append(i)
1818 raise Done
1819
1820 with self.assertRaises(Done):
1821 foo().send(None)
1822 self.assertEqual(result, [42])
1823
1824 def test_for_stop_iteration(self):
1825 class Done(Exception): pass
1826
1827 class AIter(StopIteration):
1828 i = 0
1829 def __aiter__(self):
1830 return self
1831 async def __anext__(self):
1832 if self.i:
1833 raise StopAsyncIteration
1834 self.i += 1
1835 return self.value
1836
1837 result = []
1838 async def foo():
1839 async for i in AIter(42):
1840 result.append(i)
1841 raise Done
1842
1843 with self.assertRaises(Done):
1844 foo().send(None)
1845 self.assertEqual(result, [42])
1846
1847 def test_comp_1(self):
1848 async def f(i):
1849 return i
1850
1851 async def run_list():
1852 return [await c for c in [f(1), f(41)]]
1853
1854 async def run_set():
1855 return {await c for c in [f(1), f(41)]}
1856
1857 async def run_dict1():
1858 return {await c: 'a' for c in [f(1), f(41)]}
1859
1860 async def run_dict2():
1861 return {i: await c for i, c in enumerate([f(1), f(41)])}
1862
1863 self.assertEqual(run_async(run_list()), ([], [1, 41]))
1864 self.assertEqual(run_async(run_set()), ([], {1, 41}))
1865 self.assertEqual(run_async(run_dict1()), ([], {1: 'a', 41: 'a'}))
1866 self.assertEqual(run_async(run_dict2()), ([], {0: 1, 1: 41}))
1867
1868 def test_comp_2(self):
1869 async def f(i):
1870 return i
1871
1872 async def run_list():
1873 return [s for c in [f(''), f('abc'), f(''), f(['de', 'fg'])]
1874 for s in await c]
1875
1876 self.assertEqual(
1877 run_async(run_list()),
1878 ([], ['a', 'b', 'c', 'de', 'fg']))
1879
1880 async def run_set():
1881 return {d
1882 for c in [f([f([10, 30]),
1883 f([20])])]
1884 for s in await c
1885 for d in await s}
1886
1887 self.assertEqual(
1888 run_async(run_set()),
1889 ([], {10, 20, 30}))
1890
1891 async def run_set2():
1892 return {await s
1893 for c in [f([f(10), f(20)])]
1894 for s in await c}
1895
1896 self.assertEqual(
1897 run_async(run_set2()),
1898 ([], {10, 20}))
1899
1900 def test_comp_3(self):
1901 async def f(it):
1902 for i in it:
1903 yield i
1904
1905 async def run_list():
1906 return [i + 1 async for i in f([10, 20])]
1907 self.assertEqual(
1908 run_async(run_list()),
1909 ([], [11, 21]))
1910
1911 async def run_set():
1912 return {i + 1 async for i in f([10, 20])}
1913 self.assertEqual(
1914 run_async(run_set()),
1915 ([], {11, 21}))
1916
1917 async def run_dict():
1918 return {i + 1: i + 2 async for i in f([10, 20])}
1919 self.assertEqual(
1920 run_async(run_dict()),
1921 ([], {11: 12, 21: 22}))
1922
1923 async def run_gen():
1924 gen = (i + 1 async for i in f([10, 20]))
1925 return [g + 100 async for g in gen]
1926 self.assertEqual(
1927 run_async(run_gen()),
1928 ([], [111, 121]))
1929
1930 def test_comp_4(self):
1931 async def f(it):
1932 for i in it:
1933 yield i
1934
1935 async def run_list():
1936 return [i + 1 async for i in f([10, 20]) if i > 10]
1937 self.assertEqual(
1938 run_async(run_list()),
1939 ([], [21]))
1940
1941 async def run_set():
1942 return {i + 1 async for i in f([10, 20]) if i > 10}
1943 self.assertEqual(
1944 run_async(run_set()),
1945 ([], {21}))
1946
1947 async def run_dict():
1948 return {i + 1: i + 2 async for i in f([10, 20]) if i > 10}
1949 self.assertEqual(
1950 run_async(run_dict()),
1951 ([], {21: 22}))
1952
1953 async def run_gen():
1954 gen = (i + 1 async for i in f([10, 20]) if i > 10)
1955 return [g + 100 async for g in gen]
1956 self.assertEqual(
1957 run_async(run_gen()),
1958 ([], [121]))
1959
1960 def test_comp_4_2(self):
1961 async def f(it):
1962 for i in it:
1963 yield i
1964
1965 async def run_list():
1966 return [i + 10 async for i in f(range(5)) if 0 < i < 4]
1967 self.assertEqual(
1968 run_async(run_list()),
1969 ([], [11, 12, 13]))
1970
1971 async def run_set():
1972 return {i + 10 async for i in f(range(5)) if 0 < i < 4}
1973 self.assertEqual(
1974 run_async(run_set()),
1975 ([], {11, 12, 13}))
1976
1977 async def run_dict():
1978 return {i + 10: i + 100 async for i in f(range(5)) if 0 < i < 4}
1979 self.assertEqual(
1980 run_async(run_dict()),
1981 ([], {11: 101, 12: 102, 13: 103}))
1982
1983 async def run_gen():
1984 gen = (i + 10 async for i in f(range(5)) if 0 < i < 4)
1985 return [g + 100 async for g in gen]
1986 self.assertEqual(
1987 run_async(run_gen()),
1988 ([], [111, 112, 113]))
1989
1990 def test_comp_5(self):
1991 async def f(it):
1992 for i in it:
1993 yield i
1994
1995 async def run_list():
1996 return [i + 1 for pair in ([10, 20], [30, 40]) if pair[0] > 10
1997 async for i in f(pair) if i > 30]
1998 self.assertEqual(
1999 run_async(run_list()),
2000 ([], [41]))
2001
2002 def test_comp_6(self):
2003 async def f(it):
2004 for i in it:
2005 yield i
2006
2007 async def run_list():
2008 return [i + 1 async for seq in f([(10, 20), (30,)])
2009 for i in seq]
2010
2011 self.assertEqual(
2012 run_async(run_list()),
2013 ([], [11, 21, 31]))
2014
2015 def test_comp_7(self):
2016 async def f():
2017 yield 1
2018 yield 2
2019 raise Exception('aaa')
2020
2021 async def run_list():
2022 return [i async for i in f()]
2023
2024 with self.assertRaisesRegex(Exception, 'aaa'):
2025 run_async(run_list())
2026
2027 def test_comp_8(self):
2028 async def f():
2029 return [i for i in [1, 2, 3]]
2030
2031 self.assertEqual(
2032 run_async(f()),
2033 ([], [1, 2, 3]))
2034
2035 def test_comp_9(self):
2036 async def gen():
2037 yield 1
2038 yield 2
2039 async def f():
2040 l = [i async for i in gen()]
2041 return [i for i in l]
2042
2043 self.assertEqual(
2044 run_async(f()),
2045 ([], [1, 2]))
2046
2047 def test_comp_10(self):
2048 async def f():
2049 xx = {i for i in [1, 2, 3]}
2050 return {x: x for x in xx}
2051
2052 self.assertEqual(
2053 run_async(f()),
2054 ([], {1: 1, 2: 2, 3: 3}))
2055
2056 def test_nested_comp(self):
2057 async def run_list_inside_list():
2058 return [[i + j async for i in asynciter([1, 2])] for j in [10, 20]]
2059 self.assertEqual(
2060 run_async(run_list_inside_list()),
2061 ([], [[11, 12], [21, 22]]))
2062
2063 async def run_set_inside_list():
2064 return [{i + j async for i in asynciter([1, 2])} for j in [10, 20]]
2065 self.assertEqual(
2066 run_async(run_set_inside_list()),
2067 ([], [{11, 12}, {21, 22}]))
2068
2069 async def run_list_inside_set():
2070 return {sum([i async for i in asynciter(range(j))]) for j in [3, 5]}
2071 self.assertEqual(
2072 run_async(run_list_inside_set()),
2073 ([], {3, 10}))
2074
2075 async def run_dict_inside_dict():
2076 return {j: {i: i + j async for i in asynciter([1, 2])} for j in [10, 20]}
2077 self.assertEqual(
2078 run_async(run_dict_inside_dict()),
2079 ([], {10: {1: 11, 2: 12}, 20: {1: 21, 2: 22}}))
2080
2081 async def run_list_inside_gen():
2082 gen = ([i + j async for i in asynciter([1, 2])] for j in [10, 20])
2083 return [x async for x in gen]
2084 self.assertEqual(
2085 run_async(run_list_inside_gen()),
2086 ([], [[11, 12], [21, 22]]))
2087
2088 async def run_gen_inside_list():
2089 gens = [(i async for i in asynciter(range(j))) for j in [3, 5]]
2090 return [x for g in gens async for x in g]
2091 self.assertEqual(
2092 run_async(run_gen_inside_list()),
2093 ([], [0, 1, 2, 0, 1, 2, 3, 4]))
2094
2095 async def run_gen_inside_gen():
2096 gens = ((i async for i in asynciter(range(j))) for j in [3, 5])
2097 return [x for g in gens async for x in g]
2098 self.assertEqual(
2099 run_async(run_gen_inside_gen()),
2100 ([], [0, 1, 2, 0, 1, 2, 3, 4]))
2101
2102 async def run_list_inside_list_inside_list():
2103 return [[[i + j + k async for i in asynciter([1, 2])]
2104 for j in [10, 20]]
2105 for k in [100, 200]]
2106 self.assertEqual(
2107 run_async(run_list_inside_list_inside_list()),
2108 ([], [[[111, 112], [121, 122]], [[211, 212], [221, 222]]]))
2109
2110 def test_copy(self):
2111 async def func(): pass
2112 coro = func()
2113 with self.assertRaises(TypeError):
2114 copy.copy(coro)
2115
2116 aw = coro.__await__()
2117 try:
2118 with self.assertRaises(TypeError):
2119 copy.copy(aw)
2120 finally:
2121 aw.close()
2122
2123 def test_pickle(self):
2124 async def func(): pass
2125 coro = func()
2126 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2127 with self.assertRaises((TypeError, pickle.PicklingError)):
2128 pickle.dumps(coro, proto)
2129
2130 aw = coro.__await__()
2131 try:
2132 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2133 with self.assertRaises((TypeError, pickle.PicklingError)):
2134 pickle.dumps(aw, proto)
2135 finally:
2136 aw.close()
2137
2138 def test_fatal_coro_warning(self):
2139 # Issue 27811
2140 async def func(): pass
2141 with warnings.catch_warnings(), \
2142 support.catch_unraisable_exception() as cm:
2143 warnings.filterwarnings("error")
2144 coro = func()
2145 # only store repr() to avoid keeping the coroutine alive
2146 coro_repr = repr(coro)
2147 coro = None
2148 support.gc_collect()
2149
2150 self.assertEqual(cm.unraisable.err_msg,
2151 f"Exception ignored while finalizing "
2152 f"coroutine {coro_repr}")
2153 self.assertIn("was never awaited", str(cm.unraisable.exc_value))
2154
2155 def test_for_assign_raising_stop_async_iteration(self):
2156 class BadTarget:
2157 def __setitem__(self, key, value):
2158 raise StopAsyncIteration(42)
2159 tgt = BadTarget()
2160 async def source():
2161 yield 10
2162
2163 async def run_for():
2164 with self.assertRaises(StopAsyncIteration) as cm:
2165 async for tgt[0] in source():
2166 pass
2167 self.assertEqual(cm.exception.args, (42,))
2168 return 'end'
2169 self.assertEqual(run_async(run_for()), ([], 'end'))
2170
2171 async def run_list():
2172 with self.assertRaises(StopAsyncIteration) as cm:
2173 return [0 async for tgt[0] in source()]
2174 self.assertEqual(cm.exception.args, (42,))
2175 return 'end'
2176 self.assertEqual(run_async(run_list()), ([], 'end'))
2177
2178 async def run_gen():
2179 gen = (0 async for tgt[0] in source())
2180 a = gen.asend(None)
2181 with self.assertRaises(RuntimeError) as cm:
2182 await a
2183 self.assertIsInstance(cm.exception.__cause__, StopAsyncIteration)
2184 self.assertEqual(cm.exception.__cause__.args, (42,))
2185 return 'end'
2186 self.assertEqual(run_async(run_gen()), ([], 'end'))
2187
2188 def test_for_assign_raising_stop_async_iteration_2(self):
2189 class BadIterable:
2190 def __iter__(self):
2191 raise StopAsyncIteration(42)
2192 async def badpairs():
2193 yield BadIterable()
2194
2195 async def run_for():
2196 with self.assertRaises(StopAsyncIteration) as cm:
2197 async for i, j in badpairs():
2198 pass
2199 self.assertEqual(cm.exception.args, (42,))
2200 return 'end'
2201 self.assertEqual(run_async(run_for()), ([], 'end'))
2202
2203 async def run_list():
2204 with self.assertRaises(StopAsyncIteration) as cm:
2205 return [0 async for i, j in badpairs()]
2206 self.assertEqual(cm.exception.args, (42,))
2207 return 'end'
2208 self.assertEqual(run_async(run_list()), ([], 'end'))
2209
2210 async def run_gen():
2211 gen = (0 async for i, j in badpairs())
2212 a = gen.asend(None)
2213 with self.assertRaises(RuntimeError) as cm:
2214 await a
2215 self.assertIsInstance(cm.exception.__cause__, StopAsyncIteration)
2216 self.assertEqual(cm.exception.__cause__.args, (42,))
2217 return 'end'
2218 self.assertEqual(run_async(run_gen()), ([], 'end'))
2219
2220 def test_bpo_45813_1(self):
2221 'This would crash the interpreter in 3.11a2'
2222 async def f():
2223 pass
2224 with self.assertWarns(RuntimeWarning):
2225 frame = f().cr_frame
2226 frame.clear()
2227
2228 def test_bpo_45813_2(self):
2229 'This would crash the interpreter in 3.11a2'
2230 async def f():
2231 pass
2232 gen = f()
2233 with self.assertWarns(RuntimeWarning):
2234 gen.cr_frame.clear()
2235 gen.close()
2236
2237 def test_cr_frame_after_close(self):
2238 async def f():
2239 pass
2240 gen = f()
2241 self.assertIsNotNone(gen.cr_frame)
2242 gen.close()
2243 self.assertIsNone(gen.cr_frame)
2244
2245 def test_stack_in_coroutine_throw(self):
2246 # Regression test for https://github.com/python/cpython/issues/93592
2247 async def a():
2248 return await b()
2249
2250 async def b():
2251 return await c()
2252
2253 @types.coroutine
2254 def c():
2255 try:
2256 # traceback.print_stack()
2257 yield len(traceback.extract_stack())
2258 except ZeroDivisionError:
2259 # traceback.print_stack()
2260 yield len(traceback.extract_stack())
2261
2262 coro = a()
2263 len_send = coro.send(None)
2264 len_throw = coro.throw(ZeroDivisionError)
2265 # before fixing, visible stack from throw would be shorter than from send.
2266 self.assertEqual(len_send, len_throw)
2267
2268 def test_call_aiter_once_in_comprehension(self):
2269
2270 class AsyncIterator:
2271
2272 def __init__(self):
2273 self.val = 0
2274
2275 async def __anext__(self):
2276 if self.val == 2:
2277 raise StopAsyncIteration
2278 self.val += 1
2279 return self.val
2280
2281 # No __aiter__ method
2282
2283 class C:
2284
2285 def __aiter__(self):
2286 return AsyncIterator()
2287
2288 async def run_listcomp():
2289 return [i async for i in C()]
2290
2291 async def run_asyncgen():
2292 ag = (i async for i in C())
2293 return [i async for i in ag]
2294
2295 self.assertEqual(run_async(run_listcomp()), ([], [1, 2]))
2296 self.assertEqual(run_async(run_asyncgen()), ([], [1, 2]))
2297
2298
2299 @unittest.skipIf(
2300 support.is_emscripten or support.is_wasi,
2301 "asyncio does not work under Emscripten/WASI yet."
2302 )
2303 class CoroAsyncIOCompatTest(unittest.TestCase):
2304
2305 def test_asyncio_1(self):
2306 # asyncio cannot be imported when Python is compiled without thread
2307 # support
2308 asyncio = import_helper.import_module('asyncio')
2309
2310 class MyException(Exception):
2311 pass
2312
2313 buffer = []
2314
2315 class CM:
2316 async def __aenter__(self):
2317 buffer.append(1)
2318 await asyncio.sleep(0.01)
2319 buffer.append(2)
2320 return self
2321
2322 async def __aexit__(self, exc_type, exc_val, exc_tb):
2323 await asyncio.sleep(0.01)
2324 buffer.append(exc_type.__name__)
2325
2326 async def f():
2327 async with CM():
2328 await asyncio.sleep(0.01)
2329 raise MyException
2330 buffer.append('unreachable')
2331
2332 loop = asyncio.new_event_loop()
2333 asyncio.set_event_loop(loop)
2334 try:
2335 loop.run_until_complete(f())
2336 except MyException:
2337 pass
2338 finally:
2339 loop.close()
2340 asyncio.events._set_event_loop_policy(None)
2341
2342 self.assertEqual(buffer, [1, 2, 'MyException'])
2343
2344
2345 class OriginTrackingTest(unittest.TestCase):
2346 def here(self):
2347 info = inspect.getframeinfo(inspect.currentframe().f_back)
2348 return (info.filename, info.lineno)
2349
2350 def test_origin_tracking(self):
2351 orig_depth = sys.get_coroutine_origin_tracking_depth()
2352 try:
2353 async def corofn():
2354 pass
2355
2356 sys.set_coroutine_origin_tracking_depth(0)
2357 self.assertEqual(sys.get_coroutine_origin_tracking_depth(), 0)
2358
2359 with contextlib.closing(corofn()) as coro:
2360 self.assertIsNone(coro.cr_origin)
2361
2362 sys.set_coroutine_origin_tracking_depth(1)
2363 self.assertEqual(sys.get_coroutine_origin_tracking_depth(), 1)
2364
2365 fname, lineno = self.here()
2366 with contextlib.closing(corofn()) as coro:
2367 self.assertEqual(coro.cr_origin,
2368 ((fname, lineno + 1, "test_origin_tracking"),))
2369
2370 sys.set_coroutine_origin_tracking_depth(2)
2371 self.assertEqual(sys.get_coroutine_origin_tracking_depth(), 2)
2372
2373 def nested():
2374 return (self.here(), corofn())
2375 fname, lineno = self.here()
2376 ((nested_fname, nested_lineno), coro) = nested()
2377 with contextlib.closing(coro):
2378 self.assertEqual(coro.cr_origin,
2379 ((nested_fname, nested_lineno, "nested"),
2380 (fname, lineno + 1, "test_origin_tracking")))
2381
2382 # Check we handle running out of frames correctly
2383 sys.set_coroutine_origin_tracking_depth(1000)
2384 with contextlib.closing(corofn()) as coro:
2385 self.assertTrue(2 < len(coro.cr_origin) < 1000)
2386
2387 # We can't set depth negative
2388 with self.assertRaises(ValueError):
2389 sys.set_coroutine_origin_tracking_depth(-1)
2390 # And trying leaves it unchanged
2391 self.assertEqual(sys.get_coroutine_origin_tracking_depth(), 1000)
2392
2393 finally:
2394 sys.set_coroutine_origin_tracking_depth(orig_depth)
2395
2396 def test_origin_tracking_warning(self):
2397 async def corofn():
2398 pass
2399
2400 a1_filename, a1_lineno = self.here()
2401 def a1():
2402 return corofn() # comment in a1
2403 a1_lineno += 2
2404
2405 a2_filename, a2_lineno = self.here()
2406 def a2():
2407 return a1() # comment in a2
2408 a2_lineno += 2
2409
2410 def check(depth, msg):
2411 sys.set_coroutine_origin_tracking_depth(depth)
2412 with self.assertWarns(RuntimeWarning) as cm:
2413 a2()
2414 support.gc_collect()
2415 self.assertEqual(msg, str(cm.warning))
2416
2417 orig_depth = sys.get_coroutine_origin_tracking_depth()
2418 try:
2419 check(0, f"coroutine '{corofn.__qualname__}' was never awaited")
2420 check(1, "".join([
2421 f"coroutine '{corofn.__qualname__}' was never awaited\n",
2422 "Coroutine created at (most recent call last)\n",
2423 f' File "{a1_filename}", line {a1_lineno}, in a1\n',
2424 " return corofn() # comment in a1",
2425 ]))
2426 check(2, "".join([
2427 f"coroutine '{corofn.__qualname__}' was never awaited\n",
2428 "Coroutine created at (most recent call last)\n",
2429 f' File "{a2_filename}", line {a2_lineno}, in a2\n',
2430 " return a1() # comment in a2\n",
2431 f' File "{a1_filename}", line {a1_lineno}, in a1\n',
2432 " return corofn() # comment in a1",
2433 ]))
2434
2435 finally:
2436 sys.set_coroutine_origin_tracking_depth(orig_depth)
2437
2438 def test_unawaited_warning_when_module_broken(self):
2439 # Make sure we don't blow up too bad if
2440 # warnings._warn_unawaited_coroutine is broken somehow (e.g. because
2441 # of shutdown problems)
2442 async def corofn():
2443 pass
2444
2445 orig_wuc = warnings._warn_unawaited_coroutine
2446 try:
2447 warnings._warn_unawaited_coroutine = lambda coro: 1/0
2448 with support.catch_unraisable_exception() as cm, \
2449 warnings_helper.check_warnings(
2450 (r'coroutine .* was never awaited',
2451 RuntimeWarning)):
2452 # only store repr() to avoid keeping the coroutine alive
2453 coro = corofn()
2454 coro_repr = repr(coro)
2455
2456 # clear reference to the coroutine without awaiting for it
2457 del coro
2458 support.gc_collect()
2459
2460 self.assertEqual(cm.unraisable.err_msg,
2461 f"Exception ignored while finalizing "
2462 f"coroutine {coro_repr}")
2463 self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError)
2464
2465 del warnings._warn_unawaited_coroutine
2466 with warnings_helper.check_warnings(
2467 (r'coroutine .* was never awaited', RuntimeWarning)):
2468 corofn()
2469 support.gc_collect()
2470
2471 finally:
2472 warnings._warn_unawaited_coroutine = orig_wuc
2473
2474
2475 class UnawaitedWarningDuringShutdownTest(unittest.TestCase):
2476 # https://bugs.python.org/issue32591#msg310726
2477 def test_unawaited_warning_during_shutdown(self):
2478 code = ("import asyncio\n"
2479 "async def f(): pass\n"
2480 "async def t(): asyncio.gather(f())\n"
2481 "asyncio.run(t())\n")
2482 assert_python_ok("-c", code)
2483
2484 code = ("import sys\n"
2485 "async def f(): pass\n"
2486 "sys.coro = f()\n")
2487 assert_python_ok("-c", code)
2488
2489 code = ("import sys\n"
2490 "async def f(): pass\n"
2491 "sys.corocycle = [f()]\n"
2492 "sys.corocycle.append(sys.corocycle)\n")
2493 assert_python_ok("-c", code)
2494
2495
2496 @support.cpython_only
2497 @unittest.skipIf(_testcapi is None, "requires _testcapi")
2498 class CAPITest(unittest.TestCase):
2499
2500 def test_tp_await_1(self):
2501 from _testcapi import awaitType as at
2502
2503 async def foo():
2504 future = at(iter([1]))
2505 return (await future)
2506
2507 self.assertEqual(foo().send(None), 1)
2508
2509 def test_tp_await_2(self):
2510 # Test tp_await to __await__ mapping
2511 from _testcapi import awaitType as at
2512 future = at(iter([1]))
2513 self.assertEqual(next(future.__await__()), 1)
2514
2515 def test_tp_await_3(self):
2516 from _testcapi import awaitType as at
2517
2518 async def foo():
2519 future = at(1)
2520 return (await future)
2521
2522 with self.assertRaisesRegex(
2523 TypeError, "__await__.*returned non-iterator of type 'int'"):
2524 self.assertEqual(foo().send(None), 1)
2525
2526
2527 if __name__=="__main__":
2528 unittest.main()