diff -r 67714c4d9725 Doc/library/filecmp.rst --- a/Doc/library/filecmp.rst Mon Jun 10 23:02:56 2013 -0500 +++ b/Doc/library/filecmp.rst Thu Jun 13 09:28:43 2013 +0100 @@ -27,6 +27,10 @@ Note that no external programs are called from this function, giving it portability and efficiency. + This function uses a cache for past comparisons and the results, + with a cache invalidation mechanism relying on stale signatures + or by explicitly calling :func:`clear_cache`. + .. function:: cmpfiles(dir1, dir2, common, shallow=True) @@ -48,6 +52,15 @@ one of the three returned lists. +.. function:: clear_cache() + + .. versionadded:: 3.4 + + Clear the filecmp cache. This may be useful if a file is compared so quickly + after it is modified that it is within the mtime resolution of + the underlying filesystem. + + .. _dircmp-objects: The :class:`dircmp` class diff -r 67714c4d9725 Lib/filecmp.py --- a/Lib/filecmp.py Mon Jun 10 23:02:56 2013 -0500 +++ b/Lib/filecmp.py Thu Jun 13 09:28:43 2013 +0100 @@ -6,6 +6,7 @@ Functions: cmp(f1, f2, shallow=True) -> int cmpfiles(a, b, common) -> ([], [], []) + clear_cache() """ @@ -13,7 +14,7 @@ import stat from itertools import filterfalse -__all__ = ['cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES'] +__all__ = ['clear_cache', 'cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES'] _cache = {} BUFSIZE = 8*1024 @@ -21,6 +22,9 @@ DEFAULT_IGNORES = [ 'RCS', 'CVS', 'tags', '.git', '.hg', '.bzr', '_darcs', '__pycache__'] +def clear_cache(): + """Clear the filecmp cache.""" + _cache.clear() def cmp(f1, f2, shallow=True): """Compare two files. @@ -39,7 +43,8 @@ True if the files are the same, False otherwise. This function uses a cache for past comparisons and the results, - with a cache invalidation mechanism relying on stale signatures. + with a cache invalidation mechanism relying on stale signatures + or by explicitly calling clear_cache(). """ @@ -56,7 +61,7 @@ if outcome is None: outcome = _do_cmp(f1, f2) if len(_cache) > 100: # limit the maximum size of the cache - _cache.clear() + clear_cache() _cache[f1, f2, s1, s2] = outcome return outcome diff -r 67714c4d9725 Lib/test/test_filecmp.py --- a/Lib/test/test_filecmp.py Mon Jun 10 23:02:56 2013 -0500 +++ b/Lib/test/test_filecmp.py Thu Jun 13 09:28:43 2013 +0100 @@ -39,6 +39,13 @@ self.assertFalse(filecmp.cmp(self.name, self.dir), "File and directory compare as equal") + def test_cache_clear(self): + first_compare = filecmp.cmp(self.name, self.name_same, shallow=False) + second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False) + filecmp.clear_cache() + self.assertTrue(len(filecmp._cache) == 0, + "Cache not cleared after calling clear_cache") + class DirCompareTestCase(unittest.TestCase): def setUp(self): tmpdir = tempfile.gettempdir()