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>
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
]
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):
Vincent Marchetti
David Marek
Doug Marien
+Chris Markiewicz
Sven Marnach
John Marshall
Alex Martelli
--- /dev/null
+Fix a regression introduced in GH-101251 for 3.12, resulting in an incorrect
+offset calculation in :meth:`gzip.GzipFile.seek`.