Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

pathlib ABCs: yield progress reports from WritablePath._copy_from() #131636

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from
Draft
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
Merge branch 'main' into copy-from-generator
  • Loading branch information
barneygale committed Mar 24, 2025
commit ce389f7db9b36d5d7a97cc2180e00e9a212dc6c4
6 changes: 1 addition & 5 deletions 6 Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,11 +1105,7 @@ def copy(self, target, **kwargs):
if not hasattr(target, 'with_segments'):
target = self.with_segments(target)
ensure_distinct_paths(self, target)
try:
copy_to_target = target._copy_from
except AttributeError:
raise TypeError(f"Target path is not writable: {target!r}") from None
list(copy_to_target(self, **kwargs)) # Consume generator.
list(target._copy_from(self, **kwargs)) # Consume generator.
Copy link
Contributor

@graingert graingert Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per the docs in itertools the best way to consume a generator is collections.deque:

def consume(iterator, n=None):
    "Advance the iterator n-steps ahead. If n is None, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        deque(iterator, maxlen=0)
    else:
        next(islice(iterator, n, n), None)

it's a neat little short-cut when deque is maxlen=0

if (maxlen == 0)
return consume_iterator(it);

Suggested change
list(target._copy_from(self, **kwargs)) # Consume generator.
collections.deque(target._copy_from(self, **kwargs), maxlen=0) # Consume generator.

return target.joinpath() # Empty join to ensure fresh metadata.

def copy_into(self, target_dir, **kwargs):
Expand Down
6 changes: 1 addition & 5 deletions 6 Lib/pathlib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,7 @@ def copy(self, target, **kwargs):
Recursively copy this file or directory tree to the given destination.
"""
ensure_distinct_paths(self, target)
try:
copy_to_target = target._copy_from
except AttributeError:
raise TypeError(f"Target path is not writable: {target!r}") from None
list(copy_to_target(self, **kwargs)) # Consume generator.
list(target._copy_from(self, **kwargs)) # Consume generator.
return target.joinpath() # Empty join to ensure fresh metadata.

def copy_into(self, target_dir, **kwargs):
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.