From b8a1e3e945c7f5397d44dae872d00e3ba44a2f97 Mon Sep 17 00:00:00 2001 From: "Alexander R. Vandenbulcke" Date: Mon, 18 Jan 2021 19:15:21 +0100 Subject: [PATCH 1/2] bpo-42958: Clarify the docstring for filecmp.cmp() The docstring for filecmp mentions that, with a shallow copy, the signature of the file is compared rather than a bit-by-bit comparison. As external user it is not clear what is meant by this signature. Is it a comparison of a stat between 2 files? If so, what about inode which would only be identical for a hard link. And so on... This clarification describes which parts of a stat are compared to give the end user more info when to use shallow compares and when not to. --- Lib/filecmp.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/filecmp.py b/Lib/filecmp.py index 7c47eb022cc8c0..389be3a43ac486 100644 --- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -36,7 +36,13 @@ def cmp(f1, f2, shallow=True): f2 -- Second file name - shallow -- Just check stat signature (do not read the files). + shallow -- Check file signatures instead of contents, the + signature contains the file its mode, size and + mtime. + If the size differs, the files are considerd to be + different. + If other parts of the signature indicate that the files + differ, a deep compare is performed. defaults to True. Return value: From b294ac34bdd16ac3485d1c6740825b143ce3534d Mon Sep 17 00:00:00 2001 From: "Alexander R. Vandenbulcke" Date: Wed, 20 Jan 2021 20:31:34 +0100 Subject: [PATCH 2/2] bpo-42958: Clarify doc for filecmp.cmp() State the actual interpretation of the `shallow` keyword argument to minimize possible confusion for the end user. This include: - clarify which info of `os.stat()` is used during shallow compare (file type, size and modification time) - clarify that `shallow=True` doesn't imply no deep compare can be performed under the hood. --- Doc/library/filecmp.rst | 9 +++++++-- .../Library/2021-01-21-20-03-34.bpo-42958.I8nNWP.rst | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-01-21-20-03-34.bpo-42958.I8nNWP.rst diff --git a/Doc/library/filecmp.rst b/Doc/library/filecmp.rst index c60603b30a6d7d..7e67604a309923 100644 --- a/Doc/library/filecmp.rst +++ b/Doc/library/filecmp.rst @@ -22,8 +22,13 @@ The :mod:`filecmp` module defines the following functions: 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, files with identical :func:`os.stat` signatures (file + type, size and modification time) are taken to be equal. + + If the files have a different size ``False`` will be returned. + + Comparisons with ``shallow=False`` or different signatures imply that the + contents of the files are compared. Note that no external programs are called from this function, giving it portability and efficiency. diff --git a/Misc/NEWS.d/next/Library/2021-01-21-20-03-34.bpo-42958.I8nNWP.rst b/Misc/NEWS.d/next/Library/2021-01-21-20-03-34.bpo-42958.I8nNWP.rst new file mode 100644 index 00000000000000..a5e6b016b9cf19 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-01-21-20-03-34.bpo-42958.I8nNWP.rst @@ -0,0 +1 @@ +Clarify filecmp.cmp() docstring and documentation