classification
Title: mimetypes.init() fails if no access to one of known files
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Michał Szymaniak, SilentGhost, maxking, r.david.murray, terry.reedy
Priority: normal Keywords:

Created on 2019-11-03 12:33 by Michał Szymaniak, last changed 2019-11-08 20:43 by terry.reedy.

Messages (3)
msg355897 - (view) Author: Michał Szymaniak (Michał Szymaniak) Date: 2019-11-03 12:33
When user lacks rights to read on of the mimetypes.knownfiles, mimetypes init() will throw PermissionError and library becomes unusable.

Reproduction steps:
# mkdir -p /etc/httpd/conf/
# touch /etc/httpd/conf/mime.types
# chmod a-r /etc/httpd/conf/mime.types
$ python
>>> import mimetypes
>>> mimetypes.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/mimetypes.py", line 355, in init
    db.read(file)
  File "/usr/lib/python3.7/mimetypes.py", line 204, in read
    with open(filename, encoding='utf-8') as fp:
PermissionError: [Errno 13] Permission denied: '/etc/httpd/conf/mime.types'
msg355907 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-11-03 19:41
The fix seems fairly obvious: replacing isfile check with try-catch statement for all OSErrors. Would you like to submit a pull request, Michał?
msg356260 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-11-08 20:43
An exception report is a failure to finish but not a crash as meant here.

The current code requires all known files and any file explicitly passes to be readable.  The proposed simple change would make it OK if none of them are.  But is this really OK?

Is it really OK if known files are not readable?  Should they not be readable by all?

Removing the isfile call would allow directories to be passed through to db.readfp.  This might not be a good idea.  We would definitely have to think about what errors might be raised on different systems.

I think the try-except should be in init rather than db.read.  The latter can be called directly and I think a user explicitly passing an filename should be notified.  This comment actually also applies to files passed to init.

[Sidenote: the doc says that MimeTypes "provides an interface similar to the one of the mimetypes module."  The mimetypes.db used by mimetypes functions *is* a MimeTypes instance and the user can call db methods anything after init().]

Overall, I am not convinced that the current behavior is a bug, in which case this is an 'enhancement' request that changes the current API.  I am not at all a mimetypes expert, so I nosied a couple of email people who have worked on the module and should know more.
History
Date User Action Args
2019-11-08 20:43:32terry.reedysetnosy: + terry.reedy, r.david.murray, maxking

messages: + msg356260
title: Crash on mimetypes.init() if there is no access to one of knownfiles -> mimetypes.init() fails if no access to one of known files
2019-11-03 19:41:02SilentGhostsetversions: - Python 2.7, Python 3.5, Python 3.6
nosy: + SilentGhost

messages: + msg355907

type: crash -> behavior
stage: test needed
2019-11-03 12:33:22Michał Szymaniakcreate