[Python-checkins] cpython (3.5): asyncio: Fix getaddrinfo to accept service names (for port)

yury.selivanov python-checkins at python.org
Thu Jun 2 16:52:11 EDT 2016


https://hg.python.org/cpython/rev/3ec208c01418
changeset:   101619:3ec208c01418
branch:      3.5
parent:      101617:ad7766f342a7
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Thu Jun 02 16:51:07 2016 -0400
summary:
  asyncio: Fix getaddrinfo to accept service names (for port)

Patch by A. Jesse Jiryu Davis

files:
  Lib/asyncio/base_events.py                |  22 +++++++++-
  Lib/test/test_asyncio/test_base_events.py |  20 ++++++++++
  2 files changed, 39 insertions(+), 3 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,10 +102,26 @@
     else:
         return None
 
-    if port in {None, '', b''}:
+    if port is None:
         port = 0
-    elif isinstance(port, (bytes, str)):
-        port = int(port)
+    elif isinstance(port, bytes):
+        if port == b'':
+            port = 0
+        else:
+            try:
+                port = int(port)
+            except ValueError:
+                # Might be a service name like b"http".
+                port = socket.getservbyname(port.decode('ascii'))
+    elif isinstance(port, str):
+        if port == '':
+            port = 0
+        else:
+            try:
+                port = int(port)
+            except ValueError:
+                # Might be a service name like "http".
+                port = socket.getservbyname(port)
 
     if hasattr(socket, 'inet_pton'):
         if family == socket.AF_UNSPEC:
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
@@ -146,6 +146,26 @@
             (INET, STREAM, TCP, '', ('1.2.3.4', 1)),
             base_events._ipaddr_info('1.2.3.4', b'1', INET, STREAM, TCP))
 
+    def test_getaddrinfo_servname(self):
+        INET = socket.AF_INET
+        STREAM = socket.SOCK_STREAM
+        TCP = socket.IPPROTO_TCP
+
+        self.assertEqual(
+            (INET, STREAM, TCP, '', ('1.2.3.4', 80)),
+            base_events._ipaddr_info('1.2.3.4', 'http', INET, STREAM, TCP))
+
+        self.assertEqual(
+            (INET, STREAM, TCP, '', ('1.2.3.4', 80)),
+            base_events._ipaddr_info('1.2.3.4', b'http', INET, STREAM, TCP))
+
+        # Raises "service/proto not found".
+        with self.assertRaises(OSError):
+            base_events._ipaddr_info('1.2.3.4', 'nonsense', INET, STREAM, TCP)
+
+        with self.assertRaises(OSError):
+            base_events._ipaddr_info('1.2.3.4', 'nonsense', 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