-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
filecmp.cmp() incorrect results when previously compared file is modified within modification time resolution #62349
Comments
Example: with open('file1', 'w') as f:
f.write('a')
with open('file2', 'w') as f:
f.write('a')
print filecmp.cmp('file1', 'file2', shallow=False) # true
with open('file2', 'w') as f:
f.write('b') print filecmp.cmp('file1', 'file2', shallow=False) # true Because of the caching, both calls to filecmp.cmp() return true on my system. When retrieving value from cache, the function filecmp.cmp() checks the signatures of the files: s1 = _sig(os.stat(f1))
s2 = _sig(os.stat(f2))
...
outcome = _cache.get((f1, f2, s1, s2)) But the signatures in cache are the same, if the file sizes and times of modification (os.stat().st_mtime) haven't changed from the last call, even if the content has changed. The buffer is mentioned in the documentation, but there isn't any documented way to clear it. It also isn't nice IMO, that one has to worry about the file system's resolution of the file modification time when calling a simple file comparison. |
It seems like this would be a fairly rare situation and, as you note, dependent on the underlying file system. But it would be easy to add a new function to the module to clear its cache in cases where it is known this might be a problem. In fact, in bpo-11802 a clear_cache function was proposed to solve the problem of the cache growing without bounds but that problem was solved by the simpler solution of discarding the cache when it gets above 100 entries. |
+1 for a cache clearing function like the one in re.py |
I've added a "clear_cache()" method to filecmp.py. Patch attached. I had thought about implementing an optional parameter to only invalidate the cache of a specific file object, but figured I'd keep it simple for now. First time submitting a patch, so apologies if I've done something the wrong way. |
Thanks for the patch, Mark. I've left some review comments via Rietveld (the review link next to the patch). Also, if you haven't already, please fill out the contributor form as described in the Developer's Guide (http://docs.python.org/devguide/patch.html#licensing). |
Ned, Thanks for taking the time to review. I've updated the docs, added a unit test, signed the contributor form, and made the changes/corrections from your review. Updated patch attached. |
Looks good to me, other than that the doc change should include a version added directive (which can be added by the committer): .. function:: clear_cache() + .. versionadded:: 3.4 |
Cool. I've gone ahead and generated a new patch with the version added directive included. |
New changeset bfd53dcb02ff by Ned Deily in branch 'default': |
Committed for release in 3.4.0. Thanks, Mark. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: