-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-91205: fix bug in shutil.copytree with relative links and ignore_dangling_symlinks=True #132984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
duaneg
wants to merge
3
commits into
python:main
Choose a base branch
from
duaneg:gh-91205
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2025-04-26-13-44-46.gh-issue-91205.kYPo51.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Avoid skipping symbolic links to relative paths in :func:`shutil.copytree` when | ||
``ignore_dangling_symlinks=True`` and ``symlinks=False``. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original patch used
os.path.normpath
. Do we need it? Also, shouldn't we retest whetherlinkto
is a symlink or not? if not, please also add a testThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should use
normpath
: it does a string replacement which can change the meaning if symbolic links are involved, which I think would be a bug if it can happen. If it doesn't change the meaning then it shouldn't matter, since we just use the result in one place immediately after, to check whether it exists or not.We don't need to check whether the result is a link: it may or may not be, but is correctly handled either way by the following code.
os.path.exists
will return false if it is a dangling link (and we will either skip it or carry on and raise an error depending onignore_dangling_symlinks
). Note that absolute symbolic links are handled the same way, so if this was necessary they would already be broken.Having said that, good point about testing: AFAICT this case is not currently under test. I'll add a few levels of valid links-to-links to the new test case and expand
test_copytree_dangling_symlinks
to test dangling links-to-links.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do it in a separate test function. It's easier to debug. Namely, one test for flat links and one test for multiple links. What about circular links? (are they allowed actually? namely l1 -> l2 -> l1?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, will do.
Good question. As it happens, they are treated exactly like dangling links for the purposes of this method, since
os.path.exists
will returnFalse
for them. I.e. ifsymlinks=True
they will be recreated as symbolic links (and it doesn't matter that they are circular), otherwise they will be skipped or cause an error depending if theignore_dangling_symlinks
flag is true or false, respectively.Naturally this means they are also affected by this bug. E.g. if the symlink target is a relative path that happens to exist relative to the working directory the code will think they are valid and then fail when it tries to copy their content.