Skip to content

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

Open
lpyu001 wants to merge 3 commits into
python:mainfrom
lpyu001:fix-pathlib-bug
Open

lpyu001 wants to merge 3 commits 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

@aisk aisk added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 20, 2026
@aisk

aisk commented Aug 20, 2026

Copy link
Copy Markdown
Member

This file in 3.13 is in another location (Lib/pathlib/_local.py), and 3.13 will be end of support in just a month, so I think it's ok to not backport it?

@lpyu001 lpyu001 closed this Sep 23, 2026
@lpyu001
lpyu001 deleted the fix-pathlib-bug branch September 23, 2026 02:54
@lpyu001
lpyu001 restored the fix-pathlib-bug branch September 23, 2026 03:02
@lpyu001 lpyu001 reopened this Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants