https://github.com/python/cpython/commit/89022ac78fce53e10759e80909ddebfa463c2565
commit: 89022ac78fce53e10759e80909ddebfa463c2565
branch: 3.14
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-11-12T18:57:01Z
summary:

[3.14] gh-140774: Fix clearing the read-only file attribute on Windows 
(GH-154852) (GH-161898)

SetFileInformationByHandle() interprets a FILE_BASIC_INFO with
FileAttributes == 0 as "leave the attributes unchanged", so clearing
the read-only flag of a file with no other attribute set was silently
ignored by os.chmod(follow_symlinks=True), os.fchmod() and
pathlib.Path.chmod().  Substitute FILE_ATTRIBUTE_NORMAL in that case.
(cherry picked from commit 46d437dfedfd7cc0995927085b368b0ffe895b4d)

Co-authored-by: Yusuke Hayashi <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>

files:
A Misc/NEWS.d/next/Windows/2026-07-29-13-05-00.gh-issue-140774.wA3ttr.rst
M Lib/test/test_os.py
M Modules/posixmodule.c

diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 29f15cd622e0f9..19674671c425f5 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -3362,6 +3362,68 @@ def test_unlink_removes_junction(self):
         os.unlink(self.junction)
         self.assertFalse(os.path.exists(self.junction))
 
[email protected](sys.platform == "win32", "Win32 specific tests")
+class Win32ChmodTests(unittest.TestCase):
+    # gh-140774: os.chmod(follow_symlinks=True) and os.fchmod() go through
+    # SetFileInformationByHandle(), which interprets FileAttributes == 0 as
+    # "leave the attributes unchanged".  Clearing the read-only flag of a
+    # file with no other attribute set must substitute FILE_ATTRIBUTE_NORMAL
+    # for 0 to take effect.
+
+    def setUp(self):
+        self.fname = os.path.abspath(os_helper.TESTFN)
+        self.addCleanup(os_helper.unlink, self.fname)
+        self.addCleanup(self._make_writable)
+        create_file(self.fname)
+
+    def _make_writable(self):
+        try:
+            os.chmod(self.fname, stat.S_IWRITE | stat.S_IREAD,
+                     follow_symlinks=False)
+        except OSError:
+            pass
+
+    def _set_readonly_only_attribute(self):
+        # Make FILE_ATTRIBUTE_READONLY the only attribute of the file
+        # (in particular, clear FILE_ATTRIBUTE_ARCHIVE).
+        ctypes = import_helper.import_module('ctypes')
+        wintypes = import_helper.import_module('ctypes.wintypes')
+        kernel32 = ctypes.WinDLL('Kernel32.dll', use_last_error=True)
+        SetFileAttributesW = kernel32.SetFileAttributesW
+        SetFileAttributesW.argtypes = (wintypes.LPCWSTR, wintypes.DWORD)
+        SetFileAttributesW.restype = wintypes.BOOL
+
+        if not SetFileAttributesW(self.fname, stat.FILE_ATTRIBUTE_READONLY):
+            raise ctypes.WinError(ctypes.get_last_error())
+        attributes = os.stat(self.fname).st_file_attributes
+        if attributes != stat.FILE_ATTRIBUTE_READONLY:
+            self.skipTest('file system reports extra file attributes '
+                          f'{attributes:#x}')
+
+    def _assert_writable(self):
+        self.assertEqual(os.stat(self.fname).st_file_attributes
+                         & stat.FILE_ATTRIBUTE_READONLY, 0)
+
+    def test_chmod_clears_readonly_without_other_attributes(self):
+        self._set_readonly_only_attribute()
+        os.chmod(self.fname, stat.S_IWRITE | stat.S_IREAD,
+                 follow_symlinks=True)
+        self._assert_writable()
+
+    def test_fchmod_clears_readonly_without_other_attributes(self):
+        fd = os.open(self.fname, os.O_RDWR)
+        try:
+            self._set_readonly_only_attribute()
+            os.fchmod(fd, stat.S_IWRITE | stat.S_IREAD)
+            self._assert_writable()
+
+            self._set_readonly_only_attribute()
+            os.chmod(fd, stat.S_IWRITE | stat.S_IREAD)
+            self._assert_writable()
+        finally:
+            os.close(fd)
+
+
 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
 class Win32NtTests(unittest.TestCase):
     def test_getfinalpathname_handles(self):
diff --git 
a/Misc/NEWS.d/next/Windows/2026-07-29-13-05-00.gh-issue-140774.wA3ttr.rst 
b/Misc/NEWS.d/next/Windows/2026-07-29-13-05-00.gh-issue-140774.wA3ttr.rst
new file mode 100644
index 00000000000000..aaf120efb992e7
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2026-07-29-13-05-00.gh-issue-140774.wA3ttr.rst
@@ -0,0 +1,3 @@
+Fix :func:`os.chmod` with ``follow_symlinks=True``, :func:`os.fchmod` and
+:meth:`pathlib.Path.chmod` on Windows: clearing the read-only attribute of a
+file which has no other attribute set is no longer silently ignored.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 996f5451da059a..d6310b51429fcc 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -3597,6 +3597,12 @@ win32_hchmod(HANDLE hfile, int mode)
     }
     if (mode & _S_IWRITE) {
         info.FileAttributes &= ~FILE_ATTRIBUTE_READONLY;
+        if (info.FileAttributes == 0) {
+            /* SetFileInformationByHandle() interprets FileAttributes == 0
+               as "leave the attributes unchanged"; FILE_ATTRIBUTE_NORMAL
+               is the documented way to clear all attributes. */
+            info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
+        }
     }
     else {
         info.FileAttributes |= FILE_ATTRIBUTE_READONLY;

_______________________________________________
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