def __call__(self, callable): wrapper = self.decorator(callable, *self.args, **self.kwargs) if reflect.inside_class_definition(depth=2): def method_wrapper(*args, **kwargs): obj, args = args[0], args[1:] method = types.MethodType(callable, obj, obj.__class__) return wrapper(method, *args, **kwargs) meta_wrapper = _function_mimicry(callable, method_wrapper) else: def function_wrapper(*args, **kwargs): return wrapper(callable, *args, **kwargs) meta_wrapper = _function_mimicry(callable, function_wrapper) return meta_wrapper
def meta_decorator(function): wrapper = decorator(function) if reflect.inside_class_definition(depth=2): def method_wrapper(*args, **kwargs): obj, args = args[0], args[1:] method = types.MethodType(function, obj, obj.__class__) return wrapper(method, *args, **kwargs) meta_wrapper = _function_mimicry(function, method_wrapper) else: def function_wrapper(*args, **kwargs): return wrapper(function, *args, **kwargs) meta_wrapper = _function_mimicry(function, function_wrapper) return meta_wrapper
try: meta_test3_pass = False class MetaError2(object): if "class_local" not in test_depth2(depth=1): raise RuntimeError() except TypeError: meta_test3_pass = True meta_test4_pass = not reflect.inside_class_definition(1) class MetaError3(object): bad1 = reflect.inside_class_definition(0) good = reflect.inside_class_definition(1) bad2 = reflect.inside_class_definition(2) class Dummy(object): name = reflect.class_canonical_name(depth=1) def spam(self): pass