Skip to content

Commit 7bff3cb

Browse files
committed
Issue python#18149: Add filecmp.clear_cache() to manually clear the filecmp cache.
Patch by Mark Levitt
1 parent 3fe35e6 commit 7bff3cb

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

Doc/library/filecmp.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ The :mod:`filecmp` module defines the following functions:
2727
Note that no external programs are called from this function, giving it
2828
portability and efficiency.
2929

30+
This function uses a cache for past comparisons and the results,
31+
with a cache invalidation mechanism relying on stale signatures
32+
or by explicitly calling :func:`clear_cache`.
33+
3034

3135
.. function:: cmpfiles(dir1, dir2, common, shallow=True)
3236

@@ -48,6 +52,15 @@ The :mod:`filecmp` module defines the following functions:
4852
one of the three returned lists.
4953

5054

55+
.. function:: clear_cache()
56+
57+
.. versionadded:: 3.4
58+
59+
Clear the filecmp cache. This may be useful if a file is compared so quickly
60+
after it is modified that it is within the mtime resolution of
61+
the underlying filesystem.
62+
63+
5164
.. _dircmp-objects:
5265

5366
The :class:`dircmp` class

Lib/filecmp.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
Functions:
77
cmp(f1, f2, shallow=True) -> int
88
cmpfiles(a, b, common) -> ([], [], [])
9+
clear_cache()
910
1011
"""
1112

1213
import os
1314
import stat
1415
from itertools import filterfalse
1516

16-
__all__ = ['cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES']
17+
__all__ = ['clear_cache', 'cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES']
1718

1819
_cache = {}
1920
BUFSIZE = 8*1024
2021

2122
DEFAULT_IGNORES = [
2223
'RCS', 'CVS', 'tags', '.git', '.hg', '.bzr', '_darcs', '__pycache__']
2324

25+
def clear_cache():
26+
"""Clear the filecmp cache."""
27+
_cache.clear()
2428

2529
def cmp(f1, f2, shallow=True):
2630
"""Compare two files.
@@ -39,7 +43,8 @@ def cmp(f1, f2, shallow=True):
3943
True if the files are the same, False otherwise.
4044
4145
This function uses a cache for past comparisons and the results,
42-
with a cache invalidation mechanism relying on stale signatures.
46+
with a cache invalidation mechanism relying on stale signatures
47+
or by explicitly calling clear_cache().
4348
4449
"""
4550

@@ -56,7 +61,7 @@ def cmp(f1, f2, shallow=True):
5661
if outcome is None:
5762
outcome = _do_cmp(f1, f2)
5863
if len(_cache) > 100: # limit the maximum size of the cache
59-
_cache.clear()
64+
clear_cache()
6065
_cache[f1, f2, s1, s2] = outcome
6166
return outcome
6267

Lib/test/test_filecmp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def test_different(self):
3939
self.assertFalse(filecmp.cmp(self.name, self.dir),
4040
"File and directory compare as equal")
4141

42+
def test_cache_clear(self):
43+
first_compare = filecmp.cmp(self.name, self.name_same, shallow=False)
44+
second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False)
45+
filecmp.clear_cache()
46+
self.assertTrue(len(filecmp._cache) == 0,
47+
"Cache not cleared after calling clear_cache")
48+
4249
class DirCompareTestCase(unittest.TestCase):
4350
def setUp(self):
4451
tmpdir = tempfile.gettempdir()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ Benno Leslie
738738
Christopher Tur Lesniewski-Laas
739739
Alain Leufroy
740740
Mark Levinson
741+
Mark Levitt
741742
William Lewis
742743
Akira Li
743744
Xuanji Li

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ Core and Builtins
123123
Library
124124
-------
125125

126+
- Issue #18149: Add filecmp.clear_cache() to manually clear the filecmp cache.
127+
Patch by Mark Levitt
128+
126129
- Issue #18193: Add importlib.reload().
127130

128131
- Issue #18157: Stop using imp.load_module() in pydoc.

0 commit comments

Comments
 (0)