Created
August 6, 2012 21:01
-
-
Save brondsem/3278423 to your computer and use it in GitHub Desktop.
Both a Decorator and a Context Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class skip_if_exception(object): | |
''' | |
Given an exception type, returns a decorator & context manager | |
that will raise SkipTest if that exception type is raised | |
''' | |
def __init__(self, ExcType): | |
self.ExcType = ExcType | |
def __call__(self, func): | |
return decorator(self.try_func, func) | |
def try_func(self, func, *args, **kwargs): | |
try: | |
func(*args, **kwargs) | |
except self.ExcType: | |
raise SkipTest | |
def __enter__(self): | |
pass | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
if exc_type and issubclass(exc_type, self.ExcType): | |
raise SkipTest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment