From a11c5b88b14aadf01e5714f9fc02aa42a7ded0a7 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Wed, 8 Jun 2016 12:23:41 -0400 Subject: [PATCH] Fix sock_connect to resolve addresses (as in asyncio/CPython 3.5.2) --- uvloop/includes/stdlib.pxi | 1 - uvloop/loop.pyx | 23 +++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/uvloop/includes/stdlib.pxi b/uvloop/includes/stdlib.pxi index 8de93b4..08fa92a 100644 --- a/uvloop/includes/stdlib.pxi +++ b/uvloop/includes/stdlib.pxi @@ -28,7 +28,6 @@ cdef aio_ensure_future = asyncio.ensure_future cdef aio_gather = asyncio.gather cdef aio_wait = asyncio.wait cdef aio_logger = asyncio.log.logger -cdef aio__check_resolved_address = asyncio.base_events._check_resolved_address cdef aio_iscoroutine = asyncio.iscoroutine cdef aio_iscoroutinefunction = asyncio.iscoroutinefunction cdef aio_BaseProtocol = asyncio.BaseProtocol diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index 53d623f..1159564 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -1861,28 +1861,23 @@ cdef class Loop: self._add_reader(fd, handle) return fut - def sock_connect(self, sock, address): + async def sock_connect(self, sock, address): """Connect to a remote socket at address. - The address must be already resolved to avoid the trap of hanging the - entire event loop when the address requires doing a DNS lookup. For - example, it must be an IP address, not a hostname, for AF_INET and - AF_INET6 address families. Use getaddrinfo() to resolve the hostname - asynchronously. - This method is a coroutine. """ if self._debug and sock.gettimeout() != 0: raise ValueError("the socket must be non-blocking") + fut = self._new_future() - try: - if self._debug: - aio__check_resolved_address(sock, address) - except ValueError as err: - fut.set_exception(err) - else: + if sock.family == uv.AF_UNIX: self._sock_connect(fut, sock, address) - return fut + await fut + return + + _, _, _, _, address = (await self.getaddrinfo(*address))[0] + self._sock_connect(fut, sock, address) + await fut def run_in_executor(self, executor, func, *args): if aio_iscoroutine(func) or aio_iscoroutinefunction(func):