From 65524a622e0e4d10b8498c7b8e1478239f5f478c Mon Sep 17 00:00:00 2001 From: StealthCentral <35307184+StealthCentral@users.noreply.github.com> Date: Wed, 10 Jan 2018 11:56:45 -0600 Subject: [PATCH] Update shutil.py improved copyfileobj to use less memory --- Lib/shutil.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Lib/shutil.py b/Lib/shutil.py index bd4760f1a7409d5..39cf3c6d6123e85 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -75,11 +75,18 @@ class RegistryError(Exception): def copyfileobj(fsrc, fdst, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" + buf = memoryview(bytearray(length)) + # localize + readinto = fsrc.readinto + write = fdst.write + # run loop while 1: - buf = fsrc.read(length) - if not buf: + recv_len = readinto(buf) + if recv_len < length: + # write remaining content if any. + write(buf[:recv_len]) break - fdst.write(buf) + write(buf) def _samefile(src, dst): # Macintosh, Unix.