[New-bugs-announce] [issue46512] Explicit or correct behavior of filecmp.cmpfiles w/ absolute path names

bers report at bugs.python.org
Tue Jan 25 04:46:10 EST 2022


New submission from bers <bers at gmx.net>:

It is very easy to use filecmp.cmpfiles incorrectly by passing absolute path names. This is because
1. the documentations does not say that relative path names have to be passed, and
2. filecmp.cmpfiles does not issue a warning when absolute path names are passed.

Consider this example code, which does look sensible at first glance:

    files = dir_a.glob("*")
    (equal, _, _) = filecmp.cmpfiles(dir_a, dir_b, files, shallow=False)
    print("equal:", *equal)

However, in the full example below, you will see that this code fails to detect that two files are actually different.

"""Demo behavior of filecmp.cmpfiles with absolute path names."""
import filecmp
import tempfile
from pathlib import Path

with tempfile.TemporaryDirectory() as tmpdirname:
    # prepare two different files
    tmpdir = Path(tmpdirname)
    dir_a = tmpdir / "a"
    dir_b = tmpdir / "b"
    file_a = dir_a / "foo.txt"
    file_b = dir_b / "foo.txt"

    dir_a.mkdir()
    dir_b.mkdir()
    file_a.write_text("A")
    file_b.write_text("B")

    # actually diff the files
    files = dir_a.glob("*")
    # filecmp should issue a warning here!
    (equal, _, _) = filecmp.cmpfiles(dir_a, dir_b, files, shallow=False)
    # otherwise, this result is easy to misinterpret - files are reported as equal
    print("equal:", *equal)

----------
components: Library (Lib)
messages: 411570
nosy: bers
priority: normal
severity: normal
status: open
title: Explicit or correct behavior of filecmp.cmpfiles w/ absolute path names
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46512>
_______________________________________


More information about the New-bugs-announce mailing list