summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-07-07 01:31:12 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-07-07 01:31:12 (GMT)
commit7084e736db318ba83a879e2e93e7c76264e48dbf (patch)
tree9cbd1206a5f55d5d23a50ff7b481db36b5a7e01c /Lib
parent7bc5fb6916b59e9d6e491c8519e5affa98b8aa6c (diff)
downloadcpython-7084e736db318ba83a879e2e93e7c76264e48dbf.zip
cpython-7084e736db318ba83a879e2e93e7c76264e48dbf.tar.gz
cpython-7084e736db318ba83a879e2e93e7c76264e48dbf.tar.bz2
Issue #21714: Disallow the construction of invalid paths using Path.with_name(). Original patch by Antony Lee.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pathlib.py4
-rw-r--r--Lib/test/test_pathlib.py8
2 files changed, 12 insertions, 0 deletions
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