Skip to content

Commit e4dcbbd

Browse files
authored
bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634)
1 parent 3c1b590 commit e4dcbbd

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
@@ -1,6 +1,7 @@
11
from test import support
22

33
from contextlib import contextmanager
4+
import errno
45
import imaplib
56
import os.path
67
import socketserver
@@ -69,6 +70,19 @@ def test_that_Time2Internaldate_returns_a_result(self):
6970
for t in self.timevalues():
7071
imaplib.Time2Internaldate(t)
7172

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

7387
if ssl:
7488
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)