GH-73991: Make pathlib.Path.delete() private. (#123315)

Per feedback from Paul Moore on GH-123158, it's better to defer making
`Path.delete()` public than ship it with under-designed error handling
capabilities.

We leave a remnant `_delete()` method, which is used by `move()`. Any
functionality not needed by `move()` is deleted.
This commit is contained in:
Barney Gale 2024-08-26 16:26:34 +01:00 committed by GitHub
parent a1ddaaef58
commit 033d537cd4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 48 additions and 287 deletions

View file

@ -824,34 +824,7 @@ class Path(PathBase, PurePath):
"""
os.rmdir(self)
def delete(self, ignore_errors=False, on_error=None):
"""
Delete this file or directory (including all sub-directories).
If *ignore_errors* is true, exceptions raised from scanning the
filesystem and removing files and directories are ignored. Otherwise,
if *on_error* is set, it will be called to handle the error. If
neither *ignore_errors* nor *on_error* are set, exceptions are
propagated to the caller.
"""
if self.is_dir(follow_symlinks=False):
onexc = None
if on_error:
def onexc(func, filename, err):
err.filename = filename
on_error(err)
shutil.rmtree(str(self), ignore_errors, onexc=onexc)
else:
try:
self.unlink()
except OSError as err:
if not ignore_errors:
if on_error:
on_error(err)
else:
raise
delete.avoids_symlink_attacks = shutil.rmtree.avoids_symlink_attacks
_rmtree = shutil.rmtree
def rename(self, target):
"""