diff options
author | Raymond Hettinger <python@rcn.com> | 2016-12-16 21:59:57 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-12-16 21:59:57 (GMT) |
commit | 92c481a1613c84e20acab7c37b5633f2707f35d4 (patch) | |
tree | c717cb32b9f818a10da32dddeb8f2f41e746da2c /Lib | |
parent | 076300f63e0e008448fab96cd429789ee9e1dbed (diff) | |
parent | c28dbd0452d54629328daaab0f80ad5391677d17 (diff) | |
download | cpython-92c481a1613c84e20acab7c37b5633f2707f35d4.zip cpython-92c481a1613c84e20acab7c37b5633f2707f35d4.tar.gz cpython-92c481a1613c84e20acab7c37b5633f2707f35d4.tar.bz2 |
merge
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/functools.py | 6 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 13 |
2 files changed, 17 insertions, 2 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 9845df2..45e5f87 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -574,14 +574,16 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): last = root[PREV] link = [last, root, key, result] last[NEXT] = root[PREV] = cache[key] = link - full = (len(cache) >= maxsize) + # Use the __len__() method instead of the len() function + # which could potentially be wrapped in an lru_cache itself. + full = (cache.__len__() >= maxsize) misses += 1 return result def cache_info(): """Report cache statistics""" with lock: - return _CacheInfo(hits, misses, maxsize, len(cache)) + return _CacheInfo(hits, misses, maxsize, cache.__len__()) def cache_clear(): """Clear the cache and cache statistics""" diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index ba2a52f..3a40861 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -1,4 +1,5 @@ import abc +import builtins import collections import copy from itertools import permutations @@ -1189,6 +1190,18 @@ class TestLRU: self.assertEqual(misses, 4) self.assertEqual(currsize, 2) + def test_lru_reentrancy_with_len(self): + # Test to make sure the LRU cache code isn't thrown-off by + # caching the built-in len() function. Since len() can be + # cached, we shouldn't use it inside the lru code itself. + old_len = builtins.len + try: + builtins.len = self.module.lru_cache(4)(len) + for i in [0, 0, 1, 2, 3, 3, 4, 5, 6, 1, 7, 2, 1]: + self.assertEqual(len('abcdefghijklmn'[:i]), i) + finally: + builtins.len = old_len + def test_lru_type_error(self): # Regression test for issue #28653. # lru_cache was leaking when one of the arguments |