[Python-checkins] cpython (2.7): Issue #21560: An attempt to write a data of wrong type no longer cause
serhiy.storchaka
python-checkins at python.org
Mon Mar 23 14:28:02 CET 2015
https://hg.python.org/cpython/rev/4dfe0634d11a
changeset: 95141:4dfe0634d11a
branch: 2.7
parent: 95137:000814bbc899
user: Serhiy Storchaka <storchaka at gmail.com>
date: Mon Mar 23 15:25:18 2015 +0200
summary:
Issue #21560: An attempt to write a data of wrong type no longer cause
GzipFile corruption. Original patch by Wolfgang Maier.
files:
Lib/gzip.py | 4 ++--
Lib/test/test_gzip.py | 27 +++++++++++++++++++++++++++
Misc/NEWS | 3 +++
3 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -238,9 +238,9 @@
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) & 0xffffffffL
- 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
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -30,6 +30,14 @@
def tearDown(self):
test_support.unlink(self.filename)
+ def write_and_read_back(self, data, mode='b'):
+ b_data = memoryview(data).tobytes()
+ 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)
+
@test_support.requires_unicode
def test_unicode_filename(self):
unicode_filename = test_support.TESTFN_UNICODE
@@ -60,6 +68,25 @@
# 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))
+
+ 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(UnicodeEncodeError):
+ f.write(u'\xff')
+ 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.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@
Library
-------
+- Issue #21560: An attempt to write a data of wrong type no longer cause
+ GzipFile corruption. Original patch by Wolfgang Maier.
+
- Issue #23647: Increase impalib's MAXLINE to accommodate modern mailbox sizes.
- Issue #23539: If body is None, http.client.HTTPConnection.request now sets
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list