diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index ffec9c545ee11f..87cf85c2bb59b8 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -31,7 +31,7 @@ from pathlib._os import ( vfsopen, vfspath, - ensure_different_files, ensure_distinct_paths, + ensure_different_files, ensure_distinct_paths, is_case_only_rename, copyfile2, copyfileobj, ) @@ -1381,7 +1381,11 @@ def move(self, target): except TypeError: pass else: - ensure_different_files(self, target) + # On a case-insensitive filesystem, changing only the case of + # the name refers to the same file. Rename it anyway, as + # shutil.move() does. + if not is_case_only_rename(self, target): + ensure_different_files(self, target) try: os.replace(self, target) except OSError as err: diff --git a/Lib/pathlib/_os.py b/Lib/pathlib/_os.py index 79a1969d5f83d6..00cc39696d7a5c 100644 --- a/Lib/pathlib/_os.py +++ b/Lib/pathlib/_os.py @@ -281,6 +281,23 @@ def ensure_distinct_paths(source, target): raise err +def is_case_only_rename(source, target): + """ + Return true if *target* differs from *source* only in the case of its + name, and the target directory has no separate entry with that name. + On a case-insensitive filesystem, both paths then refer to the same + file, and renaming one to the other changes the case of its name. + """ + source_name = source.name + target_name = target.name + if source_name == target_name or source_name.lower() != target_name.lower(): + return False + try: + return target_name not in os.listdir(target.parent) + except (OSError, ValueError): + return False + + def ensure_different_files(source, target): """ Raise OSError(EINVAL) if both paths refer to the same file. diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 8013b0df26ec81..e05c90d3cd941a 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -1684,6 +1684,43 @@ def test_move_file_to_itself(self): source = base / 'fileA' self.assertRaises(OSError, source.move, source) + def test_move_file_change_case(self): + base = self.cls(self.base) + source = base / 'fileA' + target = base / 'FILEA' + if not target.exists(): + self.skipTest('requires a case-insensitive filesystem') + source_text = source.read_text() + result = source.move(target) + self.assertEqual(result, target) + self.assertIn('FILEA', os.listdir(base)) + self.assertNotIn('fileA', os.listdir(base)) + self.assertEqual(source_text, target.read_text()) + + def test_move_dir_change_case(self): + base = self.cls(self.base) + source = base / 'dirC' + target = base / 'DIRC' + if not target.exists(): + self.skipTest('requires a case-insensitive filesystem') + result = source.move(target) + self.assertEqual(result, target) + self.assertIn('DIRC', os.listdir(base)) + self.assertNotIn('dirC', os.listdir(base)) + self.assertTrue(target.joinpath('fileC').is_file()) + + @os_helper.skip_unless_hardlink + def test_move_file_to_hardlink_differing_in_case(self): + base = self.cls(self.base) + source = base / 'fileA' + target = base / 'FILEA' + if target.exists(): + self.skipTest('requires a case-sensitive filesystem') + target.hardlink_to(source) + self.assertRaises(OSError, source.move, target) + self.assertTrue(source.exists()) + self.assertTrue(target.exists()) + def test_move_dir(self): base = self.cls(self.base) source = base / 'dirC' diff --git a/Misc/NEWS.d/next/Library/2026-11-21-22-08-44.gh-issue-162377.l6N5U_.rst b/Misc/NEWS.d/next/Library/2026-11-21-22-08-44.gh-issue-162377.l6N5U_.rst new file mode 100644 index 00000000000000..9ccc56ceee663a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-22-08-44.gh-issue-162377.l6N5U_.rst @@ -0,0 +1,2 @@ +Fix :meth:`pathlib.Path.move` failing with "Source and target are the same +file" when only the case of the name changes on a case-insensitive filesystem.