classification
Title: _fill_cache in _bootstrap.py crashes without directory execute permissions
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: David.Pritchard, brett.cannon, christian.heimes, eric.snow, jcea, meador.inge, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2012-12-19 14:55 by David.Pritchard, last changed 2013-01-11 20:46 by brett.cannon. This issue is now closed.

Files
File name Uploaded Description Edit
_bootstrap.py.patch David.Pritchard, 2012-12-19 14:54 diff patch to fix.
Messages (8)
msg177745 - (view) Author: David Pritchard (David.Pritchard) Date: 2012-12-19 14:54
In importlib/_bootstrap.py, there is a function _fill_cache which crashes when you try to run Python in any environment that is so restricted that write permissions are not allowed. You get a trace like:

Traceback (most recent call last):
  In line:
    [the import statement]
  File "<frozen importlib._bootstrap>", line 1558, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1516, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1470, in _find_module
  File "<frozen importlib._bootstrap>", line 1305, in find_module
  File "<frozen importlib._bootstrap>", line 1284, in _get_loader
  File "<frozen importlib._bootstrap>", line 1356, in find_loader
  File "<frozen importlib._bootstrap>", line 1392, in _fill_cache
PermissionError: [Errno 13] Permission denied: '[the directory name]'

This was not an issue when I was using Python 3.2 and I suspect it has to do with the OS error refactoring. I believe it can be fixed with a two-line patch, attached. (Sorry if it is the wrong format, it's the first one I've submitted.) The patch simply treats the case of PermissionError the same was as it does when there is a FileNotFoundError. I've tested the patch and it fixes the problem.
msg177746 - (view) Author: David Pritchard (David.Pritchard) Date: 2012-12-19 14:57
Sorry, in the main text of my message, "write permissions" should say directory execute permissions (permission to list contents of a directory)
msg177769 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-12-19 17:58
Patch looks good to me.  Are there any other exceptions that might be included here?
msg177776 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-12-19 20:22
If Antoine can't think of any then I think there aren't any more. This does need a test, though.
msg178963 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-01-03 16:35
NotADirectoryError is missing from the list of exception. The error can occur when the directory is removed and replaced by an ordinary file.
msg178986 - (view) Author: David Pritchard (David.Pritchard) Date: 2013-01-03 20:30
Here's an example where Python 3.3.0 crashes, but where the patched code works. I have only been able to trigger the bug when PYTHONPATH is set (even if to an empty value). 

(1) create a directory
(2) chmod a-rw+x on that directory
(3) export PYTHONPATH=""
(4) python3 from within that directory: should crash on loading

Note, without step (3) the error does not occur. Alternatively, do (1), (2),

(3') set PYTHONPATH to that -rw'ed directory
(4') python3 from outside of that directory: should crash on loading
msg179729 - (view) Author: Roundup Robot (python-dev) Date: 2013-01-11 20:45
New changeset 159967aa24a5 by Brett Cannon in branch '3.3':
Issue #16730: Don't raise an exception in
http://hg.python.org/cpython/rev/159967aa24a5

New changeset b94f308e9b47 by Brett Cannon in branch 'default':
Merge from 3.3 for fix for issue #16730
http://hg.python.org/cpython/rev/b94f308e9b47
msg179730 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-01-11 20:46
Tests added for both the file and unreadable cases and the appropriate exceptions now caught. Thanks to everyone for helping with this!
History
Date User Action Args
2013-01-11 20:46:09brett.cannonsetstatus: open -> closed
versions: + Python 3.4
messages: + msg179730

resolution: fixed
stage: test needed -> resolved
2013-01-11 20:45:12python-devsetnosy: + python-dev
messages: + msg179729
2013-01-08 16:02:14jceasetnosy: + jcea
2013-01-03 20:30:39David.Pritchardsetmessages: + msg178986
2013-01-03 16:35:19christian.heimessetnosy: + christian.heimes
messages: + msg178963
2013-01-03 04:47:50meador.ingesetnosy: + meador.inge
2012-12-19 20:23:01brett.cannonsettype: behavior
stage: test needed
2012-12-19 20:22:46brett.cannonsetmessages: + msg177776
2012-12-19 17:58:40eric.snowsetnosy: + eric.snow, brett.cannon
messages: + msg177769
2012-12-19 15:17:02skrahsetnosy: + pitrou
2012-12-19 14:57:32David.Pritchardsetmessages: + msg177746
2012-12-19 14:55:01David.Pritchardcreate