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.rst
that contained two conflicting descriptions aboutshutil.copy2
andshutil.move
. Clarify thatcopy2
attempts to preserve metadata but that individual metadata-copy operations may raise exceptions on some platforms or when privileges are insufficient; recommend usingshutil.copy
as thecopy_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 usingcopy
as the fallbackcopy_function
formove
allows the move to succeed when it is not possible to also copy metadata. After inspectingLib/shutil.py
,copy2
callscopystat()
, andcopystat()
may raiseOSError
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 ofcopy2
andcopy
consistent and clear: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.shutil.copy
when callers want to avoid metadata-related errors (it copies only the file data and permission bits).shutil.move
documentation conflicts withshutil.copy2
documentation #125676📚 Documentation preview 📚: https://cpython-previews--137792.org.readthedocs.build/