Skip to content

Commit 28ed39e

Browse files
bpo-36629: Add support.get_socket_conn_refused_errs() (GH-12834) (GH-12835)
Fix test_imap4_host_default_value() of test_imaplib: catch also errno.ENETUNREACH error. (cherry picked from commit 3c7931e) Co-authored-by: Victor Stinner <[email protected]>
1 parent 9f9e029 commit 28ed39e

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

Lib/test/support/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,22 @@ def __exit__(self, type_=None, value=None, traceback=None):
14091409
ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
14101410

14111411

1412+
def get_socket_conn_refused_errs():
1413+
"""
1414+
Get the different socket error numbers ('errno') which can be received
1415+
when a connection is refused.
1416+
"""
1417+
errors = [errno.ECONNREFUSED]
1418+
if hasattr(errno, 'ENETUNREACH'):
1419+
# On Solaris, ENETUNREACH is returned sometimes instead of ECONNREFUSED
1420+
errors.append(errno.ENETUNREACH)
1421+
if hasattr(errno, 'EADDRNOTAVAIL'):
1422+
# bpo-31910: socket.create_connection() fails randomly
1423+
# with EADDRNOTAVAIL on Travis CI
1424+
errors.append(errno.EADDRNOTAVAIL)
1425+
return errors
1426+
1427+
14121428
@contextlib.contextmanager
14131429
def transient_internet(resource_name, *, timeout=30.0, errnos=()):
14141430
"""Return a context manager that raises ResourceDenied when various issues

Lib/test/test_imaplib.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,8 @@ def test_imap4_host_default_value(self):
8181
except socket.error:
8282
pass
8383

84-
expected_errnos = [
85-
# This is the exception that should be raised.
86-
errno.ECONNREFUSED,
87-
]
88-
if hasattr(errno, 'EADDRNOTAVAIL'):
89-
# socket.create_connection() fails randomly with
90-
# EADDRNOTAVAIL on Travis CI.
91-
expected_errnos.append(errno.EADDRNOTAVAIL)
84+
# This is the exception that should be raised.
85+
expected_errnos = support.get_socket_conn_refused_errs()
9286
with self.assertRaises(OSError) as cm:
9387
imaplib.IMAP4()
9488
self.assertIn(cm.exception.errno, expected_errnos)

Lib/test/test_socket.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,14 +4720,7 @@ def test_create_connection(self):
47204720
# On Solaris, ENETUNREACH is returned in this circumstance instead
47214721
# of ECONNREFUSED. So, if that errno exists, add it to our list of
47224722
# expected errnos.
4723-
expected_errnos = [ errno.ECONNREFUSED, ]
4724-
if hasattr(errno, 'ENETUNREACH'):
4725-
expected_errnos.append(errno.ENETUNREACH)
4726-
if hasattr(errno, 'EADDRNOTAVAIL'):
4727-
# bpo-31910: socket.create_connection() fails randomly
4728-
# with EADDRNOTAVAIL on Travis CI
4729-
expected_errnos.append(errno.EADDRNOTAVAIL)
4730-
4723+
expected_errnos = support.get_socket_conn_refused_errs()
47314724
self.assertIn(cm.exception.errno, expected_errnos)
47324725

47334726
def test_create_connection_timeout(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``test_imap4_host_default_value()`` of ``test_imaplib``: catch also
2+
:data:`errno.ENETUNREACH` error.

0 commit comments

Comments
 (0)