diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-02-13 09:24:37 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-02-13 09:24:37 +0100 |
commit | 1b0580b3203281e700ab3df10e6d117796f9d955 (patch) | |
tree | 1f6d56d1d8b275d73c08e0824da2a4ee017db449 /Lib/asyncio/base_events.py | |
parent | 2303fecedcfbdec16999e054e401dfad3a13fc05 (diff) | |
download | cpython-1b0580b3203281e700ab3df10e6d117796f9d955.tar.gz cpython-1b0580b3203281e700ab3df10e6d117796f9d955.zip |
ayncio, Tulip issue 129: BaseEventLoop.sock_connect() now raises an error if
the address is not resolved (hostname instead of an IP address) for AF_INET and
AF_INET6 address families.
Diffstat (limited to 'Lib/asyncio/base_events.py')
-rw-r--r-- | Lib/asyncio/base_events.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 7d120424dfb..3bbf6b54661 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -41,6 +41,31 @@ class _StopError(BaseException): """Raised to stop the event loop.""" +def _check_resolved_address(sock, address): + # Ensure that the address is already resolved to avoid the trap of hanging + # the entire event loop when the address requires doing a DNS lookup. + family = sock.family + if family not in (socket.AF_INET, socket.AF_INET6): + return + + host, port = address + type_mask = 0 + if hasattr(socket, 'SOCK_NONBLOCK'): + type_mask |= socket.SOCK_NONBLOCK + if hasattr(socket, 'SOCK_CLOEXEC'): + type_mask |= socket.SOCK_CLOEXEC + # Use getaddrinfo(AI_NUMERICHOST) to ensure that the address is + # already resolved. + try: + socket.getaddrinfo(host, port, + family=family, + type=(sock.type & ~type_mask), + proto=sock.proto, + flags=socket.AI_NUMERICHOST) + except socket.gaierror as err: + raise ValueError("address must be resolved (IP address), got %r: %s" + % (address, err)) + def _raise_stop_error(*args): raise _StopError |