Skip to content

Commit 4a59c96

Browse files
csernazsvstinner
authored andcommitted
[2.7] bpo-34260, shutil: fix copy2 and copystat documentation (GH-8523) (GH-10071)
Fix the documentation of copy2, as it does not copy file ownership (user and group), only mode, mtime, atime and flags. The original text was confusing to developers as it suggested that this command is the same as 'cp -p', but according to cp(1), '-p' copies file ownership as well. Clarify which metadata is copied by shutil.copystat in its docstring. (cherry picked from commit 4f399be)
1 parent a1f45ec commit 4a59c96

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Doc/library/shutil.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ Directory and files operations
8282

8383
.. function:: copy2(src, dst)
8484

85-
Similar to :func:`shutil.copy`, but metadata is copied as well -- in fact,
86-
this is just :func:`shutil.copy` followed by :func:`copystat`. This is
87-
similar to the Unix command :program:`cp -p`.
85+
Identical to :func:`~shutil.copy` except that :func:`copy2`
86+
also attempts to preserve file metadata.
87+
88+
:func:`copy2` uses :func:`copystat` to copy the file metadata.
89+
Please see :func:`copystat` for more information.
8890

8991

9092
.. function:: ignore_patterns(\*patterns)

Lib/shutil.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ def copymode(src, dst):
105105
os.chmod(dst, mode)
106106

107107
def copystat(src, dst):
108-
"""Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
108+
"""Copy file metadata
109+
110+
Copy the permission bits, last access time, last modification time, and
111+
flags from `src` to `dst`. On Linux, copystat() also copies the "extended
112+
attributes" where possible. The file contents, owner, and group are
113+
unaffected. `src` and `dst` are path names given as strings.
114+
"""
109115
st = os.stat(src)
110116
mode = stat.S_IMODE(st.st_mode)
111117
if hasattr(os, 'utime'):
@@ -134,7 +140,10 @@ def copy(src, dst):
134140
copymode(src, dst)
135141

136142
def copy2(src, dst):
137-
"""Copy data and all stat info ("cp -p src dst").
143+
"""Copy data and metadata. Return the file's destination.
144+
145+
Metadata is copied with copystat(). Please see the copystat function
146+
for more information.
138147
139148
The destination may be a directory.
140149

0 commit comments

Comments
 (0)