]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154308: Clear file flags in os_helper.rmtree() (GH-154310)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Jul 2026 22:47:43 +0000 (01:47 +0300)
committerGitHub <noreply@github.com>
Mon, 20 Jul 2026 22:47:43 +0000 (22:47 +0000)
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.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Lib/test/support/os_helper.py

index 2c45fe2369ec36b574258e701bfc078114c5499d..daf6060940e97f0a16ca841f7f4fc0d930c49523 100644 (file)
@@ -467,12 +467,23 @@ else:
 
         def _rmtree_inner(path):
             from test.support import _force_run
+            # Clear file flags (e.g. UF_IMMUTABLE, UF_NOUNLINK on BSD).
+            if hasattr(os, 'chflags'):
+                try:
+                    os.chflags(path, 0)
+                except OSError:
+                    pass
             for name in _force_run(path, os.listdir, path):
                 fullname = os.path.join(path, name)
                 try:
                     mode = os.lstat(fullname).st_mode
                 except OSError:
                     mode = 0
+                if hasattr(os, 'lchflags'):
+                    try:
+                        os.lchflags(fullname, 0)
+                    except OSError:
+                        pass
                 if stat.S_ISDIR(mode):
                     _rmtree_inner(fullname)
                     _force_run(path, os.rmdir, fullname)