From 7084e736db318ba83a879e2e93e7c76264e48dbf Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sun, 6 Jul 2014 21:31:12 -0400 Subject: Issue #21714: Disallow the construction of invalid paths using Path.with_name(). Original patch by Antony Lee. --- Lib/pathlib.py | 4 ++++ Lib/test/test_pathlib.py | 8 ++++++++ Misc/NEWS | 3 +++ 3 files changed, 15 insertions(+) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index d3d1af8..c1ec07a 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -749,6 +749,10 @@ class PurePath(object): """Return a new path with the file name changed.""" if not self.name: raise ValueError("%r has an empty name" % (self,)) + drv, root, parts = self._flavour.parse_parts((name,)) + if (not name or name[-1] in [self._flavour.sep, self._flavour.altsep] + or drv or root or len(parts) != 1): + raise ValueError("Invalid name %r" % (name)) return self._from_parsed_parts(self._drv, self._root, self._parts[:-1] + [name]) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 6378d8c..2c3fce7 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -540,6 +540,10 @@ class _BasePurePathTest(object): self.assertRaises(ValueError, P('').with_name, 'd.xml') self.assertRaises(ValueError, P('.').with_name, 'd.xml') self.assertRaises(ValueError, P('/').with_name, 'd.xml') + self.assertRaises(ValueError, P('a/b').with_name, '') + self.assertRaises(ValueError, P('a/b').with_name, '/c') + self.assertRaises(ValueError, P('a/b').with_name, 'c/') + self.assertRaises(ValueError, P('a/b').with_name, 'c/d') def test_with_suffix_common(self): P = self.cls @@ -950,6 +954,10 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase): self.assertRaises(ValueError, P('c:').with_name, 'd.xml') self.assertRaises(ValueError, P('c:/').with_name, 'd.xml') self.assertRaises(ValueError, P('//My/Share').with_name, 'd.xml') + self.assertRaises(ValueError, P('c:a/b').with_name, 'd:') + self.assertRaises(ValueError, P('c:a/b').with_name, 'd:e') + self.assertRaises(ValueError, P('c:a/b').with_name, 'd:/e') + self.assertRaises(ValueError, P('c:a/b').with_name, '//My/Share') def test_with_suffix(self): P = self.cls diff --git a/Misc/NEWS b/Misc/NEWS index 97ffbfc..a8e6fd4 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -27,6 +27,9 @@ Core and Builtins Library ------- +- Issue #21714: Disallow the construction of invalid paths using + Path.with_name(). Original patch by Antony Lee. + - Issue #21897: Fix a crash with the f_locals attribute with closure variables when frame.clear() has been called. -- cgit v0.12 From e50dafcd636ba32db890600164698bb070d40d97 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sun, 6 Jul 2014 21:37:15 -0400 Subject: Issue #20639: calling Path.with_suffix('') allows removing the suffix again. Patch by July Tikhonov. --- 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 index c1ec07a..48b7031 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -759,11 +759,10 @@ class PurePath(object): 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 index 2c3fce7..da001f0 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -551,6 +551,9 @@ class _BasePurePathTest(object): 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 @@ class _BasePurePathTest(object): # 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 index a8e6fd4..37d16eb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -27,6 +27,9 @@ Core and Builtins 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. -- cgit v0.12