From 505f4547cb150cff2f6710f76167de42524f7c5a Mon Sep 17 00:00:00 2001 From: Casper Verbuyst Date: Wed, 21 Oct 2026 19:24:59 +0200 Subject: [PATCH 1/5] Fix the gzip write-mode seek return value Signed-off-by: Casper Verbuyst --- Lib/gzip.py | 3 +-- Lib/test/test_gzip.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Lib/gzip.py b/Lib/gzip.py index 247efc01527bb14..976837826b0fea7 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -456,12 +456,11 @@ def seek(self, offset, whence=io.SEEK_SET): for i in range(count // self._buffer_size): self.write(chunk) self.write(b'\0' * (count % self._buffer_size)) + return offset elif self.mode == READ: self._check_not_closed() return self._buffer.seek(offset, whence) - return self.offset - def readline(self, size=-1): self._check_not_closed() return self._buffer.readline(size) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 8bc8e507683cb50..7da6bf804ee3bdf 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -317,9 +317,26 @@ def test_seek_write(self): # Try seek, write test with gzip.GzipFile(self.filename, 'w') as f: for pos in range(0, 256, 16): - f.seek(pos) + self.assertEqual(f.seek(pos), pos) f.write(b'GZ\n') + def test_seek_write_return_value(self): + for whence in (io.SEEK_SET, io.SEEK_CUR): + for padding in (16, 1024 * 1024): + with self.subTest(whence=whence, padding=padding): + data = io.BytesIO() + with gzip.GzipFile(fileobj=data, mode='wb') as f: + f.write(b'header') + position = len(b'header') + padding + offset = position if whence == io.SEEK_SET else padding + self.assertEqual(f.seek(offset, whence), position) + f.write(b'record') + self.assertEqual(f.seek(0, io.SEEK_CUR), + position + len(b'record')) + self.assertEqual(f.tell(), position + len(b'record')) + self.assertEqual(gzip.decompress(data.getvalue()), + b'header' + b'\0' * padding + b'record') + def test_mode(self): self.test_write() with gzip.GzipFile(self.filename, 'r') as f: From bbe01bf864db215491b2944605de096d458007ef Mon Sep 17 00:00:00 2001 From: Casper Verbuyst Date: Wed, 21 Oct 2026 19:30:16 +0200 Subject: [PATCH 2/5] gh-160071: Add NEWS entry for gzip seek return value Signed-off-by: Casper Verbuyst --- .../next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst diff --git a/Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst b/Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst new file mode 100644 index 000000000000000..4bc36b987c3426c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst @@ -0,0 +1,2 @@ +Fix :meth:`gzip.GzipFile.seek` returning a stale position when forward seeking +in write mode leaves zero padding in the write buffer. From bc1bbbeb5391f3127256f94c6eba66bf40b91c09 Mon Sep 17 00:00:00 2001 From: Casper Verbuyst Date: Wed, 21 Oct 2026 19:55:42 +0200 Subject: [PATCH 3/5] gh-160071: Fix gzip NEWS documentation reference Signed-off-by: Casper Verbuyst --- .../Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst b/Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst index 4bc36b987c3426c..31f212a56031241 100644 --- a/Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst +++ b/Misc/NEWS.d/next/Library/2026-10-21-19-29-18.gh-issue-160071.nG4pTs.rst @@ -1,2 +1,2 @@ -Fix :meth:`gzip.GzipFile.seek` returning a stale position when forward seeking -in write mode leaves zero padding in the write buffer. +Fix :class:`gzip.GzipFile` returning a stale position from ``seek()`` when +forward seeking in write mode leaves zero padding in the write buffer. From 142f74a47dd624eaaf0f84c8314d1cbab2964e4a Mon Sep 17 00:00:00 2001 From: repzakske <31244169+repzakske@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:56:35 +0200 Subject: [PATCH 4/5] Update Lib/test/test_gzip.py Co-authored-by: Neko Asakura --- Lib/test/test_gzip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 7da6bf804ee3bdf..01bc88d6b1bfa46 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -322,7 +322,7 @@ def test_seek_write(self): def test_seek_write_return_value(self): for whence in (io.SEEK_SET, io.SEEK_CUR): - for padding in (16, 1024 * 1024): + for padding in (16, gzip._WRITE_BUFFER_SIZE + 16): with self.subTest(whence=whence, padding=padding): data = io.BytesIO() with gzip.GzipFile(fileobj=data, mode='wb') as f: From fd76a9f2c03365c56842a93393157134d0044ccf Mon Sep 17 00:00:00 2001 From: Casper Verbuyst Date: Wed, 21 Oct 2026 20:59:26 +0200 Subject: [PATCH 5/5] gh-160071: Remove duplicate gzip position assertion Signed-off-by: Casper Verbuyst --- Lib/test/test_gzip.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 01bc88d6b1bfa46..30838f0d00b6125 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -331,8 +331,6 @@ def test_seek_write_return_value(self): offset = position if whence == io.SEEK_SET else padding self.assertEqual(f.seek(offset, whence), position) f.write(b'record') - self.assertEqual(f.seek(0, io.SEEK_CUR), - position + len(b'record')) self.assertEqual(f.tell(), position + len(b'record')) self.assertEqual(gzip.decompress(data.getvalue()), b'header' + b'\0' * padding + b'record')