summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_support.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r--Lib/test/test_support.py25
1 files changed, 25 insertions, 0 deletions
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