Skip to content

gh-156035: Validate pathlib rename and replace targets before filesystem changes - #156036

Open
lpyu001 wants to merge 1 commit into
python:mainfrom
lpyu001:fix-pathlib-bug
Open

gh-156035: Validate pathlib rename and replace targets before filesystem changes#156036
lpyu001 wants to merge 1 commit into
python:mainfrom
lpyu001:fix-pathlib-bug

Conversation

@lpyu001

@lpyu001 lpyu001 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Validate targets before modifying the filesystem so invalid bytes paths raise TypeError without moving the source.

…ilesystem changes

Validate targets before modifying the filesystem so invalid bytes paths raise
TypeError without moving the source.
@aisk

aisk commented Aug 19, 2026

Copy link
Copy Markdown
Member

For a target with a trailing slash like 'foo/bar/', the OS currently raises NotADirectoryError, but with this change the slash is stripped during normalization, so rename() silently succeeds and replace() may even silently overwrite an existing file.

@aisk

aisk commented Aug 19, 2026

Copy link
Copy Markdown
Member

Maybe we can use this:

def rename(self, target):
    if hasattr(target, 'with_segments'):
        result = target
    else:
        result = self.with_segments(target)
    os.rename(self, target)
    return result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants