summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/gzip.py3
-rw-r--r--Lib/test/test_gzip.py12
2 files changed, 15 insertions, 0 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index cf8b675..177f908 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -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
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index b06b3b0..128f933 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -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):