Skip to content

Commit 5b6635f

Browse files
authored
GH-127381: pathlib ABCs: remove PathBase.rename() and replace() (#127658)
These methods are obviated by `PathBase.move()`, which can move directories and supports any `PathBase` object as a target.
1 parent e59caf6 commit 5b6635f

File tree

3 files changed

+18
-38
lines changed

3 files changed

+18
-38
lines changed

Lib/pathlib/_abc.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import functools
1515
import operator
1616
import posixpath
17-
from errno import EINVAL, EXDEV
17+
from errno import EINVAL
1818
from glob import _GlobberBase, _no_recurse_symlinks
1919
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
2020
from pathlib._os import copyfileobj
@@ -902,45 +902,10 @@ def copy_into(self, target_dir, *, follow_symlinks=True,
902902
dirs_exist_ok=dirs_exist_ok,
903903
preserve_metadata=preserve_metadata)
904904

905-
def rename(self, target):
906-
"""
907-
Rename this path to the target path.
908-
909-
The target path may be absolute or relative. Relative paths are
910-
interpreted relative to the current working directory, *not* the
911-
directory of the Path object.
912-
913-
Returns the new Path instance pointing to the target path.
914-
"""
915-
raise UnsupportedOperation(self._unsupported_msg('rename()'))
916-
917-
def replace(self, target):
918-
"""
919-
Rename this path to the target path, overwriting if that path exists.
920-
921-
The target path may be absolute or relative. Relative paths are
922-
interpreted relative to the current working directory, *not* the
923-
directory of the Path object.
924-
925-
Returns the new Path instance pointing to the target path.
926-
"""
927-
raise UnsupportedOperation(self._unsupported_msg('replace()'))
928-
929905
def move(self, target):
930906
"""
931907
Recursively move this file or directory tree to the given destination.
932908
"""
933-
self._ensure_different_file(target)
934-
try:
935-
return self.replace(target)
936-
except UnsupportedOperation:
937-
pass
938-
except TypeError:
939-
if not isinstance(target, PathBase):
940-
raise
941-
except OSError as err:
942-
if err.errno != EXDEV:
943-
raise
944909
target = self.copy(target, follow_symlinks=False, preserve_metadata=True)
945910
self._delete()
946911
return target

Lib/pathlib/_local.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import posixpath
66
import sys
7+
from errno import EXDEV
78
from glob import _StringGlobber
89
from itertools import chain
910
from _collections_abc import Sequence
@@ -876,6 +877,22 @@ def replace(self, target):
876877
os.replace(self, target)
877878
return self.with_segments(target)
878879

880+
def move(self, target):
881+
"""
882+
Recursively move this file or directory tree to the given destination.
883+
"""
884+
self._ensure_different_file(target)
885+
try:
886+
return self.replace(target)
887+
except TypeError:
888+
if not isinstance(target, PathBase):
889+
raise
890+
except OSError as err:
891+
if err.errno != EXDEV:
892+
raise
893+
# Fall back to copy+delete.
894+
return PathBase.move(self, target)
895+
879896
if hasattr(os, "symlink"):
880897
def symlink_to(self, target, target_is_directory=False):
881898
"""

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,8 +1376,6 @@ def test_unsupported_operation(self):
13761376
self.assertRaises(e, p.hardlink_to, 'foo')
13771377
self.assertRaises(e, p.mkdir)
13781378
self.assertRaises(e, p.touch)
1379-
self.assertRaises(e, p.rename, 'foo')
1380-
self.assertRaises(e, p.replace, 'foo')
13811379
self.assertRaises(e, p.chmod, 0o755)
13821380
self.assertRaises(e, p.lchmod, 0o755)
13831381
self.assertRaises(e, p.unlink)

0 commit comments

Comments
 (0)