[Python-checkins] r84054 - in python/branches/py3k/Lib: functools.py test/test_functools.py

raymond.hettinger python-checkins at python.org
Sun Aug 15 01:52:08 CEST 2010


Author: raymond.hettinger
Date: Sun Aug 15 01:52:08 2010
New Revision: 84054

Log:
Support cache sizes.

Modified:
   python/branches/py3k/Lib/functools.py
   python/branches/py3k/Lib/test/test_functools.py

Modified: python/branches/py3k/Lib/functools.py
==============================================================================
--- python/branches/py3k/Lib/functools.py	(original)
+++ python/branches/py3k/Lib/functools.py	Sun Aug 15 01:52:08 2010
@@ -144,7 +144,7 @@
                     wrapper.misses += 1
                     if len(cache) > maxsize:
                         # purge the 10% least frequently used entries
-                        for key, _ in nsmallest(maxsize // 10,
+                        for key, _ in nsmallest(maxsize // 10 or 1,
                                                 use_count.items(),
                                                 key=itemgetter(1)):
                             del cache[key], use_count[key]

Modified: python/branches/py3k/Lib/test/test_functools.py
==============================================================================
--- python/branches/py3k/Lib/test/test_functools.py	(original)
+++ python/branches/py3k/Lib/test/test_functools.py	Sun Aug 15 01:52:08 2010
@@ -482,6 +482,30 @@
         self.assertEqual(f.hits, 0)
         self.assertEqual(f.misses, 1)
 
+        # test size zero (which means "never-cache")
+        f_cnt = 0
+        @functools.lru_cache(0)
+        def f():
+            nonlocal f_cnt
+            f_cnt += 1
+            return 20
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f_cnt, 3)
+
+        # test size one
+        f_cnt = 0
+        @functools.lru_cache(1)
+        def f():
+            nonlocal f_cnt
+            f_cnt += 1
+            return 20
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f_cnt, 1)
+
     def test_lfu(self):
         def orig(x, y):
             return 3*x+y
@@ -503,6 +527,30 @@
         self.assertEqual(f.hits, 0)
         self.assertEqual(f.misses, 1)
 
+        # test size zero (which means "never-cache")
+        f_cnt = 0
+        @functools.lfu_cache(0)
+        def f():
+            nonlocal f_cnt
+            f_cnt += 1
+            return 20
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f_cnt, 3)
+
+        # test size one
+        f_cnt = 0
+        @functools.lfu_cache(1)
+        def f():
+            nonlocal f_cnt
+            f_cnt += 1
+            return 20
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f(), 20)
+        self.assertEqual(f_cnt, 1)
+
 def test_main(verbose=None):
     test_classes = (
         TestPartial,


More information about the Python-checkins mailing list