# HG changeset patch # User Brendan Cully # Date 1158938365 25200 # Node ID e4ea47c214806b1617a5d43160a7c3c036e30c23 # Parent 8342ad5abe0be4da22ca29abc2cddf47bc2027e4 Add cachefunc to abstract function call cache diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -24,6 +24,22 @@ defaultdateformats = ('%Y-%m-%d %H:%M:%S class SignalInterrupt(Exception): """Exception raised on SIGTERM and SIGHUP.""" +def cachefunc(func): + '''cache the result of function calls''' + cache = {} + if func.func_code.co_argcount == 1: + def f(arg): + if arg not in cache: + cache[arg] = func(arg) + return cache[arg] + else: + def f(*args): + if args not in cache: + cache[args] = func(*args) + return cache[args] + + return f + def pipefilter(s, cmd): '''filter string S through command CMD, returning its output''' (pout, pin) = popen2.popen2(cmd, -1, 'b')