[issue13177] Avoid chained exceptions in lru_cache
Raymond Hettinger
report at bugs.python.org
Fri Oct 14 16:31:32 CEST 2011
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
One possibility is to move the call to user_function() outside of the KeyError exception handler so that user exceptions won't be chained:
def wrapper(*args, **kwds):
nonlocal hits, misses
key = args
if kwds:
key += kwd_mark + tuple(sorted(kwds.items()))
try:
with lock:
result = cache[key]
cache_renew(key) # record recent use of this key
hits += 1
return result
except KeyError:
pass
result = user_function(*args, **kwds)
with lock:
cache[key] = result # record recent use of this key
misses += 1
if len(cache) > maxsize:
cache_popitem(0) # purge least recently used cache entry
return result
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13177>
_______________________________________
More information about the Python-bugs-list
mailing list