Skip to content

Commit

Permalink
Fix junction deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Dec 6, 2024
1 parent 1e09d70 commit bc438b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
26 changes: 12 additions & 14 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,23 +862,21 @@ def _delete(self):
Delete this file or directory (including all sub-directories).
"""
if self.is_dir(follow_symlinks=False):
self._rmtree()
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()
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.
Expand Down
16 changes: 12 additions & 4 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,10 +912,18 @@ def rmdir(self):
"""
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):
"""
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2854,7 +2854,7 @@ def setUp(self):

def tearDown(self):
base = self.cls(self.base)
base._rmtree()
base._delete()

def test_walk_topdown(self):
walker = self.walk_path.walk()
Expand Down

0 comments on commit bc438b0

Please sign in to comment.