summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pathlib.py4
-rw-r--r--Lib/test/test_pathlib.py31
2 files changed, 35 insertions, 0 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 88ebe03..f98d69e 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -870,6 +870,10 @@ class PurePath(object):
return self._from_parsed_parts(self._drv, self._root,
self._parts[:-1] + [name])
+ def with_stem(self, stem):
+ """Return a new path with the stem changed."""
+ return self.with_name(stem + self.suffix)
+
def with_suffix(self, suffix):
"""Return a new path with the file suffix changed. If the path
has no suffix, add given suffix. If the given suffix is an empty
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 8b19cff..1589282 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -559,6 +559,23 @@ class _BasePurePathTest(object):
self.assertRaises(ValueError, P('a/b').with_name, 'c/')
self.assertRaises(ValueError, P('a/b').with_name, 'c/d')
+ def test_with_stem_common(self):
+ P = self.cls
+ self.assertEqual(P('a/b').with_stem('d'), P('a/d'))
+ self.assertEqual(P('/a/b').with_stem('d'), P('/a/d'))
+ self.assertEqual(P('a/b.py').with_stem('d'), P('a/d.py'))
+ self.assertEqual(P('/a/b.py').with_stem('d'), P('/a/d.py'))
+ self.assertEqual(P('/a/b.tar.gz').with_stem('d'), P('/a/d.gz'))
+ self.assertEqual(P('a/Dot ending.').with_stem('d'), P('a/d'))
+ self.assertEqual(P('/a/Dot ending.').with_stem('d'), P('/a/d'))
+ self.assertRaises(ValueError, P('').with_stem, 'd')
+ self.assertRaises(ValueError, P('.').with_stem, 'd')
+ self.assertRaises(ValueError, P('/').with_stem, 'd')
+ self.assertRaises(ValueError, P('a/b').with_stem, '')
+ self.assertRaises(ValueError, P('a/b').with_stem, '/c')
+ self.assertRaises(ValueError, P('a/b').with_stem, 'c/')
+ self.assertRaises(ValueError, P('a/b').with_stem, 'c/d')
+
def test_with_suffix_common(self):
P = self.cls
self.assertEqual(P('a/b').with_suffix('.gz'), P('a/b.gz'))
@@ -1014,6 +1031,20 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
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_stem(self):
+ P = self.cls
+ self.assertEqual(P('c:a/b').with_stem('d'), P('c:a/d'))
+ self.assertEqual(P('c:/a/b').with_stem('d'), P('c:/a/d'))
+ self.assertEqual(P('c:a/Dot ending.').with_stem('d'), P('c:a/d'))
+ self.assertEqual(P('c:/a/Dot ending.').with_stem('d'), P('c:/a/d'))
+ self.assertRaises(ValueError, P('c:').with_stem, 'd')
+ self.assertRaises(ValueError, P('c:/').with_stem, 'd')
+ self.assertRaises(ValueError, P('//My/Share').with_stem, 'd')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:e')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:/e')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, '//My/Share')
+
def test_with_suffix(self):
P = self.cls
self.assertEqual(P('c:a/b').with_suffix('.gz'), P('c:a/b.gz'))