From 0425a8ea729617a74ab343909687d98216d25524 Mon Sep 17 00:00:00 2001 From: "R. David Murray" Date: Sat, 10 Jul 2010 13:52:13 +0000 Subject: 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. --- Doc/library/fnmatch.rst | 7 +++++++ Lib/fnmatch.py | 7 ++++++- Lib/test/test_fnmatch.py | 6 +++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Doc/library/fnmatch.rst b/Doc/library/fnmatch.rst index 7fa6148..ec78ed2 100644 --- a/Doc/library/fnmatch.rst +++ b/Doc/library/fnmatch.rst @@ -82,6 +82,13 @@ patterns. <_sre.SRE_Match object at 0x...> +.. function:: purge() + + Clear the internal pattern cache. + + .. versionadded:: 3.2 + + .. seealso:: Module :mod:`glob` diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index 26ae4cc..aa682bb 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -12,12 +12,17 @@ corresponding to PATTERN. (It does not compile it.) import re -__all__ = ["filter", "fnmatch","fnmatchcase","translate"] +__all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"] _cache = {} # Maps text patterns to compiled regexen. _cacheb = {} # Ditto for bytes patterns. _MAXCACHE = 100 # Maximum size of caches +def purge(): + """Clear the pattern cache""" + _cache.clear() + _cacheb.clear() + def fnmatch(name, pat): """Test whether FILENAME matches PATTERN. diff --git a/Lib/test/test_fnmatch.py b/Lib/test/test_fnmatch.py index 81b9ce6..496f145 100644 --- a/Lib/test/test_fnmatch.py +++ b/Lib/test/test_fnmatch.py @@ -3,10 +3,14 @@ from test import support import unittest -from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb +from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb, purge class FnmatchTestCase(unittest.TestCase): + + def tearDown(self): + purge() + def check_match(self, filename, pattern, should_match=1, fn=fnmatch): if should_match: self.assertTrue(fn(filename, pattern), -- cgit v0.12