mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00
GH-114610: Fix pathlib._abc.PurePathBase.with_suffix('.ext')
handling of stems (#114613)
Raise `ValueError` if `with_suffix('.ext')` is called on a path without a stem. Paths may only have a non-empty suffix if they also have a non-empty stem. ABC-only bugfix; no effect on public classes.
This commit is contained in:
parent
e21754d7f8
commit
809eed4805
3 changed files with 7 additions and 12 deletions
|
@ -299,10 +299,13 @@ class PurePathBase:
|
|||
has no suffix, add given suffix. If the given suffix is an empty
|
||||
string, remove the suffix from the path.
|
||||
"""
|
||||
stem = self.stem
|
||||
if not suffix:
|
||||
return self.with_name(self.stem)
|
||||
return self.with_name(stem)
|
||||
elif not stem:
|
||||
raise ValueError(f"{self!r} has an empty name")
|
||||
elif suffix.startswith('.') and len(suffix) > 1:
|
||||
return self.with_name(self.stem + suffix)
|
||||
return self.with_name(stem + suffix)
|
||||
else:
|
||||
raise ValueError(f"Invalid suffix {suffix!r}")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue