diff options
author | Manjusaka <me@manjusaka.me> | 2019-11-12 07:30:18 (GMT) |
---|---|---|
committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-11-12 07:30:18 (GMT) |
commit | 051ff526b5dc2c40c4a53d87089740358822edfa (patch) | |
tree | 3672f1a8ca53bfb008d3f56d2b8c5f29a04e3f6b /Lib | |
parent | 98480cef9dba04794bd61c7e7cca643d384c8c35 (diff) | |
download | cpython-051ff526b5dc2c40c4a53d87089740358822edfa.zip cpython-051ff526b5dc2c40c4a53d87089740358822edfa.tar.gz cpython-051ff526b5dc2c40c4a53d87089740358822edfa.tar.bz2 |
bpo-38565: add new cache_parameters method for lru_cache (GH-16916)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/functools.py | 2 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 11 |
2 files changed, 13 insertions, 0 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 3192bd0..2c01b2e 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -499,6 +499,7 @@ def lru_cache(maxsize=128, typed=False): # The user_function was passed in directly via the maxsize argument user_function, maxsize = maxsize, 128 wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo) + wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed} return update_wrapper(wrapper, user_function) elif maxsize is not None: raise TypeError( @@ -506,6 +507,7 @@ def lru_cache(maxsize=128, typed=False): def decorating_function(user_function): wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo) + wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed} return update_wrapper(wrapper, user_function) return decorating_function diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index c300270..a97ca39 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -1655,6 +1655,17 @@ class TestLRU: f_copy = copy.deepcopy(f) self.assertIs(f_copy, f) + def test_lru_cache_parameters(self): + @self.module.lru_cache(maxsize=2) + def f(): + return 1 + self.assertEqual(f.cache_parameters(), {'maxsize': 2, "typed": False}) + + @self.module.lru_cache(maxsize=1000, typed=True) + def f(): + return 1 + self.assertEqual(f.cache_parameters(), {'maxsize': 1000, "typed": True}) + @py_functools.lru_cache() def py_cached_func(x, y): |