GeezerVison is showing github.com — gh-140774: Fix clearing the read-only file attribute on Windows by yhay81 · Pull Request #154852 · python/cpython
Conversation
yhay81
commented
Jul 29, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes #140774.
On Windows, SetFileInformationByHandle() leaves attributes unchanged when FILE_BASIC_INFO.FileAttributes is zero. Thus clearing the sole read-only attribute silently failed through os.chmod(..., follow_symlinks=True), os.fchmod(), and pathlib.Path.chmod(). Use FILE_ATTRIBUTE_NORMAL in that case.
Regression tests cover both handle-based paths. test_os and test_pathlib pass.
(AI-assisted; reproduced and verified locally on Windows 11.)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Co-Authored-By: Claude Fable 5 <[email protected]>
|
This PR is stale because it has been open for 90 days with no activity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Merged latest main to refresh CI and expanded Win32ChmodTests to explicitly type SetFileAttributesW and cover os.chmod(fd, ...) and Path.chmod() as well.
cc @serhiy-storchaka @zooba (as author/reviewer of win32_hchmod() in GH-113049 / GH-113192) whenever you have a chance to take a look — check_chmod in test_posix didn't hit this because newly created NTFS files have FILE_ATTRIBUTE_ARCHIVE set, so info.FileAttributes only becomes 0 (which SetFileInformationByHandle / FILE_BASIC_INFO treats as "leave unchanged") when READONLY was the sole attribute.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your report and fix. The tests look excessive for such simple fix, but I do not see a way to make them simpler.
LGTM. 👍
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
finally:
|
|
|
os.close(fd)
|
|
|
|
|
|
def test_pathlib_chmod_clears_readonly_without_other_attributes(self):
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is redundant. Path.chmod() uses os.chmod().
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point — removed in 362a02a. Thank you for the review!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|