Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,9 @@ call fails (for example because the path doesn't exist):
*target* exists and is a file, it will be replaced silently if the user
has permission. *target* can be either a string or another path object::

If the provided *target* is a path object, it is the method's return value.
Otherwise (if it is a string), new path object is created from it and returned.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example below needs to be updated now that p.rename(target) is returning a value (double-check this in a terminal):

>>> p = Path('foo')
>>> p.open('w').write('some text')
9
>>> target = Path('bar')
>>> p.rename(target)
Path('bar')
>>> target.open().read()
'some text'

>>> p = Path('foo')
>>> p.open('w').write('some text')
9
Expand All @@ -936,6 +939,8 @@ call fails (for example because the path doesn't exist):
Rename this file or directory to the given *target*. If *target* points
to an existing file or directory, it will be unconditionally replaced.

If the provided *target* is a path object, it is the method's return value.
Otherwise (if it is a string), new path object is created from it and returned.

.. method:: Path.resolve(strict=False)

Expand Down
14 changes: 14 additions & 0 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,19 +1265,33 @@ def lstat(self):
def rename(self, target):
"""
Rename this path to the given path.

If target is an instance of Path returns it, otherwise creates
new instance from target (string) and returns it.
"""
if self._closed:
self._raise_closed()
self._accessor.rename(self, target)
if isinstance(target, str):
target = self.__class__(target)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be safer to call self.__class__ unconditionally (i.e. if a new type of object besides PurePath or str is added to pathlib in the future). If we want to support original object return in pathlib, that's really something that should be added to PurePath._from_parts.

if self._closed:
    self._raise_closed()
self._accessor.rename(self, target)
return self.__class__(target)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was considering this approach as well, just wanted to avoid having two objects representing the same thing.
I have no strong preferences for either approach


return target

def replace(self, target):
"""
Rename this path to the given path, clobbering the existing
destination if it exists.

If target is an instance of Path returns it, otherwise creates
new instance from target (string) and returns it.
"""
if self._closed:
self._raise_closed()
self._accessor.replace(self, target)
if isinstance(target, str):
target = self.__class__(target)

return target

def symlink_to(self, target, target_is_directory=False):
"""
Expand Down
12 changes: 8 additions & 4 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,12 +1645,14 @@ def test_rename(self):
size = p.stat().st_size
# Renaming to another path
q = P / 'dirA' / 'fileAA'
p.rename(q)
res = p.rename(q)
self.assertEqual(res, q)
self.assertEqual(q.stat().st_size, size)
self.assertFileNotFound(p.stat)
# Renaming to a str of a relative path
r = rel_join('fileAAA')
q.rename(r)
res = q.rename(r)
self.assertEqual(res, self.cls(r))
self.assertEqual(os.stat(r).st_size, size)
self.assertFileNotFound(q.stat)

Expand All @@ -1660,12 +1662,14 @@ def test_replace(self):
size = p.stat().st_size
# Replacing a non-existing path
q = P / 'dirA' / 'fileAA'
p.replace(q)
res = p.replace(q)
self.assertEqual(res, q)
self.assertEqual(q.stat().st_size, size)
self.assertFileNotFound(p.stat)
# Replacing another (existing) path
r = rel_join('dirB', 'fileB')
q.replace(r)
res = q.replace(r)
self.assertEqual(res, self.cls(r))
self.assertEqual(os.stat(r).st_size, size)
self.assertFileNotFound(q.stat)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pathlib.PurePath.rename and pathlib.PurePath.replace now have return values
Return value is either Path (input object) or string, based on input's type.
Patch by Janko Krstic.