summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_gzip.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-23 13:25:18 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-23 13:25:18 (GMT)
commitf689f104aa89b2dc01e2ee259de8f5d3db3bdb95 (patch)
treec0dc56eb7a733f97a5f6f660378a5794bd6f2a22 /Lib/test/test_gzip.py
parent510a6e9247389b0a78d894fa25bd53adb50c1e07 (diff)
downloadcpython-f689f104aa89b2dc01e2ee259de8f5d3db3bdb95.zip
cpython-f689f104aa89b2dc01e2ee259de8f5d3db3bdb95.tar.gz
cpython-f689f104aa89b2dc01e2ee259de8f5d3db3bdb95.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/test/test_gzip.py')
-rw-r--r--Lib/test/test_gzip.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index 7f24981..41857c7 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -30,6 +30,14 @@ class TestGzip(unittest.TestCase):
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 @@ class TestGzip(unittest.TestCase):
# 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.