]> granicus.if.org Git - python/commitdiff
Issue #21714: Disallow the construction of invalid paths using Path.with_name()....
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 7 Jul 2014 01:31:12 +0000 (21:31 -0400)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 7 Jul 2014 01:31:12 +0000 (21:31 -0400)
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS

index d3d1af8decee8e8be6ad66797ecaad387521d797..c1ec07af8eaad0242434e4c153361289c8d8ceca 100644 (file)
@@ -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])
 
index 6378d8c7f9f24c8c79af584b012aacd893ba2132..2c3fce7628ee683da4885adb72762fecb5da652d 100644 (file)
@@ -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
index 97ffbfc01aa01567995f122e44bf02eccb4780d4..a8e6fd439513c4fc572bd8cb2741bb6ecb403ec6 100644 (file)
--- 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.