Repository navigation
gh-162377: Allow Path.move() to change only the case of a name - #162409
Open
atorcam102 wants to merge 1 commit into
Open
atorcam102 wants to merge 1 commit into
atorcam102 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On a case-insensitive filesystem (macOS, Windows),
Path('readme.txt').move('README.txt')raisesOSError: [Errno 22] Source and target are the same file, whileos.rename(),os.replace(),Path.rename()andshutil.move()all rename it.The error comes from the
ensure_different_files()check inPath.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 exceptionshutil.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 namedFILEAstill 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_caseandtest_move_dir_change_case, skipped unless the filesystem is case-insensitive. They detect that by checking whetherFILEA/DIRCexist, instead ofos_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:
./python -m test test_pathlib test_shutilpasses; the hard link test runs, the case tests are skipped.pathlib: the reproducer from the issue renames both a file and a directory, and moving a path onto itself still raises.Path.move()fails to change the case on macOS #162377