#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.
This commit is contained in:
R. David Murray 2010-06-03 20:19:25 +00:00
parent 61746d580e
commit 4653fb556c
2 changed files with 26 additions and 8 deletions

View File

@ -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

View File

@ -105,7 +105,10 @@ Extension Modules
Tests
-----
- Issue #8835: test_support.transient_internet() catchs gaierror(EAI_NONAME)
- Issue #8889: test_support.transient_internet rewritten so that the new
checks also work on FreeBSD, which lacks EAI_NODATA.
- Issue #8835: test_support.transient_internet() catches gaierror(EAI_NONAME)
and gaierror(EAI_NODATA)
- Issue #7449: Skip test_socketserver if threading support is disabled