mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-32193: Convert asyncio to async/await usage (#4753)
* Convert asyncio/tasks.py to async/await * Convert asyncio/queues.py to async/await * Convert asyncio/test_utils.py to async/await * Convert asyncio/base_subprocess.py to async/await * Convert asyncio/subprocess.py to async/await * Convert asyncio/streams.py to async/await * Fix comments * Convert asyncio/locks.py to async/await * Convert asyncio.sleep to async def * Add a comment * Add missing news * Convert stubs from AbstrctEventLoop to async functions * Convert subprocess_shell/subprocess_exec * Convert connect_read_pipe/connect_write_pip to async/await syntax * Convert create_datagram_endpoint * Convert create_unix_server/create_unix_connection * Get rid of old style coroutines in unix_events.py * Convert selector_events.py to async/await * Convert wait_closed and create_connection * Drop redundant line * Convert base_events.py * Code cleanup * Drop redundant comments * Fix indentation * Add explicit tests for compatibility between old and new coroutines * Convert windows event loop to use async/await * Fix double awaiting of async function * Convert asyncio/locks.py * Improve docstring * Convert tests to async/await * Convert more tests * Convert more tests * Convert more tests * Convert tests * Improve test
This commit is contained in:
parent
ede157331b
commit
5f841b5538
22 changed files with 647 additions and 771 deletions
|
@ -285,10 +285,10 @@ class EventLoopTestsMixin:
|
|||
self.assertTrue(0.08 <= t1-t0 <= 0.8, t1-t0)
|
||||
|
||||
def test_run_until_complete_stopped(self):
|
||||
@asyncio.coroutine
|
||||
def cb():
|
||||
|
||||
async def cb():
|
||||
self.loop.stop()
|
||||
yield from asyncio.sleep(0.1, loop=self.loop)
|
||||
await asyncio.sleep(0.1, loop=self.loop)
|
||||
task = cb()
|
||||
self.assertRaises(RuntimeError,
|
||||
self.loop.run_until_complete, task)
|
||||
|
@ -1424,9 +1424,8 @@ class EventLoopTestsMixin:
|
|||
rpipe, wpipe = os.pipe()
|
||||
pipeobj = io.open(rpipe, 'rb', 1024)
|
||||
|
||||
@asyncio.coroutine
|
||||
def connect():
|
||||
t, p = yield from self.loop.connect_read_pipe(
|
||||
async def connect():
|
||||
t, p = await self.loop.connect_read_pipe(
|
||||
lambda: proto, pipeobj)
|
||||
self.assertIs(p, proto)
|
||||
self.assertIs(t, proto.transport)
|
||||
|
@ -1463,11 +1462,10 @@ class EventLoopTestsMixin:
|
|||
rpipeobj = io.open(rpipe, 'rb', 1024)
|
||||
wpipeobj = io.open(wpipe, 'w', 1024)
|
||||
|
||||
@asyncio.coroutine
|
||||
def connect():
|
||||
read_transport, _ = yield from loop.connect_read_pipe(
|
||||
async def connect():
|
||||
read_transport, _ = await loop.connect_read_pipe(
|
||||
lambda: read_proto, rpipeobj)
|
||||
write_transport, _ = yield from loop.connect_write_pipe(
|
||||
write_transport, _ = await loop.connect_write_pipe(
|
||||
lambda: write_proto, wpipeobj)
|
||||
return read_transport, write_transport
|
||||
|
||||
|
@ -1499,10 +1497,9 @@ class EventLoopTestsMixin:
|
|||
master, slave = os.openpty()
|
||||
master_read_obj = io.open(master, 'rb', 0)
|
||||
|
||||
@asyncio.coroutine
|
||||
def connect():
|
||||
t, p = yield from self.loop.connect_read_pipe(lambda: proto,
|
||||
master_read_obj)
|
||||
async def connect():
|
||||
t, p = await self.loop.connect_read_pipe(lambda: proto,
|
||||
master_read_obj)
|
||||
self.assertIs(p, proto)
|
||||
self.assertIs(t, proto.transport)
|
||||
self.assertEqual(['INITIAL', 'CONNECTED'], proto.state)
|
||||
|
@ -1713,11 +1710,10 @@ class EventLoopTestsMixin:
|
|||
if ov is not None:
|
||||
self.assertTrue(ov.pending)
|
||||
|
||||
@asyncio.coroutine
|
||||
def main():
|
||||
async def main():
|
||||
try:
|
||||
self.loop.call_soon(f.cancel)
|
||||
yield from f
|
||||
await f
|
||||
except asyncio.CancelledError:
|
||||
res = 'cancelled'
|
||||
else:
|
||||
|
@ -1750,14 +1746,13 @@ class EventLoopTestsMixin:
|
|||
self.loop._run_once_counter = 0
|
||||
self.loop._run_once = _run_once
|
||||
|
||||
@asyncio.coroutine
|
||||
def wait():
|
||||
async def wait():
|
||||
loop = self.loop
|
||||
yield from asyncio.sleep(1e-2, loop=loop)
|
||||
yield from asyncio.sleep(1e-4, loop=loop)
|
||||
yield from asyncio.sleep(1e-6, loop=loop)
|
||||
yield from asyncio.sleep(1e-8, loop=loop)
|
||||
yield from asyncio.sleep(1e-10, loop=loop)
|
||||
await asyncio.sleep(1e-2, loop=loop)
|
||||
await asyncio.sleep(1e-4, loop=loop)
|
||||
await asyncio.sleep(1e-6, loop=loop)
|
||||
await asyncio.sleep(1e-8, loop=loop)
|
||||
await asyncio.sleep(1e-10, loop=loop)
|
||||
|
||||
self.loop.run_until_complete(wait())
|
||||
# The ideal number of call is 12, but on some platforms, the selector
|
||||
|
@ -2076,9 +2071,9 @@ class SubprocessTestsMixin:
|
|||
self.assertEqual(7, proto.returncode)
|
||||
|
||||
def test_subprocess_exec_invalid_args(self):
|
||||
@asyncio.coroutine
|
||||
def connect(**kwds):
|
||||
yield from self.loop.subprocess_exec(
|
||||
|
||||
async def connect(**kwds):
|
||||
await self.loop.subprocess_exec(
|
||||
asyncio.SubprocessProtocol,
|
||||
'pwd', **kwds)
|
||||
|
||||
|
@ -2090,11 +2085,11 @@ class SubprocessTestsMixin:
|
|||
self.loop.run_until_complete(connect(shell=True))
|
||||
|
||||
def test_subprocess_shell_invalid_args(self):
|
||||
@asyncio.coroutine
|
||||
def connect(cmd=None, **kwds):
|
||||
|
||||
async def connect(cmd=None, **kwds):
|
||||
if not cmd:
|
||||
cmd = 'pwd'
|
||||
yield from self.loop.subprocess_shell(
|
||||
await self.loop.subprocess_shell(
|
||||
asyncio.SubprocessProtocol,
|
||||
cmd, **kwds)
|
||||
|
||||
|
@ -2548,20 +2543,8 @@ class AbstractEventLoopTests(unittest.TestCase):
|
|||
NotImplementedError, loop.time)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.call_soon_threadsafe, None)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.run_in_executor, f, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.set_default_executor, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.getaddrinfo, 'localhost', 8080)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.getnameinfo, ('localhost', 8080))
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.create_connection, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.create_server, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.create_datagram_endpoint, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.add_reader, 1, f)
|
||||
self.assertRaises(
|
||||
|
@ -2570,33 +2553,12 @@ class AbstractEventLoopTests(unittest.TestCase):
|
|||
NotImplementedError, loop.add_writer, 1, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.remove_writer, 1)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.sock_recv, f, 10)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.sock_recv_into, f, 10)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.sock_sendall, f, 10)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.sock_connect, f, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.sock_accept, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.add_signal_handler, 1, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.remove_signal_handler, 1)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.remove_signal_handler, 1)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.connect_read_pipe, f,
|
||||
mock.sentinel.pipe)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.connect_write_pipe, f,
|
||||
mock.sentinel.pipe)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.subprocess_shell, f,
|
||||
mock.sentinel)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.subprocess_exec, f)
|
||||
self.assertRaises(
|
||||
NotImplementedError, loop.set_exception_handler, f)
|
||||
self.assertRaises(
|
||||
|
@ -2608,6 +2570,47 @@ class AbstractEventLoopTests(unittest.TestCase):
|
|||
self.assertRaises(
|
||||
NotImplementedError, loop.set_debug, f)
|
||||
|
||||
def test_not_implemented_async(self):
|
||||
|
||||
async def inner():
|
||||
f = mock.Mock()
|
||||
loop = asyncio.AbstractEventLoop()
|
||||
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.run_in_executor(f, f)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.getaddrinfo('localhost', 8080)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.getnameinfo(('localhost', 8080))
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.create_connection(f)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.create_server(f)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.create_datagram_endpoint(f)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.sock_recv(f, 10)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.sock_recv_into(f, 10)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.sock_sendall(f, 10)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.sock_connect(f, f)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.sock_accept(f)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.connect_read_pipe(f, mock.sentinel.pipe)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.connect_write_pipe(f, mock.sentinel.pipe)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.subprocess_shell(f, mock.sentinel)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
await loop.subprocess_exec(f)
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
loop.run_until_complete(inner())
|
||||
loop.close()
|
||||
|
||||
|
||||
class ProtocolsAbsTests(unittest.TestCase):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue