Skip to content

Commit 3c7931e

Browse files
authored
bpo-36629: Add support.get_socket_conn_refused_errs() (GH-12834)
Fix test_imap4_host_default_value() of test_imaplib: catch also errno.ENETUNREACH error.
1 parent f1464f4 commit 3c7931e

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
@@ -1477,6 +1477,22 @@ def __exit__(self, type_=None, value=None, traceback=None):
14771477
ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
14781478

14791479

1480+
def get_socket_conn_refused_errs():
1481+
"""
1482+
Get the different socket error numbers ('errno') which can be received
1483+
when a connection is refused.
1484+
"""
1485+
errors = [errno.ECONNREFUSED]
1486+
if hasattr(errno, 'ENETUNREACH'):
1487+
# On Solaris, ENETUNREACH is returned sometimes instead of ECONNREFUSED
1488+
errors.append(errno.ENETUNREACH)
1489+
if hasattr(errno, 'EADDRNOTAVAIL'):
1490+
# bpo-31910: socket.create_connection() fails randomly
1491+
# with EADDRNOTAVAIL on Travis CI
1492+
errors.append(errno.EADDRNOTAVAIL)
1493+
return errors
1494+
1495+
14801496
@contextlib.contextmanager
14811497
def transient_internet(resource_name, *, timeout=30.0, errnos=()):
14821498
"""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
@@ -4804,14 +4804,7 @@ def test_create_connection(self):
48044804
# On Solaris, ENETUNREACH is returned in this circumstance instead
48054805
# of ECONNREFUSED. So, if that errno exists, add it to our list of
48064806
# expected errnos.
4807-
expected_errnos = [ errno.ECONNREFUSED, ]
4808-
if hasattr(errno, 'ENETUNREACH'):
4809-
expected_errnos.append(errno.ENETUNREACH)
4810-
if hasattr(errno, 'EADDRNOTAVAIL'):
4811-
# bpo-31910: socket.create_connection() fails randomly
4812-
# with EADDRNOTAVAIL on Travis CI
4813-
expected_errnos.append(errno.EADDRNOTAVAIL)
4814-
4807+
expected_errnos = support.get_socket_conn_refused_errs()
48154808
self.assertIn(cm.exception.errno, expected_errnos)
48164809

48174810
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)