Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit ab113e4

Browse files
committed
Allow AF_UNIX in loop.create_connection and loop.create_server.
PR #453.
1 parent fff05d4 commit ab113e4

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

asyncio/base_events.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,6 @@ def _is_dgram_socket(sock):
9898
return (sock.type & socket.SOCK_DGRAM) == socket.SOCK_DGRAM
9999

100100

101-
def _is_ip_socket(sock):
102-
if sock.family == socket.AF_INET:
103-
return True
104-
if hasattr(socket, 'AF_INET6') and sock.family == socket.AF_INET6:
105-
return True
106-
return False
107-
108-
109101
def _ipaddr_info(host, port, family, type, proto):
110102
# Try to skip getaddrinfo if "host" is already an IP. Users might have
111103
# handled name resolution in their own code and pass in resolved IPs.
@@ -795,9 +787,15 @@ def create_connection(self, protocol_factory, host=None, port=None, *,
795787
if sock is None:
796788
raise ValueError(
797789
'host and port was not specified and no sock specified')
798-
if not _is_stream_socket(sock) or not _is_ip_socket(sock):
790+
if not _is_stream_socket(sock):
791+
# We allow AF_INET, AF_INET6, AF_UNIX as long as they
792+
# are SOCK_STREAM.
793+
# We support passing AF_UNIX sockets even though we have
794+
# a dedicated API for that: create_unix_connection.
795+
# Disallowing AF_UNIX in this method, breaks backwards
796+
# compatibility.
799797
raise ValueError(
800-
'A TCP Stream Socket was expected, got {!r}'.format(sock))
798+
'A Stream Socket was expected, got {!r}'.format(sock))
801799

802800
transport, protocol = yield from self._create_connection_transport(
803801
sock, protocol_factory, ssl, server_hostname)
@@ -1054,9 +1052,9 @@ def create_server(self, protocol_factory, host=None, port=None,
10541052
else:
10551053
if sock is None:
10561054
raise ValueError('Neither host/port nor sock were specified')
1057-
if not _is_stream_socket(sock) or not _is_ip_socket(sock):
1055+
if not _is_stream_socket(sock):
10581056
raise ValueError(
1059-
'A TCP Stream Socket was expected, got {!r}'.format(sock))
1057+
'A Stream Socket was expected, got {!r}'.format(sock))
10601058
sockets = [sock]
10611059

10621060
server = Server(self, sockets)

tests/test_base_events.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,22 +1047,20 @@ def test_create_connection_host_port_sock(self):
10471047
MyProto, 'example.com', 80, sock=object())
10481048
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
10491049

1050-
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
10511050
def test_create_connection_wrong_sock(self):
1052-
sock = socket.socket(socket.AF_UNIX)
1051+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
10531052
with sock:
10541053
coro = self.loop.create_connection(MyProto, sock=sock)
10551054
with self.assertRaisesRegex(ValueError,
1056-
'A TCP Stream Socket was expected'):
1055+
'A Stream Socket was expected'):
10571056
self.loop.run_until_complete(coro)
10581057

1059-
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
10601058
def test_create_server_wrong_sock(self):
1061-
sock = socket.socket(socket.AF_UNIX)
1059+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
10621060
with sock:
10631061
coro = self.loop.create_server(MyProto, sock=sock)
10641062
with self.assertRaisesRegex(ValueError,
1065-
'A TCP Stream Socket was expected'):
1063+
'A Stream Socket was expected'):
10661064
self.loop.run_until_complete(coro)
10671065

10681066
@unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'),

0 commit comments

Comments
 (0)