From e3766fdf4fecf5ca419a98f8ad7927f36295b5f8 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 20 Jun 2025 10:47:11 +0800 Subject: [PATCH 1/3] Add note about flush/close on copyfileobj. --- Doc/library/shutil.rst | 7 +++++++ .../2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst | 2 ++ 2 files changed, 9 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst index e7c4c4f46bd011..2dde40c9d92f45 100644 --- a/Doc/library/shutil.rst +++ b/Doc/library/shutil.rst @@ -47,6 +47,13 @@ Directory and files operations 0, only the contents from the current file position to the end of the file will be copied. + :func:`copyfileobj` will *not* guarantee that the destination stream has + been flushed on completion of the copy. If you want to read from the + destination at the completion of the copy operation (for example, reading + the contents of a temporary file that has been copied from a HTTP stream), + you must ensure that you have called :func:`~io.IOBase.flush` or + :func:`~io.IOBase.close` on the file-like object before attempting to read + the destination file. .. function:: copyfile(src, dst, *, follow_symlinks=True) diff --git a/Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst b/Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst new file mode 100644 index 00000000000000..90a9a21e6d3a92 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst @@ -0,0 +1,2 @@ +The documentation for :func:`shutil.copyfileobj` now warns the user of the +need to close or flush the destination stream on completion. From 6f0d22db18436d672f334f468c197d359c82814a Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 20 Jun 2025 10:47:33 +0800 Subject: [PATCH 2/3] Update emscripten build script to follow documented advice. --- Tools/wasm/emscripten/__main__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/wasm/emscripten/__main__.py b/Tools/wasm/emscripten/__main__.py index 849bd5de44eb7b..c0d58aeaadd2cf 100644 --- a/Tools/wasm/emscripten/__main__.py +++ b/Tools/wasm/emscripten/__main__.py @@ -167,11 +167,12 @@ def make_build_python(context, working_dir): @subdir(HOST_BUILD_DIR, clean_ok=True) def make_emscripten_libffi(context, working_dir): shutil.rmtree(working_dir / "libffi-3.4.6", ignore_errors=True) - with tempfile.NamedTemporaryFile(suffix=".tar.gz") as tmp_file: + with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete_on_close=False) as tmp_file: with urlopen( "https://github.com/libffi/libffi/releases/download/v3.4.6/libffi-3.4.6.tar.gz" ) as response: shutil.copyfileobj(response, tmp_file) + tmp_file.close() shutil.unpack_archive(tmp_file.name, working_dir) call( [EMSCRIPTEN_DIR / "make_libffi.sh"], From 2ced62bed597ab5648c41b5dbed0ab56fd458011 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 21 Jun 2025 12:58:36 +0800 Subject: [PATCH 3/3] Remove NEWS entry. --- .../2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst diff --git a/Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst b/Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst deleted file mode 100644 index 90a9a21e6d3a92..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2025-06-20-10-27-11.gh-issue-135648.xCC_zC.rst +++ /dev/null @@ -1,2 +0,0 @@ -The documentation for :func:`shutil.copyfileobj` now warns the user of the -need to close or flush the destination stream on completion.