bpo-27456: Simplify sock type checks (#4922) · python/cpython@a7bd64c (original) (raw)
`@@ -82,26 +82,6 @@ def _set_reuseport(sock):
`
82
82
`'SO_REUSEPORT defined but not implemented.')
`
83
83
``
84
84
``
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
``
-
105
85
`def _ipaddr_info(host, port, family, type, proto):
`
106
86
`# Try to skip getaddrinfo if "host" is already an IP. Users might have
`
107
87
`# handled name resolution in their own code and pass in resolved IPs.
`
`@@ -112,9 +92,9 @@ def _ipaddr_info(host, port, family, type, proto):
`
112
92
`host is None:
`
113
93
`return None
`
114
94
``
115
``
`-
if _is_stream_socket(type):
`
``
95
`+
if type == socket.SOCK_STREAM:
`
116
96
`proto = socket.IPPROTO_TCP
`
117
``
`-
elif _is_dgram_socket(type):
`
``
97
`+
elif type == socket.SOCK_DGRAM:
`
118
98
`proto = socket.IPPROTO_UDP
`
119
99
`else:
`
120
100
`return None
`
`@@ -759,7 +739,7 @@ async def create_connection(self, protocol_factory, host=None, port=None,
`
759
739
`if sock is None:
`
760
740
`raise ValueError(
`
761
741
`'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:
`
763
743
`# We allow AF_INET, AF_INET6, AF_UNIX as long as they
`
764
744
`# are SOCK_STREAM.
`
765
745
`# We support passing AF_UNIX sockets even though we have
`
`@@ -809,7 +789,7 @@ async def create_datagram_endpoint(self, protocol_factory,
`
809
789
`allow_broadcast=None, sock=None):
`
810
790
`"""Create datagram connection."""
`
811
791
`if sock is not None:
`
812
``
`-
if not _is_dgram_socket(sock.type):
`
``
792
`+
if sock.type != socket.SOCK_DGRAM:
`
813
793
`raise ValueError(
`
814
794
`f'A UDP Socket was expected, got {sock!r}')
`
815
795
`if (local_addr or remote_addr or
`
`@@ -1037,7 +1017,7 @@ async def create_server(self, protocol_factory, host=None, port=None,
`
1037
1017
`else:
`
1038
1018
`if sock is None:
`
1039
1019
`raise ValueError('Neither host/port nor sock were specified')
`
1040
``
`-
if not _is_stream_socket(sock.type):
`
``
1020
`+
if sock.type != socket.SOCK_STREAM:
`
1041
1021
`raise ValueError(f'A Stream Socket was expected, got {sock!r}')
`
1042
1022
`sockets = [sock]
`
1043
1023
``
`@@ -1060,7 +1040,7 @@ async def connect_accepted_socket(self, protocol_factory, sock,
`
1060
1040
` This method is a coroutine. When completed, the coroutine
`
1061
1041
` returns a (transport, protocol) pair.
`
1062
1042
` """
`
1063
``
`-
if not _is_stream_socket(sock.type):
`
``
1043
`+
if sock.type != socket.SOCK_STREAM:
`
1064
1044
`raise ValueError(f'A Stream Socket was expected, got {sock!r}')
`
1065
1045
``
1066
1046
`transport, protocol = await self._create_connection_transport(
`