-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1077,6 +1077,30 @@ def test_copytree_symlink_dir(self): | |||||||
self.assertTrue(os.path.islink(os.path.join(dst_dir, 'link_to_dir'))) | ||||||||
self.assertIn('test.txt', os.listdir(os.path.join(dst_dir, 'link_to_dir'))) | ||||||||
|
||||||||
@os_helper.skip_unless_symlink | ||||||||
def test_copytree_relative_symlink(self): | ||||||||
# gh-91205: Ensure valid relative symlinks are copied regardless of the | ||||||||
# value of the ``ignore_dangling_symlinks`` flag. | ||||||||
src_dir = self.mkdtemp() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is necessary (here and elsewhere): the |
||||||||
dir_a = os.path.join(src_dir, 'a') | ||||||||
dir_a_dir_b = os.path.join(dir_a, 'b') | ||||||||
os.mkdir(dir_a) | ||||||||
os.mkdir(dir_a_dir_b) | ||||||||
create_file(os.path.join(dir_a, 'a.txt')) | ||||||||
# create a symlink from src/a/b/a.txt to ../a.txt | ||||||||
os.symlink(os.path.join(os.pardir, 'a.txt'), | ||||||||
os.path.join(dir_a_dir_b, 'a.txt')) | ||||||||
|
||||||||
for ignore_dangling_symlinks in (True, False): | ||||||||
with self.subTest(ignore_dangling_symlinks=ignore_dangling_symlinks): | ||||||||
dst_dir = os.path.join(self.mkdtemp(), 'x') | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
shutil.copytree( | ||||||||
dir_a_dir_b, dst_dir, symlinks=False, | ||||||||
ignore_dangling_symlinks=ignore_dangling_symlinks) | ||||||||
self.assertIn('a.txt', os.listdir(dst_dir)) | ||||||||
duaneg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
self.assertFalse( | ||||||||
os.path.islink(os.path.join(dst_dir, 'a.txt'))) | ||||||||
|
||||||||
def test_copytree_return_value(self): | ||||||||
# copytree returns its destination path. | ||||||||
src_dir = self.mkdtemp() | ||||||||
|
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``. |
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.