Skip to content
Prev Previous commit
Next Next commit
Merge branch 'main' into fix-issue-81340
  • Loading branch information
illia-v committed Jun 5, 2024
commit 184689516b8895b966aa1c9b798cbe19f3b454e4
26 changes: 25 additions & 1 deletion Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
COPY_BUFSIZE = 1024 * 1024 if _WINDOWS else 64 * 1024
# This should never be removed, see rationale in:
# https://bugs.python.org/issue43743#msg393429
_USE_CP_SENDFILE = hasattr(os, "sendfile") and sys.platform.startswith("linux")
_USE_CP_SENDFILE = (hasattr(os, "sendfile")
and sys.platform.startswith(("linux", "android")))
_USE_CP_COPY_FILE_RANGE = hasattr(os, "copy_file_range")
_HAS_FCOPYFILE = posix and hasattr(posix, "_fcopyfile") # macOS

Expand Down Expand Up @@ -497,6 +498,29 @@ def copy2(src, dst, *, follow_symlinks=True, allow_reflink=True):
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))

if hasattr(_winapi, "CopyFile2"):
src_ = os.fsdecode(src)
dst_ = os.fsdecode(dst)
flags = _winapi.COPY_FILE_ALLOW_DECRYPTED_DESTINATION # for compat
if not follow_symlinks:
flags |= _winapi.COPY_FILE_COPY_SYMLINK
try:
_winapi.CopyFile2(src_, dst_, flags)
return dst
except OSError as exc:
if (exc.winerror == _winapi.ERROR_PRIVILEGE_NOT_HELD
and not follow_symlinks):
# Likely encountered a symlink we aren't allowed to create.
# Fall back on the old code
pass
elif exc.winerror == _winapi.ERROR_ACCESS_DENIED:
# Possibly encountered a hidden or readonly file we can't
# overwrite. Fall back on old code
pass
else:
raise

copyfile(src, dst, follow_symlinks=follow_symlinks, allow_reflink=allow_reflink)
copystat(src, dst, follow_symlinks=follow_symlinks)
return dst
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.