bpo-18540: Fix EAI_NONAME in imaplib.IMAP4*() (GH-8634)
(cherry picked from commit e4dcbbd7f4
)
Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
This commit is contained in:
parent
b481aeeff7
commit
5799e5a84c
|
@ -282,7 +282,11 @@ class IMAP4:
|
||||||
|
|
||||||
|
|
||||||
def _create_socket(self):
|
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):
|
def open(self, host = '', port = IMAP4_PORT):
|
||||||
"""Setup connection to remote server on "host:port"
|
"""Setup connection to remote server on "host:port"
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from test import support
|
from test import support
|
||||||
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
import errno
|
||||||
import imaplib
|
import imaplib
|
||||||
import os.path
|
import os.path
|
||||||
import socketserver
|
import socketserver
|
||||||
|
@ -69,6 +70,19 @@ class TestImaplib(unittest.TestCase):
|
||||||
for t in self.timevalues():
|
for t in self.timevalues():
|
||||||
imaplib.Time2Internaldate(t)
|
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:
|
if ssl:
|
||||||
class SecureTCPServer(socketserver.TCPServer):
|
class SecureTCPServer(socketserver.TCPServer):
|
||||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue