Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0425a8e

Browse files
committed
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.
1 parent a851483 commit 0425a8e

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

Doc/library/fnmatch.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ patterns.
8282
<_sre.SRE_Match object at 0x...>
8383

8484

85+
.. function:: purge()
86+
87+
Clear the internal pattern cache.
88+
89+
.. versionadded:: 3.2
90+
91+
8592
.. seealso::
8693

8794
Module :mod:`glob`

Lib/fnmatch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212

1313
import re
1414

15-
__all__ = ["filter", "fnmatch","fnmatchcase","translate"]
15+
__all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"]
1616

1717
_cache = {} # Maps text patterns to compiled regexen.
1818
_cacheb = {} # Ditto for bytes patterns.
1919
_MAXCACHE = 100 # Maximum size of caches
2020

21+
def purge():
22+
"""Clear the pattern cache"""
23+
_cache.clear()
24+
_cacheb.clear()
25+
2126
def fnmatch(name, pat):
2227
"""Test whether FILENAME matches PATTERN.
2328

Lib/test/test_fnmatch.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
from test import support
44
import unittest
55

6-
from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb
6+
from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb, purge
77

88

99
class FnmatchTestCase(unittest.TestCase):
10+
11+
def tearDown(self):
12+
purge()
13+
1014
def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
1115
if should_match:
1216
self.assertTrue(fn(filename, pattern),

0 commit comments

Comments
 (0)