Skip to content

Commit a32f9a2

Browse files
committed
Merged revisions 77798 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77798 | antoine.pitrou | 2010-01-27 21:59:50 +0100 (mer., 27 janv. 2010) | 8 lines Issue #7610: Reworked implementation of the internal :class:`zipfile.ZipExtFile` class used to represent files stored inside an archive. The new implementation is significantly faster and can be wrapped in a :class:`io.BufferedReader` object for more speedups. It also solves an issue where interleaved calls to `read()` and `readline()` give wrong results. Patch by Nir Aides. ........
1 parent 176d6c4 commit a32f9a2

File tree

3 files changed

+242
-192
lines changed

3 files changed

+242
-192
lines changed

Lib/test/test_zipfile.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,45 @@ def test_random_open_stored(self):
168168
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
169169
self.zip_random_open_test(f, zipfile.ZIP_STORED)
170170

171+
def test_univeral_readaheads(self):
172+
f = io.BytesIO()
173+
174+
data = b'a\r\n' * 16 * 1024
175+
zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
176+
zipfp.writestr(TESTFN, data)
177+
zipfp.close()
178+
179+
data2 = b''
180+
zipfp = zipfile.ZipFile(f, 'r')
181+
zipopen = zipfp.open(TESTFN, 'rU')
182+
for line in zipopen:
183+
data2 += line
184+
zipfp.close()
185+
186+
self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
187+
188+
def zip_readline_read_test(self, f, compression):
189+
self.make_test_archive(f, compression)
190+
191+
# Read the ZIP archive
192+
zipfp = zipfile.ZipFile(f, "r")
193+
zipopen = zipfp.open(TESTFN)
194+
195+
data = b''
196+
while True:
197+
read = zipopen.readline()
198+
if not read:
199+
break
200+
data += read
201+
202+
read = zipopen.read(100)
203+
if not read:
204+
break
205+
data += read
206+
207+
self.assertEqual(data, self.data)
208+
zipfp.close()
209+
171210
def zip_readline_test(self, f, compression):
172211
self.make_test_archive(f, compression)
173212

@@ -195,6 +234,11 @@ def zip_iterlines_test(self, f, compression):
195234
for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
196235
self.assertEqual(zipline, line + '\n')
197236

237+
def test_readline_read_stored(self):
238+
# Issue #7610: calls to readline() interleaved with calls to read().
239+
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
240+
self.zip_readline_read_test(f, zipfile.ZIP_STORED)
241+
198242
def test_readline_stored(self):
199243
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
200244
self.zip_readline_test(f, zipfile.ZIP_STORED)
@@ -223,6 +267,12 @@ def test_random_open_deflated(self):
223267
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
224268
self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
225269

270+
@skipUnless(zlib, "requires zlib")
271+
def test_readline_read_deflated(self):
272+
# Issue #7610: calls to readline() interleaved with calls to read().
273+
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
274+
self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
275+
226276
@skipUnless(zlib, "requires zlib")
227277
def test_readline_deflated(self):
228278
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
@@ -1067,6 +1117,29 @@ def read_test(self, f, compression):
10671117
zipdata = zipfp.open(fn, "rU").read()
10681118
self.assertEqual(self.arcdata[sep], zipdata)
10691119

1120+
def readline_read_test(self, f, compression):
1121+
self.make_test_archive(f, compression)
1122+
1123+
# Read the ZIP archive
1124+
zipfp = zipfile.ZipFile(f, "r")
1125+
for sep, fn in self.arcfiles.items():
1126+
zipopen = zipfp.open(fn, "rU")
1127+
data = b''
1128+
while True:
1129+
read = zipopen.readline()
1130+
if not read:
1131+
break
1132+
data += read
1133+
1134+
read = zipopen.read(5)
1135+
if not read:
1136+
break
1137+
data += read
1138+
1139+
self.assertEqual(data, self.arcdata['\n'])
1140+
1141+
zipfp.close()
1142+
10701143
def readline_test(self, f, compression):
10711144
self.make_test_archive(f, compression)
10721145

@@ -1101,6 +1174,11 @@ def test_read_stored(self):
11011174
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
11021175
self.read_test(f, zipfile.ZIP_STORED)
11031176

1177+
def test_readline_read_stored(self):
1178+
# Issue #7610: calls to readline() interleaved with calls to read().
1179+
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1180+
self.readline_read_test(f, zipfile.ZIP_STORED)
1181+
11041182
def test_readline_stored(self):
11051183
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
11061184
self.readline_test(f, zipfile.ZIP_STORED)
@@ -1118,6 +1196,12 @@ def test_read_deflated(self):
11181196
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
11191197
self.read_test(f, zipfile.ZIP_DEFLATED)
11201198

1199+
@skipUnless(zlib, "requires zlib")
1200+
def test_readline_read_deflated(self):
1201+
# Issue #7610: calls to readline() interleaved with calls to read().
1202+
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1203+
self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1204+
11211205
@skipUnless(zlib, "requires zlib")
11221206
def test_readline_deflated(self):
11231207
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):

0 commit comments

Comments
 (0)