diff options
author | Brett Cannon <bcannon@gmail.com> | 2007-03-08 23:58:11 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2007-03-08 23:58:11 (GMT) |
commit | a30fcb4dae51705f182383a6ad5f3fb86a80f9fb (patch) | |
tree | d704230a35802a5f1a745becf7b62acecfe377df /Lib/test | |
parent | 9b2a10954037b30851322fac794ce098118f4f42 (diff) | |
download | cpython-a30fcb4dae51705f182383a6ad5f3fb86a80f9fb.zip cpython-a30fcb4dae51705f182383a6ad5f3fb86a80f9fb.tar.gz cpython-a30fcb4dae51705f182383a6ad5f3fb86a80f9fb.tar.bz2 |
Introduce test.test_support.TransientResource. It's a context manager to
surround calls to resources that may or may not be available. Specifying the
expected exception and attributes to be raised if the resource is not available
prevents overly broad catching of exceptions.
This is meant to help suppress spurious failures by raising
test.test_support.ResourceDenied if the exception matches. It would probably
be good to go through the various network tests and surround the calls to catch
connection timeouts (as done with test_socket_ssl in this commit).
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_socket_ssl.py | 3 | ||||
-rw-r--r-- | Lib/test/test_support.py | 25 |
2 files changed, 27 insertions, 1 deletions
diff --git a/Lib/test/test_socket_ssl.py b/Lib/test/test_socket_ssl.py index 3c9c9f0..8fb7e4c 100644 --- a/Lib/test/test_socket_ssl.py +++ b/Lib/test/test_socket_ssl.py @@ -27,7 +27,8 @@ def test_basic(): print "didn't raise TypeError" socket.RAND_add("this is a random string", 75.0) - f = urllib.urlopen('https://sf.net') + with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT): + f = urllib.urlopen('https://sf.net') buf = f.read() f.close() diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 82fd5f4..2e47aed 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -312,6 +312,31 @@ class EnvironmentVarGuard(object): for unset in self._unset: del self._environ[unset] +class TransientResource(object): + + """Raise ResourceDenied if an exception is raised while the context manager + is in effect that matches the specified exception and attributes.""" + + def __init__(self, exc, **kwargs): + self.exc = exc + self.attrs = kwargs + + def __enter__(self): + return self + + def __exit__(self, type_=None, value=None, traceback=None): + """If type_ is a subclass of self.exc and value has attributes matching + self.attrs, raise ResourceDenied. Otherwise let the exception + propagate (if any).""" + if type_ is not None and issubclass(self.exc, type_): + for attr, attr_value in self.attrs.iteritems(): + if not hasattr(value, attr): + break + if getattr(value, attr) != attr_value: + break + else: + raise ResourceDenied("an optional resource is not available") + #======================================================================= # Decorator for running a function in a different locale, correctly resetting |