1
+ import asyncio
1
2
import inspect
2
3
import types
3
4
import unittest
4
5
import contextlib
5
6
6
7
from test .support .import_helper import import_module
7
- from test .support import gc_collect , requires_working_socket
8
- asyncio = import_module ("asyncio" )
8
+ from test .support import gc_collect , requires_working_socket , async_yield as _async_yield
9
9
10
10
11
- requires_working_socket (module = True )
12
-
13
11
_no_default = object ()
14
12
15
13
16
14
class AwaitException (Exception ):
17
15
pass
18
16
19
17
20
- @types .coroutine
21
- def awaitable (* , throw = False ):
18
+ async def awaitable (* , throw = False ):
22
19
if throw :
23
- yield ( 'throw' ,)
20
+ await _async_yield (( 'throw' ,) )
24
21
else :
25
- yield ( 'result' ,)
22
+ await _async_yield (( 'result' ,) )
26
23
27
24
28
25
def run_until_complete (coro ):
@@ -398,12 +395,6 @@ async def gen():
398
395
an .send (None )
399
396
400
397
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
398
class MyExc (Exception ):
408
399
pass
409
400
@@ -431,11 +422,6 @@ async def agenfn():
431
422
gen2 .send (None )
432
423
433
424
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
425
440
426
class MyExc (Exception ):
441
427
pass
@@ -464,12 +450,6 @@ async def agenfn():
464
450
gen2 .send (None )
465
451
466
452
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
453
class MyExc (Exception ):
474
454
pass
475
455
@@ -502,11 +482,6 @@ async def agenfn():
502
482
gen2 .send (None )
503
483
504
484
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
485
511
486
class MyExc (Exception ):
512
487
pass
@@ -572,12 +547,6 @@ async def gen():
572
547
aclose .close ()
573
548
574
549
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
550
async def agenfn ():
582
551
try :
583
552
await _async_yield (None )
@@ -593,11 +562,6 @@ async def agenfn():
593
562
gen .close ()
594
563
595
564
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
565
602
566
class MyExc (Exception ):
603
567
pass
@@ -620,6 +584,7 @@ async def agenfn():
620
584
gen .close ()
621
585
622
586
587
+ @requires_working_socket ()
623
588
class AsyncGenAsyncioTest (unittest .TestCase ):
624
589
625
590
def setUp (self ):
@@ -710,7 +675,6 @@ async def __anext__(self):
710
675
self .check_async_iterator_anext (MyAsyncIter )
711
676
712
677
def test_python_async_iterator_types_coroutine_anext (self ):
713
- import types
714
678
class MyAsyncIterWithTypesCoro :
715
679
"""Asynchronously yield 1, then 2."""
716
680
def __init__ (self ):
@@ -852,10 +816,6 @@ async def do_test():
852
816
self .assertEqual (result , "completed" )
853
817
854
818
def test_anext_iter (self ):
855
- @types .coroutine
856
- def _async_yield (v ):
857
- return (yield v )
858
-
859
819
class MyError (Exception ):
860
820
pass
861
821
@@ -897,16 +857,15 @@ def test3(anext):
897
857
self .assertEqual (g .send (None ), 1 )
898
858
899
859
def test4 (anext ):
900
- @types .coroutine
901
- def _async_yield (v ):
902
- yield v * 10
903
- return (yield (v * 10 + 1 ))
860
+ async def yield_twice (v ):
861
+ await _async_yield (v * 10 )
862
+ return await _async_yield (v * 10 + 1 )
904
863
905
864
async def agenfn ():
906
865
try :
907
- await _async_yield (1 )
866
+ await yield_twice (1 )
908
867
except MyError :
909
- await _async_yield (2 )
868
+ await yield_twice (2 )
910
869
return
911
870
yield
912
871
@@ -918,14 +877,13 @@ async def agenfn():
918
877
g .throw (MyError ('val' ))
919
878
920
879
def test5 (anext ):
921
- @types .coroutine
922
- def _async_yield (v ):
923
- yield v * 10
924
- return (yield (v * 10 + 1 ))
880
+ async def yield_twice (v ):
881
+ await _async_yield (v * 10 )
882
+ return await _async_yield (v * 10 + 1 )
925
883
926
884
async def agenfn ():
927
885
try :
928
- await _async_yield (1 )
886
+ await yield_twice (1 )
929
887
except MyError :
930
888
return
931
889
yield 'aaa'
@@ -937,13 +895,12 @@ async def agenfn():
937
895
g .throw (MyError ())
938
896
939
897
def test6 (anext ):
940
- @types .coroutine
941
- def _async_yield (v ):
942
- yield v * 10
943
- return (yield (v * 10 + 1 ))
898
+ async def yield_twice (v ):
899
+ await _async_yield (v * 10 )
900
+ return await _async_yield (v * 10 + 1 )
944
901
945
902
async def agenfn ():
946
- await _async_yield (1 )
903
+ await yield_twice (1 )
947
904
yield 'aaa'
948
905
949
906
agen = agenfn ()
@@ -2010,10 +1967,6 @@ class MyException(Exception):
2010
1967
gc_collect () # does not warn unawaited
2011
1968
2012
1969
def test_asend_send_already_running (self ):
2013
- @types .coroutine
2014
- def _async_yield (v ):
2015
- return (yield v )
2016
-
2017
1970
async def agenfn ():
2018
1971
while True :
2019
1972
await _async_yield (1 )
@@ -2034,10 +1987,6 @@ async def agenfn():
2034
1987
2035
1988
2036
1989
def test_athrow_send_already_running (self ):
2037
- @types .coroutine
2038
- def _async_yield (v ):
2039
- return (yield v )
2040
-
2041
1990
async def agenfn ():
2042
1991
while True :
2043
1992
await _async_yield (1 )
0 commit comments