E670 GH-73991: Add `pathlib.Path.copy()` by barneygale · Pull Request #119058 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-73991: Add pathlib.Path.copy() #119058

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 19 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix handling of symlinks to directories on Windows.
  • Loading branch information
barneygale committed Jun 5, 2024
commit c683c2dfa72330801fa35de355020f1fa7807126
11 changes: 3 additions & 8 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
import grp
except ImportError:
grp = None
try:
import _winapi
except ImportError:
_winapi = None

from ._abc import UnsupportedOperation, PurePathBase, PathBase
from ._os import copyfile


__all__ = [
Expand Down Expand Up @@ -784,7 +781,7 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
if not exist_ok or not self.is_dir():
raise

if hasattr(_winapi, 'CopyFile2'):
if copyfile:
def copy(self, target, follow_symlinks=True):
"""
Copy the contents of this file to the given target. If this file is a
Expand All @@ -799,9 +796,7 @@ def copy(self, target, follow_symlinks=True):
# Use generic implementation from PathBase.
return PathBase.copy(self, target, follow_symlinks=follow_symlinks)
raise

flags = 0 if follow_symlinks else _winapi.COPY_FILE_COPY_SYMLINK
_winapi.CopyFile2(os.fspath(self), target, flags)
copyfile(os.fspath(self), target, follow_symlinks)

def chmod(self, mode, *, follow_symlinks=True):
"""
Expand Down
34 changes: 34 additions & 0 deletions Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from errno import EBADF, EOPNOTSUPP, ETXTBSY, EXDEV
import os
import stat
import sys
try:
import fcntl
Expand All @@ -13,6 +14,10 @@
import posix
except ImportError:
posix = None
try:
import _winapi
except ImportError:
_winapi = None


def get_copy_blocksize(infd):
Expand Down Expand Up @@ -87,6 +92,35 @@ def copyfd(source_fd, target_fd):
copyfd = None


if _winapi and hasattr(_winapi, 'CopyFile2'):
def is_dirlink(path):
try:
st = os.lstat(path)
except OSError:
return False
return (st.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY and
st.st_reparse_tag == stat.IO_REPARSE_TAG_SYMLINK)

def copyfile(source, target, follow_symlinks):
"""
Copy from one file to another using CopyFile2 (Windows only).
"""
if follow_symlinks:
flags = 0
else:
flags = _winapi.COPY_FILE_COPY_SYMLINK
try:
_winapi.CopyFile2(source, target, flags)
except OSError as err:
# Check for ERROR_ACCESS_DENIED
if err.winerror != 5 or not is_dirlink(source):
raise err
flags |= _winapi.COPY_FILE_DIRECTORY
_winapi.CopyFile2(source, target, flags)
else:
copyfile = None


def copyfileobj(source_f, target_f):
"""
Copy data from file-like object source_f to file-like object target_f.
Expand Down
0