]> granicus.if.org Git - python/commitdiff
bpo-36629: Add support.get_socket_conn_refused_errs() (GH-12834)
authorVictor Stinner <vstinner@redhat.com>
Mon, 15 Apr 2019 10:34:53 +0000 (12:34 +0200)
committerGitHub <noreply@github.com>
Mon, 15 Apr 2019 10:34:53 +0000 (12:34 +0200)
Fix test_imap4_host_default_value() of test_imaplib: catch also
errno.ENETUNREACH error.

Lib/test/support/__init__.py
Lib/test/test_imaplib.py
Lib/test/test_socket.py
Misc/NEWS.d/next/Tests/2019-04-15-11-57-39.bpo-36629.ySnaL3.rst [new file with mode: 0644]

index 5bd15a2feae9d7574d395a7ef9ee2d4e71be1969..2bb561b4cee1371ede367d2b6cabeabfd9244c8d 100644 (file)
@@ -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
index a060143e1f6be4327d342a06bd26ac41f0d559a4..aec36af6c525f1a644cde7350cf564ceabe2b2af 100644 (file)
@@ -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)
index b0bdb11d9028d7ff1fdabdc0755e67162e5208c0..815f9adce6772a3b021b54569975086e0f8e2f41 100644 (file)
@@ -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):
diff --git a/Misc/NEWS.d/next/Tests/2019-04-15-11-57-39.bpo-36629.ySnaL3.rst b/Misc/NEWS.d/next/Tests/2019-04-15-11-57-39.bpo-36629.ySnaL3.rst
new file mode 100644 (file)
index 0000000..0837a23
--- /dev/null
@@ -0,0 +1,2 @@
+Fix ``test_imap4_host_default_value()`` of ``test_imaplib``: catch also
+:data:`errno.ENETUNREACH` error.