# HG changeset patch # User steve@McMurphy # Date 1399332359 25200 # Node ID ad50edf7a54e7a113ab4142e5d952f47187f0ed5 # Parent 3aa5fae8c31318101a5c07565c288fb6d42c6a1e use clearer "shallow" comparion semantics in filecmp.cmp diff -r 3aa5fae8c313 -r ad50edf7a54e Doc/library/filecmp.rst --- a/Doc/library/filecmp.rst Sun May 04 04:45:57 2014 -0700 +++ b/Doc/library/filecmp.rst Mon May 05 16:25:59 2014 -0700 @@ -21,8 +21,8 @@ Compare the files named *f1* and *f2*, returning ``True`` if they seem equal, ``False`` otherwise. - If *shallow* is true, files with identical :func:`os.stat` signatures are - taken to be equal. Otherwise, the contents of the files are compared. + If *shallow* is true, only the files' :func:`os.stat` signatures are + compared. If *shallow* is false, the contents of the files are compared. Note that no external programs are called from this function, giving it portability and efficiency. diff -r 3aa5fae8c313 -r ad50edf7a54e Lib/filecmp.py --- a/Lib/filecmp.py Sun May 04 04:45:57 2014 -0700 +++ b/Lib/filecmp.py Mon May 05 16:25:59 2014 -0700 @@ -52,8 +52,8 @@ s2 = _sig(os.stat(f2)) if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG: return False - if shallow and s1 == s2: - return True + if shallow: + return s1 == s2 if s1[1] != s2[1]: return False diff -r 3aa5fae8c313 -r ad50edf7a54e Lib/test/test_filecmp.py --- a/Lib/test/test_filecmp.py Sun May 04 04:45:57 2014 -0700 +++ b/Lib/test/test_filecmp.py Mon May 05 16:25:59 2014 -0700 @@ -12,19 +12,32 @@ self.name = support.TESTFN self.name_same = support.TESTFN + '-same' self.name_diff = support.TESTFN + '-diff' + self.name_same_diffsig = support.TESTFN + '-same-diffsig' + self.name_diff_samesig = support.TESTFN + '-diff-samesig' data = 'Contents of file go here.\n' - for name in [self.name, self.name_same, self.name_diff]: + for name in [self.name, self.name_same, self.name_diff, + self.name_same_diffsig]: with open(name, 'w') as output: output.write(data) with open(self.name_diff, 'a+') as output: output.write('An extra line.\n') + + with open(self.name_diff_samesig, 'w') as output: + output.write(data.upper()) + + st = os.stat(self.name) + os.utime(self.name_same_diffsig, (st.st_atime, st.st_mtime - 24*60*60)) + os.utime(self.name_diff_samesig, (st.st_atime, st.st_mtime)) + self.dir = tempfile.gettempdir() def tearDown(self): os.unlink(self.name) os.unlink(self.name_same) os.unlink(self.name_diff) + os.unlink(self.name_same_diffsig) + os.unlink(self.name_diff_samesig) def test_matching(self): self.assertTrue(filecmp.cmp(self.name, self.name_same), @@ -49,6 +62,22 @@ self.assertTrue(len(filecmp._cache) == 0, "Cache not cleared after calling clear_cache") + def test_shallow_false_negative(self): + self.assertFalse(filecmp.cmp(self.name, self.name_same_diffsig), + "Shallow comparison not done by file signature") + + def test_shallow_false_positive(self): + self.assertTrue(filecmp.cmp(self.name, self.name_diff_samesig), + "Shallow comparison not done by file signature") + + def test_deep_comparisons(self): + self.assertTrue(filecmp.cmp(self.name, self.name_same_diffsig, + shallow=False), + "Deep comparison not done by file contents") + self.assertFalse(filecmp.cmp(self.name, self.name_diff_samesig, + shallow=False), + "Deep comparison not done by file contents") + class DirCompareTestCase(unittest.TestCase): def setUp(self): tmpdir = tempfile.gettempdir()