1+ import asyncio
12import inspect
23import types
34import unittest
45import contextlib
56
67from 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
99
1010
11- requires_working_socket (module = True )
12-
1311_no_default = object ()
1412
1513
1614class AwaitException (Exception ):
1715 pass
1816
1917
20- @types .coroutine
21- def awaitable (* , throw = False ):
18+ async def awaitable (* , throw = False ):
2219 if throw :
23- yield ( 'throw' ,)
20+ await _async_yield (( 'throw' ,) )
2421 else :
25- yield ( 'result' ,)
22+ await _async_yield (( 'result' ,) )
2623
2724
2825def run_until_complete (coro ):
@@ -398,12 +395,6 @@ async def gen():
398395 an .send (None )
399396
400397 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-
407398 class MyExc (Exception ):
408399 pass
409400
@@ -431,11 +422,6 @@ async def agenfn():
431422 gen2 .send (None )
432423
433424 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 )
439425
440426 class MyExc (Exception ):
441427 pass
@@ -464,12 +450,6 @@ async def agenfn():
464450 gen2 .send (None )
465451
466452 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-
473453 class MyExc (Exception ):
474454 pass
475455
@@ -502,11 +482,6 @@ async def agenfn():
502482 gen2 .send (None )
503483
504484 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 )
510485
511486 class MyExc (Exception ):
512487 pass
@@ -572,12 +547,6 @@ async def gen():
572547 aclose .close ()
573548
574549 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-
581550 async def agenfn ():
582551 try :
583552 await _async_yield (None )
@@ -593,11 +562,6 @@ async def agenfn():
593562 gen .close ()
594563
595564 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 )
601565
602566 class MyExc (Exception ):
603567 pass
@@ -647,6 +611,7 @@ async def agenfn():
647611 self .assertEqual (cm .exception .value , 2 )
648612
649613
614+ @requires_working_socket ()
650615class AsyncGenAsyncioTest (unittest .TestCase ):
651616
652617 def setUp (self ):
@@ -737,7 +702,6 @@ async def __anext__(self):
737702 self .check_async_iterator_anext (MyAsyncIter )
738703
739704 def test_python_async_iterator_types_coroutine_anext (self ):
740- import types
741705 class MyAsyncIterWithTypesCoro :
742706 """Asynchronously yield 1, then 2."""
743707 def __init__ (self ):
@@ -1037,10 +1001,6 @@ async def do_test():
10371001 self .assertEqual (result , "completed" )
10381002
10391003 def test_anext_iter (self ):
1040- @types .coroutine
1041- def _async_yield (v ):
1042- return (yield v )
1043-
10441004 class MyError (Exception ):
10451005 pass
10461006
@@ -1082,16 +1042,15 @@ def test3(anext):
10821042 self .assertEqual (g .send (None ), 1 )
10831043
10841044 def test4 (anext ):
1085- @types .coroutine
1086- def _async_yield (v ):
1087- yield v * 10
1088- return (yield (v * 10 + 1 ))
1045+ async def yield_twice (v ):
1046+ await _async_yield (v * 10 )
1047+ return await _async_yield (v * 10 + 1 )
10891048
10901049 async def agenfn ():
10911050 try :
1092- await _async_yield (1 )
1051+ await yield_twice (1 )
10931052 except MyError :
1094- await _async_yield (2 )
1053+ await yield_twice (2 )
10951054 return
10961055 yield
10971056
@@ -1103,14 +1062,13 @@ async def agenfn():
11031062 g .throw (MyError ('val' ))
11041063
11051064 def test5 (anext ):
1106- @types .coroutine
1107- def _async_yield (v ):
1108- yield v * 10
1109- return (yield (v * 10 + 1 ))
1065+ async def yield_twice (v ):
1066+ await _async_yield (v * 10 )
1067+ return await _async_yield (v * 10 + 1 )
11101068
11111069 async def agenfn ():
11121070 try :
1113- await _async_yield (1 )
1071+ await yield_twice (1 )
11141072 except MyError :
11151073 return
11161074 yield 'aaa'
@@ -1122,13 +1080,12 @@ async def agenfn():
11221080 g .throw (MyError ())
11231081
11241082 def test6 (anext ):
1125- @types .coroutine
1126- def _async_yield (v ):
1127- yield v * 10
1128- return (yield (v * 10 + 1 ))
1083+ async def yield_twice (v ):
1084+ await _async_yield (v * 10 )
1085+ return await _async_yield (v * 10 + 1 )
11291086
11301087 async def agenfn ():
1131- await _async_yield (1 )
1088+ await yield_twice (1 )
11321089 yield 'aaa'
11331090
11341091 agen = agenfn ()
@@ -2276,10 +2233,6 @@ class MyException(Exception):
22762233 gc_collect () # does not warn unawaited
22772234
22782235 def test_asend_send_already_running (self ):
2279- @types .coroutine
2280- def _async_yield (v ):
2281- return (yield v )
2282-
22832236 async def agenfn ():
22842237 while True :
22852238 await _async_yield (1 )
@@ -2300,10 +2253,6 @@ async def agenfn():
23002253
23012254
23022255 def test_athrow_send_already_running (self ):
2303- @types .coroutine
2304- def _async_yield (v ):
2305- return (yield v )
2306-
23072256 async def agenfn ():
23082257 while True :
23092258 await _async_yield (1 )
0 commit comments