diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-01-26 00:23:40 -0800 |
|---|---|---|
| committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-01-26 03:23:40 -0500 |
| commit | b2b023c657ba8c3f4a24d0c847d10fe8e2a73d44 (patch) | |
| tree | 724e099bc4b39eaf42a4a42aed559c1ced13d4fa /Lib/functools.py | |
| parent | a6a8524bb1c78c7425346ec20ecffc02d1d02a79 (diff) | |
| download | cpython-git-b2b023c657ba8c3f4a24d0c847d10fe8e2a73d44.tar.gz | |
bpo-35780: Fix errors in lru_cache() C code (GH-11623) (GH-11682)
Diffstat (limited to 'Lib/functools.py')
| -rw-r--r-- | Lib/functools.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 24b011dc04..592f156fe4 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -413,7 +413,7 @@ class _HashedSeq(list): def _make_key(args, kwds, typed, kwd_mark = (object(),), - fasttypes = {int, str, frozenset, type(None)}, + fasttypes = {int, str}, tuple=tuple, type=type, len=len): """Make a cache key from optionally typed positional and keyword arguments @@ -469,8 +469,11 @@ def lru_cache(maxsize=128, typed=False): # Early detection of an erroneous call to @lru_cache without any arguments # resulting in the inner function being passed to maxsize instead of an - # integer or None. - if maxsize is not None and not isinstance(maxsize, int): + # integer or None. Negative maxsize is treated as 0. + if isinstance(maxsize, int): + if maxsize < 0: + maxsize = 0 + elif maxsize is not None: raise TypeError('Expected maxsize to be an integer or None') def decorating_function(user_function): @@ -537,6 +540,7 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): link[NEXT] = root hits += 1 return result + misses += 1 result = user_function(*args, **kwds) with lock: if key in cache: @@ -574,7 +578,6 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): # Use the cache_len bound 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(): |
