diff options
author | Barney Gale <barney.gale@gmail.com> | 2024-12-06 18:10:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-06 18:10:00 (GMT) |
commit | 5b6635f772d187d6049a56bfea76855644cd4ca1 (patch) | |
tree | aa852396d04ad18c9922bbc66a8584478eeca5f4 /Lib | |
parent | e59caf67cdb8dae26470f00599ea8dbb00968a73 (diff) | |
download | cpython-5b6635f772d187d6049a56bfea76855644cd4ca1.zip cpython-5b6635f772d187d6049a56bfea76855644cd4ca1.tar.gz cpython-5b6635f772d187d6049a56bfea76855644cd4ca1.tar.bz2 |
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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pathlib/_abc.py | 37 | ||||
-rw-r--r-- | Lib/pathlib/_local.py | 17 | ||||
-rw-r--r-- | Lib/test/test_pathlib/test_pathlib_abc.py | 2 |
3 files changed, 18 insertions, 38 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 86617ff..11a11ec 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -14,7 +14,7 @@ resemble pathlib's PurePath and Path respectively. import functools import operator import posixpath -from errno import EINVAL, EXDEV +from errno import EINVAL from glob import _GlobberBase, _no_recurse_symlinks from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO from pathlib._os import copyfileobj @@ -902,45 +902,10 @@ class PathBase(PurePathBase): dirs_exist_ok=dirs_exist_ok, preserve_metadata=preserve_metadata) - def rename(self, target): - """ - Rename this path to the target path. - - The target path may be absolute or relative. Relative paths are - interpreted relative to the current working directory, *not* the - directory of the Path object. - - Returns the new Path instance pointing to the target path. - """ - raise UnsupportedOperation(self._unsupported_msg('rename()')) - - def replace(self, target): - """ - Rename this path to the target path, overwriting if that path exists. - - The target path may be absolute or relative. Relative paths are - interpreted relative to the current working directory, *not* the - directory of the Path object. - - Returns the new Path instance pointing to the target path. - """ - raise UnsupportedOperation(self._unsupported_msg('replace()')) - def move(self, target): """ Recursively move this file or directory tree to the given destination. """ - self._ensure_different_file(target) - try: - return self.replace(target) - except UnsupportedOperation: - pass - except TypeError: - if not isinstance(target, PathBase): - raise - except OSError as err: - if err.errno != EXDEV: - raise target = self.copy(target, follow_symlinks=False, preserve_metadata=True) self._delete() return target diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index bb8a252..250bc12 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -4,6 +4,7 @@ import operator import os import posixpath import sys +from errno import EXDEV from glob import _StringGlobber from itertools import chain from _collections_abc import Sequence @@ -876,6 +877,22 @@ class Path(PathBase, PurePath): os.replace(self, target) return self.with_segments(target) + def move(self, target): + """ + Recursively move this file or directory tree to the given destination. + """ + self._ensure_different_file(target) + try: + return self.replace(target) + except TypeError: + if not isinstance(target, PathBase): + raise + except OSError as err: + if err.errno != EXDEV: + raise + # Fall back to copy+delete. + return PathBase.move(self, target) + if hasattr(os, "symlink"): def symlink_to(self, target, target_is_directory=False): """ diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 7ba3fa8..00153e3 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -1376,8 +1376,6 @@ class PathBaseTest(PurePathBaseTest): self.assertRaises(e, p.hardlink_to, 'foo') self.assertRaises(e, p.mkdir) self.assertRaises(e, p.touch) - self.assertRaises(e, p.rename, 'foo') - self.assertRaises(e, p.replace, 'foo') self.assertRaises(e, p.chmod, 0o755) self.assertRaises(e, p.lchmod, 0o755) self.assertRaises(e, p.unlink) |