gh-125676: Make shutil documentation consistent about copy2/copy behavior and remove ambiguity #137792
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.
Summary
Fix documentation in
shutil.rstthat contained two conflicting descriptions aboutshutil.copy2andshutil.move. Clarify thatcopy2attempts to preserve metadata but that individual metadata-copy operations may raise exceptions on some platforms or when privileges are insufficient; recommend usingshutil.copyas thecopy_functionwhen higher robustness (avoiding metadata-related failures) is desired.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 usingcopyas the fallbackcopy_functionformoveallows the move to succeed when it is not possible to also copy metadata. After inspectingLib/shutil.py,copy2callscopystat(), andcopystat()may raiseOSErrorfor 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 ofcopy2andcopyconsistent and clear:copy2attempts to preserve additional metadata but that individual metadata-copy operations may fail and raise exceptions on certain platforms or when privileges are insufficient.shutil.copywhen callers want to avoid metadata-related errors (it copies only the file data and permission bits).shutil.movedocumentation conflicts withshutil.copy2documentation #125676📚 Documentation preview 📚: https://cpython-previews--137792.org.readthedocs.build/