[Python-checkins] cpython (merge 3.5 -> 3.6): merge

raymond.hettinger python-checkins at python.org
Fri Dec 16 17:01:26 EST 2016


https://hg.python.org/cpython/rev/b3d27b5f2d23
changeset:   105689:b3d27b5f2d23
branch:      3.6
parent:      105686:a33047e08711
parent:      105688:c23f8614151d
user:        Raymond Hettinger <python at rcn.com>
date:        Fri Dec 16 13:59:32 2016 -0800
summary:
  merge

files:
  Lib/functools.py           |   6 ++++--
  Lib/test/test_functools.py |  13 +++++++++++++
  Misc/NEWS                  |   3 +++
  3 files changed, 20 insertions(+), 2 deletions(-)


diff --git a/Lib/functools.py b/Lib/functools.py
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -574,14 +574,16 @@
                     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
--- 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 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -14,6 +14,9 @@
   to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
   os.fsencode() and os.fsdecode() which are already using UTF-8.
 
+- Issue #28991:  functools.lru_cache() was susceptible to an obscure $
+  bug triggerable by a monkey-patched len() function.
+
 - Issue #28147: Fix a memory leak in split-table dictionaries: setattr()
   must not convert combined table into split table. Patch written by INADA
   Naoki.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list