7846: limit fnmatch pattern cache to _MAXCACHE=100 entries.

Patch by Andrew Clegg.
diff --git a/Lib/test/test_fnmatch.py b/Lib/test/test_fnmatch.py
index 6d2f9ad..81b9ce6 100644
--- a/Lib/test/test_fnmatch.py
+++ b/Lib/test/test_fnmatch.py
@@ -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)