bpo-31163: Added return values to pathlib.Path instance's rename and … · python/cpython@088a09a (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -946,23 +946,32 @@ call fails (for example because the path doesn't exist).
946 946
947 947 .. method:: Path.rename(target)
948 948
949 - Rename this file or directory to the given *target*. On Unix, if
950 - *target* exists and is a file, it will be replaced silently if the user
951 - has permission. *target* can be either a string or another path object::
949 + Rename this file or directory to the given *target*, and return a new Path
950 + instance pointing to *target*. On Unix, if *target* exists and is a file,
951 + it will be replaced silently if the user has permission. *target* can be
952 + either a string or another path object::
952 953
953 954 >>> p = Path('foo')
954 955 >>> p.open('w').write('some text')
955 956 9
956 957 >>> target = Path('bar')
957 958 >>> p.rename(target)
959 + PosixPath('bar')
958 960 >>> target.open().read()
959 961 'some text'
960 962
963 + .. versionchanged:: 3.8
964 + Added return value, return the new Path instance.
965 +
961 966
962 967 .. method:: Path.replace(target)
963 968
964 - Rename this file or directory to the given *target*. If *target* points
965 - to an existing file or directory, it will be unconditionally replaced.
969 + Rename this file or directory to the given *target*, and return a new Path
970 + instance pointing to *target*. If *target* points to an existing file or
971 + directory, it will be unconditionally replaced.
972 +
973 + .. versionchanged:: 3.8
974 + Added return value, return the new Path instance.
966 975
967 976
968 977 .. method:: Path.resolve(strict=False)
Original file line number Diff line number Diff line change
@@ -1341,20 +1341,24 @@ def link_to(self, target):
1341 1341
1342 1342 def rename(self, target):
1343 1343 """
1344 - Rename this path to the given path.
1344 + Rename this path to the given path,
1345 + and return a new Path instance pointing to the given path.
1345 1346 """
1346 1347 if self._closed:
1347 1348 self._raise_closed()
1348 1349 self._accessor.rename(self, target)
1350 +return self.__class__(target)
1349 1351
1350 1352 def replace(self, target):
1351 1353 """
1352 1354 Rename this path to the given path, clobbering the existing
1353 - destination if it exists.
1355 + destination if it exists, and return a new Path instance
1356 + pointing to the given path.
1354 1357 """
1355 1358 if self._closed:
1356 1359 self._raise_closed()
1357 1360 self._accessor.replace(self, target)
1361 +return self.__class__(target)
1358 1362
1359 1363 def symlink_to(self, target, target_is_directory=False):
1360 1364 """
Original file line number Diff line number Diff line change
@@ -1767,12 +1767,14 @@ def test_rename(self):
1767 1767 size = p.stat().st_size
1768 1768 # Renaming to another path.
1769 1769 q = P / 'dirA' / 'fileAA'
1770 -p.rename(q)
1770 +renamed_p = p.rename(q)
1771 +self.assertEqual(renamed_p, q)
1771 1772 self.assertEqual(q.stat().st_size, size)
1772 1773 self.assertFileNotFound(p.stat)
1773 1774 # Renaming to a str of a relative path.
1774 1775 r = rel_join('fileAAA')
1775 -q.rename(r)
1776 +renamed_q = q.rename(r)
1777 +self.assertEqual(renamed_q, self.cls(r))
1776 1778 self.assertEqual(os.stat(r).st_size, size)
1777 1779 self.assertFileNotFound(q.stat)
1778 1780
@@ -1782,12 +1784,14 @@ def test_replace(self):
1782 1784 size = p.stat().st_size
1783 1785 # Replacing a non-existing path.
1784 1786 q = P / 'dirA' / 'fileAA'
1785 -p.replace(q)
1787 +replaced_p = p.replace(q)
1788 +self.assertEqual(replaced_p, q)
1786 1789 self.assertEqual(q.stat().st_size, size)
1787 1790 self.assertFileNotFound(p.stat)
1788 1791 # Replacing another (existing) path.
1789 1792 r = rel_join('dirB', 'fileB')
1790 -q.replace(r)
1793 +replaced_q = q.replace(r)
1794 +self.assertEqual(replaced_q, self.cls(r))
1791 1795 self.assertEqual(os.stat(r).st_size, size)
1792 1796 self.assertFileNotFound(q.stat)
1793 1797
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +pathlib.Path instance's rename and replace methods now return the new Path
2 +instance.