gh-102956: Fix returning of empty byte strings after seek in zipfile … (#103565)

gh-102956: Fix returning of empty byte strings after seek in zipfile module. This was a regression in 3.12.0 due to a performance enhancement.
This commit is contained in:
Jokimax 2023-10-25 00:15:42 +03:00 committed by GitHub
parent e5168ff3f8
commit c73b0f3560
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 5 deletions

View file

@ -1136,8 +1136,12 @@ class ZipExtFile(io.BufferedIOBase):
read_offset = new_pos - curr_pos
buff_offset = read_offset + self._offset
if buff_offset >= 0 and buff_offset < len(self._readbuffer):
# Just move the _offset index if the new position is in the _readbuffer
self._offset = buff_offset
read_offset = 0
# Fast seek uncompressed unencrypted file
if self._compress_type == ZIP_STORED and self._decrypter is None and read_offset > 0:
elif self._compress_type == ZIP_STORED and self._decrypter is None and read_offset > 0:
# disable CRC checking after first seeking - it would be invalid
self._expected_crc = None
# seek actual file taking already buffered data into account
@ -1148,10 +1152,6 @@ class ZipExtFile(io.BufferedIOBase):
# flush read buffer
self._readbuffer = b''
self._offset = 0
elif buff_offset >= 0 and buff_offset < len(self._readbuffer):
# Just move the _offset index if the new position is in the _readbuffer
self._offset = buff_offset
read_offset = 0
elif read_offset < 0:
# Position is before the current position. Reset the ZipExtFile
self._fileobj.seek(self._orig_compress_start)