From 689e991c3620a77bd29b529a73aeb3dab97a9f34 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Thu, 15 Jul 2021 11:17:22 -0400 Subject: [PATCH 1/3] Updated filecmp.cmp docstring and docs --- Doc/library/filecmp.rst | 10 ++++++++-- Lib/filecmp.py | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Doc/library/filecmp.rst b/Doc/library/filecmp.rst index c60603b30a6d7d..c799921d8c1104 100644 --- a/Doc/library/filecmp.rst +++ b/Doc/library/filecmp.rst @@ -22,8 +22,14 @@ 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: if stat signature matches, return True; if it does not + match, fall back to the logic of shallow=False. + + If *shallow* is false: return False if file sizes differ, otherwise compare full + file contents. + + Note that :func:`os.stat` signature, as used here, contains 3 values: type, size + and mtime. Note that no external programs are called from this function, giving it portability and efficiency. diff --git a/Lib/filecmp.py b/Lib/filecmp.py index 7c47eb022cc8c0..dbbeb18b08c549 100644 --- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -36,8 +36,13 @@ def cmp(f1, f2, shallow=True): f2 -- Second file name - shallow -- Just check stat signature (do not read the files). - defaults to True. + shallow [True] + True: - if stat signature matches, return True; otherwise fall back to + the logic of shallow=False. + + False: - return False if file sizes differ, otherwise compare full file contents. + + Note that stat signature contains 3 values: type, size and mtime. Return value: From 687ecc67646bd3c7ad6025c4451986345ca602fc Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Thu, 15 Jul 2021 11:19:26 -0400 Subject: [PATCH 2/3] add news entry --- .../next/Documentation/2021-07-15-11-19-03.bpo-42958.gC5IHM.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2021-07-15-11-19-03.bpo-42958.gC5IHM.rst diff --git a/Misc/NEWS.d/next/Documentation/2021-07-15-11-19-03.bpo-42958.gC5IHM.rst b/Misc/NEWS.d/next/Documentation/2021-07-15-11-19-03.bpo-42958.gC5IHM.rst new file mode 100644 index 00000000000000..c93b84d095526e --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2021-07-15-11-19-03.bpo-42958.gC5IHM.rst @@ -0,0 +1,2 @@ +Updated the docstring and docs of :func:`filecmp.cmp` to be more accurate +and less confusing especially in respect to *shallow* arg. From 042be98165635dce470c793023712b7dd1aad87e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 4 Aug 2021 20:48:00 +0200 Subject: [PATCH 3/3] Reword to include @AlexVndnblcke's suggestions and restore natural language flow Co-authored-by: Alexander Vandenbulcke --- Doc/library/filecmp.rst | 11 ++++------- Lib/filecmp.py | 10 +++------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Doc/library/filecmp.rst b/Doc/library/filecmp.rst index c799921d8c1104..83e9e14ddcacd8 100644 --- a/Doc/library/filecmp.rst +++ b/Doc/library/filecmp.rst @@ -22,14 +22,11 @@ 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: if stat signature matches, return True; if it does not - match, fall back to the logic of shallow=False. + If *shallow* is true and the :func:`os.stat` signatures (file type, size, and + modification time) of both files are identical, the files are taken to be + equal. - If *shallow* is false: return False if file sizes differ, otherwise compare full - file contents. - - Note that :func:`os.stat` signature, as used here, contains 3 values: type, size - and mtime. + Otherwise, the files are treated as different if their sizes or contents differ. Note that no external programs are called from this function, giving it portability and efficiency. diff --git a/Lib/filecmp.py b/Lib/filecmp.py index dbbeb18b08c549..70a4b23c982205 100644 --- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -36,13 +36,9 @@ def cmp(f1, f2, shallow=True): f2 -- Second file name - shallow [True] - True: - if stat signature matches, return True; otherwise fall back to - the logic of shallow=False. - - False: - return False if file sizes differ, otherwise compare full file contents. - - Note that stat signature contains 3 values: type, size and mtime. + shallow -- treat files as identical if their stat signatures (type, size, + mtime) are identical. Otherwise, files are considered different + if their sizes or contents differ. [default: True] Return value: