Skip to content

Commit 89cd883

Browse files
committed
update comments
1 parent c3166e1 commit 89cd883

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Lib/shutil.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ def _fastcopy_sendfile(fsrc, fdst):
170170
offset += sent
171171

172172
def _copybinfileobj(fsrc, fdst, length=COPY_BUFSIZE):
173-
"""Copy 2 regular file objects open in binary mode.
174-
This is used on Windows only for files >= 128 MiB as it appears to
175-
give a considerable boost.
176-
"""
173+
"""readinto()/memoryview() based variant of copyfileobj()."""
177174
# Localize variable access to minimize overhead.
178175
fsrc_readinto = fsrc.readinto
179176
fdst_write = fdst.write
@@ -221,7 +218,7 @@ def copyfile(src, dst, *, follow_symlinks=True):
221218
if _samefile(src, dst):
222219
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
223220

224-
fsize = 0
221+
filesize = 0
225222
for i, fn in enumerate([src, dst]):
226223
try:
227224
st = os.stat(fn)
@@ -233,34 +230,35 @@ def copyfile(src, dst, *, follow_symlinks=True):
233230
if stat.S_ISFIFO(st.st_mode):
234231
raise SpecialFileError("`%s` is a named pipe" % fn)
235232
if _WINDOWS and i == 0:
236-
fsize = st.st_size
233+
filesize = st.st_size
237234

238235
if not follow_symlinks and os.path.islink(src):
239236
os.symlink(os.readlink(src), dst)
240237
else:
241238
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
239+
# Linux / Solaris
242240
if _HAS_SENDFILE:
243241
try:
244242
_fastcopy_sendfile(fsrc, fdst)
245243
return dst
246244
except _GiveupOnFastCopy:
247245
pass
248-
249-
if _HAS_FCOPYFILE:
246+
# macOS
247+
elif _HAS_FCOPYFILE:
250248
try:
251249
_fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA)
252250
return dst
253251
except _GiveupOnFastCopy:
254252
pass
255-
256-
if _WINDOWS and fsize >= 128 * 1024 * 1024:
257-
# Use alternate memoryview() based implementation on Windows
258-
# for files >= 128 MiB. It appears this gives a considerable
259-
# speedup, see:
260-
# https://github.com/python/cpython/pull/7160#discussion_r195162475
253+
# Windows: for files >= 128 MiB in size we observe a
254+
# considerable speedup by using a readinto()/memoryview()
255+
# variant of copyfileobj(), see:
256+
# https://github.com/python/cpython/pull/7160#discussion_r195162475
257+
elif _WINDOWS and filesize >= 128 * 1024 * 1024:
261258
_copybinfileobj(fsrc, fdst)
262-
else:
263-
copyfileobj(fsrc, fdst)
259+
return dst
260+
261+
copyfileobj(fsrc, fdst)
264262

265263
return dst
266264

0 commit comments

Comments
 (0)