https://github.com/python/cpython/commit/809eed48058ea7391df57ead09dff53bcc5d81e9
commit: 809eed48058ea7391df57ead09dff53bcc5d81e9
branch: main
author: Barney Gale <[email protected]>
committer: barneygale <[email protected]>
date: 2024-01-30T14:25:16Z
summary:

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.

files:
M Lib/pathlib/_abc.py
M Lib/test/test_pathlib/test_pathlib.py
M Lib/test/test_pathlib/test_pathlib_abc.py

diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index ad5684829ebc80..580d631cbf3b53 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -299,10 +299,13 @@ def with_suffix(self, suffix):
         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}")
 
diff --git a/Lib/test/test_pathlib/test_pathlib.py 
b/Lib/test/test_pathlib/test_pathlib.py
index 5ce3b605c58e63..a8cc30ef0ab63f 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -327,13 +327,6 @@ def test_with_stem_empty(self):
         self.assertRaises(ValueError, P('a/b').with_stem, '')
         self.assertRaises(ValueError, P('a/b').with_stem, '.')
 
-    def test_with_suffix_empty(self):
-        # Path doesn't have a "filename" component.
-        P = self.cls
-        self.assertRaises(ValueError, P('').with_suffix, '.gz')
-        self.assertRaises(ValueError, P('.').with_suffix, '.gz')
-        self.assertRaises(ValueError, P('/').with_suffix, '.gz')
-
     def test_relative_to_several_args(self):
         P = self.cls
         p = P('a/b')
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py 
b/Lib/test/test_pathlib/test_pathlib_abc.py
index ab989cb5503f99..18a612f6b81bac 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -977,9 +977,8 @@ def test_with_suffix_windows(self):
     def test_with_suffix_empty(self):
         P = self.cls
         # Path doesn't have a "filename" component.
-        self.assertEqual(P('').with_suffix('.gz'), P('.gz'))
-        self.assertEqual(P('.').with_suffix('.gz'), P('..gz'))
-        self.assertEqual(P('/').with_suffix('.gz'), P('/.gz'))
+        self.assertRaises(ValueError, P('').with_suffix, '.gz')
+        self.assertRaises(ValueError, P('/').with_suffix, '.gz')
 
     def test_with_suffix_seps(self):
         P = self.cls

_______________________________________________
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