Skip to content

Commit c09b4a0

Browse files
committed
fix(handlers): improve gzip reader according to recent changes in cpython.
The gzip._GzipReader we're inheriting from had some changes to stay up to date with changes in zlib library and to perform some optimization. Specifically: - GzipFile.read has been optimized. There is no longer a unconsumed_tail member to write back to padded file. This is instead handled by the ZlibDecompressor itself, which has an internal buffer. - _add_read_data has been inlined, as it was just two calls. We've adapted our own code to reflect these changes. More info: python/cpython#97664
1 parent 43dc0c7 commit c09b4a0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

unblob/handlers/compression/_gzip_reader.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import gzip
2+
import zlib
23

34
from ...file_utils import DEFAULT_BUFSIZE
45

@@ -10,14 +11,19 @@ def read_header(self):
1011
self._init_read()
1112
return self._read_gzip_header()
1213

14+
def _add_read_data(self, data):
15+
self._crc = zlib.crc32(data, self._crc)
16+
self._stream_size = self._stream_size + len(data)
17+
1318
def read(self):
1419
uncompress = b""
1520

1621
while True:
1722
buf = self._fp.read(DEFAULT_BUFSIZE)
1823

1924
uncompress = self._decompressor.decompress(buf, DEFAULT_BUFSIZE)
20-
self._fp.prepend(self._decompressor.unconsumed_tail)
25+
if hasattr(self._decompressor, "unconsumed_tail"):
26+
self._fp.prepend(self._decompressor.unconsumed_tail)
2127
self._fp.prepend(self._decompressor.unused_data)
2228

2329
if uncompress != b"":

0 commit comments

Comments
 (0)