Skip to content

Commit 8b1cbd4

Browse files
committed
Ignore PermissionError during import from cwd
On macOS `getcwd(3)` can return EACCES if a path component isn't readable, resulting in PermissionError. `PathFinder.find_spec()` now catches these and ignores them - the same treatment as a missing/deleted cwd.
1 parent f0df35e commit 8b1cbd4

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

Doc/reference/import.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,10 +871,10 @@ module.
871871

872872
The current working directory -- denoted by an empty string -- is handled
873873
slightly differently from other entries on :data:`sys.path`. First, if the
874-
current working directory is found to not exist, no value is stored in
875-
:data:`sys.path_importer_cache`. Second, the value for the current working
876-
directory is looked up fresh for each module lookup. Third, the path used for
877-
:data:`sys.path_importer_cache` and returned by
874+
current working directory cannot be determined or is found to not exist, no
875+
value is stored in :data:`sys.path_importer_cache`. Second, the value for the
876+
current working directory is looked up fresh for each module lookup. Third,
877+
the path used for :data:`sys.path_importer_cache` and returned by
878878
:meth:`importlib.machinery.PathFinder.find_spec` will be the actual current
879879
working directory and not the empty string.
880880

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,7 @@ def _path_importer_cache(cls, path):
14931493
if path == '':
14941494
try:
14951495
path = _os.getcwd()
1496-
except FileNotFoundError:
1496+
except (FileNotFoundError, PermissionError):
14971497
# Don't cache the failure as the cwd can easily change to
14981498
# a valid directory later on.
14991499
return None

Lib/test/test_importlib/import_/test_path.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from test.test_importlib import fixtures
12
from test.test_importlib import util
23

34
importlib = util.import_importlib('importlib')
@@ -153,6 +154,21 @@ def test_deleted_cwd(self):
153154
# Do not want FileNotFoundError raised.
154155
self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
155156

157+
def test_permission_error_cwd(self):
158+
new_dir = new_dir = tempfile.mkdtemp()
159+
self.addCleanup(os.rmdir, new_dir)
160+
161+
with fixtures.save_cwd(), util.import_state(path=['']):
162+
os.chdir(new_dir)
163+
try:
164+
os.chmod(new_dir, 0o000)
165+
except OSError:
166+
self.skipTest("platform does not allow "
167+
"changing mode of the cwd")
168+
169+
# Do not want PermissionError raised.
170+
self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
171+
156172
def test_invalidate_caches_finders(self):
157173
# Finders with an invalidate_caches() method have it called.
158174
class FakeFinder:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
If the current working directory cannot be determined due to permissions,
2+
then import will no longer raise :exc:`PermissionError`. Patch by Alex
3+
Willmer.

0 commit comments

Comments
 (0)