summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-06-03 20:19:25 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-06-03 20:19:25 (GMT)
commit4653fb556c5dacdbd4d8afadd2f73a6c0b394157 (patch)
tree63ae428bc786c6f37d1bf9f9f4395d5b10855d2b /Lib/test
parent61746d580e956bc2dda7fcf230a581e463a4d186 (diff)
downloadcpython-4653fb556c5dacdbd4d8afadd2f73a6c0b394157.zip
cpython-4653fb556c5dacdbd4d8afadd2f73a6c0b394157.tar.gz
cpython-4653fb556c5dacdbd4d8afadd2f73a6c0b394157.tar.bz2
#8889: rewrite transient_internet so we don't use EAI_NODATA on FreeBSD.
FreeBSD doesn't have socket.EAI_NODATA. I rewrote the routine because there's no easy way to conditionally include a context manager in a with statement. As a side benefit, instead of a stack of context managers there's now only one.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_support.py29
1 files changed, 22 insertions, 7 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 8add116..6e7853b 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -750,17 +750,32 @@ class TransientResource(object):
raise ResourceDenied("an optional resource is not available")
+_transients = {
+ IOError: (errno.ECONNRESET, errno.ETIMEDOUT),
+ socket.error: (errno.ECONNRESET,),
+ socket.gaierror: [getattr(socket, t)
+ for t in ('EAI_NODATA', 'EAI_NONAME')
+ if hasattr(socket, t)],
+ }
@contextlib.contextmanager
def transient_internet():
"""Return a context manager that raises ResourceDenied when various issues
- with the Internet connection manifest themselves as exceptions."""
- time_out = TransientResource(IOError, errno=errno.ETIMEDOUT)
- socket_peer_reset = TransientResource(socket.error, errno=errno.ECONNRESET)
- ioerror_peer_reset = TransientResource(IOError, errno=errno.ECONNRESET)
- dns_nodata = TransientResource(socket.gaierror, errno=socket.EAI_NODATA)
- dns_noname = TransientResource(socket.gaierror, errno=socket.EAI_NONAME)
- with time_out, socket_peer_reset, ioerror_peer_reset, dns_nodata, dns_noname:
+ with the Internet connection manifest themselves as exceptions.
+
+ Errors caught:
+ timeout IOError errno = ETIMEDOUT
+ socket reset socket.error, IOError errno = ECONNRESET
+ dns no data socket.gaierror errno = EAI_NODATA
+ dns no name socket.gaierror errno = EAI_NONAME
+ """
+ try:
yield
+ except tuple(_transients) as err:
+ for errtype in _transients:
+ if isinstance(err, errtype) and err.errno in _transients[errtype]:
+ raise ResourceDenied("could not establish network "
+ "connection ({})".format(err))
+ raise
@contextlib.contextmanager