This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unhandled IOError filecmp.cmpfiles() if file not readable
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: asvetlov, python-dev, terry.reedy, till
Priority: normal Keywords: patch

Created on 2012-11-30 18:27 by till, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
filecmp-minimal.py till, 2012-11-30 18:27 sample script reproducing the error
filecmp_exception.patch till, 2012-11-30 18:27 Patch to catch the IOError review
Messages (7)
msg176700 - (view) Author: Till Maas (till) Date: 2012-11-30 18:27
If filecmp is used with files that the user is not allowed to read, it creates an IOError exception instead of returning the file's name as an file that could not compared. The documentation says:

http://docs.python.org/2/library/filecmp.html
Returns three lists of file names: match, mismatch, errors. [...] errors lists the names of files which could not be compared. Files are listed in errors if they don’t exist in one of the directories, the user lacks permission to read them or if the comparison could not be done for some other reason.

The attached file fails with an IOError:
$ python filecmp-minimal.py 
Traceback (most recent call last):
  File "filecmp-minimal.py", line 21, in <module>
    print filecmp.cmpfiles(join(t, "a"), join(t, "b"), ["testfile"], shallow=False)
  File "/usr/lib64/python2.7/filecmp.py", line 258, in cmpfiles
    res[_cmp(ax, bx, shallow)].append(x)
  File "/usr/lib64/python2.7/filecmp.py", line 270, in _cmp
    return not abs(cmp(a, b, sh))
  File "/usr/lib64/python2.7/filecmp.py", line 53, in cmp
    outcome = _do_cmp(f1, f2)
  File "/usr/lib64/python2.7/filecmp.py", line 66, in _do_cmp
    with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
IOError: [Errno 13] Permission denied: '/tmp/tmpp93pmt/a/testfile'

A quick glance at the current code in hg shows, that this bug is probably also present in all later Python versions than 2.7. I will attach a small patch that fixes this bug.
msg177144 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-12-08 05:19
3.3.0, Win 7, 64 bit, with print() and exception as e (which also works in 2.7 for more portable code ;-)

>>> 
(['testfile'], [], [])

It this correct for Windows, where I believe chmod is a no-op?
msg177463 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-12-14 16:05
The error can be reproduced for 2.7 and 3.2.
Starting from 3.3 os.error and IOError both aliases for OSError and patch doesn't needed.
msg188683 - (view) Author: Till Maas (till) Date: 2013-05-07 19:45
When might this be patched in Python 2.X? This Exception makes the function pretty useless for me, since it makes by custom compare script crash.
msg188747 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-05-09 03:43
New changeset 1823cf6e1084 by Terry Jan Reedy in branch '2.7':
Issue 16584: in filecomp._cmp, catch IOError as well as os.error.
http://hg.python.org/cpython/rev/1823cf6e1084
msg188749 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-05-09 03:53
I don't use filecmp and cannot reproduce problem on Windows, for reason stated, but this looks correct, so I am taking Till's word that this fixes the problem, at least for him.

Intentionally applied to 2.7 only for reason stated by Andrew.

Till, if you submit more patches, especially any that are not so trivial, please sign PSF Contributor Licence Agreement.
http://www.python.org/psf/contrib/
msg188758 - (view) Author: Till Maas (till) Date: 2013-05-09 09:39
I just tried on a Windows 8 system with python from GIMP. The error occurs there as well if I compare two empty files after I removed permissions for one of the files. I do not know how to manage Windows' file ACLs in python, therefore I created the test case using the Explorer.
History
Date User Action Args
2022-04-11 14:57:38adminsetgithub: 60788
2013-05-09 09:39:42tillsetmessages: + msg188758
2013-05-09 03:53:34terry.reedysetstatus: open -> closed
resolution: fixed
messages: + msg188749

stage: patch review -> resolved
2013-05-09 03:43:04python-devsetnosy: + python-dev
messages: + msg188747
2013-05-09 03:28:09terry.reedysetassignee: terry.reedy
2013-05-07 19:45:10tillsetmessages: + msg188683
2012-12-14 16:05:38asvetlovsetnosy: + asvetlov
messages: + msg177463
2012-12-08 05:19:01terry.reedysetnosy: + terry.reedy
messages: + msg177144
2012-12-01 12:24:47daniel.urbansettype: crash -> behavior
stage: patch review
2012-11-30 18:27:54tillsetfiles: + filecmp_exception.patch
keywords: + patch
2012-11-30 18:27:20tillcreate