summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-23 13:25:43 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-23 13:25:43 (GMT)
commitd4c2ac83944e48a02e5f11adba312d5b3b985150 (patch)
tree25dbafd2fa8763fa4388610ab9744c336433aac1 /Lib
parentf6e31b79a873cce039070289bcf1d6fe434cb19e (diff)
downloadcpython-d4c2ac83944e48a02e5f11adba312d5b3b985150.zip
cpython-d4c2ac83944e48a02e5f11adba312d5b3b985150.tar.gz
cpython-d4c2ac83944e48a02e5f11adba312d5b3b985150.tar.bz2
Issue #21560: An attempt to write a data of wrong type no longer cause
GzipFile corruption. Original patch by Wolfgang Maier.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/gzip.py4
-rw-r--r--Lib/test/test_gzip.py33
2 files changed, 35 insertions, 2 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index f934d4f..8b12225 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -339,9 +339,9 @@ class GzipFile(io.BufferedIOBase):
data = data.tobytes()
if len(data) > 0:
- self.size = self.size + len(data)
+ self.fileobj.write(self.compress.compress(data))
+ self.size += len(data)
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
- self.fileobj.write( self.compress.compress(data) )
self.offset += len(data)
return len(data)
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index b7a7e03..b417044 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -43,6 +43,14 @@ class BaseTest(unittest.TestCase):
class TestGzip(BaseTest):
+ def write_and_read_back(self, data, mode='b'):
+ b_data = bytes(data)
+ with gzip.GzipFile(self.filename, 'w'+mode) as f:
+ l = f.write(data)
+ self.assertEqual(l, len(b_data))
+ with gzip.GzipFile(self.filename, 'r'+mode) as f:
+ self.assertEqual(f.read(), b_data)
+
def test_write(self):
with gzip.GzipFile(self.filename, 'wb') as f:
f.write(data1 * 50)
@@ -57,6 +65,31 @@ class TestGzip(BaseTest):
# Test multiple close() calls.
f.close()
+ # The following test_write_xy methods test that write accepts
+ # the corresponding bytes-like object type as input
+ # and that the data written equals bytes(xy) in all cases.
+ def test_write_memoryview(self):
+ self.write_and_read_back(memoryview(data1 * 50))
+ m = memoryview(bytes(range(256)))
+ data = m.cast('B', shape=[8,8,4])
+ self.write_and_read_back(data)
+
+ def test_write_bytearray(self):
+ self.write_and_read_back(bytearray(data1 * 50))
+
+ def test_write_incompatible_type(self):
+ # Test that non-bytes-like types raise TypeError.
+ # Issue #21560: attempts to write incompatible types
+ # should not affect the state of the fileobject
+ with gzip.GzipFile(self.filename, 'wb') as f:
+ with self.assertRaises(TypeError):
+ f.write('a')
+ with self.assertRaises(TypeError):
+ f.write([1])
+ f.write(data1)
+ with gzip.GzipFile(self.filename, 'rb') as f:
+ self.assertEqual(f.read(), data1)
+
def test_read(self):
self.test_write()
# Try reading.