def decorator(func): def dostrip(*args, **kw): result = func(*args, **kw) while len(result) and result[-1] == " ": result = result[:-1] return result return rewrap(func, dostrip)
def wrapper(fn): def newfunc(*args, **kwargs): l=lock if not hasattr(l, 'acquire'): if callable(l): l=l(*args, **kwargs) else: raise ValueError, "lock must be either a Lock or a callable that returns a Lock" l.acquire() try: return fn(*args, **kwargs) finally: l.release() return rewrap(fn, newfunc)
def decorator(func): def trace(*args, **kw): # default to logger that was passed by module, but # can override by passing logger=foo as function parameter. # make sure this doesnt conflict with one of the parameters # you are expecting filename = os.path.normcase(func.func_code.co_filename) func_name = func.func_code.co_name lineno = func.func_code.co_firstlineno l2 = kw.get('logger', log) if l2 is None: l2 = logging.getLogger("trace.%s" % func.__module__) if isinstance(l2, basestring): l2 = logging.getLogger(l2) message = "ENTER %s(" % func_name for arg in args: message = message + repr(arg) + ", " for k,v in kw.items(): message = message + "%s=%s" % (k,repr(v)) message = message + ")" frame = sys._getframe(2) doLog(l2, logging.INFO, os.path.normcase(frame.f_code.co_filename), frame.f_lineno, message, args=[], exc_info=None, func=frame.f_code.co_name) try: result = "Bad exception raised: Exception was not a derived class of 'Exception'" try: result = func(*args, **kw) except (KeyboardInterrupt, Exception), e: result = "EXCEPTION RAISED" doLog(l2, logging.INFO, filename, lineno, "EXCEPTION: %s\n" % e, args=[], exc_info=sys.exc_info(), func=func_name) raise finally: doLog(l2, logging.INFO, filename, lineno, "LEAVE %s --> %s\n" % (func_name, result), args=[], exc_info=None, func=func_name) return result return rewrap(func, trace)
def decorator(func): def trace(*args, **kw): # default to logger that was passed by module, but # can override by passing logger=foo as function parameter. # make sure this doesn't conflict with one of the parameters # you are expecting filename = os.path.normcase(func.func_code.co_filename) func_name = func.func_code.co_name lineno = func.func_code.co_firstlineno l2 = kw.get('logger', log) if l2 is None: l2 = logging.getLogger("trace.%s" % func.__module__) if isinstance(l2, basestring): l2 = logging.getLogger(l2) message = "ENTER %s(" % func_name for arg in args: message = message + repr(arg) + ", " for k,v in kw.items(): message = message + "%s=%s" % (k,repr(v)) message = message + ")" frame = sys._getframe(2) doLog(l2, logging.INFO, os.path.normcase(frame.f_code.co_filename), frame.f_lineno, message, args=[], exc_info=None, func=frame.f_code.co_name) try: result = "Bad exception raised: Exception was not a derived class of 'Exception'" try: result = func(*args, **kw) except (KeyboardInterrupt, Exception), e: result = "EXCEPTION RAISED" doLog(l2, logging.INFO, filename, lineno, "EXCEPTION: %s\n" % e, args=[], exc_info=sys.exc_info(), func=func_name) raise finally: doLog(l2, logging.INFO, filename, lineno, "LEAVE %s --> %s\n" % (func_name, result), args=[], exc_info=None, func=func_name) return result return rewrap(func, trace)
def manager(func): """Emulate 2.5 ``@contextlib.contextmanager`` decorator""" gcm = _GeneratorContextManager return rewrap(func, lambda *args, **kwds: gcm(func(*args, **kwds)))
def a(f): def g(self): return self.ctrl.atomically(f, self) return rewrap(f, g)
def decorator(func): def dostrip(*args, **kw): result = func(*args, **kw) while len(result) and result[-1] == " ": result=result[:-1] return result return rewrap(func, dostrip)