changeset: 91564:c2636b5816a3 branch: 3.4 parent: 91562:78fa18e95445 user: Antoine Pitrou date: Sun Jul 06 21:31:12 2014 -0400 files: Lib/pathlib.py Lib/test/test_pathlib.py Misc/NEWS description: Issue #21714: Disallow the construction of invalid paths using Path.with_name(). Original patch by Antony Lee. diff -r 78fa18e95445 -r c2636b5816a3 Lib/pathlib.py --- a/Lib/pathlib.py Sun Jul 06 16:14:33 2014 -0700 +++ b/Lib/pathlib.py Sun Jul 06 21:31:12 2014 -0400 @@ -749,6 +749,10 @@ """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 -r 78fa18e95445 -r c2636b5816a3 Lib/test/test_pathlib.py --- a/Lib/test/test_pathlib.py Sun Jul 06 16:14:33 2014 -0700 +++ b/Lib/test/test_pathlib.py Sun Jul 06 21:31:12 2014 -0400 @@ -540,6 +540,10 @@ 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 @@ 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 -r 78fa18e95445 -r c2636b5816a3 Misc/NEWS --- a/Misc/NEWS Sun Jul 06 16:14:33 2014 -0700 +++ b/Misc/NEWS Sun Jul 06 21:31:12 2014 -0400 @@ -27,6 +27,9 @@ 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.