FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-125413: `pathlib.Path.copy()`: get common metadata keys only once by barneygale · Pull Request #125990 · python/cpython · GitHub

/ cpython Public
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
30 changes: 14 additions & 16 deletions Lib/pathlib/_abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL