mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
[3.12] gh-108111: Flush gzip write buffer before seeking, fixing bad writes (GH-108341) (#108402)
gh-108111: Flush gzip write buffer before seeking, fixing bad writes (GH-108341)
(cherry picked from commit 2eb60c1934
)
Co-authored-by: Chris Markiewicz <effigies@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
parent
459f24aef0
commit
20357ed2a4
4 changed files with 18 additions and 0 deletions
|
@ -401,6 +401,9 @@ class GzipFile(_compression.BaseStream):
|
||||||
|
|
||||||
def seek(self, offset, whence=io.SEEK_SET):
|
def seek(self, offset, whence=io.SEEK_SET):
|
||||||
if self.mode == WRITE:
|
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_SET:
|
||||||
if whence == io.SEEK_CUR:
|
if whence == io.SEEK_CUR:
|
||||||
offset = self.offset + offset
|
offset = self.offset + offset
|
||||||
|
|
|
@ -665,6 +665,18 @@ class TestGzip(BaseTest):
|
||||||
]
|
]
|
||||||
self.assertEqual(fc.modes, expected_modes)
|
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):
|
class TestOpen(BaseTest):
|
||||||
def test_binary_modes(self):
|
def test_binary_modes(self):
|
||||||
|
|
|
@ -1149,6 +1149,7 @@ Colin Marc
|
||||||
Vincent Marchetti
|
Vincent Marchetti
|
||||||
David Marek
|
David Marek
|
||||||
Doug Marien
|
Doug Marien
|
||||||
|
Chris Markiewicz
|
||||||
Sven Marnach
|
Sven Marnach
|
||||||
John Marshall
|
John Marshall
|
||||||
Alex Martelli
|
Alex Martelli
|
||||||
|
|
|
@ -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`.
|
Loading…
Add table
Add a link
Reference in a new issue