Skip to content

Commit 24b2f57

Browse files
author
Tyler Goodlet
committed
Document exposure of the copyfileobj length arg in shutil
1 parent 7fb7091 commit 24b2f57

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

Doc/library/shutil.rst

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ Directory and files operations
3939

4040
.. function:: copyfileobj(fsrc, fdst[, length])
4141

42-
Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
43-
The integer *length*, if given, is the buffer size. In particular, a negative
44-
*length* value means to copy the data without looping over the source data in
45-
chunks; by default the data is read in chunks to avoid uncontrolled memory
46-
consumption. Note that if the current file position of the *fsrc* object is not
47-
0, only the contents from the current file position to the end of the file will
48-
be copied.
42+
Copy the contents of the file-like object *fsrc* to the file-like object
43+
*fdst*. Only the contents from the current file position to the end of
44+
the file will be copied.
4945

46+
The integer *length*, if given, is the buffer size; the default value
47+
in bytes is 16 KiB. A negative *length* value means to copy the data without
48+
looping over the source data in chunks; by default the data is read in
49+
chunks to avoid uncontrolled memory consumption.
5050

51-
.. function:: copyfile(src, dst, *, follow_symlinks=True)
51+
.. function:: copyfile(src, dst, *, follow_symlinks=True, length=None)
5252

5353
Copy the contents (no metadata) of the file named *src* to a file named
5454
*dst* and return *dst*. *src* and *dst* are path names given as strings.
@@ -65,6 +65,9 @@ Directory and files operations
6565
a new symbolic link will be created instead of copying the
6666
file *src* points to.
6767

68+
The integer *length*, if given, is the in-memory buffer size; the default
69+
value in bytes is 16 KiB (see :func:`shutil.copyfileobj`).
70+
6871
.. versionchanged:: 3.3
6972
:exc:`IOError` used to be raised instead of :exc:`OSError`.
7073
Added *follow_symlinks* argument.
@@ -74,6 +77,9 @@ Directory and files operations
7477
Raise :exc:`SameFileError` instead of :exc:`Error`. Since the former is
7578
a subclass of the latter, this change is backward compatible.
7679

80+
.. versionchanged:: 3.7
81+
Added *length* parameter.
82+
7783

7884
.. exception:: SameFileError
7985

@@ -141,7 +147,7 @@ Directory and files operations
141147
.. versionchanged:: 3.3
142148
Added *follow_symlinks* argument and support for Linux extended attributes.
143149

144-
.. function:: copy(src, dst, *, follow_symlinks=True)
150+
.. function:: copy(src, dst, *, follow_symlinks=True, length=None)
145151

146152
Copies the file *src* to the file or directory *dst*. *src* and *dst*
147153
should be strings. If *dst* specifies a directory, the file will be
@@ -153,6 +159,9 @@ Directory and files operations
153159
is true and *src* is a symbolic link, *dst* will be a copy of
154160
the file *src* refers to.
155161

162+
The integer *length*, if given, is the in-memory buffer size; the default
163+
value in bytes is 16 KiB (see :func:`shutil.copyfileobj`).
164+
156165
:func:`~shutil.copy` copies the file data and the file's permission
157166
mode (see :func:`os.chmod`). Other metadata, like the
158167
file's creation and modification times, is not preserved.
@@ -163,7 +172,11 @@ Directory and files operations
163172
Added *follow_symlinks* argument.
164173
Now returns path to the newly created file.
165174

166-
.. function:: copy2(src, dst, *, follow_symlinks=True)
175+
.. versionchanged:: 3.7
176+
Added `length` parameter
177+
178+
179+
.. function:: copy2(src, dst, *, follow_symlinks=True, length=None)
167180

168181
Identical to :func:`~shutil.copy` except that :func:`copy2`
169182
also attempts to preserve all file metadata.
@@ -176,6 +189,9 @@ Directory and files operations
176189
unavailable, :func:`copy2` will preserve all the metadata
177190
it can; :func:`copy2` never returns failure.
178191

192+
The integer *length*, if given, is the in-memory buffer size; the default
193+
value in bytes is 16 KiB (see :func:`shutil.copyfileobj`).
194+
179195
:func:`copy2` uses :func:`copystat` to copy the file metadata.
180196
Please see :func:`copystat` for more information
181197
about platform support for modifying symbolic link metadata.
@@ -185,6 +201,10 @@ Directory and files operations
185201
file system attributes too (currently Linux only).
186202
Now returns path to the newly created file.
187203

204+
.. versionchanged:: 3.7
205+
Added `length` parameter
206+
207+
188208
.. function:: ignore_patterns(\*patterns)
189209

190210
This factory function creates a function that can be used as a callable for

Lib/shutil.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ class RegistryError(Exception):
7676
def copyfileobj(fsrc, fdst, length=None):
7777
"""Copy data from file-like object `fsrc` to file-like object `fdst`.
7878
79-
An in-memory buffer size can be set with `length`; the default is 16 KB.
79+
An in-memory buffer size in bytes can be set with `length`; the default is
80+
16 KiB.
8081
"""
8182
if not length:
82-
length = 16*1024
83+
length = 16 * 1024
8384
while 1:
8485
buf = fsrc.read(length)
8586
if not buf:
@@ -104,8 +105,8 @@ def copyfile(src, dst, *, follow_symlinks=True, length=None):
104105
If follow_symlinks is not set and src is a symbolic link, a new
105106
symlink will be created instead of copying the file it points to.
106107
107-
An in memory buffer size can be set with `length`; the default is 16 kB.
108-
108+
An in-memory buffer size in bytes can be set with `length`; the default is
109+
16 KiB.
109110
"""
110111
if _samefile(src, dst):
111112
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
@@ -242,6 +243,9 @@ def copy(src, dst, *, follow_symlinks=True, length=None):
242243
If source and destination are the same file, a SameFileError will be
243244
raised.
244245
246+
An in-memory buffer size in bytes can be set with `length`; the default is
247+
16 KiB.
248+
245249
"""
246250
if os.path.isdir(dst):
247251
dst = os.path.join(dst, os.path.basename(src))
@@ -258,6 +262,8 @@ def copy2(src, dst, *, follow_symlinks=True, length=None):
258262
If follow_symlinks is false, symlinks won't be followed. This
259263
resembles GNU's "cp -P src dst".
260264
265+
An in-memory buffer size in bytes can be set with `length`; the default is
266+
16 KiB.
261267
"""
262268
if os.path.isdir(dst):
263269
dst = os.path.join(dst, os.path.basename(src))

0 commit comments

Comments
 (0)