Fix 'refleak' introduced by fnmatch cache purge tests.

This introduces a 'purge' function for the fnmatch module analogous
to the 'purge' function in the re module.
This commit is contained in:
R. David Murray 2010-07-10 13:52:13 +00:00
parent a851483527
commit 0425a8ea72
3 changed files with 18 additions and 2 deletions

View File

@ -82,6 +82,13 @@ patterns.
<_sre.SRE_Match object at 0x...> <_sre.SRE_Match object at 0x...>
.. function:: purge()
Clear the internal pattern cache.
.. versionadded:: 3.2
.. seealso:: .. seealso::
Module :mod:`glob` Module :mod:`glob`

View File

@ -12,12 +12,17 @@ corresponding to PATTERN. (It does not compile it.)
import re import re
__all__ = ["filter", "fnmatch","fnmatchcase","translate"] __all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"]
_cache = {} # Maps text patterns to compiled regexen. _cache = {} # Maps text patterns to compiled regexen.
_cacheb = {} # Ditto for bytes patterns. _cacheb = {} # Ditto for bytes patterns.
_MAXCACHE = 100 # Maximum size of caches _MAXCACHE = 100 # Maximum size of caches
def purge():
"""Clear the pattern cache"""
_cache.clear()
_cacheb.clear()
def fnmatch(name, pat): def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN. """Test whether FILENAME matches PATTERN.

View File

@ -3,10 +3,14 @@
from test import support from test import support
import unittest import unittest
from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb, purge
class FnmatchTestCase(unittest.TestCase): class FnmatchTestCase(unittest.TestCase):
def tearDown(self):
purge()
def check_match(self, filename, pattern, should_match=1, fn=fnmatch): def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
if should_match: if should_match:
self.assertTrue(fn(filename, pattern), self.assertTrue(fn(filename, pattern),