Skip to content
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

GH-125413: pathlib.Path.copy(): get common metadata keys only once #125990

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
30 changes: 14 additions & 16 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,16 +837,6 @@ def _write_metadata(self, metadata, *, follow_symlinks=True):
"""
raise UnsupportedOperation(self._unsupported_msg('_write_metadata()'))

def _copy_metadata(self, target, *, follow_symlinks=True):
"""
Copies metadata (permissions, timestamps, etc) from this path to target.
"""
# Metadata types supported by both source and target.
keys = self._readable_metadata & target._writable_metadata
if keys:
metadata = self._read_metadata(keys, follow_symlinks=follow_symlinks)
target._write_metadata(metadata, follow_symlinks=follow_symlinks)

def _copy_file(self, target):
"""
Copy the contents of this file to the given target.
Expand All @@ -872,24 +862,32 @@ def copy(self, target, *, follow_symlinks=True, dirs_exist_ok=False,
if not isinstance(target, PathBase):
target = self.with_segments(target)
self._ensure_distinct_path(target)
if preserve_metadata:
# Metadata types supported by both source and target.
metadata_keys = self._readable_metadata & target._writable_metadata
else:
metadata_keys = None
stack = [(self, target)]
while stack:
src, dst = stack.pop()
if not follow_symlinks and src.is_symlink():
dst._symlink_to_target_of(src)
if preserve_metadata:
src._copy_metadata(dst, follow_symlinks=False)
if metadata_keys:
metadata = src._read_metadata(metadata_keys, follow_symlinks=False)
dst._write_metadata(metadata, follow_symlinks=False)
elif src.is_dir():
children = src.iterdir()
dst.mkdir(exist_ok=dirs_exist_ok)
stack.extend((child, dst.joinpath(child.name))
for child in children)
if preserve_metadata:
src._copy_metadata(dst)
if metadata_keys:
metadata = src._read_metadata(metadata_keys)
dst._write_metadata(metadata)
else:
src._copy_file(dst)
if preserve_metadata:
src._copy_metadata(dst)
if metadata_keys:
metadata = src._read_metadata(metadata_keys)
dst._write_metadata(metadata)
return target

def copy_into(self, target_dir, *, follow_symlinks=True,
Expand Down
Loading