Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
GH-73991: Rework pathlib.Path.copytree() into copy()#122369
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
20 commits Select commit Hold shift + click to select a range
b7ce838 GH-73991: Rework `pathlib.Path.copytree()` into `copy()`.
barneygale 76cf893 Simplify handling of symlinks
barneygale b10a3b5 Inline _copy_metadata()
barneygale a67f5a7 Further symlink tweaks
barneygale 017dd86 Fix copying metadata to non-symlinks
barneygale fcc8af1 Specify target_is_directory when symlinking.
barneygale 57efe1f Tiny simplification.
barneygale 8bef16b Skip unnecessary is_dir() when copying symlinks on POSIX.
barneygale a51ce63 Docs tweaks
barneygale 51b1e6d Fix CopyFile2 check
barneygale 429c7b6 Merge branch 'main' into merge-copy-copytree
barneygale 023b3a4 Docs tweak
barneygale 4d6d2dc Address review feedback
barneygale 7801edf Merge branch 'main' into merge-copy-copytree
barneygale 5ce229b Merge branch 'main' into merge-copy-copytree
barneygale 5a94525 Return the target as a Path object.
barneygale 3b6405b Apply suggestions from code review
barneygale 03f9b7f Address some review feedback
barneygale 02e9237 Simplify tracebacks
barneygale fed93fc Use absolute imports
barneygale 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
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 |
|---|---|---|
| @@ -16,7 +16,16 @@ | ||
| import posixpath | ||
| from glob import _GlobberBase, _no_recurse_symlinks | ||
| from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO | ||
| from ._os import UnsupportedOperation, copyfileobj | ||
| from pathlib._os import copyfileobj | ||
| __all__ = ["UnsupportedOperation"] | ||
| class UnsupportedOperation(NotImplementedError): | ||
| """An exception that is raised when an unsupported operation is attempted. | ||
| """ | ||
| pass | ||
| @functools.cache | ||
| @@ -761,6 +770,13 @@ def symlink_to(self, target, target_is_directory=False): | ||
| """ | ||
| raise UnsupportedOperation(self._unsupported_msg('symlink_to()')) | ||
| def _symlink_to_target_of(self, link): | ||
| """ | ||
| Make this path a symlink with the same target as the given link. This | ||
| is used by copy(). | ||
| """ | ||
| self.symlink_to(link.readlink()) | ||
| def hardlink_to(self, target): | ||
| """ | ||
| Make this path a hard link pointing to the same file as *target*. | ||
| @@ -806,21 +822,12 @@ def _copy_metadata(self, target, *, follow_symlinks=True): | ||
| metadata = self._read_metadata(keys, follow_symlinks=follow_symlinks) | ||
| target._write_metadata(metadata, follow_symlinks=follow_symlinks) | ||
| def copy(self, target, *, follow_symlinks=True, preserve_metadata=False): | ||
| def _copy_file(self, target): | ||
| """ | ||
| Copy the contents of this file to the given target. If this file is a | ||
| symlink and follow_symlinks is false, a symlink will be created at the | ||
| target. | ||
| Copy the contents of this file to the given target. | ||
barneygale marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| if not isinstance(target, PathBase): | ||
| target = self.with_segments(target) | ||
| if self._samefile_safe(target): | ||
| raise OSError(f"{self!r} and{target!r} are the same file") | ||
| if not follow_symlinks and self.is_symlink(): | ||
| target.symlink_to(self.readlink()) | ||
| if preserve_metadata: | ||
| self._copy_metadata(target, follow_symlinks=False) | ||
| return | ||
| with self.open('rb') as source_f: | ||
| try: | ||
| with target.open('wb') as target_f: | ||
| @@ -832,42 +839,39 @@ def copy(self, target, *, follow_symlinks=True, preserve_metadata=False): | ||
| f'Directory does not exist:{target}') from e | ||
| else: | ||
| raise | ||
| if preserve_metadata: | ||
| self._copy_metadata(target) | ||
| def copytree(self, target, *, follow_symlinks=True, | ||
| preserve_metadata=False, dirs_exist_ok=False, | ||
| ignore=None, on_error=None): | ||
| def copy(self, target, *, follow_symlinks=True, dirs_exist_ok=False, | ||
| preserve_metadata=False, ignore=None, on_error=None): | ||
| """ | ||
| Recursively copy this directory tree to the given destination. | ||
| Recursively copy this file or directory tree to the given destination. | ||
| """ | ||
| if not isinstance(target, PathBase): | ||
| target = self.with_segments(target) | ||
| if on_error is None: | ||
| def on_error(err): | ||
| raise err | ||
| stack = [(self, target)] | ||
| while stack: | ||
| source_dir, target_dir = stack.pop() | ||
| src, dst = stack.pop() | ||
| try: | ||
| sources = source_dir.iterdir() | ||
| target_dir.mkdir(exist_ok=dirs_exist_ok) | ||
| if preserve_metadata: | ||
| source_dir._copy_metadata(target_dir) | ||
| for source in sources: | ||
| if ignore and ignore(source): | ||
| continue | ||
| try: | ||
| if source.is_dir(follow_symlinks=follow_symlinks): | ||
| stack.append((source, target_dir.joinpath(source.name))) | ||
| else: | ||
| source.copy(target_dir.joinpath(source.name), | ||
| follow_symlinks=follow_symlinks, | ||
| preserve_metadata=preserve_metadata) | ||
| except OSError as err: | ||
| on_error(err) | ||
| if not follow_symlinks and src.is_symlink(): | ||
| dst._symlink_to_target_of(src) | ||
| if preserve_metadata: | ||
| src._copy_metadata(dst, follow_symlinks=False) | ||
| elif src.is_dir(): | ||
| children = src.iterdir() | ||
| dst.mkdir(exist_ok=dirs_exist_ok) | ||
barneygale marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| for child in children: | ||
| if not (ignore and ignore(child)): | ||
| stack.append((child, dst.joinpath(child.name))) | ||
| if preserve_metadata: | ||
| src._copy_metadata(dst) | ||
| else: | ||
| src._copy_file(dst) | ||
| if preserve_metadata: | ||
| src._copy_metadata(dst) | ||
| except OSError as err: | ||
| if on_error is None: | ||
| raise | ||
| on_error(err) | ||
| return target | ||
| def rename(self, target): | ||
| """ | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.