summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-04-15 10:34:53 (GMT)
committerGitHub <noreply@github.com>2019-04-15 10:34:53 (GMT)
commit3c7931e514faf509a39c218c2c9f55efb434628f (patch)
tree0c4bdae0b4ecd84d1ea036893ed70747137ee3f6 /Lib
parentf1464f4d2ecf9b809ff768c523c5eea1abd31c55 (diff)
downloadcpython-3c7931e514faf509a39c218c2c9f55efb434628f.zip
cpython-3c7931e514faf509a39c218c2c9f55efb434628f.tar.gz
cpython-3c7931e514faf509a39c218c2c9f55efb434628f.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/support/__init__.py16
-rw-r--r--Lib/test/test_imaplib.py10
-rw-r--r--Lib/test/test_socket.py9
3 files changed, 19 insertions, 16 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 5bd15a2..2bb561b 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1477,6 +1477,22 @@ socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
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
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index a060143..aec36af 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -81,14 +81,8 @@ class TestImaplib(unittest.TestCase):
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)
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index b0bdb11..815f9ad 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -4804,14 +4804,7 @@ class NetworkConnectionNoServer(unittest.TestCase):
# 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):