New submission from Dean Morin <morin.d...@gmail.com>:

By default `filecmp.cmp()` has `shallow=True` which can produce some surprising 
behavior.

In the docs it states:

> If shallow is true, files with identical os.stat() signatures are taken to be 
> equal.

However the "signature" only considers the file mode, file size, and file 
modification time, which is not sufficient. `cmp()` will return `True` (files 
are equal) in some circumstances for files that actually differ. Depending on 
the underlying file system, the same python script will return `True` or 
`False` when `cmp()` is called on the exact same files. I'll add the 
long-winded details at the bottom.

To fix, I believe `st.st_ino` should be included in `_sig` 
(https://github.com/python/cpython/blob/3.7/Lib/filecmp.py#L68).

I'm in the middle of a move, but I can make a PR in the next couple weeks if 
this seems like a reasonable fix and no one else gets around to it.

The long version is that we're migrating some existing reports to a new data 
source. The goal is to produce identical csv files from both data sources. I 
have a python script that pulls down both csv files and uses `cmp()` to compare 
them. 

On my machine, the script correctly discovers the differences between the two. 
One of the date columns has incorrect dates in the new version.

However on my colleagues machine, the script fails to discover the differences 
and shows that the csv files are identical.

The difference is that on my machine, `os.stat(f).st_mtime` is a timestamp 
which includes fractional seconds (1529108360.1955538), but only includes the 
seconds (1529108360.0) on my colleagues machine. Since only the dates differed 
within the csvs, both files had the same file mode, file size, and both were 
downloaded within the same second.

We got a few more people to see what they got for `st_mtime`. The link could be 
the file system used. We're all using macs, but for those of us using an APFS 
Volume disk, `st_mtime` returns a timestamp which includes fractional seconds, 
and for those of us using a Logical Volume Mac OS Extended disk, it returns a 
timestamp which only includes the seconds (1529108360.0).

When comparing os.stat() between the two differing csv files, the only 
difference (other than fractional seconds for various timestamps) was `st_ino` 
which is why I believe it should be included in `_sig()`.

----------
components: Library (Lib)
messages: 319904
nosy: Dean Morin
priority: normal
severity: normal
status: open
title: filecmp.cmp returns True on files that differ
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33896>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to