[Python-checkins] r82730 - in python/branches/py3k: Lib/fnmatch.py Lib/test/test_fnmatch.py Misc/ACKS Misc/NEWS
r.david.murray
python-checkins at python.org
Fri Jul 9 14:23:21 CEST 2010
Author: r.david.murray
Date: Fri Jul 9 14:23:21 2010
New Revision: 82730
Log:
7846: limit fnmatch pattern cache to _MAXCACHE=100 entries.
Patch by Andrew Clegg.
Modified:
python/branches/py3k/Lib/fnmatch.py
python/branches/py3k/Lib/test/test_fnmatch.py
python/branches/py3k/Misc/ACKS
python/branches/py3k/Misc/NEWS
Modified: python/branches/py3k/Lib/fnmatch.py
==============================================================================
--- python/branches/py3k/Lib/fnmatch.py (original)
+++ python/branches/py3k/Lib/fnmatch.py Fri Jul 9 14:23:21 2010
@@ -16,6 +16,7 @@
_cache = {} # Maps text patterns to compiled regexen.
_cacheb = {} # Ditto for bytes patterns.
+_MAXCACHE = 100 # Maximum size of caches
def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN.
@@ -48,6 +49,8 @@
res = bytes(res_str, 'ISO-8859-1')
else:
res = translate(pat)
+ if len(cache) >= _MAXCACHE:
+ cache.clear()
cache[pat] = regex = re.compile(res)
return regex.match
Modified: python/branches/py3k/Lib/test/test_fnmatch.py
==============================================================================
--- python/branches/py3k/Lib/test/test_fnmatch.py (original)
+++ python/branches/py3k/Lib/test/test_fnmatch.py Fri Jul 9 14:23:21 2010
@@ -3,7 +3,7 @@
from test import support
import unittest
-from fnmatch import fnmatch, fnmatchcase
+from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb
class FnmatchTestCase(unittest.TestCase):
@@ -60,6 +60,21 @@
self.check_match(b'test\xff', b'te*\xff')
self.check_match(b'foo\nbar', b'foo*')
+ 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)
+
+ # bytes pattern cache
+ for i in range(_MAXCACHE + 1):
+ fnmatch(b'foo', b'?' * i)
+ self.assertLessEqual(len(_cacheb), _MAXCACHE)
+
def test_main():
support.run_unittest(FnmatchTestCase)
Modified: python/branches/py3k/Misc/ACKS
==============================================================================
--- python/branches/py3k/Misc/ACKS (original)
+++ python/branches/py3k/Misc/ACKS Fri Jul 9 14:23:21 2010
@@ -146,6 +146,7 @@
David Cinege
Mike Clarkson
Brad Clements
+Andrew Clegg
Steve Clift
Nick Coghlan
Josh Cogliati
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Fri Jul 9 14:23:21 2010
@@ -470,6 +470,8 @@
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__.
More information about the Python-checkins
mailing list