8000 gh-135648: Document that `shutil.copyfileobj` doesn't flush (#135737) · python/cpython@34393cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 34393cb

Browse files
authored
gh-135648: Document that shutil.copyfileobj doesn't flush (#135737)
Adds a note about flush/close on copyfileobj, and updates the Emscripten build script to follow documented advice.
1 parent 2793b68 commit 34393cb

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Doc/library/shutil.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ Directory and files operations
4747
0, only the contents from the current file position to the end of the file will
4848
be copied.
4949

50+
:func:`copyfileobj` will *not* guarantee that the destination stream has
51+
been flushed on completion of the copy. If you want to read from the
52+
destination at the completion of the copy operation (for example, reading
53+
the contents of a temporary file that has been copied from a HTTP stream),
54+
you must ensure that you have called :func:`~io.IOBase.flush` or
55+
:func:`~io.IOBase.close` on the file-like object before attempting to read
56+
the destination file.
5057

5158
.. function:: copyfile(src, dst, *, follow_symlinks=True)
5259

Tools/wasm/emscripten/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,12 @@ def make_build_python(context, working_dir):
167167
@subdir(HOST_BUILD_DIR, clean_ok=True)
168168
def make_emscripten_libffi(context, working_dir):
169169
shutil.rmtree(working_dir / "libffi-3.4.6", ignore_errors=True)
170-
with tempfile.NamedTemporaryFile(suffix=".tar.gz") as tmp_file:
170+
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete_on_close=False) as tmp_file:
171171
with urlopen(
172172
"https://github.com/libffi/libffi/releases/download/v3.4.6/libffi-3.4.6.tar.gz"
173173
) as response:
174174
shutil.copyfileobj(response, tmp_file)
175+
tmp_file.close()
175176
shutil.unpack_archive(tmp_file.name, working_dir)
176177
call(
177178
[EMSCRIPTEN_DIR / "make_libffi.sh"],

0 commit comments

Comments
 (0)
0