summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-01-30 14:25:16 (GMT)
committerGitHub <noreply@github.com>2024-01-30 14:25:16 (GMT)
commit809eed48058ea7391df57ead09dff53bcc5d81e9 (patch)
tree310860ec40ccf4fa8d5996e52907979a7a8163c9 /Lib/pathlib
parente21754d7f8336d4647e28f355d8a3dbd5a2c7545 (diff)
downloadcpython-809eed48058ea7391df57ead09dff53bcc5d81e9.zip
cpython-809eed48058ea7391df57ead09dff53bcc5d81e9.tar.gz
cpython-809eed48058ea7391df57ead09dff53bcc5d81e9.tar.bz2
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.
Diffstat (limited to 'Lib/pathlib')
-rw-r--r--Lib/pathlib/_abc.py7
1 files changed, 5 insertions, 2 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index ad56848..580d631 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -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}")