Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
test: GzipFile.readinto{,1}
  • Loading branch information
effigies committed Feb 12, 2025
commit b2601d420f7c4fb22beb6f162e244e57810e7e84
32 changes: 32 additions & 0 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,38 @@ def test_read1(self):
self.assertEqual(f.tell(), nread)
self.assertEqual(b''.join(blocks), data1 * 50)

def test_readinto(self):
# 10MB of uncompressible data to ensure multiple reads
large_data = os.urandom(10 * 2**20)
with gzip.GzipFile(self.filename, 'wb') as f:
f.write(large_data)

buf = bytearray(len(large_data))
with gzip.GzipFile(self.filename, 'r') as f:
nbytes = f.readinto(buf)
self.assertEqual(nbytes, len(large_data))
self.assertEqual(buf, large_data)

def test_readinto1(self):
# 10MB of uncompressible data to ensure multiple reads
large_data = os.urandom(10 * 2**20)
with gzip.GzipFile(self.filename, 'wb') as f:
f.write(large_data)

nread = 0
buf = bytearray(len(large_data))
memview = memoryview(buf) # Simplifies slicing
with gzip.GzipFile(self.filename, 'r') as f:
for count in range(200):
nbytes = f.readinto1(memview[nread:])
Comment thread
effigies marked this conversation as resolved.
if not nbytes:
break
nread += nbytes
self.assertEqual(f.tell(), nread)
self.assertEqual(buf, large_data)
# readinto1() should require multiple loops
self.assertGreater(count, 1)

@bigmemtest(size=_4G, memuse=1)
def test_read_large(self, size):
# Read chunk size over UINT_MAX should be supported, despite zlib's
Expand Down