]> granicus.if.org Git - python/commitdiff
Merged revisions 82732,82734,82738 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Fri, 9 Jul 2010 13:29:33 +0000 (13:29 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Fri, 9 Jul 2010 13:29:33 +0000 (13:29 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/release27-maint

................
  r82732 | r.david.murray | 2010-07-09 09:16:00 -0400 (Fri, 09 Jul 2010) | 13 lines

  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.
  ........
................
  r82734 | r.david.murray | 2010-07-09 09:17:45 -0400 (Fri, 09 Jul 2010) | 9 lines

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

  ........
    r82731 | r.david.murray | 2010-07-09 09:14:03 -0400 (Fri, 09 Jul 2010) | 2 lines

    Fix sort order mistake in Misc/ACKS.
  ........
................
  r82738 | r.david.murray | 2010-07-09 09:25:56 -0400 (Fri, 09 Jul 2010) | 2 lines

  Add missing NEWS entry for 7646.
................

Lib/fnmatch.py
Lib/test/test_fnmatch.py
Misc/ACKS
Misc/NEWS

index 4c149eb78171526415301d30c8663f7ebc2fc819..b1c37f59dff0cc3cf89e81e60b24e06d496309c1 100644 (file)
@@ -15,6 +15,7 @@ import re
 __all__ = ["filter", "fnmatch","fnmatchcase","translate"]
 
 _cache = {}
+_MAXCACHE = 100
 
 def fnmatch(name, pat):
     """Test whether FILENAME matches PATTERN.
@@ -44,6 +45,8 @@ def filter(names, pat):
     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 @@ def fnmatchcase(name, pat):
 
     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
 
index 3a55b85f6232bca61eb0b44af20c481fcb3efd42..015c580b61ff1a7128f923e8e51037199860a2b6 100644 (file)
@@ -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 @@ class FnmatchTestCase(unittest.TestCase):
         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.assertTrue(len(_cache) <= _MAXCACHE)
 
 def test_main():
     test_support.run_unittest(FnmatchTestCase)
index 94a22a860ecbf21a42a2ff7b0b81e1eff5e9dc1b..b6634a789b7b326fadbeebf41470ef877ef556bc 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -132,6 +132,7 @@ Tom Christiansen
 Vadim Chugunov
 David Cinege
 Mike Clarkson
+Andrew Clegg
 Brad Clements
 Steve Clift
 Nick Coghlan
index 0efac1b772a3964373428d57a9f056120a6ad857..f6f8044ac8098da63915847b14f276fb1007b545 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,8 @@ C-API
 Library
 -------
 
+- Issue #7646: The fnmatch pattern cache no longer grows without bound.
+
 - Issue #9136: Fix 'dictionary changed size during iteration'
   RuntimeError produced when profiling the decimal module.  This was
   due to a dangerous iteration over 'locals()' in Context.__init__.