Skip to content

Commit 19c7238

Browse files
committed
Sync-up with 3.7 by backporting minor lru_cache code beautification
1 parent 4ee3914 commit 19c7238

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Lib/functools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
492492
hits = misses = 0
493493
full = False
494494
cache_get = cache.get # bound method to lookup a key or return None
495+
cache_len = cache.__len__ # get cache size without calling len()
495496
lock = RLock() # because linkedlist updates aren't threadsafe
496497
root = [] # root of the circular doubly linked list
497498
root[:] = [root, root, None, None] # initialize by pointing to self
@@ -573,16 +574,16 @@ def wrapper(*args, **kwds):
573574
last = root[PREV]
574575
link = [last, root, key, result]
575576
last[NEXT] = root[PREV] = cache[key] = link
576-
# Use the __len__() method instead of the len() function
577+
# Use the cache_len bound method instead of the len() function
577578
# which could potentially be wrapped in an lru_cache itself.
578-
full = (cache.__len__() >= maxsize)
579+
full = (cache_len() >= maxsize)
579580
misses += 1
580581
return result
581582

582583
def cache_info():
583584
"""Report cache statistics"""
584585
with lock:
585-
return _CacheInfo(hits, misses, maxsize, cache.__len__())
586+
return _CacheInfo(hits, misses, maxsize, cache_len())
586587

587588
def cache_clear():
588589
"""Clear the cache and cache statistics"""

0 commit comments

Comments
 (0)