Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,22 @@ def __exit__(self, type_=None, value=None, traceback=None):
ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)


def get_socket_conn_refused_errs():
"""
Get the different socket error numbers ('errno') which can be received
when a connection is refused.
"""
errors = [errno.ECONNREFUSED]
if hasattr(errno, 'ENETUNREACH'):
# On Solaris, ENETUNREACH is returned sometimes instead of ECONNREFUSED
errors.append(errno.ENETUNREACH)
if hasattr(errno, 'EADDRNOTAVAIL'):
# bpo-31910: socket.create_connection() fails randomly
# with EADDRNOTAVAIL on Travis CI
errors.append(errno.EADDRNOTAVAIL)
return errors


@contextlib.contextmanager
def transient_internet(resource_name, *, timeout=30.0, errnos=()):
"""Return a context manager that raises ResourceDenied when various issues
Expand Down
10 changes: 2 additions & 8 deletions Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,8 @@ def test_imap4_host_default_value(self):
except socket.error:
pass

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)
# This is the exception that should be raised.
expected_errnos = support.get_socket_conn_refused_errs()
with self.assertRaises(OSError) as cm:
imaplib.IMAP4()
self.assertIn(cm.exception.errno, expected_errnos)
Expand Down
9 changes: 1 addition & 8 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4720,14 +4720,7 @@ def test_create_connection(self):
# On Solaris, ENETUNREACH is returned in this circumstance instead
# of ECONNREFUSED. So, if that errno exists, add it to our list of
# expected errnos.
expected_errnos = [ errno.ECONNREFUSED, ]
if hasattr(errno, 'ENETUNREACH'):
expected_errnos.append(errno.ENETUNREACH)
if hasattr(errno, 'EADDRNOTAVAIL'):
# bpo-31910: socket.create_connection() fails randomly
# with EADDRNOTAVAIL on Travis CI
expected_errnos.append(errno.EADDRNOTAVAIL)

expected_errnos = support.get_socket_conn_refused_errs()
self.assertIn(cm.exception.errno, expected_errnos)

def test_create_connection_timeout(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``test_imap4_host_default_value()`` of ``test_imaplib``: catch also
:data:`errno.ENETUNREACH` error.