Repository navigation
GzipFile.seek makes invalid write if buffer is not flushed in Python 3.12rc1 #108111
Copy link
Copy link
Closed
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Aug 18, 2023 Looking at #101251, which is where I expect this was introduced, I suspect this is the fix:
def seek(self, offset, whence=io.SEEK_SET): if self.mode == WRITE: + self._check_not_closed() + self._buffer.flush() if whence != io.SEEK_SET: if whence == io.SEEK_CUR:Here:
Lines 402 to 420 in daed54d
def seek(self, offset, whence=io.SEEK_SET): if self.mode == WRITE: if whence != io.SEEK_SET: if whence == io.SEEK_CUR: offset = self.offset + offset else: raise ValueError('Seek from end not supported') if offset < self.offset: raise OSError('Negative seek in write mode') count = offset - self.offset chunk = b'\0' * self._buffer_size for i in range(count // self._buffer_size): self.write(chunk) self.write(b'\0' * (count % self._buffer_size)) elif self.mode == READ: self._check_not_closed() return self._buffer.seek(offset, whence) return self.offset - changed the title
[-]Seeking to current position in writable gzip stream incorrectly appends zero bytes[/-][+]GzipFile.seek makes invalid write if buffer is not flushed in Python 3.12rc1[/+]on Aug 22, 2023 - added a commit that references this issue
on Aug 24, 2023 - added a commit that references this issue
on Aug 24, 2023 Thank you!
Reacted by Chris Markiewicz- added a commit that references this issue
on Dec 14, 2023
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Bug report
Checklist
and am confident this bug has not been reported before
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.12.0rc1 (main, Aug 16 2023, 05:03:59) [GCC 12.2.0]
A clear and concise description of the bug:
I have code that writes out sections of a data file in chunks, and uses seeks to ensure that the position is correct before writing.
In the following example, I write 5 bytes, seek to position 5 and write five more bytes. If I flush the buffer, the result is as expected. If I do not, 5 null bytes are written between the two groups of intended bytes.
In the case where I seek but don't tell, I get spurious
\x00bytes:Here is the output in Python 3.10.10:
Linked PRs