From be48d2abd822c40f16fc36f4e35cade6f971dcfe Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 22 Jul 2024 04:02:34 +0100 Subject: [PATCH] Cunningly avoid specifying the problematic cases. --- Doc/library/pathlib.rst | 4 ++++ Lib/pathlib/_abc.py | 2 +- Lib/pathlib/_local.py | 8 ++------ Lib/test/test_pathlib/test_pathlib.py | 12 ------------ Lib/test/test_pathlib/test_pathlib_abc.py | 20 -------------------- 5 files changed, 7 insertions(+), 39 deletions(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 244c1d1b5ef92c..8720db3bf74341 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -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. diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 9c073af2a5fb62..b83d616f097284 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -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. """ diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index 2003f7c5cadad2..fc656927ed0eb1 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -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): """ diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 3d994cf8e56084..a4c6032dac0e52 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -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() diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index e139d4e8590a15..d3a3fa9d61de4a 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -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' @@ -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'