| 522 | |
| 523 | |
| 524 | class _GzipReader(_streams.DecompressReader): |
| 525 | def __init__(self, fp): |
| 526 | super().__init__(_PaddedFile(fp), zlib._ZlibDecompressor, |
| 527 | wbits=-zlib.MAX_WBITS) |
| 528 | # Set flag indicating start of a new member |
| 529 | self._new_member = True |
| 530 | self._last_mtime = None |
| 531 | |
| 532 | def _init_read(self): |
| 533 | self._crc = zlib.crc32(b"") |
| 534 | self._stream_size = 0 # Decompressed size of unconcatenated stream |
| 535 | |
| 536 | def _read_gzip_header(self): |
| 537 | last_mtime = _read_gzip_header(self._fp) |
| 538 | if last_mtime is None: |
| 539 | return False |
| 540 | self._last_mtime = last_mtime |
| 541 | return True |
| 542 | |
| 543 | def read(self, size=-1): |
| 544 | if size < 0: |
| 545 | return self.readall() |
| 546 | # size=0 is special because decompress(max_length=0) is not supported |
| 547 | if not size: |
| 548 | return b"" |
| 549 | |
| 550 | # For certain input data, a single |
| 551 | # call to decompress() may not return |
| 552 | # any data. In this case, retry until we get some data or reach EOF. |
| 553 | while True: |
| 554 | if self._decompressor.eof: |
| 555 | # Ending case: we've come to the end of a member in the file, |
| 556 | # so finish up this member, and read a new gzip header. |
| 557 | # Check the CRC and file size, and set the flag so we read |
| 558 | # a new member |
| 559 | self._read_eof() |
| 560 | self._new_member = True |
| 561 | self._decompressor = self._decomp_factory( |
| 562 | **self._decomp_args) |
| 563 | |
| 564 | if self._new_member: |
| 565 | # If the _new_member flag is set, we have to |
| 566 | # jump to the next member, if there is one. |
| 567 | self._init_read() |
| 568 | if not self._read_gzip_header(): |
| 569 | self._size = self._pos |
| 570 | return b"" |
| 571 | self._new_member = False |
| 572 | |
| 573 | # Read a chunk of data from the file |
| 574 | if self._decompressor.needs_input: |
| 575 | buf = self._fp.read(READ_BUFFER_SIZE) |
| 576 | uncompress = self._decompressor.decompress(buf, size) |
| 577 | else: |
| 578 | uncompress = self._decompressor.decompress(b"", size) |
| 579 | |
| 580 | if self._decompressor.unused_data != b"": |
| 581 | # Prepend the already read bytes to the fileobj so they can |
no outgoing calls
no test coverage detected
searching dependent graphs…