Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading