Clean garbage tmpdirs with tempfile.TemporaryDirectory#7941
Clean garbage tmpdirs with tempfile.TemporaryDirectory#7941graingert wants to merge 1 commit intopytest-dev:mainfrom
Conversation
a53c978 to
09b0e20
Compare
src/_pytest/pathlib.py
Outdated
| garbage = parent.joinpath(f"garbage-{uuid.uuid4()}") | ||
| path.rename(garbage) | ||
| rm_rf(garbage) | ||
| with tempfile.TemporaryDirectory( |
There was a problem hiding this comment.
This should spec a root directory and a prefix
There was a problem hiding this comment.
dir is the root directory
|
IIUC, this is basically a hack to coerce |
|
Since the rmtree helper is simple, how about replicating |
|
so it turns out this PR does fix my original issue, but my test case is hitting #7947 |
09b0e20 to
216c7af
Compare
I'm just composing a comment for this now - and I'd like to comment the use of the rename to "garbage-", but I'm not really clear on why this rename was done originally: https://github.com/pytest-dev/pytest/pull/3988/files#diff-b2559484fd51ba4a312585ab8375eda68c43b0b66bb329ec036106255928a2b9R169 |
5ba2ca9 to
ceea364
Compare
|
this breaks on windows and I don't know why |
|
@graingert the name garbage was replicated from pylib which uses it as indicator of folder names that can be removed even if things fail from the processs that initially tries to take them out |
d3407fe to
3d4a8b1
Compare
|
I rebased to fix a conflict. |
Python has a very nice rm_rf hidden in tempfile.TemporaryDirectory._rmtree we can use it to cleanup our "garbage-" dir https://github.com/python/cpython/blob/3d43f1dce3e9aaded38f9a2c73e3c66acf85505c/Lib/tempfile.py#L784-L812
3d4a8b1 to
f78b56a
Compare
…-dev#7940) The on_rm_rf_error handler previously only set S_IRUSR|S_IWUSR (read+write) when retrying failed removals, and silently skipped os.open/os.scandir failures. This meant directories lacking execute permission (S_IXUSR) could not be traversed or removed by shutil.rmtree. Now the handler: - Uses S_IRWXU (read+write+execute) for all permission fixes - Handles os.open and os.scandir PermissionError by fixing permissions on the path and its parent, then removing the entry directly - Includes a guard against infinite recursion when chmod has no effect Supersedes pytest-dev#7941 which took the approach of delegating to tempfile.TemporaryDirectory._rmtree. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Claude Opus <claude@anthropic.com>
Closes #7940
see also #7933 for my first attempt