Skip to content

gh-162377: Allow Path.move() to change only the case of a name - #162409

Open
atorcam102 wants to merge 1 commit into
python:mainfrom
atorcam102:gh-162377-path-move-case
Open

atorcam102 wants to merge 1 commit into
python:mainfrom
atorcam102:gh-162377-path-move-case

Conversation

@atorcam102

@atorcam102 atorcam102 commented Nov 21, 2026 •

Copy link
Copy Markdown

On a case-insensitive filesystem (macOS, Windows), Path('readme.txt').move('README.txt') raises OSError: [Errno 22] Source and target are the same file, while os.rename(), os.replace(), Path.rename() and shutil.move() all rename it.

The error comes from the ensure_different_files() check in Path.move(): both names refer to the same file, so the file ids match. This PR skips that check when the target differs from the source only in the case of its name, which is the same exception shutil.move() makes. The check still runs when the target directory has a separate entry with the new name, so on a case-sensitive filesystem moving a file onto a hard link to itself named FILEA still raises instead of doing a silent no-op. Moving a path onto itself also still raises.

Tests added to test_pathlib:

  • test_move_file_change_case and test_move_dir_change_case, skipped unless the filesystem is case-insensitive. They detect that by checking whether FILEA / DIRC exist, instead of os_helper.fs_is_case_insensitive(), which upper-cases the whole path and gave a wrong answer on a case-insensitive mount under a case-sensitive root.
  • test_move_file_to_hardlink_differing_in_case, skipped unless the filesystem is case-sensitive.

Tested:

  • Linux (ext4): ./python -m test test_pathlib test_shutil passes; the hard link test runs, the case tests are skipped.
  • A case-insensitive NTFS directory, accessed from WSL: the two case tests fail with the original error before the change and pass after it.
  • Windows with Python 3.15.2 and the patched pathlib: the reproducer from the issue renames both a file and a directory, and moving a path onto itself still raises.

On a case-insensitive filesystem, a target that differs from the source
only in the case of its name refers to the same file, so move() raised
"Source and target are the same file" instead of renaming it. Skip that
check when only the case changes and the target directory has no
separate entry with the new name, as shutil.move() already does.
@python-cla-bot

python-cla-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant