]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
asyncio: Fix getaddrinfo to accept None/str/bytes for 'port' arg
authorYury Selivanov <yselivanov@sprymix.com>
Fri, 20 May 2016 21:44:19 +0000 (17:44 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Fri, 20 May 2016 21:44:19 +0000 (17:44 -0400)
Patch by A. Jesse Jiryu Davis.

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

index 313cc316e533e82e05106ee21b3d2161d9765dbc..e5e9394835f7e0007804b2b7c9b9a5965fff27bb 100644 (file)
@@ -102,6 +102,11 @@ def _ipaddr_info(host, port, family, type, proto):
     else:
         return None
 
+    if port in {None, ''}:
+        port = 0
+    elif isinstance(port, (bytes, str)):
+        port = int(port)
+
     if hasattr(socket, 'inet_pton'):
         if family == socket.AF_UNSPEC:
             afs = [socket.AF_INET, socket.AF_INET6]
index ef93dc0e033319667c7872a49c0aaf635a76d753..678ba30e39b440464c73b6adf58d1b9f65820c29 100644 (file)
@@ -120,6 +120,28 @@ class BaseEventTests(test_utils.TestCase):
             (INET6, STREAM, TCP, '', ('::3%lo0', 1)),
             base_events._ipaddr_info('::3%lo0', 1, INET6, STREAM, TCP))
 
+    def test_port_parameter_types(self):
+        # Test obscure kinds of arguments for "port".
+        INET = socket.AF_INET
+        STREAM = socket.SOCK_STREAM
+        TCP = socket.IPPROTO_TCP
+
+        self.assertEqual(
+            (INET, STREAM, TCP, '', ('1.2.3.4', 0)),
+            base_events._ipaddr_info('1.2.3.4', None, INET, STREAM, TCP))
+
+        self.assertEqual(
+            (INET, STREAM, TCP, '', ('1.2.3.4', 0)),
+            base_events._ipaddr_info('1.2.3.4', '', INET, STREAM, TCP))
+
+        self.assertEqual(
+            (INET, STREAM, TCP, '', ('1.2.3.4', 1)),
+            base_events._ipaddr_info('1.2.3.4', '1', INET, STREAM, TCP))
+
+        self.assertEqual(
+            (INET, STREAM, TCP, '', ('1.2.3.4', 1)),
+            base_events._ipaddr_info('1.2.3.4', b'1', INET, STREAM, TCP))
+
     @patch_socket
     def test_ipaddr_info_no_inet_pton(self, m_socket):
         del m_socket.inet_pton