summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2016-11-09 20:47:47 (GMT)
committerYury Selivanov <yury@magic.io>2016-11-09 20:47:47 (GMT)
commit954c7dd0ccbe01668856d6a9816aeb1fe07bce7e (patch)
tree16bbc28ba590cf3d68c21406349e2b2cf431c002 /Lib/asyncio
parent6c1065061770e843f55be0506489c931a2cb7e96 (diff)
parenta1a8b7d3d7f628aec31be364c77cbb3e21cdbc0b (diff)
downloadcpython-954c7dd0ccbe01668856d6a9816aeb1fe07bce7e.zip
cpython-954c7dd0ccbe01668856d6a9816aeb1fe07bce7e.tar.gz
cpython-954c7dd0ccbe01668856d6a9816aeb1fe07bce7e.tar.bz2
Merge 3.5 (issue #28652)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/base_events.py56
-rw-r--r--Lib/asyncio/unix_events.py4
2 files changed, 47 insertions, 13 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index 85cc1e4..4eed468 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -84,12 +84,26 @@ def _set_reuseport(sock):
'SO_REUSEPORT defined but not implemented.')
-# Linux's sock.type is a bitmask that can include extra info about socket.
-_SOCKET_TYPE_MASK = 0
-if hasattr(socket, 'SOCK_NONBLOCK'):
- _SOCKET_TYPE_MASK |= socket.SOCK_NONBLOCK
-if hasattr(socket, 'SOCK_CLOEXEC'):
- _SOCKET_TYPE_MASK |= socket.SOCK_CLOEXEC
+def _is_stream_socket(sock):
+ # Linux's socket.type is a bitmask that can include extra info
+ # about socket, therefore we can't do simple
+ # `sock_type == socket.SOCK_STREAM`.
+ return (sock.type & socket.SOCK_STREAM) == socket.SOCK_STREAM
+
+
+def _is_dgram_socket(sock):
+ # Linux's socket.type is a bitmask that can include extra info
+ # about socket, therefore we can't do simple
+ # `sock_type == socket.SOCK_DGRAM`.
+ return (sock.type & socket.SOCK_DGRAM) == socket.SOCK_DGRAM
+
+
+def _is_ip_socket(sock):
+ if sock.family == socket.AF_INET:
+ return True
+ if hasattr(socket, 'AF_INET6') and sock.family == socket.AF_INET6:
+ return True
+ return False
def _ipaddr_info(host, port, family, type, proto):
@@ -102,8 +116,12 @@ def _ipaddr_info(host, port, family, type, proto):
host is None:
return None
- type &= ~_SOCKET_TYPE_MASK
if type == socket.SOCK_STREAM:
+ # Linux only:
+ # getaddrinfo() can raise when socket.type is a bit mask.
+ # So if socket.type is a bit mask of SOCK_STREAM, and say
+ # SOCK_NONBLOCK, we simply return None, which will trigger
+ # a call to getaddrinfo() letting it process this request.
proto = socket.IPPROTO_TCP
elif type == socket.SOCK_DGRAM:
proto = socket.IPPROTO_UDP
@@ -124,7 +142,9 @@ def _ipaddr_info(host, port, family, type, proto):
return None
if family == socket.AF_UNSPEC:
- afs = [socket.AF_INET, socket.AF_INET6]
+ afs = [socket.AF_INET]
+ if hasattr(socket, 'AF_INET6'):
+ afs.append(socket.AF_INET6)
else:
afs = [family]
@@ -772,9 +792,13 @@ class BaseEventLoop(events.AbstractEventLoop):
raise OSError('Multiple exceptions: {}'.format(
', '.join(str(exc) for exc in exceptions)))
- elif sock is None:
- raise ValueError(
- 'host and port was not specified and no sock specified')
+ else:
+ if sock is None:
+ raise ValueError(
+ 'host and port was not specified and no sock specified')
+ if not _is_stream_socket(sock) or not _is_ip_socket(sock):
+ raise ValueError(
+ 'A TCP Stream Socket was expected, got {!r}'.format(sock))
transport, protocol = yield from self._create_connection_transport(
sock, protocol_factory, ssl, server_hostname)
@@ -818,6 +842,9 @@ class BaseEventLoop(events.AbstractEventLoop):
allow_broadcast=None, sock=None):
"""Create datagram connection."""
if sock is not None:
+ if not _is_dgram_socket(sock):
+ raise ValueError(
+ 'A UDP Socket was expected, got {!r}'.format(sock))
if (local_addr or remote_addr or
family or proto or flags or
reuse_address or reuse_port or allow_broadcast):
@@ -1028,6 +1055,9 @@ class BaseEventLoop(events.AbstractEventLoop):
else:
if sock is None:
raise ValueError('Neither host/port nor sock were specified')
+ if not _is_stream_socket(sock) or not _is_ip_socket(sock):
+ raise ValueError(
+ 'A TCP Stream Socket was expected, got {!r}'.format(sock))
sockets = [sock]
server = Server(self, sockets)
@@ -1049,6 +1079,10 @@ class BaseEventLoop(events.AbstractEventLoop):
This method is a coroutine. When completed, the coroutine
returns a (transport, protocol) pair.
"""
+ if not _is_stream_socket(sock):
+ raise ValueError(
+ 'A Stream Socket was expected, got {!r}'.format(sock))
+
transport, protocol = yield from self._create_connection_transport(
sock, protocol_factory, ssl, '', server_side=True)
if self._debug:
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
index 2843678..46e91f6 100644
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -235,7 +235,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
if sock is None:
raise ValueError('no path and sock were specified')
if (sock.family != socket.AF_UNIX or
- sock.type != socket.SOCK_STREAM):
+ not base_events._is_stream_socket(sock)):
raise ValueError(
'A UNIX Domain Stream Socket was expected, got {!r}'
.format(sock))
@@ -289,7 +289,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
'path was not specified, and no sock specified')
if (sock.family != socket.AF_UNIX or
- sock.type != socket.SOCK_STREAM):
+ not base_events._is_stream_socket(sock)):
raise ValueError(
'A UNIX Domain Stream Socket was expected, got {!r}'
.format(sock))