]> granicus.if.org Git - python/commitdiff
Issue #28652: Partially rollback previous changes
authorYury Selivanov <yury@magic.io>
Mon, 21 Nov 2016 22:47:27 +0000 (17:47 -0500)
committerYury Selivanov <yury@magic.io>
Mon, 21 Nov 2016 22:47:27 +0000 (17:47 -0500)
Allow AF_UNIX in create_server & create_connection

Lib/asyncio/base_events.py
Lib/test/test_asyncio/test_base_events.py

index aa7836713a6fd77c272fdd6402907762e71a485a..50153f8d4bd291fa42a4267c99353eb25c966ba0 100644 (file)
@@ -98,14 +98,6 @@ def _is_dgram_socket(sock):
     return (sock.type & socket.SOCK_DGRAM) == socket.SOCK_DGRAM
 
 
-def _is_ip_socket(sock):
-    if sock.family == socket.AF_INET:
-        return True
-    if hasattr(socket, 'AF_INET6') and sock.family == socket.AF_INET6:
-        return True
-    return False
-
-
 def _ipaddr_info(host, port, family, type, proto):
     # 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.
@@ -795,9 +787,15 @@ class BaseEventLoop(events.AbstractEventLoop):
             if sock is None:
                 raise ValueError(
                     'host and port was not specified and no sock specified')
-            if not _is_stream_socket(sock) or not _is_ip_socket(sock):
+            if not _is_stream_socket(sock):
+                # We allow AF_INET, AF_INET6, AF_UNIX as long as they
+                # are SOCK_STREAM.
+                # We support passing AF_UNIX sockets even though we have
+                # a dedicated API for that: create_unix_connection.
+                # Disallowing AF_UNIX in this method, breaks backwards
+                # compatibility.
                 raise ValueError(
-                    'A TCP Stream Socket was expected, got {!r}'.format(sock))
+                    'A Stream Socket was expected, got {!r}'.format(sock))
 
         transport, protocol = yield from self._create_connection_transport(
             sock, protocol_factory, ssl, server_hostname)
@@ -1054,9 +1052,9 @@ class BaseEventLoop(events.AbstractEventLoop):
         else:
             if sock is None:
                 raise ValueError('Neither host/port nor sock were specified')
-            if not _is_stream_socket(sock) or not _is_ip_socket(sock):
+            if not _is_stream_socket(sock):
                 raise ValueError(
-                    'A TCP Stream Socket was expected, got {!r}'.format(sock))
+                    'A Stream Socket was expected, got {!r}'.format(sock))
             sockets = [sock]
 
         server = Server(self, sockets)
index 2a93923f8cd51338dc78382c38bb0044dbbbf09f..3f1ec651742e4374d547c3f20a98c3b262340de7 100644 (file)
@@ -1047,22 +1047,20 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
             MyProto, 'example.com', 80, sock=object())
         self.assertRaises(ValueError, self.loop.run_until_complete, coro)
 
-    @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
     def test_create_connection_wrong_sock(self):
-        sock = socket.socket(socket.AF_UNIX)
+        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         with sock:
             coro = self.loop.create_connection(MyProto, sock=sock)
             with self.assertRaisesRegex(ValueError,
-                                        'A TCP Stream Socket was expected'):
+                                        'A Stream Socket was expected'):
                 self.loop.run_until_complete(coro)
 
-    @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
     def test_create_server_wrong_sock(self):
-        sock = socket.socket(socket.AF_UNIX)
+        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         with sock:
             coro = self.loop.create_server(MyProto, sock=sock)
             with self.assertRaisesRegex(ValueError,
-                                        'A TCP Stream Socket was expected'):
+                                        'A Stream Socket was expected'):
                 self.loop.run_until_complete(coro)
 
     @unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'),