]> granicus.if.org Git - python/commitdiff
bpo-38319: Fix shutil._fastcopy_sendfile(): set sendfile() max block size (GH-16491...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 1 Oct 2019 07:55:02 +0000 (00:55 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Tue, 1 Oct 2019 07:55:02 +0000 (09:55 +0200)
(cherry picked from commit 94e165096fd65e8237e60de570fb609604ab94c9)

Co-authored-by: Giampaolo Rodola <g.rodola@gmail.com>
Lib/shutil.py
Lib/socket.py
Misc/NEWS.d/next/Library/2019-09-30-22-06-33.bpo-38319.5QjiDa.rst [new file with mode: 0644]

index 5c1255a6713de61ba8180eb6e298cdbe862e18e2..1e89256cc3444370a1421990e0962a4f984b2a32 100644 (file)
@@ -135,9 +135,13 @@ def _fastcopy_sendfile(fsrc, fdst):
     # should not make any difference, also in case the file content
     # changes while being copied.
     try:
-        blocksize = max(os.fstat(infd).st_size, 2 ** 23)  # min 8MB
-    except Exception:
-        blocksize = 2 ** 27  # 128MB
+        blocksize = max(os.fstat(infd).st_size, 2 ** 23)  # min 8MiB
+    except OSError:
+        blocksize = 2 ** 27  # 128MiB
+    # On 32-bit architectures truncate to 1GiB to avoid OverflowError,
+    # see bpo-38319.
+    if sys.maxsize < 2 ** 32:
+        blocksize = min(blocksize, 2 ** 30)
 
     offset = 0
     while True:
index af2ed0e76a4981f7ca6af12cef6e85676800321d..813f4ef5c3e1f579dd0d5f35e39a2eb6cf034595 100644 (file)
@@ -355,8 +355,8 @@ class socket(_socket.socket):
                 raise _GiveupOnSendfile(err)  # not a regular file
             if not fsize:
                 return 0  # empty file
-            blocksize = fsize if not count else count
-
+            # Truncate to 1GiB to avoid OverflowError, see bpo-38319.
+            blocksize = min(count or fsize, 2 ** 30)
             timeout = self.gettimeout()
             if timeout == 0:
                 raise ValueError("non-blocking sockets are not supported")
diff --git a/Misc/NEWS.d/next/Library/2019-09-30-22-06-33.bpo-38319.5QjiDa.rst b/Misc/NEWS.d/next/Library/2019-09-30-22-06-33.bpo-38319.5QjiDa.rst
new file mode 100644 (file)
index 0000000..376a9e4
--- /dev/null
@@ -0,0 +1,2 @@
+sendfile() used in socket and shutil modules was raising OverflowError for
+files >= 2GiB on 32-bit architectures.  (patch by Giampaolo Rodola)