comparison mercurial/util.py @ 28832:f5ff10f6fa6b

util: use __code__ (available since py2.6)
author timeless <timeless@mozdev.org>
date Tue, 29 Mar 2016 17:43:23 +0000
parents 59dd920c0ddc
children 68a946e83188
comparison
equal deleted inserted replaced
28831:6b86ce3e3576 28832:f5ff10f6fa6b
448 ) 448 )
449 449
450 def cachefunc(func): 450 def cachefunc(func):
451 '''cache the result of function calls''' 451 '''cache the result of function calls'''
452 # XXX doesn't handle keywords args 452 # XXX doesn't handle keywords args
453 if func.func_code.co_argcount == 0: 453 if func.__code__.co_argcount == 0:
454 cache = [] 454 cache = []
455 def f(): 455 def f():
456 if len(cache) == 0: 456 if len(cache) == 0:
457 cache.append(func()) 457 cache.append(func())
458 return cache[0] 458 return cache[0]
459 return f 459 return f
460 cache = {} 460 cache = {}
461 if func.func_code.co_argcount == 1: 461 if func.__code__.co_argcount == 1:
462 # we gain a small amount of time because 462 # we gain a small amount of time because
463 # we don't need to pack/unpack the list 463 # we don't need to pack/unpack the list
464 def f(arg): 464 def f(arg):
465 if arg not in cache: 465 if arg not in cache:
466 cache[arg] = func(arg) 466 cache[arg] = func(arg)
698 698
699 def lrucachefunc(func): 699 def lrucachefunc(func):
700 '''cache most recent results of function calls''' 700 '''cache most recent results of function calls'''
701 cache = {} 701 cache = {}
702 order = collections.deque() 702 order = collections.deque()
703 if func.func_code.co_argcount == 1: 703 if func.__code__.co_argcount == 1:
704 def f(arg): 704 def f(arg):
705 if arg not in cache: 705 if arg not in cache:
706 if len(cache) > 20: 706 if len(cache) > 20:
707 del cache[order.popleft()] 707 del cache[order.popleft()]
708 cache[arg] = func(arg) 708 cache[arg] = func(arg)