summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/base_events.py
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2017-12-16 00:32:25 (GMT)
committerGitHub <noreply@github.com>2017-12-16 00:32:25 (GMT)
commite796b2fe26f220107ac50667de6cc86c82b465e3 (patch)
treecdf3d558b8686983e5de71ea5783770eb415ea8d /Lib/asyncio/base_events.py
parent4ac5150e068a3a795ef00465f6dff51747b62b91 (diff)
downloadcpython-e796b2fe26f220107ac50667de6cc86c82b465e3.zip
cpython-e796b2fe26f220107ac50667de6cc86c82b465e3.tar.gz
cpython-e796b2fe26f220107ac50667de6cc86c82b465e3.tar.bz2
bpo-27456: Ensure TCP_NODELAY is set on linux (#4231)
Diffstat (limited to 'Lib/asyncio/base_events.py')
-rw-r--r--Lib/asyncio/base_events.py43
1 files changed, 22 insertions, 21 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index 80d2b69..398497d 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -82,18 +82,24 @@ def _set_reuseport(sock):
'SO_REUSEPORT defined but not implemented.')
-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_stream_socket(sock_type):
+ if hasattr(socket, 'SOCK_NONBLOCK'):
+ # Linux's socket.type is a bitmask that can include extra info
+ # about socket (like SOCK_NONBLOCK bit), therefore we can't do simple
+ # `sock_type == socket.SOCK_STREAM`, see
+ # https://github.com/torvalds/linux/blob/v4.13/include/linux/net.h#L77
+ # for more details.
+ return (sock_type & 0xF) == socket.SOCK_STREAM
+ else:
+ return sock_type == 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_dgram_socket(sock_type):
+ if hasattr(socket, 'SOCK_NONBLOCK'):
+ # See the comment in `_is_stream_socket`.
+ return (sock_type & 0xF) == socket.SOCK_DGRAM
+ else:
+ return sock_type == socket.SOCK_DGRAM
def _ipaddr_info(host, port, family, type, proto):
@@ -106,14 +112,9 @@ def _ipaddr_info(host, port, family, type, proto):
host is None:
return None
- 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.
+ if _is_stream_socket(type):
proto = socket.IPPROTO_TCP
- elif type == socket.SOCK_DGRAM:
+ elif _is_dgram_socket(type):
proto = socket.IPPROTO_UDP
else:
return None
@@ -758,7 +759,7 @@ class BaseEventLoop(events.AbstractEventLoop):
if sock is None:
raise ValueError(
'host and port was not specified and no sock specified')
- if not _is_stream_socket(sock):
+ if not _is_stream_socket(sock.type):
# We allow AF_INET, AF_INET6, AF_UNIX as long as they
# are SOCK_STREAM.
# We support passing AF_UNIX sockets even though we have
@@ -808,7 +809,7 @@ class BaseEventLoop(events.AbstractEventLoop):
allow_broadcast=None, sock=None):
"""Create datagram connection."""
if sock is not None:
- if not _is_dgram_socket(sock):
+ if not _is_dgram_socket(sock.type):
raise ValueError(
f'A UDP Socket was expected, got {sock!r}')
if (local_addr or remote_addr or
@@ -1036,7 +1037,7 @@ class BaseEventLoop(events.AbstractEventLoop):
else:
if sock is None:
raise ValueError('Neither host/port nor sock were specified')
- if not _is_stream_socket(sock):
+ if not _is_stream_socket(sock.type):
raise ValueError(f'A Stream Socket was expected, got {sock!r}')
sockets = [sock]
@@ -1059,7 +1060,7 @@ class BaseEventLoop(events.AbstractEventLoop):
This method is a coroutine. When completed, the coroutine
returns a (transport, protocol) pair.
"""
- if not _is_stream_socket(sock):
+ if not _is_stream_socket(sock.type):
raise ValueError(f'A Stream Socket was expected, got {sock!r}')
transport, protocol = await self._create_connection_transport(