Skip to content

Commit 671a13a

Browse files
bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634)
(cherry picked from commit e4dcbbd) Co-authored-by: Berker Peksag <[email protected]>
1 parent b0bf51b commit 671a13a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

‎Lib/imaplib.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ def __exit__(self, *args):
282282

283283

284284
def _create_socket(self):
285-
return socket.create_connection((self.host, self.port))
285+
# Default value of IMAP4.host is '', but socket.getaddrinfo()
286+
# (which is used by socket.create_connection()) expects None
287+
# as a default value for host.
288+
host = None if not self.host else self.host
289+
return socket.create_connection((host, self.port))
286290

287291
def open(self, host = '', port = IMAP4_PORT):
288292
"""Setup connection to remote server on "host:port"

‎Lib/test/test_imaplib.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
threading = support.import_module('threading')
66

77
from contextlib import contextmanager
8+
import errno
89
import imaplib
910
import os.path
1011
import socketserver
@@ -73,6 +74,19 @@ def test_that_Time2Internaldate_returns_a_result(self):
7374
for t in self.timevalues():
7475
imaplib.Time2Internaldate(t)
7576

77+
def test_imap4_host_default_value(self):
78+
expected_errnos = [
79+
# This is the exception that should be raised.
80+
errno.ECONNREFUSED,
81+
]
82+
if hasattr(errno, 'EADDRNOTAVAIL'):
83+
# socket.create_connection() fails randomly with
84+
# EADDRNOTAVAIL on Travis CI.
85+
expected_errnos.append(errno.EADDRNOTAVAIL)
86+
with self.assertRaises(OSError) as cm:
87+
imaplib.IMAP4()
88+
self.assertIn(cm.exception.errno, expected_errnos)
89+
7690

7791
if ssl:
7892
class SecureTCPServer(socketserver.TCPServer):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :class:`imaplib.IMAP4` and :class:`imaplib.IMAP4_SSL` classes now
2+
resolve to the local host IP correctly when the default value of *host*
3+
parameter (``''``) is used.

0 commit comments

Comments
 (0)