diff options
author | Barney Gale <barney.gale@gmail.com> | 2024-12-08 18:45:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-08 18:45:09 (GMT) |
commit | 7f8ec523021427a5c1ab3ce0cdd6e4bb909f1dc5 (patch) | |
tree | 7939eb62c8eb50421ab9024e0d9b27c0e04bf90c /Lib/pathlib | |
parent | 2367759212f609b8ddf3218003b3ccd8e72849ae (diff) | |
download | cpython-7f8ec523021427a5c1ab3ce0cdd6e4bb909f1dc5.zip cpython-7f8ec523021427a5c1ab3ce0cdd6e4bb909f1dc5.tar.gz cpython-7f8ec523021427a5c1ab3ce0cdd6e4bb909f1dc5.tar.bz2 |
GH-127381: pathlib ABCs: remove `PathBase.unlink()` and `rmdir()` (#127736)
Virtual filesystems don't always make a distinction between deleting files
and empty directories, and sometimes support deleting non-empty directories
in a single operation. Here we remove `PathBase.unlink()` and `rmdir()`,
leaving `_delete()` as the sole deletion method, now made abstract. I hope
to drop the underscore prefix later on.
Diffstat (limited to 'Lib/pathlib')
-rw-r--r-- | Lib/pathlib/_abc.py | 43 | ||||
-rw-r--r-- | Lib/pathlib/_local.py | 16 |
2 files changed, 18 insertions, 41 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 820970f..309eab2 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -840,6 +840,12 @@ class PathBase(PurePathBase): dirs_exist_ok=dirs_exist_ok, preserve_metadata=preserve_metadata) + def _delete(self): + """ + Delete this file or directory (including all sub-directories). + """ + raise UnsupportedOperation(self._unsupported_msg('_delete()')) + def move(self, target): """ Recursively move this file or directory tree to the given destination. @@ -874,43 +880,6 @@ class PathBase(PurePathBase): """ self.chmod(mode, follow_symlinks=False) - def unlink(self, missing_ok=False): - """ - Remove this file or link. - If the path is a directory, use rmdir() instead. - """ - raise UnsupportedOperation(self._unsupported_msg('unlink()')) - - def rmdir(self): - """ - Remove this directory. The directory must be empty. - """ - raise UnsupportedOperation(self._unsupported_msg('rmdir()')) - - def _delete(self): - """ - Delete this file or directory (including all sub-directories). - """ - if self.is_symlink() or self.is_junction(): - self.unlink() - elif self.is_dir(): - self._rmtree() - else: - self.unlink() - - def _rmtree(self): - def on_error(err): - raise err - results = self.walk( - on_error=on_error, - top_down=False, # So we rmdir() empty directories. - follow_symlinks=False) - for dirpath, _, filenames in results: - for filename in filenames: - filepath = dirpath / filename - filepath.unlink() - dirpath.rmdir() - def owner(self, *, follow_symlinks=True): """ Return the login name of the file owner. diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index 250bc12..f87069c 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -846,10 +846,18 @@ class Path(PathBase, PurePath): """ os.rmdir(self) - def _rmtree(self): - # Lazy import to improve module import time - import shutil - shutil.rmtree(self) + def _delete(self): + """ + Delete this file or directory (including all sub-directories). + """ + if self.is_symlink() or self.is_junction(): + self.unlink() + elif self.is_dir(): + # Lazy import to improve module import time + import shutil + shutil.rmtree(self) + else: + self.unlink() def rename(self, target): """ |