]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-108111: Flush gzip write buffer before seeking, fixing bad writes (GH-10834...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 24 Aug 2023 10:30:16 +0000 (03:30 -0700)
committerGitHub <noreply@github.com>
Thu, 24 Aug 2023 10:30:16 +0000 (12:30 +0200)
gh-108111: Flush gzip write buffer before seeking, fixing bad writes (GH-108341)
(cherry picked from commit 2eb60c1934f47671e6b3c9b90b6d9f1912d829a0)

Co-authored-by: Chris Markiewicz <effigies@gmail.com>
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Lib/gzip.py
Lib/test/test_gzip.py
Misc/ACKS
Misc/NEWS.d/next/Library/2023-08-22-17-27-12.gh-issue-108111.N7a4u_.rst [new file with mode: 0644]

index cf8b675064ce890c4d153008534ead576d672056..177f9080dc5af8b3fce7cff5a7089234cc674b3d 100644 (file)
@@ -401,6 +401,9 @@ class GzipFile(_compression.BaseStream):
 
     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
index b06b3b09411d62823a62cd336c2ea7ae661e78ed..128f933787a3f675c7a338774c77ecd83d891451 100644 (file)
@@ -665,6 +665,18 @@ class TestGzip(BaseTest):
         ]
         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):
index 645ad5b700baaac2272aae9be477c192f6080994..d16ec34a1f090640b254bdfd36e478dd2e41485d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1149,6 +1149,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 (file)
index 0000000..8eafa6c
--- /dev/null
@@ -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`.