@@ -413,17 +413,16 @@ class _AsyncioTests:
413
413
def test_stdin_not_inheritable (self ):
414
414
# asyncio issue #209: stdin must not be inheritable, otherwise
415
415
# the Process.communicate() hangs
416
- @asyncio .coroutine
417
- def len_message (message ):
416
+ async def len_message (message ):
418
417
code = 'import sys; data = sys.stdin.read(); print(len(data))'
419
- proc = yield from asyncio .create_subprocess_exec (
418
+ proc = await asyncio .create_subprocess_exec (
420
419
sys .executable , b'-W' , b'ignore' , b'-c' , code ,
421
420
stdin = asyncio .subprocess .PIPE ,
422
421
stdout = asyncio .subprocess .PIPE ,
423
422
stderr = asyncio .subprocess .PIPE ,
424
423
close_fds = False )
425
- stdout , stderr = yield from proc .communicate (message )
426
- exitcode = yield from proc .wait ()
424
+ stdout , stderr = await proc .communicate (message )
425
+ exitcode = await proc .wait ()
427
426
return (stdout , exitcode )
428
427
429
428
output , exitcode = self .loop .run_until_complete (len_message (b'abc' ))
@@ -433,21 +432,20 @@ def len_message(message):
433
432
def test_stdin_stdout_pipe (self ):
434
433
args = self .PROGRAM_CAT
435
434
436
- @asyncio .coroutine
437
- def run (data ):
438
- proc = yield from asyncio .create_subprocess_exec (
435
+ async def run (data ):
436
+ proc = await asyncio .create_subprocess_exec (
439
437
* args ,
440
438
stdin = subprocess .PIPE ,
441
439
stdout = subprocess .PIPE )
442
440
443
441
# feed data
444
442
proc .stdin .write (data )
445
- yield from proc .stdin .drain ()
443
+ await proc .stdin .drain ()
446
444
proc .stdin .close ()
447
445
448
446
# get output and exitcode
449
- data = yield from proc .stdout .read ()
450
- exitcode = yield from proc .wait ()
447
+ data = await proc .stdout .read ()
448
+ exitcode = await proc .wait ()
451
449
return (exitcode , data )
452
450
453
451
task = run (b'some data' )
@@ -459,19 +457,18 @@ def run(data):
459
457
def test_stdin_stdout_file (self ):
460
458
args = self .PROGRAM_CAT
461
459
462
- @asyncio .coroutine
463
- def run (data , stdout ):
464
- proc = yield from asyncio .create_subprocess_exec (
460
+ async def run (data , stdout ):
461
+ proc = await asyncio .create_subprocess_exec (
465
462
* args ,
466
463
stdin = subprocess .PIPE ,
467
464
stdout = stdout )
468
465
469
466
# feed data
470
467
proc .stdin .write (data )
471
- yield from proc .stdin .drain ()
468
+ await proc .stdin .drain ()
472
469
proc .stdin .close ()
473
470
474
- exitcode = yield from proc .wait ()
471
+ exitcode = await proc .wait ()
475
472
return exitcode
476
473
477
474
with tempfile .TemporaryFile ('w+b' ) as new_stdout :
@@ -486,14 +483,13 @@ def run(data, stdout):
486
483
def test_stdin_stderr_file (self ):
487
484
args = self .PROGRAM_ERROR
488
485
489
- @asyncio .coroutine
490
- def run (stderr ):
491
- proc = yield from asyncio .create_subprocess_exec (
486
+ async def run (stderr ):
487
+ proc = await asyncio .create_subprocess_exec (
492
488
* args ,
493
489
stdin = subprocess .PIPE ,
494
490
stderr = stderr )
495
491
496
- exitcode = yield from proc .wait ()
492
+ exitcode = await proc .wait ()
497
493
return exitcode
498
494
499
495
with tempfile .TemporaryFile ('w+b' ) as new_stderr :
@@ -508,13 +504,12 @@ def run(stderr):
508
504
def test_communicate (self ):
509
505
args = self .PROGRAM_CAT
510
506
511
- @asyncio .coroutine
512
- def run (data ):
513
- proc = yield from asyncio .create_subprocess_exec (
507
+ async def run (data ):
508
+ proc = await asyncio .create_subprocess_exec (
514
509
* args ,
515
510
stdin = subprocess .PIPE ,
516
511
stdout = subprocess .PIPE )
517
- stdout , stderr = yield from proc .communicate (data )
512
+ stdout , stderr = await proc .communicate (data )
518
513
return proc .returncode , stdout
519
514
520
515
task = run (b'some data' )
@@ -560,14 +555,13 @@ def test_send_signal(self):
560
555
stdout = subprocess .PIPE )
561
556
proc = self .loop .run_until_complete (create )
562
557
563
- @asyncio .coroutine
564
- def send_signal (proc ):
558
+ async def send_signal (proc ):
565
559
# basic synchronization to wait until the program is sleeping
566
- line = yield from proc .stdout .readline ()
560
+ line = await proc .stdout .readline ()
567
561
self .assertEqual (line , b'sleeping\n ' )
568
562
569
563
proc .send_signal (signal .SIGHUP )
570
- returncode = (yield from proc .wait ())
564
+ returncode = (await proc .wait ())
571
565
return returncode
572
566
573
567
returncode = self .loop .run_until_complete (send_signal (proc ))
@@ -576,16 +570,15 @@ def send_signal(proc):
576
570
def test_cancel_process_wait (self ):
577
571
# Issue #23140: cancel Process.wait()
578
572
579
- @asyncio .coroutine
580
- def cancel_wait ():
581
- proc = yield from asyncio .create_subprocess_exec (
573
+ async def cancel_wait ():
574
+ proc = await asyncio .create_subprocess_exec (
582
575
* self .PROGRAM_BLOCKED )
583
576
584
577
# Create an internal future waiting on the process exit
585
578
task = self .loop .create_task (proc .wait ())
586
579
self .loop .call_soon (task .cancel )
587
580
try :
588
- yield from task
581
+ await task
589
582
except asyncio .CancelledError :
590
583
pass
591
584
@@ -594,24 +587,23 @@ def cancel_wait():
594
587
595
588
# Kill the process and wait until it is done
596
589
proc .kill ()
597
- yield from proc .wait ()
590
+ await proc .wait ()
598
591
599
592
self .loop .run_until_complete (cancel_wait ())
600
593
601
594
def test_cancel_make_subprocess_transport_exec (self ):
602
- @asyncio .coroutine
603
- def cancel_make_transport ():
595
+ async def cancel_make_transport ():
604
596
coro = asyncio .create_subprocess_exec (* self .PROGRAM_BLOCKED )
605
597
task = self .loop .create_task (coro )
606
598
607
599
self .loop .call_soon (task .cancel )
608
600
try :
609
- yield from task
601
+ await task
610
602
except asyncio .CancelledError :
611
603
pass
612
604
613
605
# Give the process handler some time to close itself
614
- yield from asyncio .sleep (0.3 )
606
+ await asyncio .sleep (0.3 )
615
607
gc .collect ()
616
608
617
609
# ignore the log:
@@ -620,20 +612,19 @@ def cancel_make_transport():
620
612
self .loop .run_until_complete (cancel_make_transport ())
621
613
622
614
def test_cancel_post_init (self ):
623
- @asyncio .coroutine
624
- def cancel_make_transport ():
615
+ async def cancel_make_transport ():
625
616
coro = self .loop .subprocess_exec (asyncio .SubprocessProtocol ,
626
617
* self .PROGRAM_BLOCKED )
627
618
task = self .loop .create_task (coro )
628
619
629
620
self .loop .call_soon (task .cancel )
630
621
try :
631
- yield from task
622
+ await task
632
623
except asyncio .CancelledError :
633
624
pass
634
625
635
626
# Give the process handler some time to close itself
636
- yield from asyncio .sleep (0.3 )
627
+ await asyncio .sleep (0.3 )
637
628
gc .collect ()
638
629
639
630
# ignore the log:
@@ -654,14 +645,13 @@ def __init__(self):
654
645
def connection_lost (self , exc ):
655
646
self .closed .set_result (1 )
656
647
657
- @asyncio .coroutine
658
- def test_subprocess ():
659
- transport , protocol = yield from loop .subprocess_exec (
648
+ async def test_subprocess ():
649
+ transport , protocol = await loop .subprocess_exec (
660
650
Protocol , * self .PROGRAM_BLOCKED )
661
651
pid = transport .get_pid ()
662
652
transport .close ()
663
653
self .assertIsNone (transport .get_returncode ())
664
- yield from protocol .closed
654
+ await protocol .closed
665
655
self .assertIsNotNone (transport .get_returncode ())
666
656
with self .assertRaises (ProcessLookupError ):
667
657
os .kill (pid , 0 )
0 commit comments