https://github.com/python/cpython/commit/0df683f36cde812e11ee5a19a4f3da3d456b9b20
commit: 0df683f36cde812e11ee5a19a4f3da3d456b9b20
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-21T06:51:33+03:00
summary:

[3.14] gh-154308: Clear file flags in os_helper.rmtree() (GH-154310) (GH-154315)

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 1f649fecb645d70b9c48268839e4177d8fe4e1d1)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>

files:
M Lib/test/support/os_helper.py

diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py
index 2c45fe2369ec36b..daf6060940e97f0 100644
--- a/Lib/test/support/os_helper.py
+++ b/Lib/test/support/os_helper.py
@@ -467,12 +467,23 @@ def _rmtree(path):
 
         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)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to