Skip to content

Commit

Permalink
Cunningly avoid specifying the problematic cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Jul 22, 2024
1 parent 04b115a commit be48d2a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 39 deletions.
4 changes: 4 additions & 0 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,10 @@ Copying, moving and deleting
Recursively move this file or directory tree to the given *target*, and
return a new :class:`!Path` instance pointing to *target*.

If the *target* doesn't exist it will be created. If both this path and the
*target* are existing files, then the target is overwritten. If the
*target* is a non-empty directory, :exc:`OSError` is raised.

If both paths are on the same filesystem, the move is performed with
:func:`os.replace`. Otherwise, this path is copied (preserving metadata and
symlinks) and then deleted.
Expand Down
2 changes: 1 addition & 1 deletion Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ def unlink(self, missing_ok=False):
"""
raise UnsupportedOperation(self._unsupported_msg('unlink()'))

def rmdir(self, missing_ok=False):
def rmdir(self):
"""
Remove this directory. The directory must be empty.
"""
Expand Down
8 changes: 2 additions & 6 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,15 +825,11 @@ def unlink(self, missing_ok=False):
if not missing_ok:
raise

def rmdir(self, missing_ok=False):
def rmdir(self):
"""
Remove this directory. The directory must be empty.
"""
try:
os.rmdir(self)
except FileNotFoundError:
if not missing_ok:
raise
os.rmdir(self)

def rmtree(self, ignore_errors=False, on_error=None):
"""
Expand Down
12 changes: 0 additions & 12 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,26 +776,14 @@ def test_move_file_to_file_other_fs(self):
def test_move_file_to_dir_other_fs(self):
self.test_move_file_to_dir()

@patch_replace
def test_move_file_to_empty_dir_other_fs(self):
self.test_move_file_to_empty_dir()

@patch_replace
def test_move_dir_other_fs(self):
self.test_move_dir()

@patch_replace
def test_move_dir_to_file_other_fs(self):
self.test_move_dir_to_file()

@patch_replace
def test_move_dir_to_dir_other_fs(self):
self.test_move_dir_to_dir()

@patch_replace
def test_move_dir_to_empty_dir_other_fs(self):
self.test_move_dir_to_empty_dir()

@patch_replace
def test_move_dir_into_itself_other_fs(self):
self.test_move_dir_into_itself()
Expand Down
20 changes: 0 additions & 20 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2011,13 +2011,6 @@ def test_move_file_to_dir(self):
target = base / 'dirB'
self.assertRaises(OSError, source.move, target)

def test_move_file_to_empty_dir(self):
base = self.cls(self.base)
source = base / 'fileA'
target = base / 'fileA_moved'
target.mkdir()
self.assertRaises(OSError, source.move, target)

def test_move_dir(self):
base = self.cls(self.base)
source = base / 'dirC'
Expand All @@ -2034,25 +2027,12 @@ def test_move_dir(self):
self.assertTrue(target.joinpath('fileC').read_text(),
"this is file C\n")

def test_move_dir_to_file(self):
base = self.cls(self.base)
source = base / 'dirB'
target = base / 'fileA'
self.assertRaises(OSError, source.move, target)

def test_move_dir_to_dir(self):
base = self.cls(self.base)
source = base / 'dirC'
target = base / 'dirB'
self.assertRaises(OSError, source.move, target)

def test_move_dir_to_empty_dir(self):
base = self.cls(self.base)
source = base / 'dirC'
target = base / 'dirC_moved'
target.mkdir()
self.assertRaises(OSError, source.move, target)

def test_move_dir_into_itself(self):
base = self.cls(self.base)
source = base / 'dirC'
Expand Down

0 comments on commit be48d2a

Please sign in to comment.