Skip to content

Commit 7384b22

Browse files
committed
Convert Future-returning loop methods to coroutines.
The following event loop methods are now coroutines: * getaddrinfo() * sock_recv() * sock_recv_into() * sock_accept() * subprocess_shell() * subprocess_exec()
1 parent 087a906 commit 7384b22

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

tests/test_dns.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import socket
23
import unittest
34

@@ -165,7 +166,9 @@ def test_getaddrinfo_close_loop(self):
165166
raise unittest.SkipTest
166167

167168
async def run():
168-
fut = self.loop.getaddrinfo('example.com', 80)
169+
fut = self.loop.create_task(
170+
self.loop.getaddrinfo('example.com', 80))
171+
await asyncio.sleep(0, loop=self.loop)
169172
fut.cancel()
170173
self.loop.stop()
171174

tests/test_sockets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ async def kill(fut):
544544
fut.cancel()
545545

546546
async def recv(sock):
547-
fut = self.loop.sock_recv(sock, 10)
547+
fut = self.loop.create_task(self.loop.sock_recv(sock, 10))
548548
await asyncio.sleep(0.1, loop=self.loop)
549549
self.loop.remove_reader(sock)
550550
sock.close()

tests/test_unix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class _TestSSL(tb.SSLTestCase):
461461
def test_create_unix_server_ssl_1(self):
462462
CNT = 0 # number of clients that were successful
463463
TOTAL_CNT = 25 # total number of clients that test will create
464-
TIMEOUT = 5.0 # timeout for this test
464+
TIMEOUT = 10.0 # timeout for this test
465465

466466
A_DATA = b'A' * 1024 * 1024
467467
B_DATA = b'B' * 1024 * 1024

uvloop/loop.pyx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,17 +1363,19 @@ cdef class Loop:
13631363

13641364
return future.result()
13651365

1366-
def getaddrinfo(self, object host, object port, *,
1367-
int family=0, int type=0, int proto=0, int flags=0):
1366+
@cython.iterable_coroutine
1367+
async def getaddrinfo(self, object host, object port, *,
1368+
int family=0, int type=0, int proto=0, int flags=0):
13681369

13691370
addr = __static_getaddrinfo_pyaddr(host, port, family,
13701371
type, proto, flags)
13711372
if addr is not None:
13721373
fut = self._new_future()
13731374
fut.set_result([addr])
1374-
return fut
1375+
return await fut
13751376

1376-
return self._getaddrinfo(host, port, family, type, proto, flags, 1)
1377+
return await self._getaddrinfo(
1378+
host, port, family, type, proto, flags, 1)
13771379

13781380
@cython.iterable_coroutine
13791381
async def getnameinfo(self, sockaddr, int flags=0):
@@ -2102,7 +2104,8 @@ cdef class Loop:
21022104
"""Remove a writer callback."""
21032105
self._remove_writer(fileobj)
21042106

2105-
def sock_recv(self, sock, n):
2107+
@cython.iterable_coroutine
2108+
async def sock_recv(self, sock, n):
21062109
"""Receive data from the socket.
21072110
21082111
The return value is a bytes object representing the data received.
@@ -2126,9 +2129,10 @@ cdef class Loop:
21262129
fut, sock, n)
21272130

21282131
self._add_reader(sock, handle)
2129-
return fut
2132+
return await fut
21302133

2131-
def sock_recv_into(self, sock, buf):
2134+
@cython.iterable_coroutine
2135+
async def sock_recv_into(self, sock, buf):
21322136
"""Receive data from the socket.
21332137
21342138
The received data is written into *buf* (a writable buffer).
@@ -2151,7 +2155,7 @@ cdef class Loop:
21512155
fut, sock, buf)
21522156

21532157
self._add_reader(sock, handle)
2154-
return fut
2158+
return await fut
21552159

21562160
@cython.iterable_coroutine
21572161
async def sock_sendall(self, sock, data):
@@ -2206,7 +2210,8 @@ cdef class Loop:
22062210
finally:
22072211
socket_dec_io_ref(sock)
22082212

2209-
def sock_accept(self, sock):
2213+
@cython.iterable_coroutine
2214+
async def sock_accept(self, sock):
22102215
"""Accept a connection.
22112216
22122217
The socket must be bound to an address and listening for connections.
@@ -2231,7 +2236,7 @@ cdef class Loop:
22312236
fut, sock)
22322237

22332238
self._add_reader(sock, handle)
2234-
return fut
2239+
return await fut
22352240

22362241
@cython.iterable_coroutine
22372242
async def sock_connect(self, sock, address):
@@ -2390,9 +2395,10 @@ cdef class Loop:
23902395

23912396
return proc, protocol
23922397

2393-
def subprocess_shell(self, protocol_factory, cmd, *,
2394-
shell=True,
2395-
**kwargs):
2398+
@cython.iterable_coroutine
2399+
async def subprocess_shell(self, protocol_factory, cmd, *,
2400+
shell=True,
2401+
**kwargs):
23962402

23972403
if not shell:
23982404
raise ValueError("shell must be True")
@@ -2401,19 +2407,20 @@ cdef class Loop:
24012407
if shell:
24022408
args = [b'/bin/sh', b'-c'] + args
24032409

2404-
return self.__subprocess_run(protocol_factory, args, shell=True,
2405-
**kwargs)
2410+
return await self.__subprocess_run(protocol_factory, args, shell=True,
2411+
**kwargs)
24062412

2407-
def subprocess_exec(self, protocol_factory, program, *args,
2408-
shell=False, **kwargs):
2413+
@cython.iterable_coroutine
2414+
async def subprocess_exec(self, protocol_factory, program, *args,
2415+
shell=False, **kwargs):
24092416

24102417
if shell:
24112418
raise ValueError("shell must be False")
24122419

24132420
args = list((program,) + args)
24142421

2415-
return self.__subprocess_run(protocol_factory, args, shell=False,
2416-
**kwargs)
2422+
return await self.__subprocess_run(protocol_factory, args, shell=False,
2423+
**kwargs)
24172424

24182425
@cython.iterable_coroutine
24192426
async def connect_read_pipe(self, proto_factory, pipe):

0 commit comments

Comments
 (0)