@@ -170,10 +170,7 @@ def _fastcopy_sendfile(fsrc, fdst):
170
170
offset += sent
171
171
172
172
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()."""
177
174
# Localize variable access to minimize overhead.
178
175
fsrc_readinto = fsrc .readinto
179
176
fdst_write = fdst .write
@@ -221,7 +218,7 @@ def copyfile(src, dst, *, follow_symlinks=True):
221
218
if _samefile (src , dst ):
222
219
raise SameFileError ("{!r} and {!r} are the same file" .format (src , dst ))
223
220
224
- fsize = 0
221
+ filesize = 0
225
222
for i , fn in enumerate ([src , dst ]):
226
223
try :
227
224
st = os .stat (fn )
@@ -233,34 +230,35 @@ def copyfile(src, dst, *, follow_symlinks=True):
233
230
if stat .S_ISFIFO (st .st_mode ):
234
231
raise SpecialFileError ("`%s` is a named pipe" % fn )
235
232
if _WINDOWS and i == 0 :
236
- fsize = st .st_size
233
+ filesize = st .st_size
237
234
238
235
if not follow_symlinks and os .path .islink (src ):
239
236
os .symlink (os .readlink (src ), dst )
240
237
else :
241
238
with open (src , 'rb' ) as fsrc , open (dst , 'wb' ) as fdst :
239
+ # Linux / Solaris
242
240
if _HAS_SENDFILE :
243
241
try :
244
242
_fastcopy_sendfile (fsrc , fdst )
245
243
return dst
246
244
except _GiveupOnFastCopy :
247
245
pass
248
-
249
- if _HAS_FCOPYFILE :
246
+ # macOS
247
+ elif _HAS_FCOPYFILE :
250
248
try :
251
249
_fastcopy_fcopyfile (fsrc , fdst , posix ._COPYFILE_DATA )
252
250
return dst
253
251
except _GiveupOnFastCopy :
254
252
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 :
261
258
_copybinfileobj (fsrc , fdst )
262
- else :
263
- copyfileobj (fsrc , fdst )
259
+ return dst
260
+
261
+ copyfileobj (fsrc , fdst )
264
262
265
263
return dst
266
264
0 commit comments