cpython: bfd53dcb02ff (original) (raw)
Mercurial > cpython
changeset 84124:bfd53dcb02ff
Issue #18149: Add filecmp.clear_cache() to manually clear the filecmp cache. Patch by Mark Levitt [#18149]
Ned Deily nad@acm.org | |
---|---|
date | Fri, 14 Jun 2013 15:19:11 -0700 |
parents | 01da7bf11ca1 |
children | 8ee9f50b0538 |
files | Doc/library/filecmp.rst Lib/filecmp.py Lib/test/test_filecmp.py Misc/ACKS Misc/NEWS |
diffstat | 5 files changed, 32 insertions(+), 3 deletions(-)[+] [-] Doc/library/filecmp.rst 13 Lib/filecmp.py 11 Lib/test/test_filecmp.py 7 Misc/ACKS 1 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/filecmp.rst
+++ b/Doc/library/filecmp.rst
@@ -27,6 +27,10 @@ The :mod:filecmp
module defines the fo
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 @@ The :mod:filecmp
module defines the fo
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
--- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -6,6 +6,7 @@ Classes: Functions: cmp(f1, f2, shallow=True) -> int cmpfiles(a, b, common) -> ([], [], [])
""" @@ -13,7 +14,7 @@ import os import stat from itertools import filterfalse -all = ['cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES'] +all = ['clear_cache', 'cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES'] _cache = {} BUFSIZE = 81024 @@ -21,6 +22,9 @@ BUFSIZE = 81024 DEFAULT_IGNORES = [ 'RCS', 'CVS', 'tags', '.git', '.hg', '.bzr', '_darcs', 'pycache'] +def clear_cache():
def cmp(f1, f2, shallow=True): """Compare two files. @@ -39,7 +43,8 @@ def cmp(f1, f2, shallow=True): 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
- or by explicitly calling clear_cache().
""" @@ -56,7 +61,7 @@ def cmp(f1, f2, shallow=True): if outcome is None: outcome = _do_cmp(f1, f2) if len(_cache) > 100: # limit the maximum size of the cache
_cache.clear()[](#l2.44)
--- a/Lib/test/test_filecmp.py +++ b/Lib/test/test_filecmp.py @@ -39,6 +39,13 @@ class FileCompareTestCase(unittest.TestC 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)[](#l3.8)
second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False)[](#l3.9)
filecmp.clear_cache()[](#l3.10)
self.assertTrue(len(filecmp._cache) == 0,[](#l3.11)
"Cache not cleared after calling clear_cache")[](#l3.12)
+ class DirCompareTestCase(unittest.TestCase): def setUp(self): tmpdir = tempfile.gettempdir()
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -738,6 +738,7 @@ Benno Leslie Christopher Tur Lesniewski-Laas Alain Leufroy Mark Levinson +Mark Levitt William Lewis Akira Li Xuanji Li
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -123,6 +123,9 @@ Core and Builtins Library ------- +- Issue #18149: Add filecmp.clear_cache() to manually clear the filecmp cache.