[Python-checkins] cpython (3.5): asyncio: Fix getaddrinfo to accept None/str/bytes for 'port' arg

yury.selivanov python-checkins at python.org
Fri May 20 17:44:54 EDT 2016


https://hg.python.org/cpython/rev/1570e3855ce8
changeset:   101459:1570e3855ce8
branch:      3.5
parent:      101457:e715ffd2d938
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Fri May 20 17:44:19 2016 -0400
summary:
  asyncio: Fix getaddrinfo to accept None/str/bytes for 'port' arg

Patch by A. Jesse Jiryu Davis.

files:
  Lib/asyncio/base_events.py                |   5 ++
  Lib/test/test_asyncio/test_base_events.py |  22 +++++++++++
  2 files changed, 27 insertions(+), 0 deletions(-)


diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -102,6 +102,11 @@
     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]
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -120,6 +120,28 @@
             (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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list