[Python-checkins] r82732 - in python/branches/release27-maint: Lib/fnmatch.py Lib/test/test_fnmatch.py Misc/ACKS

r.david.murray python-checkins at python.org
Fri Jul 9 15:16:00 CEST 2010


Author: r.david.murray
Date: Fri Jul  9 15:16:00 2010
New Revision: 82732

Log:
Merged revisions 82730 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

'merge' in the sense that this is the same fix by the same author.

........
  r82730 | r.david.murray | 2010-07-09 08:23:21 -0400 (Fri, 09 Jul 2010) | 4 lines
  
  7846: limit fnmatch pattern cache to _MAXCACHE=100 entries.
  
  Patch by Andrew Clegg.
........


Modified:
   python/branches/release27-maint/   (props changed)
   python/branches/release27-maint/Lib/fnmatch.py
   python/branches/release27-maint/Lib/test/test_fnmatch.py
   python/branches/release27-maint/Misc/ACKS

Modified: python/branches/release27-maint/Lib/fnmatch.py
==============================================================================
--- python/branches/release27-maint/Lib/fnmatch.py	(original)
+++ python/branches/release27-maint/Lib/fnmatch.py	Fri Jul  9 15:16:00 2010
@@ -15,6 +15,7 @@
 __all__ = ["filter", "fnmatch","fnmatchcase","translate"]
 
 _cache = {}
+_MAXCACHE = 100
 
 def fnmatch(name, pat):
     """Test whether FILENAME matches PATTERN.
@@ -44,6 +45,8 @@
     pat=os.path.normcase(pat)
     if not pat in _cache:
         res = translate(pat)
+        if len(_cache) >= _MAXCACHE:
+            _cache.clear()
         _cache[pat] = re.compile(res)
     match=_cache[pat].match
     if os.path is posixpath:
@@ -66,6 +69,8 @@
 
     if not pat in _cache:
         res = translate(pat)
+        if len(_cache) >= _MAXCACHE:
+            _cache.clear()
         _cache[pat] = re.compile(res)
     return _cache[pat].match(name) is not None
 

Modified: python/branches/release27-maint/Lib/test/test_fnmatch.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_fnmatch.py	(original)
+++ python/branches/release27-maint/Lib/test/test_fnmatch.py	Fri Jul  9 15:16:00 2010
@@ -3,7 +3,7 @@
 from test import test_support
 import unittest
 
-from fnmatch import fnmatch, fnmatchcase
+from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache
 
 
 class FnmatchTestCase(unittest.TestCase):
@@ -49,6 +49,15 @@
         check('AbC', 'abc', 0, fnmatchcase)
         check('abc', 'AbC', 0, fnmatchcase)
 
+    def test_cache_clearing(self):
+        # check that caches do not grow too large
+        # http://bugs.python.org/issue7846
+
+        # string pattern cache
+        for i in range(_MAXCACHE + 1):
+            fnmatch('foo', '?' * i)
+
+        self.assertLessEqual(len(_cache), _MAXCACHE)
 
 def test_main():
     test_support.run_unittest(FnmatchTestCase)

Modified: python/branches/release27-maint/Misc/ACKS
==============================================================================
--- python/branches/release27-maint/Misc/ACKS	(original)
+++ python/branches/release27-maint/Misc/ACKS	Fri Jul  9 15:16:00 2010
@@ -144,6 +144,7 @@
 David Cinege
 Mike Clarkson
 Brad Clements
+Andrew Clegg
 Steve Clift
 Nick Coghlan
 Josh Cogliati


More information about the Python-checkins mailing list