Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.2k
GH-125413: Add privatepathlib.Path
method to write metadata#130238
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -102,16 +102,16 @@ def _sendfile(source_fd, target_fd): | ||
if _winapi and hasattr(_winapi, 'CopyFile2'): | ||
def_copyfile2(source, target): | ||
""" | ||
Copy from one file to another using CopyFile2 (Windows only). | ||
""" | ||
_winapi.CopyFile2(source, target, 0) | ||
else: | ||
_copyfile2 = None | ||
def_copyfileobj(source_f, target_f): | ||
""" | ||
Copy data from file-like object source_f to file-like object target_f. | ||
""" | ||
@@ -200,70 +200,6 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None, | ||
raise TypeError(f"{cls.__name__} can't be opened with mode {mode!r}") | ||
def ensure_distinct_paths(source, target): | ||
""" | ||
Raise OSError(EINVAL) if the other path is within this path. | ||
@@ -284,94 +220,6 @@ def ensure_distinct_paths(source, target): | ||
raise err | ||
def ensure_different_files(source, target): | ||
""" | ||
Raise OSError(EINVAL) if both paths refer to the same file. | ||
@@ -394,6 +242,102 @@ def ensure_different_files(source, target): | ||
raise err | ||
def copy_file(source, target, follow_symlinks=True, dirs_exist_ok=False, | ||
preserve_metadata=False): | ||
""" | ||
Recursively copy the given source ReadablePath to the given target WritablePath. | ||
""" | ||
info = source.info | ||
if not follow_symlinks and info.is_symlink(): | ||
target.symlink_to(source.readlink(), info.is_dir()) | ||
if preserve_metadata: | ||
target._write_info(info, follow_symlinks=False) | ||
elif info.is_dir(): | ||
children = source.iterdir() | ||
target.mkdir(exist_ok=dirs_exist_ok) | ||
for src in children: | ||
dst = target.joinpath(src.name) | ||
copy_file(src, dst, follow_symlinks, dirs_exist_ok, preserve_metadata) | ||
if preserve_metadata: | ||
target._write_info(info) | ||
else: | ||
if _copyfile2: | ||
# Use fast OS routine for local file copying where available. | ||
try: | ||
source_p = os.fspath(source) | ||
target_p = os.fspath(target) | ||
except TypeError: | ||
pass | ||
else: | ||
_copyfile2(source_p, target_p) | ||
return | ||
barneygale marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
ensure_different_files(source, target) | ||
with magic_open(source, 'rb') as source_f: | ||
with magic_open(target, 'wb') as target_f: | ||
_copyfileobj(source_f, target_f) | ||
if preserve_metadata: | ||
target._write_info(info) | ||
def copy_info(info, target, follow_symlinks=True): | ||
"""Copy metadata from the given PathInfo to the given local path.""" | ||
copy_times_ns = ( | ||
hasattr(info, '_access_time_ns') and | ||
hasattr(info, '_mod_time_ns') and | ||
(follow_symlinks or os.utime in os.supports_follow_symlinks)) | ||
if copy_times_ns: | ||
t0 = info._access_time_ns(follow_symlinks=follow_symlinks) | ||
t1 = info._mod_time_ns(follow_symlinks=follow_symlinks) | ||
os.utime(target, ns=(t0, t1), follow_symlinks=follow_symlinks) | ||
# We must copy extended attributes before the file is (potentially) | ||
# chmod()'ed read-only, otherwise setxattr() will error with -EACCES. | ||
copy_xattrs = ( | ||
hasattr(info, '_xattrs') and | ||
hasattr(os, 'setxattr') and | ||
(follow_symlinks or os.setxattr in os.supports_follow_symlinks)) | ||
if copy_xattrs: | ||
xattrs = info._xattrs(follow_symlinks=follow_symlinks) | ||
for attr, value in xattrs: | ||
try: | ||
os.setxattr(target, attr, value, follow_symlinks=follow_symlinks) | ||
except OSError as e: | ||
if e.errno not in (EPERM, ENOTSUP, ENODATA, EINVAL, EACCES): | ||
raise | ||
copy_posix_permissions = ( | ||
hasattr(info, '_posix_permissions') and | ||
(follow_symlinks or os.chmod in os.supports_follow_symlinks)) | ||
if copy_posix_permissions: | ||
posix_permissions = info._posix_permissions(follow_symlinks=follow_symlinks) | ||
try: | ||
os.chmod(target, posix_permissions, follow_symlinks=follow_symlinks) | ||
except NotImplementedError: | ||
# if we got a NotImplementedError, it's because | ||
# * follow_symlinks=False, | ||
# * lchown() is unavailable, and | ||
# * either | ||
# * fchownat() is unavailable or | ||
# * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW. | ||
# (it returned ENOSUP.) | ||
# therefore we're out of options--we simply cannot chown the | ||
# symlink. give up, suppress the error. | ||
# (which is what shutil always did in this circumstance.) | ||
pass | ||
copy_bsd_flags = ( | ||
hasattr(info, '_bsd_flags') and | ||
hasattr(os, 'chflags') and | ||
(follow_symlinks or os.chflags in os.supports_follow_symlinks)) | ||
if copy_bsd_flags: | ||
bsd_flags = info._bsd_flags(follow_symlinks=follow_symlinks) | ||
try: | ||
os.chflags(target, bsd_flags, follow_symlinks=follow_symlinks) | ||
except OSError as why: | ||
if why.errno not in (EOPNOTSUPP, ENOTSUP): | ||
raise | ||
class _PathInfoBase: | ||
__slots__ = ('_path', '_stat_result', '_lstat_result') | ||
2 changes: 2 additions & 0 deletionsMisc/NEWS.d/next/Library/2025-02-21-20-16-32.gh-issue-125413.YJ7Msf.rst
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Speed up :meth:`Path.copy <pathlib.Path.copy>` by making better use of | ||
:attr:`~pathlib.Path.info` internally. |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.