Skip to content

bpo-27456: Simplify sock type checks #4922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,6 @@ def _set_reuseport(sock):
'SO_REUSEPORT defined but not implemented.')


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_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):
# Try to skip getaddrinfo if "host" is already an IP. Users might have
# handled name resolution in their own code and pass in resolved IPs.
Expand All @@ -112,9 +92,9 @@ def _ipaddr_info(host, port, family, type, proto):
host is None:
return None

if _is_stream_socket(type):
if type == socket.SOCK_STREAM:
proto = socket.IPPROTO_TCP
elif _is_dgram_socket(type):
elif type == socket.SOCK_DGRAM:
proto = socket.IPPROTO_UDP
else:
return None
Expand Down Expand Up @@ -759,7 +739,7 @@ async def create_connection(self, protocol_factory, host=None, port=None,
if sock is None:
raise ValueError(
'host and port was not specified and no sock specified')
if not _is_stream_socket(sock.type):
if sock.type != socket.SOCK_STREAM:
# 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
Expand Down Expand Up @@ -809,7 +789,7 @@ async def create_datagram_endpoint(self, protocol_factory,
allow_broadcast=None, sock=None):
"""Create datagram connection."""
if sock is not None:
if not _is_dgram_socket(sock.type):
if sock.type != socket.SOCK_DGRAM:
raise ValueError(
f'A UDP Socket was expected, got {sock!r}')
if (local_addr or remote_addr or
Expand Down Expand Up @@ -1037,7 +1017,7 @@ async def create_server(self, protocol_factory, host=None, port=None,
else:
if sock is None:
raise ValueError('Neither host/port nor sock were specified')
if not _is_stream_socket(sock.type):
if sock.type != socket.SOCK_STREAM:
raise ValueError(f'A Stream Socket was expected, got {sock!r}')
sockets = [sock]

Expand All @@ -1060,7 +1040,7 @@ async def connect_accepted_socket(self, protocol_factory, sock,
This method is a coroutine. When completed, the coroutine
returns a (transport, protocol) pair.
"""
if not _is_stream_socket(sock.type):
if sock.type != socket.SOCK_STREAM:
raise ValueError(f'A Stream Socket was expected, got {sock!r}')

transport, protocol = await self._create_connection_transport(
Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _test_selector_event(selector, fd, event):
if hasattr(socket, 'TCP_NODELAY'):
def _set_nodelay(sock):
if (sock.family in {socket.AF_INET, socket.AF_INET6} and
base_events._is_stream_socket(sock.type) and
sock.type == socket.SOCK_STREAM and
sock.proto == socket.IPPROTO_TCP):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
else:
Expand Down
4 changes: 2 additions & 2 deletions Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ async def create_unix_connection(self, protocol_factory, path=None, *,
if sock is None:
raise ValueError('no path and sock were specified')
if (sock.family != socket.AF_UNIX or
not base_events._is_stream_socket(sock.type)):
sock.type != socket.SOCK_STREAM):
raise ValueError(
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
sock.setblocking(False)
Expand Down Expand Up @@ -276,7 +276,7 @@ async def create_unix_server(self, protocol_factory, path=None, *,
'path was not specified, and no sock specified')

if (sock.family != socket.AF_UNIX or
not base_events._is_stream_socket(sock.type)):
sock.type != socket.SOCK_STREAM):
raise ValueError(
f'A UNIX Domain Stream Socket was expected, got {sock!r}')

Expand Down