Skip to content

Commit 0f49745

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-154308: Clear file flags in os_helper.rmtree() (GH-154310) (GH-154314)
On BSD systems a test may leave behind files or directories with flags such as UF_IMMUTABLE or UF_NOUNLINK set, which prevent the directory from being modified or removed. Clear the flags before removing, so cleaning up a temporary directory does not fail. (cherry picked from commit 1f649fe) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0fd1209 commit 0f49745

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

Lib/test/support/os_helper.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,23 @@ def _rmtree(path):
467467

468468
def _rmtree_inner(path):
469469
from test.support import _force_run
470+
# Clear file flags (e.g. UF_IMMUTABLE, UF_NOUNLINK on BSD).
471+
if hasattr(os, 'chflags'):
472+
try:
473+
os.chflags(path, 0)
474+
except OSError:
475+
pass
470476
for name in _force_run(path, os.listdir, path):
471477
fullname = os.path.join(path, name)
472478
try:
473479
mode = os.lstat(fullname).st_mode
474480
except OSError:
475481
mode = 0
482+
if hasattr(os, 'lchflags'):
483+
try:
484+
os.lchflags(fullname, 0)
485+
except OSError:
486+
pass
476487
if stat.S_ISDIR(mode):
477488
_rmtree_inner(fullname)
478489
_force_run(path, os.rmdir, fullname)

0 commit comments

Comments
 (0)