Skip to content

Commit 7fb7091

Browse files
author
Tyler Goodlet
committed
Expose copyfileobj() length arg for public use
When copying large files the copy time can be sufficiently shortened by increasing the memory buffer used in the `copyfileobj()` routine. Expose the `length` argument from `copyfileobj()` upwards for use throughout the module.
1 parent ff40ecd commit 7fb7091

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

Lib/shutil.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ class RegistryError(Exception):
7373
and unpacking registries fails"""
7474

7575

76-
def copyfileobj(fsrc, fdst, length=16*1024):
77-
"""copy data from file-like object fsrc to file-like object fdst"""
76+
def copyfileobj(fsrc, fdst, length=None):
77+
"""Copy data from file-like object `fsrc` to file-like object `fdst`.
78+
79+
An in-memory buffer size can be set with `length`; the default is 16 KB.
80+
"""
81+
if not length:
82+
length = 16*1024
7883
while 1:
7984
buf = fsrc.read(length)
8085
if not buf:
@@ -93,12 +98,14 @@ def _samefile(src, dst):
9398
return (os.path.normcase(os.path.abspath(src)) ==
9499
os.path.normcase(os.path.abspath(dst)))
95100

96-
def copyfile(src, dst, *, follow_symlinks=True):
101+
def copyfile(src, dst, *, follow_symlinks=True, length=None):
97102
"""Copy data from src to dst.
98103
99104
If follow_symlinks is not set and src is a symbolic link, a new
100105
symlink will be created instead of copying the file it points to.
101106
107+
An in memory buffer size can be set with `length`; the default is 16 kB.
108+
102109
"""
103110
if _samefile(src, dst):
104111
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
@@ -119,7 +126,7 @@ def copyfile(src, dst, *, follow_symlinks=True):
119126
else:
120127
with open(src, 'rb') as fsrc:
121128
with open(dst, 'wb') as fdst:
122-
copyfileobj(fsrc, fdst)
129+
copyfileobj(fsrc, fdst, length=length)
123130
return dst
124131

125132
def copymode(src, dst, *, follow_symlinks=True):
@@ -224,7 +231,7 @@ def lookup(name):
224231
raise
225232
_copyxattr(src, dst, follow_symlinks=follow)
226233

227-
def copy(src, dst, *, follow_symlinks=True):
234+
def copy(src, dst, *, follow_symlinks=True, length=None):
228235
"""Copy data and mode bits ("cp src dst"). Return the file's destination.
229236
230237
The destination may be a directory.
@@ -238,11 +245,11 @@ def copy(src, dst, *, follow_symlinks=True):
238245
"""
239246
if os.path.isdir(dst):
240247
dst = os.path.join(dst, os.path.basename(src))
241-
copyfile(src, dst, follow_symlinks=follow_symlinks)
248+
copyfile(src, dst, follow_symlinks=follow_symlinks, length=length)
242249
copymode(src, dst, follow_symlinks=follow_symlinks)
243250
return dst
244251

245-
def copy2(src, dst, *, follow_symlinks=True):
252+
def copy2(src, dst, *, follow_symlinks=True, length=None):
246253
"""Copy data and all stat info ("cp -p src dst"). Return the file's
247254
destination."
248255
@@ -254,7 +261,7 @@ def copy2(src, dst, *, follow_symlinks=True):
254261
"""
255262
if os.path.isdir(dst):
256263
dst = os.path.join(dst, os.path.basename(src))
257-
copyfile(src, dst, follow_symlinks=follow_symlinks)
264+
copyfile(src, dst, follow_symlinks=follow_symlinks, length=length)
258265
copystat(src, dst, follow_symlinks=follow_symlinks)
259266
return dst
260267

0 commit comments

Comments
 (0)