mirror of
https://github.com/python/cpython.git
synced 2025-08-02 08:02:56 +00:00
bpo-27456: Simplify sock type checks (#4922)
Recent sock.type fix (see bug 32331) makes sock.type checks simpler in asyncio.
This commit is contained in:
parent
5d8624647d
commit
a7bd64c0c0
3 changed files with 9 additions and 29 deletions
|
@ -82,26 +82,6 @@ def _set_reuseport(sock):
|
||||||
'SO_REUSEPORT defined but not implemented.')
|
'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):
|
def _ipaddr_info(host, port, family, type, proto):
|
||||||
# Try to skip getaddrinfo if "host" is already an IP. Users might have
|
# 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.
|
# handled name resolution in their own code and pass in resolved IPs.
|
||||||
|
@ -112,9 +92,9 @@ def _ipaddr_info(host, port, family, type, proto):
|
||||||
host is None:
|
host is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if _is_stream_socket(type):
|
if type == socket.SOCK_STREAM:
|
||||||
proto = socket.IPPROTO_TCP
|
proto = socket.IPPROTO_TCP
|
||||||
elif _is_dgram_socket(type):
|
elif type == socket.SOCK_DGRAM:
|
||||||
proto = socket.IPPROTO_UDP
|
proto = socket.IPPROTO_UDP
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
@ -759,7 +739,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
if sock is None:
|
if sock is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'host and port was not specified and no sock specified')
|
'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
|
# We allow AF_INET, AF_INET6, AF_UNIX as long as they
|
||||||
# are SOCK_STREAM.
|
# are SOCK_STREAM.
|
||||||
# We support passing AF_UNIX sockets even though we have
|
# We support passing AF_UNIX sockets even though we have
|
||||||
|
@ -809,7 +789,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
allow_broadcast=None, sock=None):
|
allow_broadcast=None, sock=None):
|
||||||
"""Create datagram connection."""
|
"""Create datagram connection."""
|
||||||
if sock is not None:
|
if sock is not None:
|
||||||
if not _is_dgram_socket(sock.type):
|
if sock.type != socket.SOCK_DGRAM:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f'A UDP Socket was expected, got {sock!r}')
|
f'A UDP Socket was expected, got {sock!r}')
|
||||||
if (local_addr or remote_addr or
|
if (local_addr or remote_addr or
|
||||||
|
@ -1037,7 +1017,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
else:
|
else:
|
||||||
if sock is None:
|
if sock is None:
|
||||||
raise ValueError('Neither host/port nor sock were specified')
|
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}')
|
raise ValueError(f'A Stream Socket was expected, got {sock!r}')
|
||||||
sockets = [sock]
|
sockets = [sock]
|
||||||
|
|
||||||
|
@ -1060,7 +1040,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
This method is a coroutine. When completed, the coroutine
|
This method is a coroutine. When completed, the coroutine
|
||||||
returns a (transport, protocol) pair.
|
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}')
|
raise ValueError(f'A Stream Socket was expected, got {sock!r}')
|
||||||
|
|
||||||
transport, protocol = await self._create_connection_transport(
|
transport, protocol = await self._create_connection_transport(
|
||||||
|
|
|
@ -41,7 +41,7 @@ def _test_selector_event(selector, fd, event):
|
||||||
if hasattr(socket, 'TCP_NODELAY'):
|
if hasattr(socket, 'TCP_NODELAY'):
|
||||||
def _set_nodelay(sock):
|
def _set_nodelay(sock):
|
||||||
if (sock.family in {socket.AF_INET, socket.AF_INET6} and
|
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.proto == socket.IPPROTO_TCP):
|
||||||
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -222,7 +222,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
|
||||||
if sock is None:
|
if sock is None:
|
||||||
raise ValueError('no path and sock were specified')
|
raise ValueError('no path and sock were specified')
|
||||||
if (sock.family != socket.AF_UNIX or
|
if (sock.family != socket.AF_UNIX or
|
||||||
not base_events._is_stream_socket(sock.type)):
|
sock.type != socket.SOCK_STREAM):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
|
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
|
||||||
sock.setblocking(False)
|
sock.setblocking(False)
|
||||||
|
@ -276,7 +276,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
|
||||||
'path was not specified, and no sock specified')
|
'path was not specified, and no sock specified')
|
||||||
|
|
||||||
if (sock.family != socket.AF_UNIX or
|
if (sock.family != socket.AF_UNIX or
|
||||||
not base_events._is_stream_socket(sock.type)):
|
sock.type != socket.SOCK_STREAM):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
|
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue