This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Path.with_name can construct invalid paths
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, pitrou, python-dev
Priority: normal Keywords: patch

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

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)'

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.
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!
Date User Action Args
2022-04-11 14:58:04adminsetgithub: 65913
2014-07-07 01:40:30pitrousetstatus: open -> closed
resolution: fixed
messages: + msg222431

stage: needs patch -> resolved
2014-07-07 01:38:48python-devsetnosy: + python-dev
messages: + msg222429
2014-06-23 00:46:33Antony.Leesetfiles: + pathlib-with_name-with_suffix.patch
keywords: + patch
messages: + msg221329
2014-06-22 15:51:08pitrousetmessages: + msg221263
2014-06-12 01:42:23Antony.Leesetmessages: + msg220327
2014-06-11 13:38:29pitrousetnosy: + pitrou
messages: + msg220269

type: behavior
stage: needs patch
2014-06-11 05:39:26Antony.Leecreate