[Python-checkins] bpo-36629: Add support.get_socket_conn_refused_errs() (GH-12834)
Victor Stinner
webhook-mailer at python.org
Mon Apr 15 06:35:07 EDT 2019
https://github.com/python/cpython/commit/3c7931e514faf509a39c218c2c9f55efb434628f
commit: 3c7931e514faf509a39c218c2c9f55efb434628f
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2019年04月15日T12:34:53+02:00
summary:
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.
files:
A Misc/NEWS.d/next/Tests/2019-04-15-11-57-39.bpo-36629.ySnaL3.rst
M Lib/test/support/__init__.py
M Lib/test/test_imaplib.py
M Lib/test/test_socket.py
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 5bd15a2feae9..2bb561b4cee1 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1477,6 +1477,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
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index a060143e1f6b..aec36af6c525 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -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)
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index b0bdb11d9028..815f9adce677 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -4804,14 +4804,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):
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
index 000000000000..0837a233d582
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-04-15-11-57-39.bpo-36629.ySnaL3.rst
@@ -0,0 +1,2 @@
+Fix ``test_imap4_host_default_value()`` of ``test_imaplib``: catch also
+:data:`errno.ENETUNREACH` error.
More information about the Python-checkins
mailing list