[Python-checkins] bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634)

Berker Peksag webhook-mailer at python.org
Mon Aug 6 22:38:08 EDT 2018


https://github.com/python/cpython/commit/671a13a7b6ff1022a6fd868e5842687123ab9fd1
commit: 671a13a7b6ff1022a6fd868e5842687123ab9fd1
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Berker Peksag <berker.peksag at gmail.com>
date: 2018-08-07T05:38:05+03:00
summary:

bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634)

(cherry picked from commit e4dcbbd7f4ac18d01c0ec85f64ae98b8281ed403)

Co-authored-by: Berker Peksag <berker.peksag at gmail.com>

files:
A Misc/NEWS.d/next/Library/2018-08-02-21-28-38.bpo-18540.AryoYY.rst
M Lib/imaplib.py
M Lib/test/test_imaplib.py

diff --git a/Lib/imaplib.py b/Lib/imaplib.py
index 700b19bbc5af..b9a01d636f78 100644
--- a/Lib/imaplib.py
+++ b/Lib/imaplib.py
@@ -282,7 +282,11 @@ def __exit__(self, *args):
 
 
     def _create_socket(self):
-        return socket.create_connection((self.host, self.port))
+        # Default value of IMAP4.host is '', but socket.getaddrinfo()
+        # (which is used by socket.create_connection()) expects None
+        # as a default value for host.
+        host = None if not self.host else self.host
+        return socket.create_connection((host, self.port))
 
     def open(self, host = '', port = IMAP4_PORT):
         """Setup connection to remote server on "host:port"
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index 93ba61a1a99e..0593a3756b08 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -5,6 +5,7 @@
 threading = support.import_module('threading')
 
 from contextlib import contextmanager
+import errno
 import imaplib
 import os.path
 import socketserver
@@ -73,6 +74,19 @@ def test_that_Time2Internaldate_returns_a_result(self):
         for t in self.timevalues():
             imaplib.Time2Internaldate(t)
 
+    def test_imap4_host_default_value(self):
+        expected_errnos = [
+            # This is the exception that should be raised.
+            errno.ECONNREFUSED,
+        ]
+        if hasattr(errno, 'EADDRNOTAVAIL'):
+            # socket.create_connection() fails randomly with
+            # EADDRNOTAVAIL on Travis CI.
+            expected_errnos.append(errno.EADDRNOTAVAIL)
+        with self.assertRaises(OSError) as cm:
+            imaplib.IMAP4()
+        self.assertIn(cm.exception.errno, expected_errnos)
+
 
 if ssl:
     class SecureTCPServer(socketserver.TCPServer):
diff --git a/Misc/NEWS.d/next/Library/2018-08-02-21-28-38.bpo-18540.AryoYY.rst b/Misc/NEWS.d/next/Library/2018-08-02-21-28-38.bpo-18540.AryoYY.rst
new file mode 100644
index 000000000000..3ffd9f6a3754
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-08-02-21-28-38.bpo-18540.AryoYY.rst
@@ -0,0 +1,3 @@
+The :class:`imaplib.IMAP4` and :class:`imaplib.IMAP4_SSL` classes now
+resolve to the local host IP correctly when the default value of *host*
+parameter (``''``) is used.



More information about the Python-checkins mailing list