Skip to content

Commit b2601d4

Browse files
committed
test: GzipFile.readinto{,1}
1 parent 5431ada commit b2601d4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Lib/test/test_gzip.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,38 @@ def test_read1(self):
141141
self.assertEqual(f.tell(), nread)
142142
self.assertEqual(b''.join(blocks), data1 * 50)
143143

144+
def test_readinto(self):
145+
# 10MB of uncompressible data to ensure multiple reads
146+
large_data = os.urandom(10 * 2**20)
147+
with gzip.GzipFile(self.filename, 'wb') as f:
148+
f.write(large_data)
149+
150+
buf = bytearray(len(large_data))
151+
with gzip.GzipFile(self.filename, 'r') as f:
152+
nbytes = f.readinto(buf)
153+
self.assertEqual(nbytes, len(large_data))
154+
self.assertEqual(buf, large_data)
155+
156+
def test_readinto1(self):
157+
# 10MB of uncompressible data to ensure multiple reads
158+
large_data = os.urandom(10 * 2**20)
159+
with gzip.GzipFile(self.filename, 'wb') as f:
160+
f.write(large_data)
161+
162+
nread = 0
163+
buf = bytearray(len(large_data))
164+
memview = memoryview(buf) # Simplifies slicing
165+
with gzip.GzipFile(self.filename, 'r') as f:
166+
for count in range(200):
167+
nbytes = f.readinto1(memview[nread:])
168+
if not nbytes:
169+
break
170+
nread += nbytes
171+
self.assertEqual(f.tell(), nread)
172+
self.assertEqual(buf, large_data)
173+
# readinto1() should require multiple loops
174+
self.assertGreater(count, 1)
175+
144176
@bigmemtest(size=_4G, memuse=1)
145177
def test_read_large(self, size):
146178
# Read chunk size over UINT_MAX should be supported, despite zlib's

0 commit comments

Comments
 (0)