diff Lib/zipfile.py @ 64672:fb90f2ff1764

Issue #9837: The read() method of ZipExtFile objects (as returned by ZipFile.open()) could return more bytes than requested.
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 12 Sep 2010 14:51:20 +0000
parents fd4606efc700
children ba6581ecb6f1
line wrap: on
line diff
--- a/Lib/zipfile.py	Sun Sep 12 13:55:02 2010 +0000
+++ b/Lib/zipfile.py	Sun Sep 12 14:51:20 2010 +0000
@@ -564,17 +564,20 @@
         """Read and return up to n bytes.
         If the argument is omitted, None, or negative, data is read and returned until EOF is reached..
         """
-
         buf = b''
-        while n < 0 or n is None or n > len(buf):
-            data = self.read1(n)
+        if n is None:
+            n = -1
+        while True:
+            if n < 0:
+                data = self.read1(n)
+            elif n > len(buf):
+                data = self.read1(n - len(buf))
+            else:
+                return buf
             if len(data) == 0:
                 return buf
-
             buf += data
 
-        return buf
-
     def _update_crc(self, newdata, eof):
         # Update the CRC using the given data.
         if self._expected_crc is None: