Repository navigation
fix: safely handle restrictive directory cleanup and close scandir iterator (fixes #15169) - #15171
Open
saifmodan2006 wants to merge 2 commits into
Open
saifmodan2006 wants to merge 2 commits into
saifmodan2006 wants to merge 2 commits into
Conversation
for more information, see https://pre-commit.ci
|
Verified this on macOS with Python 3.14.6. The three new regression tests in |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #15169.
When test suites create directories with restrictive modes such as
0o000(for example, tests exercising non-browseable or permission-denied directories), pytest's subsequent cleanup of numbered directories (cleanup_numbered_dir->rm_rf(garbage)) could fail withOSError: [Errno 39] Directory not emptyor leak unclosedscandiriterators resulting inPytestUnraisableExceptionWarning. When running test suites with-W error, this warning would turn into an unhandled exception during session finish, causing clean test runs to fail.Changes Made
src/_pytest/pathlib.py:on_rm_rf_error:IsADirectoryErrorand(func in (os.unlink, os.remove) and p.is_dir()): when directory traversal was unable to identify a directory (e.g. restrictive permissions on POSIX), chmod the entry and recursively remove it withrm_rf(p).ENOTEMPTY/EEXISTwhenfunc is os.rmdir: if removing a parent directory fails because child entries were skipped due to restrictive permissions, grant_chmod_rwxacross the directory tree and retry removal.rm_rf:_chmod_rwx(str(path))at the start ofrm_rfso the target directory has read/write/exec permissions beforeshutil.rmtreetraversal begins.find_prefixed:os.scandir(root)with awith os.scandir(root) as it:context manager to ensure theScandirIteratoris properly finalized and closed, preventingResourceWarning: unclosed scandir iteratoron Python 3.14+.ENOTEMPTYandEEXISTfromerrno.testing/test_tmpdir.py:test_on_rm_rf_error_is_a_directory_error: verifiesIsADirectoryErroris handled cleanly byon_rm_rf_errorwithout warnings.test_on_rm_rf_error_directory_not_empty: verifiesENOTEMPTYonos.rmdirtriggers recursive chmod and cleanup without warnings.test_find_prefixed_closes_scandir_iterator: verifiesfind_prefixedcloses thescandiriterator without emittingResourceWarning.Testing
testing/test_tmpdir.py: 53 passed, 9 skipped.testing/test_pathlib.py: 113 passed.