[Python-checkins] r84739 - in python/branches/release27-maint: Lib/test/test_zipfile.py Lib/zipfile.py Misc/NEWS

antoine.pitrou python-checkins at python.org
Sun Sep 12 16:56:27 CEST 2010


Author: antoine.pitrou
Date: Sun Sep 12 16:56:27 2010
New Revision: 84739

Log:
Merged revisions 84737 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r84737 | antoine.pitrou | 2010-09-12 16:51:20 +0200 (dim., 12 sept. 2010) | 4 lines
  
  Issue #9837: The read() method of ZipExtFile objects (as returned by
  ZipFile.open()) could return more bytes than requested.
........


Modified:
   python/branches/release27-maint/   (props changed)
   python/branches/release27-maint/Lib/test/test_zipfile.py
   python/branches/release27-maint/Lib/zipfile.py
   python/branches/release27-maint/Misc/NEWS

Modified: python/branches/release27-maint/Lib/test/test_zipfile.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_zipfile.py	(original)
+++ python/branches/release27-maint/Lib/test/test_zipfile.py	Sun Sep 12 16:56:27 2010
@@ -929,6 +929,26 @@
     def test_read_with_bad_crc_deflated(self):
         self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
 
+    def check_read_return_size(self, compression):
+        # Issue #9837: ZipExtFile.read() shouldn't return more bytes
+        # than requested.
+        for test_size in (1, 4095, 4096, 4097, 16384):
+            file_size = test_size + 1
+            junk = b''.join(struct.pack('B', randint(0, 255))
+                            for x in range(file_size))
+            with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
+                zipf.writestr('foo', junk)
+                with zipf.open('foo', 'r') as fp:
+                    buf = fp.read(test_size)
+                    self.assertEqual(len(buf), test_size)
+
+    def test_read_return_size_stored(self):
+        self.check_read_return_size(zipfile.ZIP_STORED)
+
+    @skipUnless(zlib, "requires zlib")
+    def test_read_return_size_deflated(self):
+        self.check_read_return_size(zipfile.ZIP_DEFLATED)
+
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)

Modified: python/branches/release27-maint/Lib/zipfile.py
==============================================================================
--- python/branches/release27-maint/Lib/zipfile.py	(original)
+++ python/branches/release27-maint/Lib/zipfile.py	Sun Sep 12 16:56:27 2010
@@ -565,17 +565,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 = ''
-        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:

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Sun Sep 12 16:56:27 2010
@@ -43,6 +43,9 @@
 Library
 -------
 
+- Issue #9837: The read() method of ZipExtFile objects (as returned by
+  ZipFile.open()) could return more bytes than requested.
+
 - Issue #9825: removed __del__ from the definition of collections.OrderedDict.
   This prevents user-created self-referencing ordered dictionaries from
   becoming permanently uncollectable GC garbage.  The downside is that


More information about the Python-checkins mailing list