Skip to content

Commit 304191c

Browse files
committed
Ensure a shallow compare remains shallow
Consider the case where either the mode and the size of a file is identical but the modification time isn't. This means that the all guard-if-clauses would be skipped and an actual deep compare would be performed. To ensure that a shallow copy indeed is shallow, perform: - a check if both files are regular files; - if the compare is shallow perform a diff of the file signatures (which include the mode, size and the modification time) and return true if the signatures match and false if they don't. This second evaluation ensures that a shallow copy won't result into a deep compare.
1 parent 0f47c8a commit 304191c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Lib/filecmp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ def cmp(f1, f2, shallow=True):
5555
s2 = _sig(os.stat(f2))
5656
if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
5757
return False
58-
if shallow and s1 == s2:
59-
return True
58+
if shallow:
59+
return s1 == s2
60+
6061
if s1[1] != s2[1]:
6162
return False
62-
6363
outcome = _cache.get((f1, f2, s1, s2))
6464
if outcome is None:
6565
outcome = _do_cmp(f1, f2)

0 commit comments

Comments
 (0)