8000 GH-73991: Support preserving metadata in `pathlib.Path.copy()` by barneygale · Pull Request #120806 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-73991: Support preserving metadata in pathlib.Path.copy() #120806

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 15 commits into from
Jul 6, 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
docstrings, re-order methods.
  • Loading branch information
barneygale committed Jun 27, 2024
commit 15cc3db3e8ef1742b5c627578e4683ee0e994312
18 changes: 12 additions & 6 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,18 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
"""
raise UnsupportedOperation(self._unsupported_msg('mkdir()'))

def _get_metadata(self, follow_symlinks):
"""
Returns path metadata as a dict with string keys.
"""
return {}

def _set_metadata(self, metadata, follow_symlinks):
"""
Sets path metadata from the given dict with string keys.
"""
pass

def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
"""
Copy the contents of this file to the given target. If this file is a
Expand Down Expand Up @@ -819,12 +831,6 @@ def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
if preserve_metadata:
target._set_metadata(self._get_metadata(True), True)

def _get_metadata(self, follow_symlinks):
return {}

def _set_metadata(self, metadata, follow_symlinks):
pass

def copytree(self, target, *, follow_symlinks=True, dirs_exist_ok=False,
ignore=None, on_error=None):
"""
Expand Down
6 changes: 3 additions & 3 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,9 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
if not exist_ok or not self.is_dir():
raise

_get_metadata = get_file_metadata
_set_metadata = set_file_metadata

if copyfile:
def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
"""
Expand All @@ -800,9 +803,6 @@ def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
raise
copyfile(os.fspath(self), target, follow_symlinks)

_get_metadata = get_file_metadata
_set_metadata = set_file_metadata

def chmod(self, mode, *, follow_symlinks=True):
"""
Change the permissions of the path, like os.chmod().
Expand Down
6 changes: 6 additions & 0 deletions Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def copyfileobj(source_f, target_f):


def get_file_metadata(path, follow_symlinks):
"""
Returns local path metadata as a dict with string keys.
"""
st = os.stat(path, follow_symlinks=follow_symlinks)
result = {
'mode': stat.S_IMODE(st.st_mode),
Expand All @@ -180,6 +183,9 @@ def get_file_metadata(path, follow_symlinks):


def set_file_metadata(path, metadata, follow_symlinks):
"""
Sets local path metadata from the given dict with string keys.
"""
def _nop(*args, ns=None, follow_symlinks=None):
pass

Expand Down
0