diff --git a/trio/_network.py b/trio/_network.py index 6df9c666c7..ee5f80c0df 100644 --- a/trio/_network.py +++ b/trio/_network.py @@ -56,7 +56,7 @@ class SocketStream(HalfCloseableStream): def __init__(self, sock): if not tsocket.is_trio_socket(sock): raise TypeError("SocketStream requires trio socket object") - if sock._real_type != tsocket.SOCK_STREAM: + if tsocket._real_type(sock.type) != tsocket.SOCK_STREAM: raise ValueError("SocketStream requires a SOCK_STREAM socket") try: sock.getpeername() diff --git a/trio/socket.py b/trio/socket.py index 412e52f2fd..58e7f0a889 100644 --- a/trio/socket.py +++ b/trio/socket.py @@ -229,6 +229,13 @@ def is_trio_socket(obj): getattr(_stdlib_socket, "SOCK_NONBLOCK", 0) | getattr(_stdlib_socket, "SOCK_CLOEXEC", 0)) +# Hopefully Python will eventually make something like this public +# (see bpo-21327) but I don't want to make it public myself and then +# find out they picked a different name... this is used internally in +# this file and also elsewhere in trio. +def _real_type(type_num): + return type_num & _SOCK_TYPE_MASK + class _SocketType: def __init__(self, sock): if type(sock) is not _stdlib_socket.socket: @@ -241,12 +248,6 @@ def __init__(self, sock): self._sock.setblocking(False) self._did_SHUT_WR = False - # Hopefully Python will eventually make something like this public - # (see bpo-21327) but I don't want to make it public myself and then - # find out they picked a different name... this is used internally in - # this file and also elsewhere in trio. - self._real_type = sock.type & _SOCK_TYPE_MASK - # Defaults: if self._sock.family == AF_INET6: try: @@ -362,7 +363,7 @@ def _check_address(self, address, *, require_resolved): _stdlib_socket.getaddrinfo( address[0], address[1], self._sock.family, - self._real_type, + _real_type(self._sock.type), self._sock.proto, flags=_NUMERIC_ONLY) except gaierror as exc: @@ -399,7 +400,7 @@ async def _resolve_address(self, address, flags): gai_res = await getaddrinfo( address[0], address[1], self._sock.family, - self._real_type, + _real_type(self._sock.type), self._sock.proto, flags) # AFAICT from the spec it's not possible for getaddrinfo to return an