Skip to content

Instantly share code, notes, and snippets.

@brondsem
Created August 6, 2012 21:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brondsem/3278423 to your computer and use it in GitHub Desktop.
Save brondsem/3278423 to your computer and use it in GitHub Desktop.
Both a Decorator and a Context Manager
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