gh-115911: Ignore PermissionError during import from cwd (#116131)

Ignore PermissionError when checking cwd during import

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.

Introduces `test.support.os_helper.save_mode(path, ...)`, a context manager
that restores the mode of a path on exit.

This is allows finer control of exception handling and robust environment
restoration across platforms in `FinderTests.test_permission_error_cwd()`.

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Co-authored-by: Brett Cannon <brett@python.org>
This commit is contained in:
Alex Willmer 2025-01-26 19:00:28 +00:00 committed by GitHub
parent 914c232e93
commit a8dc6d6d44
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 58 additions and 5 deletions

View file

@ -1244,7 +1244,7 @@ class PathFinder:
if path == '':
try:
path = _os.getcwd()
except FileNotFoundError:
except (FileNotFoundError, PermissionError):
# Don't cache the failure as the cwd can easily change to
# a valid directory later on.
return None