Issue 21714: Path.with_name can construct invalid paths (original) (raw)

Created on 2014-06-11 05:39 by Antony.Lee, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pathlib-with_name-with_suffix.patch Antony.Lee,2014-06-23 00:46 review
Messages (7)
msg220235 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-06-11 05:39
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.
msg220269 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-11 13:38
I think raising ValueError is the way to go. Would you like to try writing a patch for it?
msg220327 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-06-12 01:42
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?
msg221263 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-22 15:51
> should "path.with_name('foo/')" be allowed? For sanity, I think path separators should be disallowed.
msg221329 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-06-23 00:46
The attached patch fixes all the issues mentioned, and also integrates the fixes of issue 20639 (issues with with_suffix) as they are quite similar.
msg222429 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-07-07 01:38
New changeset c2636b5816a3 by Antoine Pitrou in branch '3.4': Issue #21714: Disallow the construction of invalid paths using Path.with_name(). Original patch by Antony Lee. http://hg.python.org/cpython/rev/c2636b5816a3
msg222431 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-07-07 01:40
Ok, patch now applied (as well as the original patch for with_suffix()). Thank you very much!
History
Date User Action Args
2022-04-11 14:58:04 admin set github: 65913
2014-07-07 01:40:30 pitrou set status: open -> closedresolution: fixedmessages: + stage: needs patch -> resolved
2014-07-07 01:38:48 python-dev set nosy: + python-devmessages: +
2014-06-23 00:46:33 Antony.Lee set files: + pathlib-with_name-with_suffix.patchkeywords: + patchmessages: +
2014-06-22 15:51:08 pitrou set messages: +
2014-06-12 01:42:23 Antony.Lee set messages: +
2014-06-11 13:38:29 pitrou set nosy: + pitroumessages: + type: behaviorstage: needs patch
2014-06-11 05:39:26 Antony.Lee create