[Python-checkins] cpython (3.4): Issue #20639: calling Path.with_suffix('') allows removing the suffix again.

antoine.pitrou python-checkins at python.org
Mon Jul 7 03:38:49 CEST 2014


http://hg.python.org/cpython/rev/0d84855861ff
changeset:   91565:0d84855861ff
branch:      3.4
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Jul 06 21:37:15 2014 -0400
summary:
  Issue #20639: calling Path.with_suffix('') allows removing the suffix again.
Patch by July Tikhonov.

files:
  Lib/pathlib.py           |  7 +++----
  Lib/test/test_pathlib.py |  6 ++++++
  Misc/NEWS                |  3 +++
  3 files changed, 12 insertions(+), 4 deletions(-)


diff --git a/Lib/pathlib.py b/Lib/pathlib.py
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -759,11 +759,10 @@
     def with_suffix(self, suffix):
         """Return a new path with the file suffix changed (or added, if none)."""
         # XXX if suffix is None, should the current suffix be removed?
-        drv, root, parts = self._flavour.parse_parts((suffix,))
-        if drv or root or len(parts) != 1:
+        f = self._flavour
+        if f.sep in suffix or f.altsep and f.altsep in suffix:
             raise ValueError("Invalid suffix %r" % (suffix))
-        suffix = parts[0]
-        if not suffix.startswith('.'):
+        if suffix and not suffix.startswith('.') or suffix == '.':
             raise ValueError("Invalid suffix %r" % (suffix))
         name = self.name
         if not name:
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -551,6 +551,9 @@
         self.assertEqual(P('/a/b').with_suffix('.gz'), P('/a/b.gz'))
         self.assertEqual(P('a/b.py').with_suffix('.gz'), P('a/b.gz'))
         self.assertEqual(P('/a/b.py').with_suffix('.gz'), P('/a/b.gz'))
+        # Stripping suffix
+        self.assertEqual(P('a/b.py').with_suffix(''), P('a/b'))
+        self.assertEqual(P('/a/b').with_suffix(''), P('/a/b'))
         # Path doesn't have a "filename" component
         self.assertRaises(ValueError, P('').with_suffix, '.gz')
         self.assertRaises(ValueError, P('.').with_suffix, '.gz')
@@ -558,9 +561,12 @@
         # Invalid suffix
         self.assertRaises(ValueError, P('a/b').with_suffix, 'gz')
         self.assertRaises(ValueError, P('a/b').with_suffix, '/')
+        self.assertRaises(ValueError, P('a/b').with_suffix, '.')
         self.assertRaises(ValueError, P('a/b').with_suffix, '/.gz')
         self.assertRaises(ValueError, P('a/b').with_suffix, 'c/d')
         self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d')
+        self.assertRaises(ValueError, P('a/b').with_suffix, './.d')
+        self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.')
 
     def test_relative_to_common(self):
         P = self.cls
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
 Library
 -------
 
+- Issue #20639: calling Path.with_suffix('') allows removing the suffix
+  again.  Patch by July Tikhonov.
+
 - Issue #21714: Disallow the construction of invalid paths using
   Path.with_name().  Original patch by Antony Lee.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list