Skip to content

Commit a7bd64c

Browse files
authored
bpo-27456: Simplify sock type checks (#4922)
Recent sock.type fix (see bug 32331) makes sock.type checks simpler in asyncio.
1 parent 5d86246 commit a7bd64c

File tree

3 files changed

+9
-29
lines changed

3 files changed

+9
-29
lines changed

Lib/asyncio/base_events.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,6 @@ def _set_reuseport(sock):
8282
'SO_REUSEPORT defined but not implemented.')
8383

8484

85-
def _is_stream_socket(sock_type):
86-
if hasattr(socket, 'SOCK_NONBLOCK'):
87-
# Linux's socket.type is a bitmask that can include extra info
88-
# about socket (like SOCK_NONBLOCK bit), therefore we can't do simple
89-
# `sock_type == socket.SOCK_STREAM`, see
90-
# https://github.com/torvalds/linux/blob/v4.13/include/linux/net.h#L77
91-
# for more details.
92-
return (sock_type & 0xF) == socket.SOCK_STREAM
93-
else:
94-
return sock_type == socket.SOCK_STREAM
95-
96-
97-
def _is_dgram_socket(sock_type):
98-
if hasattr(socket, 'SOCK_NONBLOCK'):
99-
# See the comment in `_is_stream_socket`.
100-
return (sock_type & 0xF) == socket.SOCK_DGRAM
101-
else:
102-
return sock_type == socket.SOCK_DGRAM
103-
104-
10585
def _ipaddr_info(host, port, family, type, proto):
10686
# Try to skip getaddrinfo if "host" is already an IP. Users might have
10787
# handled name resolution in their own code and pass in resolved IPs.
@@ -112,9 +92,9 @@ def _ipaddr_info(host, port, family, type, proto):
11292
host is None:
11393
return None
11494

115-
if _is_stream_socket(type):
95+
if type == socket.SOCK_STREAM:
11696
proto = socket.IPPROTO_TCP
117-
elif _is_dgram_socket(type):
97+
elif type == socket.SOCK_DGRAM:
11898
proto = socket.IPPROTO_UDP
11999
else:
120100
return None
@@ -759,7 +739,7 @@ async def create_connection(self, protocol_factory, host=None, port=None,
759739
if sock is None:
760740
raise ValueError(
761741
'host and port was not specified and no sock specified')
762-
if not _is_stream_socket(sock.type):
742+
if sock.type != socket.SOCK_STREAM:
763743
# We allow AF_INET, AF_INET6, AF_UNIX as long as they
764744
# are SOCK_STREAM.
765745
# We support passing AF_UNIX sockets even though we have
@@ -809,7 +789,7 @@ async def create_datagram_endpoint(self, protocol_factory,
809789
allow_broadcast=None, sock=None):
810790
"""Create datagram connection."""
811791
if sock is not None:
812-
if not _is_dgram_socket(sock.type):
792+
if sock.type != socket.SOCK_DGRAM:
813793
raise ValueError(
814794
f'A UDP Socket was expected, got {sock!r}')
815795
if (local_addr or remote_addr or
@@ -1037,7 +1017,7 @@ async def create_server(self, protocol_factory, host=None, port=None,
10371017
else:
10381018
if sock is None:
10391019
raise ValueError('Neither host/port nor sock were specified')
1040-
if not _is_stream_socket(sock.type):
1020+
if sock.type != socket.SOCK_STREAM:
10411021
raise ValueError(f'A Stream Socket was expected, got {sock!r}')
10421022
sockets = [sock]
10431023

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

10661046
transport, protocol = await self._create_connection_transport(

Lib/asyncio/selector_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _test_selector_event(selector, fd, event):
4141
if hasattr(socket, 'TCP_NODELAY'):
4242
def _set_nodelay(sock):
4343
if (sock.family in {socket.AF_INET, socket.AF_INET6} and
44-
base_events._is_stream_socket(sock.type) and
44+
sock.type == socket.SOCK_STREAM and
4545
sock.proto == socket.IPPROTO_TCP):
4646
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
4747
else:

Lib/asyncio/unix_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ async def create_unix_connection(self, protocol_factory, path=None, *,
222222
if sock is None:
223223
raise ValueError('no path and sock were specified')
224224
if (sock.family != socket.AF_UNIX or
225-
not base_events._is_stream_socket(sock.type)):
225+
sock.type != socket.SOCK_STREAM):
226226
raise ValueError(
227227
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
228228
sock.setblocking(False)
@@ -276,7 +276,7 @@ async def create_unix_server(self, protocol_factory, path=None, *,
276276
'path was not specified, and no sock specified')
277277

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

0 commit comments

Comments
 (0)