[Python-checkins] cpython (3.3): Issue #19053: ZipExtFile.read1() with non-zero argument no more returns empty

serhiy.storchaka python-checkins at python.org
Fri Sep 27 21:15:21 CEST 2013


http://hg.python.org/cpython/rev/460b0ccbab7f
changeset:   85813:460b0ccbab7f
branch:      3.3
parent:      85807:ac5343148fb3
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Sep 27 22:11:57 2013 +0300
summary:
  Issue #19053: ZipExtFile.read1() with non-zero argument no more returns empty
bytes until end of data.

files:
  Lib/test/test_zipfile.py |  39 ++++++++++++++++++++++++++++
  Lib/zipfile.py           |  23 ++++++++++-----
  Misc/NEWS                |   3 ++
  3 files changed, 57 insertions(+), 8 deletions(-)


diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -158,6 +158,45 @@
         for f in get_files(self):
             self.zip_random_open_test(f, self.compression)
 
+    def zip_read1_test(self, f, compression):
+        self.make_test_archive(f, compression)
+
+        # Read the ZIP archive
+        with zipfile.ZipFile(f, "r") as zipfp, \
+             zipfp.open(TESTFN) as zipopen:
+            zipdata = []
+            while True:
+                read_data = zipopen.read1(-1)
+                if not read_data:
+                    break
+                zipdata.append(read_data)
+
+        self.assertEqual(b''.join(zipdata), self.data)
+
+    def test_read1(self):
+        for f in get_files(self):
+            self.zip_read1_test(f, self.compression)
+
+    def zip_read1_10_test(self, f, compression):
+        self.make_test_archive(f, compression)
+
+        # Read the ZIP archive
+        with zipfile.ZipFile(f, "r") as zipfp, \
+             zipfp.open(TESTFN) as zipopen:
+            zipdata = []
+            while True:
+                read_data = zipopen.read1(10)
+                self.assertLessEqual(len(read_data), 10)
+                if not read_data:
+                    break
+                zipdata.append(read_data)
+
+        self.assertEqual(b''.join(zipdata), self.data)
+
+    def test_read1_10(self):
+        for f in get_files(self):
+            self.zip_read1_10_test(f, self.compression)
+
     def zip_readline_read_test(self, f, compression):
         self.make_test_archive(f, compression)
 
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -785,8 +785,11 @@
             buf = self._readbuffer[self._offset:]
             self._readbuffer = b''
             self._offset = 0
-            data = self._read1(self.MAX_N)
-            buf += data
+            while not self._eof:
+                data = self._read1(self.MAX_N)
+                if data:
+                    buf += data
+                    break
             return buf
 
         end = n + self._offset
@@ -800,12 +803,16 @@
         self._readbuffer = b''
         self._offset = 0
         if n > 0:
-            data = self._read1(n)
-            if n < len(data):
-                self._readbuffer = data
-                self._offset = n
-                data = data[:n]
-            buf += data
+            while not self._eof:
+                data = self._read1(n)
+                if n < len(data):
+                    self._readbuffer = data
+                    self._offset = n
+                    buf += data[:n]
+                    break
+                if data:
+                    buf += data
+                    break
         return buf
 
     def _read1(self, n):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -71,6 +71,9 @@
 Library
 -------
 
+- Issue #19053: ZipExtFile.read1() with non-zero argument no more returns empty
+  bytes until end of data.
+
 - Issue #19028: Fixed tkinter.Tkapp.merge() for non-string arguments.
 
 - Issue #3015: Fixed tkinter with wantobject=False.  Any Tcl command call

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list