Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

duaneg
Copy link

@duaneg duaneg commented Apr 26, 2025

Fix bug where shutil.copytree was skipping copying the contents of symbolic links to relative paths when the ignore_dangling_symlinks flag was set.

def test_copytree_relative_symlink(self):
# Ensure valid relative symlinks are copied regardless of the value of
# the ignore_dangling_symlinks flag (see gh-91205)
src_dir = self.mkdtemp()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
src_dir = self.mkdtemp()
src_dir = self.mkdtemp()
self.addCleanup(shutil.rmtree, src_dir, ignore_errors=True)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary (here and elsewhere): the self.mkdtemp() method called here calls self.addCleanup(os_helper.rmtree, d) on the result, so unless I'm missing something it shouldn't be required. I note most of the calls to self.mkdtemp() in this module do not add extra cleanup, although some do. The test doesn't warn about files being left behind, so they are being cleaned up properly without this.


for ignore_dangling_symlinks in (True, False):
with self.subTest(ignore_dangling_symlinks=ignore_dangling_symlinks):
dst_dir = os.path.join(self.mkdtemp(), 'x')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dst_dir = os.path.join(self.mkdtemp(), 'x')
dst_dir = os.path.join(self.mkdtemp(), 'x')
self.addCleanup(shutil.rmtree, dst_dir, ignore_errors=True)

# if the link is not to an absolute path it is relative to
# the source (see gh-91205)
if not os.path.isabs(linkto):
linkto = os.path.join(os.path.dirname(srcname), linkto)
Copy link
Member

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 whether linkto is a symlink or not? if not, please also add a test

Copy link
Author

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 on ignore_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.

@bedevere-app
Copy link

bedevere-app bot commented Apr 26, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@duaneg duaneg requested a review from picnixz April 26, 2025 22:31
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.

2 participants