Skip to content

Commit 0bb811d

Browse files
committed
tests: Stop using @asyncio.coroutine in tests
1 parent eb8d7ba commit 0bb811d

File tree

3 files changed

+42
-56
lines changed

3 files changed

+42
-56
lines changed

tests/test_base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,7 @@ def test_set_task_factory(self):
523523
class MyTask(asyncio.Task):
524524
pass
525525

526-
@asyncio.coroutine
527-
def coro():
526+
async def coro():
528527
pass
529528

530529
factory = lambda loop, coro: MyTask(coro, loop=loop)
@@ -765,8 +764,7 @@ def test_uvloop_policy(self):
765764
@unittest.skipUnless(hasattr(asyncio, '_get_running_loop'),
766765
'No asyncio._get_running_loop')
767766
def test_running_loop_within_a_loop(self):
768-
@asyncio.coroutine
769-
def runner(loop):
767+
async def runner(loop):
770768
loop.run_forever()
771769

772770
try:

tests/test_pipes.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ def test_read_pipe(self):
6969
rpipe, wpipe = os.pipe()
7070
pipeobj = io.open(rpipe, 'rb', 1024)
7171

72-
@asyncio.coroutine
73-
def connect():
74-
t, p = yield from self.loop.connect_read_pipe(
72+
async def connect():
73+
t, p = await self.loop.connect_read_pipe(
7574
lambda: proto, pipeobj)
7675
self.assertIs(p, proto)
7776
self.assertIs(t, proto.transport)
@@ -102,10 +101,9 @@ def test_read_pty_output(self):
102101
master, slave = os.openpty()
103102
master_read_obj = io.open(master, 'rb', 0)
104103

105-
@asyncio.coroutine
106-
def connect():
107-
t, p = yield from self.loop.connect_read_pipe(lambda: proto,
108-
master_read_obj)
104+
async def connect():
105+
t, p = await self.loop.connect_read_pipe(
106+
lambda: proto, master_read_obj)
109107
self.assertIs(p, proto)
110108
self.assertIs(t, proto.transport)
111109
self.assertEqual(['INITIAL', 'CONNECTED'], proto.state)

tests/test_process.py

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,16 @@ class _AsyncioTests:
413413
def test_stdin_not_inheritable(self):
414414
# asyncio issue #209: stdin must not be inheritable, otherwise
415415
# the Process.communicate() hangs
416-
@asyncio.coroutine
417-
def len_message(message):
416+
async def len_message(message):
418417
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(
420419
sys.executable, b'-W', b'ignore', b'-c', code,
421420
stdin=asyncio.subprocess.PIPE,
422421
stdout=asyncio.subprocess.PIPE,
423422
stderr=asyncio.subprocess.PIPE,
424423
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()
427426
return (stdout, exitcode)
428427

429428
output, exitcode = self.loop.run_until_complete(len_message(b'abc'))
@@ -433,21 +432,20 @@ def len_message(message):
433432
def test_stdin_stdout_pipe(self):
434433
args = self.PROGRAM_CAT
435434

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(
439437
*args,
440438
stdin=subprocess.PIPE,
441439
stdout=subprocess.PIPE)
442440

443441
# feed data
444442
proc.stdin.write(data)
445-
yield from proc.stdin.drain()
443+
await proc.stdin.drain()
446444
proc.stdin.close()
447445

448446
# 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()
451449
return (exitcode, data)
452450

453451
task = run(b'some data')
@@ -459,19 +457,18 @@ def run(data):
459457
def test_stdin_stdout_file(self):
460458
args = self.PROGRAM_CAT
461459

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(
465462
*args,
466463
stdin=subprocess.PIPE,
467464
stdout=stdout)
468465

469466
# feed data
470467
proc.stdin.write(data)
471-
yield from proc.stdin.drain()
468+
await proc.stdin.drain()
472469
proc.stdin.close()
473470

474-
exitcode = yield from proc.wait()
471+
exitcode = await proc.wait()
475472
return exitcode
476473

477474
with tempfile.TemporaryFile('w+b') as new_stdout:
@@ -486,14 +483,13 @@ def run(data, stdout):
486483
def test_stdin_stderr_file(self):
487484
args = self.PROGRAM_ERROR
488485

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(
492488
*args,
493489
stdin=subprocess.PIPE,
494490
stderr=stderr)
495491

496-
exitcode = yield from proc.wait()
492+
exitcode = await proc.wait()
497493
return exitcode
498494

499495
with tempfile.TemporaryFile('w+b') as new_stderr:
@@ -508,13 +504,12 @@ def run(stderr):
508504
def test_communicate(self):
509505
args = self.PROGRAM_CAT
510506

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(
514509
*args,
515510
stdin=subprocess.PIPE,
516511
stdout=subprocess.PIPE)
517-
stdout, stderr = yield from proc.communicate(data)
512+
stdout, stderr = await proc.communicate(data)
518513
return proc.returncode, stdout
519514

520515
task = run(b'some data')
@@ -560,14 +555,13 @@ def test_send_signal(self):
560555
stdout=subprocess.PIPE)
561556
proc = self.loop.run_until_complete(create)
562557

563-
@asyncio.coroutine
564-
def send_signal(proc):
558+
async def send_signal(proc):
565559
# basic synchronization to wait until the program is sleeping
566-
line = yield from proc.stdout.readline()
560+
line = await proc.stdout.readline()
567561
self.assertEqual(line, b'sleeping\n')
568562

569563
proc.send_signal(signal.SIGHUP)
570-
returncode = (yield from proc.wait())
564+
returncode = (await proc.wait())
571565
return returncode
572566

573567
returncode = self.loop.run_until_complete(send_signal(proc))
@@ -576,16 +570,15 @@ def send_signal(proc):
576570
def test_cancel_process_wait(self):
577571
# Issue #23140: cancel Process.wait()
578572

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(
582575
*self.PROGRAM_BLOCKED)
583576

584577
# Create an internal future waiting on the process exit
585578
task = self.loop.create_task(proc.wait())
586579
self.loop.call_soon(task.cancel)
587580
try:
588-
yield from task
581+
await task
589582
except asyncio.CancelledError:
590583
pass
591584

@@ -594,24 +587,23 @@ def cancel_wait():
594587

595588
# Kill the process and wait until it is done
596589
proc.kill()
597-
yield from proc.wait()
590+
await proc.wait()
598591

599592
self.loop.run_until_complete(cancel_wait())
600593

601594
def test_cancel_make_subprocess_transport_exec(self):
602-
@asyncio.coroutine
603-
def cancel_make_transport():
595+
async def cancel_make_transport():
604596
coro = asyncio.create_subprocess_exec(*self.PROGRAM_BLOCKED)
605597
task = self.loop.create_task(coro)
606598

607599
self.loop.call_soon(task.cancel)
608600
try:
609-
yield from task
601+
await task
610602
except asyncio.CancelledError:
611603
pass
612604

613605
# Give the process handler some time to close itself
614-
yield from asyncio.sleep(0.3)
606+
await asyncio.sleep(0.3)
615607
gc.collect()
616608

617609
# ignore the log:
@@ -620,20 +612,19 @@ def cancel_make_transport():
620612
self.loop.run_until_complete(cancel_make_transport())
621613

622614
def test_cancel_post_init(self):
623-
@asyncio.coroutine
624-
def cancel_make_transport():
615+
async def cancel_make_transport():
625616
coro = self.loop.subprocess_exec(asyncio.SubprocessProtocol,
626617
*self.PROGRAM_BLOCKED)
627618
task = self.loop.create_task(coro)
628619

629620
self.loop.call_soon(task.cancel)
630621
try:
631-
yield from task
622+
await task
632623
except asyncio.CancelledError:
633624
pass
634625

635626
# Give the process handler some time to close itself
636-
yield from asyncio.sleep(0.3)
627+
await asyncio.sleep(0.3)
637628
gc.collect()
638629

639630
# ignore the log:
@@ -654,14 +645,13 @@ def __init__(self):
654645
def connection_lost(self, exc):
655646
self.closed.set_result(1)
656647

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(
660650
Protocol, *self.PROGRAM_BLOCKED)
661651
pid = transport.get_pid()
662652
transport.close()
663653
self.assertIsNone(transport.get_returncode())
664-
yield from protocol.closed
654+
await protocol.closed
665655
self.assertIsNotNone(transport.get_returncode())
666656
with self.assertRaises(ProcessLookupError):
667657
os.kill(pid, 0)

0 commit comments

Comments
 (0)