Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-39390: fix argument types for ignore callback of shutil.copytree #18122

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

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def _ignore_patterns(path, names):
def _copytree(entries, src, dst, symlinks, ignore, copy_function,
ignore_dangling_symlinks, dirs_exist_ok=False):
if ignore is not None:
ignored_names = ignore(src, {x.name for x in entries})
ignored_names = ignore(os.fspath(src), [x.name for x in entries])
else:
ignored_names = set()

Expand Down
42 changes: 42 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,48 @@ def _filter(src, names):
shutil.rmtree(src_dir)
shutil.rmtree(os.path.dirname(dst_dir))

def test_copytree_arg_types_of_ignore(self):
join = os.path.join
exists = os.path.exists

tmp_dir = self.mkdtemp()
src_dir = join(tmp_dir, "source")

os.mkdir(join(src_dir))
os.mkdir(join(src_dir, 'test_dir'))
os.mkdir(os.path.join(src_dir, 'test_dir', 'subdir'))
write_file((src_dir, 'test_dir', 'subdir', 'test.txt'), '456')

invokations = []

def _ignore(src, names):
invokations.append(src)
self.assertIsInstance(src, str)
self.assertIsInstance(names, list)
self.assertEqual(len(names), len(set(names)))
for name in names:
self.assertIsInstance(name, str)
return []
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd add a check to make sure this function is called 3 times. Something like:

    def test_copytree_arg_types_of_ignore(self):
        def _ignore(src, names):
            ...
            flags.append(None)

        flags = []
        ...  # body 
        self.assertEqual(len(flags), 3)  # last line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since it's called recursively, it's actually called a total of 9 times, but in any case it works.


dst_dir = join(self.mkdtemp(), 'destination')
shutil.copytree(src_dir, dst_dir, ignore=_ignore)
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir',
'test.txt')))

dst_dir = join(self.mkdtemp(), 'destination')
shutil.copytree(pathlib.Path(src_dir), dst_dir, ignore=_ignore)
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir',
'test.txt')))

dst_dir = join(self.mkdtemp(), 'destination')
src_dir_entry = list(os.scandir(tmp_dir))[0]
self.assertIsInstance(src_dir_entry, os.DirEntry)
shutil.copytree(src_dir_entry, dst_dir, ignore=_ignore)
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir',
'test.txt')))

self.assertEqual(len(invokations), 9)

def test_copytree_retains_permissions(self):
tmp_dir = self.mkdtemp()
src_dir = os.path.join(tmp_dir, 'source')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a regression with the `ignore` callback of :func:`shutil.copytree`.
The argument types are now str and List[str] again.