-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Path.with_name can construct invalid paths #65913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Path.with_name can be used to construct paths containing slashes as name contents (rather than as separators), as demonstrated below. $ python -c 'from pathlib import Path; print(Path("foo").with_name("bar/baz").name)'
bar/baz This should be changed to either raise a ValueError, or behave like path.parent / new_name. Given the amount of checking in the related with_suffix method (and the fact that the second option can be readily implemented by hand), the first option seems preferable. |
I think raising ValueError is the way to go. Would you like to try writing a patch for it? |
I have the patch almost ready, but ran into another issue: should "path.with_name('foo/')" be allowed? It may make sense to treat it like "path.with_name('foo')", just like 'Path("foo/") == Path("foo")'. The implementation is also simpler with it, as it can reuse the "parse_parts" approach used by "with_suffix". But this also raises a separate issue, which is that with the current implementation, we have "Path('foo').with_suffix('.bar') == Path('foo').with_suffix('.bar/')", and that is less reasonable. Should I open a separate issue for that? |
For sanity, I think path separators should be disallowed. |
The attached patch fixes all the issues mentioned, and also integrates the fixes of bpo-20639 (issues with with_suffix) as they are quite similar. |
New changeset c2636b5816a3 by Antoine Pitrou in branch '3.4': |
Ok, patch now applied (as well as the original patch for with_suffix()). Thank you very much! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: