From 2eb60c1934f47671e6b3c9b90b6d9f1912d829a0 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Thu, 24 Aug 2023 04:23:01 -0400 Subject: [PATCH] gh-108111: Flush gzip write buffer before seeking, fixing bad writes (#108341) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ɓukasz Langa --- Lib/gzip.py | 3 +++ Lib/test/test_gzip.py | 12 ++++++++++++ Misc/ACKS | 1 + .../2023-08-22-17-27-12.gh-issue-108111.N7a4u_.rst | 2 ++ 4 files changed, 18 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-08-22-17-27-12.gh-issue-108111.N7a4u_.rst diff --git a/Lib/gzip.py b/Lib/gzip.py index cf8b675064ce89..177f9080dc5af8 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -401,6 +401,9 @@ def seekable(self): def seek(self, offset, whence=io.SEEK_SET): if self.mode == WRITE: + self._check_not_closed() + # Flush buffer to ensure validity of self.offset + self._buffer.flush() if whence != io.SEEK_SET: if whence == io.SEEK_CUR: offset = self.offset + offset diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index b06b3b09411d62..128f933787a3f6 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -665,6 +665,18 @@ def flush(self, mode=-1): ] self.assertEqual(fc.modes, expected_modes) + def test_write_seek_write(self): + # Make sure that offset is up-to-date before seeking + # See issue GH-108111 + b = io.BytesIO() + message = b"important message here." + with gzip.GzipFile(fileobj=b, mode='w') as f: + f.write(message) + f.seek(len(message)) + f.write(message) + data = b.getvalue() + self.assertEqual(gzip.decompress(data), message * 2) + class TestOpen(BaseTest): def test_binary_modes(self): diff --git a/Misc/ACKS b/Misc/ACKS index 475c6ec3dab73b..70c4ea37adda75 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1150,6 +1150,7 @@ Colin Marc Vincent Marchetti David Marek Doug Marien +Chris Markiewicz Sven Marnach John Marshall Alex Martelli diff --git a/Misc/NEWS.d/next/Library/2023-08-22-17-27-12.gh-issue-108111.N7a4u_.rst b/Misc/NEWS.d/next/Library/2023-08-22-17-27-12.gh-issue-108111.N7a4u_.rst new file mode 100644 index 00000000000000..8eafa6cfbbf8cf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-08-22-17-27-12.gh-issue-108111.N7a4u_.rst @@ -0,0 +1,2 @@ +Fix a regression introduced in GH-101251 for 3.12, resulting in an incorrect +offset calculation in :meth:`gzip.GzipFile.seek`.