GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-73991: Add `pathlib.Path.copy()` by barneygale · Pull Request #119058 · python/cpython

Conversation

@barneygale

@barneygale barneygale commented May 14, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Add a Path.copy() method that copies the content of one file to another.

This method is similar to shutil.copyfile() but differs in the following ways:

  • Uses fcntl.FICLONE where available (see shutil: add reflink=False to file copy functions to control clone/CoW copies (use copy_file_range) #81338)
  • Uses os.copy_file_range where available (see Use copy_file_range() in shutil.copyfile() (server-side copy) #81340)
  • Uses _winapi.CopyFile2 where available, even though this copies more metadata than the other implementations. This makes WindowsPath.copy() more similar to shutil.copy2().

The method is deliberately less specified than the shutil functions to allow OS-specific optimizations that might copy more or less metadata.

Incorporates code by @illia-v and @giampaolo from #81338 and #93152 respectively.

  • Issue: Support moving across filesystems in pathlib.Path, as shutil.move() does #73991

📚 Documentation preview 📚: https://cpython-previews--119058.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

nineteendo reacted with thumbs up emoji hdhoang reacted with rocket emoji
All reactions
  • 👍 1 reaction
  • 🚀 1 reaction
@barneygale
pythonGH-73991: Add pathlib.Path.copy() method.
7128a7a
Add a `Path.copy()` method that copies a file to a target file or directory
using `shutil.copy2()`.

In the private pathlib ABCs, we add a version that supports copying from
one instance of `PathBase` to another. We don't copy metadata, because
doing so probably requires new APIs that haven't been designed yet.
@barneygale barneygale added the topic-pathlib label May 14, 2024
@bedevere-app bedevere-app Bot added the awaiting core review label May 14, 2024
@bedevere-app bedevere-app Bot mentioned this pull request May 14, 2024
Support moving across filesystems in pathlib.Path, as shutil.move() does #73991
Closed
@barneygale barneygale changed the title GH-73991: Add pathlib.Path.copy() method. GH-73991: Add pathlib.Path.copy() May 15, 2024
barneygale added 3 commits May 15, 2024 01:36
@barneygale
Fix tests, add news
4cf63e6
@barneygale
Comment on use of shutil.copy2()
e83f53e
@barneygale
Use fcopyfile() / sendfile() where available.
092a0e0
@barneygale

barneygale commented May 27, 2024

Copy link
Copy Markdown
Contributor Author

Marking this PR as a draft, because I think we'll need to introduce os.copy() first.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
barneygale marked this pull request as draft May 27, 2024 14:40
@bedevere-app bedevere-app Bot removed the awaiting core review label May 27, 2024
barneygale added 2 commits June 3, 2024 03:58
@barneygale
Merge branch 'main' into pythongh-73991-copy
ac37fa5
@barneygale
Drop usage of shutil
a9a216e
@barneygale
barneygale marked this pull request as ready for review June 3, 2024 06:42
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 3, 2024
@barneygale
Fix tests, add FICLONE support
cbf6174
@barneygale
barneygale requested a review from zooba June 3, 2024 20:56
zooba
zooba reviewed Jun 4, 2024
View reviewed changes

@zooba zooba left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Just some thoughts, nothing I'd necessarily block the PR on. I like the way it looks overall!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/pathlib/_local.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/pathlib/_abc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

barneygale added 4 commits June 4, 2024 16:50
@barneygale
Move to pathlib._os.
738f8ed
@barneygale
Move a bit more code into _os.
83cb4dc
@barneygale
Handle classes that subclass PathBase and os.PathLike but not Path.
0aceea2
@barneygale
Reword
eec9e7f
@barneygale barneygale mentioned this pull request Jun 5, 2024
[WIP] GH-73991: Add os.copy() and friends #119079
Closed
eryksun
eryksun reviewed Jun 5, 2024
View reviewed changes
Comment thread Lib/pathlib/_local.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

barneygale added 4 commits June 5, 2024 18:38
@barneygale
Add test for directory symlinks
fddcd73
@barneygale
Fix handling of symlinks to directories on Windows.
c683c2d
@barneygale
Expose _winapi.COPY_FILE_DIRECTORY
0be714f
@barneygale
Test copying empty file.
5169b9a
eryksun
eryksun reviewed Jun 6, 2024
View reviewed changes
Comment thread Lib/pathlib/_os.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

eryksun
eryksun reviewed Jun 6, 2024
View reviewed changes
Comment thread Lib/pathlib/_os.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

eryksun
eryksun reviewed Jun 6, 2024
View reviewed changes
Comment thread Lib/pathlib/_os.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

zooba
zooba reviewed Jun 6, 2024
View reviewed changes
Comment thread Modules/_winapi.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

barneygale and others added 2 commits June 6, 2024 20:21
@barneygale @eryksun
Apply suggestions from code review
7c9c893
Co-authored-by: Eryk Sun <[email protected]>
@barneygale
Drop follow_symlinks argument for now.
0e1e4f1
@barneygale
barneygale requested review from eryksun and zooba June 12, 2024 22:07
@zooba

zooba commented Jun 12, 2024

Copy link
Copy Markdown
Member

Looks fine to me, but I only quickly reskimmed it. I'll be away for the next few days, so don't wait for me if you guys think it's ready.

barneygale reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

barneygale added 2 commits June 13, 2024 00:50
@barneygale
Merge branch 'main' into pythongh-73991-copy
8434924
@barneygale
Merge branch 'main' into pythongh-73991-copy
d8cb6c6
@barneygale
barneygale merged commit 7c38097 into python:main Jun 14, 2024
@bedevere-app bedevere-app Bot removed the awaiting core review label Jun 14, 2024
mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
@barneygale @eryksun @mrahtz
pythonGH-73991: Add pathlib.Path.copy() (python#119058)
7e37fbe
Add a `Path.copy()` method that copies the content of one file to another.

This method is similar to `shutil.copyfile()` but differs in the following ways:

- Uses `fcntl.FICLONE` where available (see pythonGH-81338)
- Uses `os.copy_file_range` where available (see pythonGH-81340)
- Uses `_winapi.CopyFile2` where available, even though this copies more metadata than the other implementations. This makes `WindowsPath.copy()` more similar to `shutil.copy2()`.

The method is presently _less_ specified than the `shutil` functions to allow OS-specific optimizations that might copy more or less metadata.

Incorporates code from pythonGH-81338 and pythonGH-93152.

Co-authored-by: Eryk Sun <[email protected]>
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
@barneygale @eryksun @estyxx
pythonGH-73991: Add pathlib.Path.copy() (python#119058)
595b213
Add a `Path.copy()` method that copies the content of one file to another.

This method is similar to `shutil.copyfile()` but differs in the following ways:

- Uses `fcntl.FICLONE` where available (see pythonGH-81338)
- Uses `os.copy_file_range` where available (see pythonGH-81340)
- Uses `_winapi.CopyFile2` where available, even though this copies more metadata than the other implementations. This makes `WindowsPath.copy()` more similar to `shutil.copy2()`.

The method is presently _less_ specified than the `shutil` functions to allow OS-specific optimizations that might copy more or less metadata.

Incorporates code from pythonGH-81338 and pythonGH-93152.

Co-authored-by: Eryk Sun <[email protected]>
@adang1345 adang1345 mentioned this pull request Jul 19, 2024
Notable changes in Python 3.14 adang1345/PythonVista#30
Closed
@mhsmith mhsmith mentioned this pull request Jul 31, 2024
gh-116622: Don't expose FICLONE ioctl on Android #122522
Merged
@illia-v illia-v mentioned this pull request Jul 31, 2024
gh-81340: Use copy_file_range in shutil.copyfile copy functions #93152
Merged
@opk12 opk12 mentioned this pull request Oct 4, 2024
Documenting that the new (3.14) pathlib copy functionality uses Copy-on-Write #124985
Closed
@lukts30 lukts30 mentioned this pull request Jan 17, 2025
[RFE] Support reflink (CoW copies) mesonbuild/meson#14153
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zooba zooba Awaiting requested review from zooba
@eryksun eryksun Awaiting requested review from eryksun

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-pathlib

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@barneygale @zooba @eryksun
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.