Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

moreati
Copy link
Contributor

@moreati moreati commented Feb 29, 2024

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().

Fixes #115911


📚 Documentation preview 📚: https://cpython-previews--116131.org.readthedocs.build/

@moreati
Copy link
Contributor Author

moreati commented Mar 1, 2024

The new test fails (as intended) without the accompanying fix. Local test run on macOS 14.3.1

======================================================================
ERROR: test_permission_error_cwd (test.test_importlib.import_.test_path.Frozen_FindModuleTests.test_permission_error_cwd)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alex/src/cpython/Lib/test/test_importlib/import_/test_path.py", line 175, in test_permission_error_cwd
    self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 1544, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1516, in _get_spec
  File "<frozen importlib._bootstrap_external>", line 1495, in _path_importer_cache
PermissionError: [Errno 13] Permission denied

Copy link
Member

@jaraco jaraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but I'd like to see another review from someone more familiar with importlib internals.

Lib/test/test_importlib/import_/test_path.py Outdated Show resolved Hide resolved
Lib/test/support/os_helper.py Outdated Show resolved Hide resolved
Lib/test/support/os_helper.py Outdated Show resolved Hide resolved
Lib/test/test_importlib/import_/test_path.py Outdated Show resolved Hide resolved
Doc/reference/import.rst Outdated Show resolved Hide resolved
@jaraco jaraco removed their assignment Aug 29, 2024
@moreati moreati force-pushed the issue115911 branch 2 times, most recently from 076a29d to b79fd8d Compare August 31, 2024 21:27
Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one of the decorators you're adding might not be necessary, or at least can be simplified. Otherwise LGTM!

Lib/test/support/os_helper.py Outdated Show resolved Hide resolved
Lib/test/support/os_helper.py Outdated Show resolved Hide resolved
Lib/test/support/os_helper.py Outdated Show resolved Hide resolved
Lib/test/test_importlib/import_/test_path.py Outdated Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Sep 13, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@moreati
Copy link
Contributor Author

moreati commented Sep 14, 2024

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Sep 14, 2024

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

importlib: PermissionError during startup if working directory isn't readable
3 participants