summaryrefslogtreecommitdiffstats
path: root/Lib/test/support
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-04-29 07:36:20 (GMT)
committerGitHub <noreply@github.com>2020-04-29 07:36:20 (GMT)
commitbfb1cf44658934cbcd9707fb717d6770c78fbeb3 (patch)
treea9dcce79d42f76509491b3d8b165f6737143cbc5 /Lib/test/support
parentbb4a585d903e7fe0a46ded8c2ee3f47435ad6a66 (diff)
downloadcpython-bfb1cf44658934cbcd9707fb717d6770c78fbeb3.zip
cpython-bfb1cf44658934cbcd9707fb717d6770c78fbeb3.tar.gz
cpython-bfb1cf44658934cbcd9707fb717d6770c78fbeb3.tar.bz2
bpo-40275: Move transient_internet from test.support to socket_helper (GH-19711)
Diffstat (limited to 'Lib/test/support')
-rw-r--r--Lib/test/support/__init__.py88
-rw-r--r--Lib/test/support/socket_helper.py89
2 files changed, 90 insertions, 87 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index ee5882f..bd21574 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -78,7 +78,7 @@ __all__ = [
"requires_linux_version", "requires_mac_ver",
"check_syntax_error", "check_syntax_warning",
"TransientResource", "time_out", "socket_peer_reset", "ioerror_peer_reset",
- "transient_internet", "BasicTestRunner", "run_unittest", "run_doctest",
+ "BasicTestRunner", "run_unittest", "run_doctest",
"skip_unless_symlink", "requires_gzip", "requires_bz2", "requires_lzma",
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
"requires_IEEE_754", "skip_unless_xattr", "requires_zlib",
@@ -144,8 +144,6 @@ SHORT_TIMEOUT = 30.0
# option.
LONG_TIMEOUT = 5 * 60.0
-_NOT_SET = object()
-
class Error(Exception):
"""Base class for regression test exceptions."""
@@ -1387,90 +1385,6 @@ ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
@contextlib.contextmanager
-def transient_internet(resource_name, *, timeout=_NOT_SET, errnos=()):
- """Return a context manager that raises ResourceDenied when various issues
- with the Internet connection manifest themselves as exceptions."""
- import socket
- import nntplib
- import urllib.error
- if timeout is _NOT_SET:
- timeout = INTERNET_TIMEOUT
-
- default_errnos = [
- ('ECONNREFUSED', 111),
- ('ECONNRESET', 104),
- ('EHOSTUNREACH', 113),
- ('ENETUNREACH', 101),
- ('ETIMEDOUT', 110),
- # socket.create_connection() fails randomly with
- # EADDRNOTAVAIL on Travis CI.
- ('EADDRNOTAVAIL', 99),
- ]
- default_gai_errnos = [
- ('EAI_AGAIN', -3),
- ('EAI_FAIL', -4),
- ('EAI_NONAME', -2),
- ('EAI_NODATA', -5),
- # Encountered when trying to resolve IPv6-only hostnames
- ('WSANO_DATA', 11004),
- ]
-
- denied = ResourceDenied("Resource %r is not available" % resource_name)
- captured_errnos = errnos
- gai_errnos = []
- if not captured_errnos:
- captured_errnos = [getattr(errno, name, num)
- for (name, num) in default_errnos]
- gai_errnos = [getattr(socket, name, num)
- for (name, num) in default_gai_errnos]
-
- def filter_error(err):
- n = getattr(err, 'errno', None)
- if (isinstance(err, socket.timeout) or
- (isinstance(err, socket.gaierror) and n in gai_errnos) or
- (isinstance(err, urllib.error.HTTPError) and
- 500 <= err.code <= 599) or
- (isinstance(err, urllib.error.URLError) and
- (("ConnectionRefusedError" in err.reason) or
- ("TimeoutError" in err.reason) or
- ("EOFError" in err.reason))) or
- n in captured_errnos):
- if not verbose:
- sys.stderr.write(denied.args[0] + "\n")
- raise denied from err
-
- old_timeout = socket.getdefaulttimeout()
- try:
- if timeout is not None:
- socket.setdefaulttimeout(timeout)
- yield
- except nntplib.NNTPTemporaryError as err:
- if verbose:
- sys.stderr.write(denied.args[0] + "\n")
- raise denied from err
- except OSError as err:
- # urllib can wrap original socket errors multiple times (!), we must
- # unwrap to get at the original error.
- while True:
- a = err.args
- if len(a) >= 1 and isinstance(a[0], OSError):
- err = a[0]
- # The error can also be wrapped as args[1]:
- # except socket.error as msg:
- # raise OSError('socket error', msg).with_traceback(sys.exc_info()[2])
- elif len(a) >= 2 and isinstance(a[1], OSError):
- err = a[1]
- else:
- break
- filter_error(err)
- raise
- # XXX should we catch generic exceptions and look for their
- # __cause__ or __context__?
- finally:
- socket.setdefaulttimeout(old_timeout)
-
-
-@contextlib.contextmanager
def captured_output(stream_name):
"""Return a context manager used by captured_stdout/stdin/stderr
that temporarily replaces the sys stream *stream_name* with a StringIO."""
diff --git a/Lib/test/support/socket_helper.py b/Lib/test/support/socket_helper.py
index 5f4a7f1..b09c248 100644
--- a/Lib/test/support/socket_helper.py
+++ b/Lib/test/support/socket_helper.py
@@ -1,7 +1,11 @@
+import contextlib
import errno
import socket
import unittest
+from .. import support
+
+
HOST = "localhost"
HOSTv4 = "127.0.0.1"
HOSTv6 = "::1"
@@ -175,3 +179,88 @@ def get_socket_conn_refused_errs():
if not IPV6_ENABLED:
errors.append(errno.EAFNOSUPPORT)
return errors
+
+
+_NOT_SET = object()
+
+@contextlib.contextmanager
+def transient_internet(resource_name, *, timeout=_NOT_SET, errnos=()):
+ """Return a context manager that raises ResourceDenied when various issues
+ with the Internet connection manifest themselves as exceptions."""
+ import nntplib
+ import urllib.error
+ if timeout is _NOT_SET:
+ timeout = support.INTERNET_TIMEOUT
+
+ default_errnos = [
+ ('ECONNREFUSED', 111),
+ ('ECONNRESET', 104),
+ ('EHOSTUNREACH', 113),
+ ('ENETUNREACH', 101),
+ ('ETIMEDOUT', 110),
+ # socket.create_connection() fails randomly with
+ # EADDRNOTAVAIL on Travis CI.
+ ('EADDRNOTAVAIL', 99),
+ ]
+ default_gai_errnos = [
+ ('EAI_AGAIN', -3),
+ ('EAI_FAIL', -4),
+ ('EAI_NONAME', -2),
+ ('EAI_NODATA', -5),
+ # Encountered when trying to resolve IPv6-only hostnames
+ ('WSANO_DATA', 11004),
+ ]
+
+ denied = support.ResourceDenied("Resource %r is not available" % resource_name)
+ captured_errnos = errnos
+ gai_errnos = []
+ if not captured_errnos:
+ captured_errnos = [getattr(errno, name, num)
+ for (name, num) in default_errnos]
+ gai_errnos = [getattr(socket, name, num)
+ for (name, num) in default_gai_errnos]
+
+ def filter_error(err):
+ n = getattr(err, 'errno', None)
+ if (isinstance(err, socket.timeout) or
+ (isinstance(err, socket.gaierror) and n in gai_errnos) or
+ (isinstance(err, urllib.error.HTTPError) and
+ 500 <= err.code <= 599) or
+ (isinstance(err, urllib.error.URLError) and
+ (("ConnectionRefusedError" in err.reason) or
+ ("TimeoutError" in err.reason) or
+ ("EOFError" in err.reason))) or
+ n in captured_errnos):
+ if not support.verbose:
+ sys.stderr.write(denied.args[0] + "\n")
+ raise denied from err
+
+ old_timeout = socket.getdefaulttimeout()
+ try:
+ if timeout is not None:
+ socket.setdefaulttimeout(timeout)
+ yield
+ except nntplib.NNTPTemporaryError as err:
+ if support.verbose:
+ sys.stderr.write(denied.args[0] + "\n")
+ raise denied from err
+ except OSError as err:
+ # urllib can wrap original socket errors multiple times (!), we must
+ # unwrap to get at the original error.
+ while True:
+ a = err.args
+ if len(a) >= 1 and isinstance(a[0], OSError):
+ err = a[0]
+ # The error can also be wrapped as args[1]:
+ # except socket.error as msg:
+ # raise OSError('socket error', msg).with_traceback(sys.exc_info()[2])
+ elif len(a) >= 2 and isinstance(a[1], OSError):
+ err = a[1]
+ else:
+ break
+ filter_error(err)
+ raise
+ # XXX should we catch generic exceptions and look for their
+ # __cause__ or __context__?
+ finally:
+ socket.setdefaulttimeout(old_timeout)