|
|
@@ -12,12 +12,17 @@ def must_close(errorfile = sys.stderr, wrap_verify = False): |
|
|
|
already been called.""" |
|
|
|
def class_decorator(cls): |
|
|
|
|
|
|
|
def is_method_or_function(x): |
|
|
|
return inspect.ismethod(x) or inspect.isfunction(x) |
|
|
|
|
|
|
|
def wrap_class_method(wrapper): |
|
|
|
try: |
|
|
|
orig = getattr(cls, wrapper.__name__).__func__ |
|
|
|
except Exception: |
|
|
|
orig = getattr(cls, wrapper.__name__) |
|
|
|
except AttributeError: |
|
|
|
orig = lambda x: None |
|
|
|
setattr(cls, wrapper.__name__, decorator.decorator(wrapper, orig)) |
|
|
|
if is_method_or_function(orig): |
|
|
|
setattr(cls, wrapper.__name__, |
|
|
|
decorator.decorator(wrapper, orig)) |
|
|
|
|
|
|
|
@wrap_class_method |
|
|
|
def __init__(orig, self, *args, **kwargs): |
|
|
@@ -49,16 +54,16 @@ def must_close(errorfile = sys.stderr, wrap_verify = False): |
|
|
|
raise AssertionError("called " + str(orig) + " after close") |
|
|
|
return orig(self, *args, **kwargs) |
|
|
|
if wrap_verify: |
|
|
|
for (name, method) in inspect.getmembers(cls, inspect.ismethod): |
|
|
|
# Skip class methods |
|
|
|
if method.__self__ is not None: |
|
|
|
continue |
|
|
|
for (name, method) in inspect.getmembers(cls, is_method_or_function): |
|
|
|
# Skip some methods |
|
|
|
if name in [ "__del__", "__init__" ]: |
|
|
|
continue |
|
|
|
# Set up wrapper |
|
|
|
setattr(cls, name, decorator.decorator(verifier, |
|
|
|
method.__func__)) |
|
|
|
if inspect.ismethod(method): |
|
|
|
func = method.__func__ |
|
|
|
else: |
|
|
|
func = method |
|
|
|
setattr(cls, name, decorator.decorator(verifier, func)) |
|
|
|
|
|
|
|
return cls |
|
|
|
return class_decorator |