Skip to content
Closed
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
5 changes: 5 additions & 0 deletions Doc/library/shutil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ Directory and files operations
each file. It will be called with the source path and the destination path
as arguments. By default, :func:`shutil.copy2` is used, but any function
that supports the same signature (like :func:`shutil.copy`) can be used.
If *use_srcentry* is true, the *copy_fucntion* gets the srcentry, otherwise
it gets the srcname. By default the functions copy() and copy2() get the
srcentry.

.. versionchanged:: 3.3
Copy metadata when *symlinks* is false.
Expand All @@ -261,6 +264,8 @@ Directory and files operations
Platform-specific fast-copy syscalls may be used internally in order to
copy the file more efficiently. See
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
Added *use_srcentry* argument to control if the srcentry or the srcname
is passed to the *copy_fucntion*.

.. function:: rmtree(path, ignore_errors=False, onerror=None)

Expand Down
11 changes: 7 additions & 4 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,14 @@ def _ignore_patterns(path, names):
return _ignore_patterns

def _copytree(entries, src, dst, symlinks, ignore, copy_function,
ignore_dangling_symlinks):
ignore_dangling_symlinks, use_srcentry):
if ignore is not None:
ignored_names = ignore(src, set(os.listdir(src)))
else:
ignored_names = set()

os.makedirs(dst)
errors = []
use_srcentry = copy_function is copy2 or copy_function is copy

for srcentry in entries:
if srcentry.name in ignored_names:
Expand Down Expand Up @@ -489,7 +488,7 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
return dst

def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
ignore_dangling_symlinks=False):
ignore_dangling_symlinks=False, use_srcentry=True):
"""Recursively copy a directory tree.

The destination directory must not already exist.
Expand Down Expand Up @@ -522,12 +521,16 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
to copy each file. It will be called with the source path and the
destination path as arguments. By default, copy2() is used, but any
function that supports the same signature (like copy()) can be used.
When this copy_function should recieve a srcentry (like copy2() and
copy()), set use_srcentry to True, otherwise the function will pass
only the srcname to the copy_function.

"""
with os.scandir(src) as entries:
return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
ignore=ignore, copy_function=copy_function,
ignore_dangling_symlinks=ignore_dangling_symlinks)
ignore_dangling_symlinks=ignore_dangling_symlinks,
use_srcentry=use_srcentry)

# version vulnerable to race conditions
def _rmtree_unsafe(path, onerror):
Expand Down