https://github.com/python/cpython/commit/e3dedeae7abbeda0cb3f1d872ebbb914635d64f2
commit: e3dedeae7abbeda0cb3f1d872ebbb914635d64f2
branch: main
author: Barney Gale <[email protected]>
committer: barneygale <[email protected]>
date: 2024-02-24T19:37:03Z
summary:

GH-114610: Fix `pathlib.PurePath.with_stem('')` handling of file extensions 
(#114612)

Raise `ValueError` if `with_stem('')` is called on a path with a file
extension. Paths may only have an empty stem if they also have an empty
suffix.

files:
A Misc/NEWS.d/next/Library/2024-01-26-16-42-31.gh-issue-114610.S18Vuz.rst
M Lib/pathlib/_abc.py
M Lib/test/test_pathlib/test_pathlib_abc.py

diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 27c6b4e367a050..44fea525b6cac7 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -313,7 +313,14 @@ def with_name(self, name):
 
     def with_stem(self, stem):
         """Return a new path with the stem changed."""
-        return self.with_name(stem + self.suffix)
+        suffix = self.suffix
+        if not suffix:
+            return self.with_name(stem)
+        elif not stem:
+            # If the suffix is non-empty, we can't make the stem empty.
+            raise ValueError(f"{self!r} has a non-empty suffix")
+        else:
+            return self.with_name(stem + suffix)
 
     def with_suffix(self, suffix):
         """Return a new path with the file suffix changed.  If the path
@@ -324,6 +331,7 @@ def with_suffix(self, suffix):
         if not suffix:
             return self.with_name(stem)
         elif not stem:
+            # If the stem is empty, we can't make the suffix non-empty.
             raise ValueError(f"{self!r} has an empty name")
         elif suffix.startswith('.') and len(suffix) > 1:
             return self.with_name(stem + suffix)
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py 
b/Lib/test/test_pathlib/test_pathlib_abc.py
index 1d30deca8f7a1b..5bfb76f85c7909 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -957,6 +957,8 @@ def test_with_stem_empty(self):
         self.assertEqual(P('/').with_stem('d'), P('/d'))
         self.assertEqual(P('a/b').with_stem(''), P('a/'))
         self.assertEqual(P('a/b').with_stem('.'), P('a/.'))
+        self.assertRaises(ValueError, P('foo.gz').with_stem, '')
+        self.assertRaises(ValueError, P('/a/b/foo.gz').with_stem, '')
 
     def test_with_stem_seps(self):
         P = self.cls
diff --git 
a/Misc/NEWS.d/next/Library/2024-01-26-16-42-31.gh-issue-114610.S18Vuz.rst 
b/Misc/NEWS.d/next/Library/2024-01-26-16-42-31.gh-issue-114610.S18Vuz.rst
new file mode 100644
index 00000000000000..519aede72aaf29
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-26-16-42-31.gh-issue-114610.S18Vuz.rst
@@ -0,0 +1,4 @@
+Fix bug where :meth:`pathlib.PurePath.with_stem` converted a non-empty path
+suffix to a stem when given an empty *stem* argument. It now raises
+:exc:`ValueError`, just like :meth:`pathlib.PurePath.with_suffix` does when
+called on a path with an empty stem, given a non-empty *suffix* argument.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to