Skip to content

gh-125676: Make shutil documentation consistent about copy2/copy behavior and remove ambiguity #137792

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tangyuan0821
Copy link

@tangyuan0821 tangyuan0821 commented Aug 15, 2025

Summary
Fix documentation in shutil.rst that contained two conflicting descriptions about shutil.copy2 and shutil.move. Clarify that copy2 attempts to preserve metadata but that individual metadata-copy operations may raise exceptions on some platforms or when privileges are insufficient; recommend using shutil.copy as the copy_function when higher robustness (avoiding metadata-related failures) is desired.

Related issue
Fixes: #125676

Motivation
The original text contained two mutually contradictory statements: one suggested that copy2 "never raises an exception because it cannot preserve file metadata", while another said that using copy as the fallback copy_function for move allows the move to succeed when it is not possible to also copy metadata. After inspecting Lib/shutil.py, copy2 calls copystat(), and copystat() may raise OSError for some metadata operations (for example, due to lack of privileges or platform limitations). The documentation should therefore state the behavior more precisely to avoid misleading users.

Files changed

  • Doc/library/shutil.rst — modify a few lines to make the descriptions of copy2 and copy consistent and clear:
    • State that copy2 attempts to preserve additional metadata but that individual metadata-copy operations may fail and raise exceptions on certain platforms or when privileges are insufficient.
    • Recommend shutil.copy when callers want to avoid metadata-related errors (it copies only the file data and permission bits).

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

shutil.move documentation conflicts with shutil.copy2 documentation
1 participant