Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 1 | """Test script for the gzip module. |
| 2 | """ |
| 3 | |
| 4 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Martin Panter | e99e977 | 2015-11-20 08:13:35 +0000 | [diff] [blame] | 6 | from test.support import bigmemtest, _4G |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 7 | import os |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 8 | import io |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 9 | import struct |
Serhiy Storchaka | bca63b3 | 2015-03-23 14:59:48 +0200 | [diff] [blame] | 10 | import array |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 11 | gzip = support.import_module('gzip') |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 13 | data1 = b""" int length=DEFAULTALLOC, err = Z_OK; |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 14 | PyObject *RetVal; |
| 15 | int flushmode = Z_FINISH; |
| 16 | unsigned long start_total_out; |
| 17 | |
| 18 | """ |
| 19 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 20 | data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */ |
Neal Norwitz | 014f103 | 2004-07-29 03:55:56 +0000 | [diff] [blame] | 21 | /* See http://www.gzip.org/zlib/ |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 22 | /* See http://www.winimage.com/zLibDll for Windows */ |
| 23 | """ |
| 24 | |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 25 | |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 26 | class UnseekableIO(io.BytesIO): |
| 27 | def seekable(self): |
| 28 | return False |
| 29 | |
| 30 | def tell(self): |
| 31 | raise io.UnsupportedOperation |
| 32 | |
| 33 | def seek(self, *args): |
| 34 | raise io.UnsupportedOperation |
| 35 | |
| 36 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 37 | class BaseTest(unittest.TestCase): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 38 | filename = support.TESTFN |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 39 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 40 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 41 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 42 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 43 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 44 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 45 | |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 46 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 47 | class TestGzip(BaseTest): |
Serhiy Storchaka | bca63b3 | 2015-03-23 14:59:48 +0200 | [diff] [blame] | 48 | def write_and_read_back(self, data, mode='b'): |
| 49 | b_data = bytes(data) |
| 50 | with gzip.GzipFile(self.filename, 'w'+mode) as f: |
| 51 | l = f.write(data) |
| 52 | self.assertEqual(l, len(b_data)) |
| 53 | with gzip.GzipFile(self.filename, 'r'+mode) as f: |
| 54 | self.assertEqual(f.read(), b_data) |
| 55 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 56 | def test_write(self): |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 57 | with gzip.GzipFile(self.filename, 'wb') as f: |
| 58 | f.write(data1 * 50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 59 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 60 | # Try flush and fileno. |
| 61 | f.flush() |
| 62 | f.fileno() |
| 63 | if hasattr(os, 'fsync'): |
| 64 | os.fsync(f.fileno()) |
| 65 | f.close() |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 66 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 67 | # Test multiple close() calls. |
| 68 | f.close() |
| 69 | |
Serhiy Storchaka | bca63b3 | 2015-03-23 14:59:48 +0200 | [diff] [blame] | 70 | # The following test_write_xy methods test that write accepts |
| 71 | # the corresponding bytes-like object type as input |
| 72 | # and that the data written equals bytes(xy) in all cases. |
| 73 | def test_write_memoryview(self): |
| 74 | self.write_and_read_back(memoryview(data1 * 50)) |
| 75 | m = memoryview(bytes(range(256))) |
| 76 | data = m.cast('B', shape=[8,8,4]) |
| 77 | self.write_and_read_back(data) |
| 78 | |
| 79 | def test_write_bytearray(self): |
| 80 | self.write_and_read_back(bytearray(data1 * 50)) |
| 81 | |
| 82 | def test_write_array(self): |
| 83 | self.write_and_read_back(array.array('I', data1 * 40)) |
| 84 | |
| 85 | def test_write_incompatible_type(self): |
| 86 | # Test that non-bytes-like types raise TypeError. |
| 87 | # Issue #21560: attempts to write incompatible types |
| 88 | # should not affect the state of the fileobject |
| 89 | with gzip.GzipFile(self.filename, 'wb') as f: |
| 90 | with self.assertRaises(TypeError): |
| 91 | f.write('') |
| 92 | with self.assertRaises(TypeError): |
| 93 | f.write([]) |
| 94 | f.write(data1) |
| 95 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 96 | self.assertEqual(f.read(), data1) |
| 97 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 98 | def test_read(self): |
| 99 | self.test_write() |
| 100 | # Try reading. |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 101 | with gzip.GzipFile(self.filename, 'r') as f: |
| 102 | d = f.read() |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 103 | self.assertEqual(d, data1*50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 104 | |
Antoine Pitrou | 4ec4b0c | 2011-04-04 21:00:37 +0200 | [diff] [blame] | 105 | def test_read1(self): |
| 106 | self.test_write() |
| 107 | blocks = [] |
| 108 | nread = 0 |
| 109 | with gzip.GzipFile(self.filename, 'r') as f: |
| 110 | while True: |
| 111 | d = f.read1() |
| 112 | if not d: |
| 113 | break |
| 114 | blocks.append(d) |
| 115 | nread += len(d) |
| 116 | # Check that position was updated correctly (see issue10791). |
| 117 | self.assertEqual(f.tell(), nread) |
| 118 | self.assertEqual(b''.join(blocks), data1 * 50) |
| 119 | |
Martin Panter | e99e977 | 2015-11-20 08:13:35 +0000 | [diff] [blame] | 120 | @bigmemtest(size=_4G, memuse=1) |
| 121 | def test_read_large(self, size): |
| 122 | # Read chunk size over UINT_MAX should be supported, despite zlib's |
| 123 | # limitation per low-level call |
| 124 | compressed = gzip.compress(data1, compresslevel=1) |
| 125 | f = gzip.GzipFile(fileobj=io.BytesIO(compressed), mode='rb') |
| 126 | self.assertEqual(f.read(size), data1) |
| 127 | |
Antoine Pitrou | 7980eaa | 2010-10-06 21:21:18 +0000 | [diff] [blame] | 128 | def test_io_on_closed_object(self): |
| 129 | # Test that I/O operations on closed GzipFile objects raise a |
| 130 | # ValueError, just like the corresponding functions on file objects. |
| 131 | |
| 132 | # Write to a file, open it for reading, then close it. |
| 133 | self.test_write() |
| 134 | f = gzip.GzipFile(self.filename, 'r') |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 135 | fileobj = f.fileobj |
| 136 | self.assertFalse(fileobj.closed) |
Antoine Pitrou | 7980eaa | 2010-10-06 21:21:18 +0000 | [diff] [blame] | 137 | f.close() |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 138 | self.assertTrue(fileobj.closed) |
Antoine Pitrou | 7980eaa | 2010-10-06 21:21:18 +0000 | [diff] [blame] | 139 | with self.assertRaises(ValueError): |
| 140 | f.read(1) |
| 141 | with self.assertRaises(ValueError): |
| 142 | f.seek(0) |
| 143 | with self.assertRaises(ValueError): |
| 144 | f.tell() |
| 145 | # Open the file for writing, then close it. |
| 146 | f = gzip.GzipFile(self.filename, 'w') |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 147 | fileobj = f.fileobj |
| 148 | self.assertFalse(fileobj.closed) |
Antoine Pitrou | 7980eaa | 2010-10-06 21:21:18 +0000 | [diff] [blame] | 149 | f.close() |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 150 | self.assertTrue(fileobj.closed) |
Antoine Pitrou | 7980eaa | 2010-10-06 21:21:18 +0000 | [diff] [blame] | 151 | with self.assertRaises(ValueError): |
| 152 | f.write(b'') |
| 153 | with self.assertRaises(ValueError): |
| 154 | f.flush() |
| 155 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 156 | def test_append(self): |
| 157 | self.test_write() |
| 158 | # Append to the previous file |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 159 | with gzip.GzipFile(self.filename, 'ab') as f: |
| 160 | f.write(data2 * 15) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 161 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 162 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 163 | d = f.read() |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 164 | self.assertEqual(d, (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 165 | |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 166 | def test_many_append(self): |
| 167 | # Bug #1074261 was triggered when reading a file that contained |
| 168 | # many, many members. Create such a file and verify that reading it |
| 169 | # works. |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 170 | with gzip.GzipFile(self.filename, 'wb', 9) as f: |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 171 | f.write(b'a') |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 172 | for i in range(0, 200): |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 173 | with gzip.GzipFile(self.filename, "ab", 9) as f: # append |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 174 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 175 | |
| 176 | # Try reading the file |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 177 | with gzip.GzipFile(self.filename, "rb") as zgfile: |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 178 | contents = b"" |
| 179 | while 1: |
| 180 | ztxt = zgfile.read(8192) |
| 181 | contents += ztxt |
| 182 | if not ztxt: break |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 183 | self.assertEqual(contents, b'a'*201) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 184 | |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 185 | def test_exclusive_write(self): |
| 186 | with gzip.GzipFile(self.filename, 'xb') as f: |
| 187 | f.write(data1 * 50) |
| 188 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 189 | self.assertEqual(f.read(), data1 * 50) |
| 190 | with self.assertRaises(FileExistsError): |
| 191 | gzip.GzipFile(self.filename, 'xb') |
| 192 | |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 193 | def test_buffered_reader(self): |
| 194 | # Issue #7471: a GzipFile can be wrapped in a BufferedReader for |
| 195 | # performance. |
| 196 | self.test_write() |
| 197 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 198 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 199 | with io.BufferedReader(f) as r: |
| 200 | lines = [line for line in r] |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 201 | |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 202 | self.assertEqual(lines, 50 * data1.splitlines(keepends=True)) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 203 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 204 | def test_readline(self): |
| 205 | self.test_write() |
| 206 | # Try .readline() with varying line lengths |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 207 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 208 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 209 | line_length = 0 |
| 210 | while 1: |
| 211 | L = f.readline(line_length) |
| 212 | if not L and line_length != 0: break |
| 213 | self.assertTrue(len(L) <= line_length) |
| 214 | line_length = (line_length + 1) % 50 |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 215 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 216 | def test_readlines(self): |
| 217 | self.test_write() |
| 218 | # Try .readlines() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 219 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 220 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 221 | L = f.readlines() |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 222 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 223 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 224 | while 1: |
| 225 | L = f.readlines(150) |
| 226 | if L == []: break |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 227 | |
| 228 | def test_seek_read(self): |
| 229 | self.test_write() |
| 230 | # Try seek, read test |
| 231 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 232 | with gzip.GzipFile(self.filename) as f: |
| 233 | while 1: |
| 234 | oldpos = f.tell() |
| 235 | line1 = f.readline() |
| 236 | if not line1: break |
| 237 | newpos = f.tell() |
| 238 | f.seek(oldpos) # negative seek |
| 239 | if len(line1)>10: |
| 240 | amount = 10 |
| 241 | else: |
| 242 | amount = len(line1) |
| 243 | line2 = f.read(amount) |
| 244 | self.assertEqual(line1[:amount], line2) |
| 245 | f.seek(newpos) # positive seek |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 246 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 247 | def test_seek_whence(self): |
| 248 | self.test_write() |
| 249 | # Try seek(whence=1), read test |
| 250 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 251 | with gzip.GzipFile(self.filename) as f: |
| 252 | f.read(10) |
| 253 | f.seek(10, whence=1) |
| 254 | y = f.read(10) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 255 | self.assertEqual(y, data1[20:30]) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 256 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 257 | def test_seek_write(self): |
| 258 | # Try seek, write test |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 259 | with gzip.GzipFile(self.filename, 'w') as f: |
| 260 | for pos in range(0, 256, 16): |
| 261 | f.seek(pos) |
| 262 | f.write(b'GZ\n') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 263 | |
| 264 | def test_mode(self): |
| 265 | self.test_write() |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 266 | with gzip.GzipFile(self.filename, 'r') as f: |
| 267 | self.assertEqual(f.myfileobj.mode, 'rb') |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 268 | support.unlink(self.filename) |
| 269 | with gzip.GzipFile(self.filename, 'x') as f: |
| 270 | self.assertEqual(f.myfileobj.mode, 'xb') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 271 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 272 | def test_1647484(self): |
| 273 | for mode in ('wb', 'rb'): |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 274 | with gzip.GzipFile(self.filename, mode) as f: |
| 275 | self.assertTrue(hasattr(f, "name")) |
| 276 | self.assertEqual(f.name, self.filename) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 277 | |
Georg Brandl | 9f1c1dc | 2010-11-20 11:25:01 +0000 | [diff] [blame] | 278 | def test_paddedfile_getattr(self): |
| 279 | self.test_write() |
| 280 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 281 | self.assertTrue(hasattr(f.fileobj, "name")) |
| 282 | self.assertEqual(f.fileobj.name, self.filename) |
| 283 | |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 284 | def test_mtime(self): |
| 285 | mtime = 123456789 |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 286 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 287 | fWrite.write(data1) |
| 288 | with gzip.GzipFile(self.filename) as fRead: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 289 | self.assertTrue(hasattr(fRead, 'mtime')) |
| 290 | self.assertIsNone(fRead.mtime) |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 291 | dataRead = fRead.read() |
| 292 | self.assertEqual(dataRead, data1) |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 293 | self.assertEqual(fRead.mtime, mtime) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 294 | |
| 295 | def test_metadata(self): |
| 296 | mtime = 123456789 |
| 297 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 298 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 299 | fWrite.write(data1) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 300 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 301 | with open(self.filename, 'rb') as fRead: |
| 302 | # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 303 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 304 | idBytes = fRead.read(2) |
| 305 | self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 306 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 307 | cmByte = fRead.read(1) |
| 308 | self.assertEqual(cmByte, b'\x08') # deflate |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 309 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 310 | flagsByte = fRead.read(1) |
| 311 | self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 312 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 313 | mtimeBytes = fRead.read(4) |
| 314 | self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 315 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 316 | xflByte = fRead.read(1) |
| 317 | self.assertEqual(xflByte, b'\x02') # maximum compression |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 318 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 319 | osByte = fRead.read(1) |
| 320 | self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 321 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 322 | # Since the FNAME flag is set, the zero-terminated filename follows. |
| 323 | # RFC 1952 specifies that this is the name of the input file, if any. |
| 324 | # However, the gzip module defaults to storing the name of the output |
| 325 | # file in this field. |
| 326 | expected = self.filename.encode('Latin-1') + b'\x00' |
| 327 | nameBytes = fRead.read(len(expected)) |
| 328 | self.assertEqual(nameBytes, expected) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 329 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 330 | # Since no other flags were set, the header ends here. |
| 331 | # Rather than process the compressed data, let's seek to the trailer. |
| 332 | fRead.seek(os.stat(self.filename).st_size - 8) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 333 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 334 | crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1] |
| 335 | self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83') |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 336 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 337 | isizeBytes = fRead.read(4) |
| 338 | self.assertEqual(isizeBytes, struct.pack('<i', len(data1))) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | 308705e | 2009-01-10 16:22:51 +0000 | [diff] [blame] | 340 | def test_with_open(self): |
| 341 | # GzipFile supports the context management protocol |
| 342 | with gzip.GzipFile(self.filename, "wb") as f: |
| 343 | f.write(b"xxx") |
| 344 | f = gzip.GzipFile(self.filename, "rb") |
| 345 | f.close() |
| 346 | try: |
| 347 | with f: |
| 348 | pass |
| 349 | except ValueError: |
| 350 | pass |
| 351 | else: |
| 352 | self.fail("__enter__ on a closed file didn't raise an exception") |
| 353 | try: |
| 354 | with gzip.GzipFile(self.filename, "wb") as f: |
| 355 | 1/0 |
| 356 | except ZeroDivisionError: |
| 357 | pass |
| 358 | else: |
| 359 | self.fail("1/0 didn't raise an exception") |
| 360 | |
Antoine Pitrou | 8e33fd7 | 2010-01-13 14:37:26 +0000 | [diff] [blame] | 361 | def test_zero_padded_file(self): |
| 362 | with gzip.GzipFile(self.filename, "wb") as f: |
| 363 | f.write(data1 * 50) |
| 364 | |
| 365 | # Pad the file with zeroes |
| 366 | with open(self.filename, "ab") as f: |
| 367 | f.write(b"\x00" * 50) |
| 368 | |
| 369 | with gzip.GzipFile(self.filename, "rb") as f: |
| 370 | d = f.read() |
| 371 | self.assertEqual(d, data1 * 50, "Incorrect data in file") |
| 372 | |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 373 | def test_non_seekable_file(self): |
| 374 | uncompressed = data1 * 50 |
| 375 | buf = UnseekableIO() |
| 376 | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
| 377 | f.write(uncompressed) |
| 378 | compressed = buf.getvalue() |
| 379 | buf = UnseekableIO(compressed) |
| 380 | with gzip.GzipFile(fileobj=buf, mode="rb") as f: |
| 381 | self.assertEqual(f.read(), uncompressed) |
| 382 | |
Antoine Pitrou | c3ed2e7 | 2010-09-29 10:49:46 +0000 | [diff] [blame] | 383 | def test_peek(self): |
| 384 | uncompressed = data1 * 200 |
| 385 | with gzip.GzipFile(self.filename, "wb") as f: |
| 386 | f.write(uncompressed) |
| 387 | |
| 388 | def sizes(): |
| 389 | while True: |
| 390 | for n in range(5, 50, 10): |
| 391 | yield n |
| 392 | |
| 393 | with gzip.GzipFile(self.filename, "rb") as f: |
| 394 | f.max_read_chunk = 33 |
| 395 | nread = 0 |
| 396 | for n in sizes(): |
| 397 | s = f.peek(n) |
| 398 | if s == b'': |
| 399 | break |
| 400 | self.assertEqual(f.read(len(s)), s) |
| 401 | nread += len(s) |
| 402 | self.assertEqual(f.read(100), b'') |
| 403 | self.assertEqual(nread, len(uncompressed)) |
| 404 | |
Antoine Pitrou | 4ec4b0c | 2011-04-04 21:00:37 +0200 | [diff] [blame] | 405 | def test_textio_readlines(self): |
| 406 | # Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile. |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 407 | lines = (data1 * 50).decode("ascii").splitlines(keepends=True) |
Antoine Pitrou | 4ec4b0c | 2011-04-04 21:00:37 +0200 | [diff] [blame] | 408 | self.test_write() |
| 409 | with gzip.GzipFile(self.filename, 'r') as f: |
| 410 | with io.TextIOWrapper(f, encoding="ascii") as t: |
| 411 | self.assertEqual(t.readlines(), lines) |
| 412 | |
Nadeem Vawda | 892b0b9 | 2012-01-18 09:25:58 +0200 | [diff] [blame] | 413 | def test_fileobj_from_fdopen(self): |
| 414 | # Issue #13781: Opening a GzipFile for writing fails when using a |
| 415 | # fileobj created with os.fdopen(). |
| 416 | fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT) |
| 417 | with os.fdopen(fd, "wb") as f: |
| 418 | with gzip.GzipFile(fileobj=f, mode="w") as g: |
| 419 | pass |
| 420 | |
Nadeem Vawda | 103e811 | 2012-06-20 01:35:22 +0200 | [diff] [blame] | 421 | def test_bytes_filename(self): |
| 422 | str_filename = self.filename |
| 423 | try: |
| 424 | bytes_filename = str_filename.encode("ascii") |
| 425 | except UnicodeEncodeError: |
| 426 | self.skipTest("Temporary file name needs to be ASCII") |
| 427 | with gzip.GzipFile(bytes_filename, "wb") as f: |
| 428 | f.write(data1 * 50) |
| 429 | with gzip.GzipFile(bytes_filename, "rb") as f: |
| 430 | self.assertEqual(f.read(), data1 * 50) |
| 431 | # Sanity check that we are actually operating on the right file. |
| 432 | with gzip.GzipFile(str_filename, "rb") as f: |
| 433 | self.assertEqual(f.read(), data1 * 50) |
| 434 | |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 435 | def test_decompress_limited(self): |
| 436 | """Decompressed data buffering should be limited""" |
| 437 | bomb = gzip.compress(bytes(int(2e6)), compresslevel=9) |
| 438 | self.assertLess(len(bomb), io.DEFAULT_BUFFER_SIZE) |
| 439 | |
| 440 | bomb = io.BytesIO(bomb) |
| 441 | decomp = gzip.GzipFile(fileobj=bomb) |
| 442 | self.assertEqual(bytes(1), decomp.read(1)) |
| 443 | max_decomp = 1 + io.DEFAULT_BUFFER_SIZE |
| 444 | self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp, |
| 445 | "Excessive amount of data was decompressed") |
| 446 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 447 | # Testing compress/decompress shortcut functions |
| 448 | |
| 449 | def test_compress(self): |
| 450 | for data in [data1, data2]: |
| 451 | for args in [(), (1,), (6,), (9,)]: |
| 452 | datac = gzip.compress(data, *args) |
| 453 | self.assertEqual(type(datac), bytes) |
| 454 | with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f: |
| 455 | self.assertEqual(f.read(), data) |
| 456 | |
| 457 | def test_decompress(self): |
| 458 | for data in (data1, data2): |
| 459 | buf = io.BytesIO() |
| 460 | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
| 461 | f.write(data) |
| 462 | self.assertEqual(gzip.decompress(buf.getvalue()), data) |
| 463 | # Roundtrip with compress |
| 464 | datac = gzip.compress(data) |
| 465 | self.assertEqual(gzip.decompress(datac), data) |
| 466 | |
Serhiy Storchaka | 7c3922f | 2013-01-22 17:01:59 +0200 | [diff] [blame] | 467 | def test_read_truncated(self): |
| 468 | data = data1*50 |
| 469 | # Drop the CRC (4 bytes) and file size (4 bytes). |
| 470 | truncated = gzip.compress(data)[:-8] |
| 471 | with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: |
| 472 | self.assertRaises(EOFError, f.read) |
| 473 | with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: |
| 474 | self.assertEqual(f.read(len(data)), data) |
| 475 | self.assertRaises(EOFError, f.read, 1) |
| 476 | # Incomplete 10-byte header. |
| 477 | for i in range(2, 10): |
| 478 | with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f: |
| 479 | self.assertRaises(EOFError, f.read, 1) |
| 480 | |
Serhiy Storchaka | 7e69f00 | 2013-04-08 22:35:02 +0300 | [diff] [blame] | 481 | def test_read_with_extra(self): |
| 482 | # Gzip data with an extra field |
| 483 | gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff' |
| 484 | b'\x05\x00Extra' |
| 485 | b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00') |
| 486 | with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f: |
| 487 | self.assertEqual(f.read(), b'Test') |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 488 | |
Ned Deily | 6120739 | 2014-03-09 14:44:34 -0700 | [diff] [blame] | 489 | def test_prepend_error(self): |
| 490 | # See issue #20875 |
| 491 | with gzip.open(self.filename, "wb") as f: |
| 492 | f.write(data1) |
| 493 | with gzip.open(self.filename, "rb") as f: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 494 | f._buffer.raw._fp.prepend() |
Ned Deily | 6120739 | 2014-03-09 14:44:34 -0700 | [diff] [blame] | 495 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 496 | class TestOpen(BaseTest): |
| 497 | def test_binary_modes(self): |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 498 | uncompressed = data1 * 50 |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 499 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 500 | with gzip.open(self.filename, "wb") as f: |
| 501 | f.write(uncompressed) |
| 502 | with open(self.filename, "rb") as f: |
| 503 | file_data = gzip.decompress(f.read()) |
| 504 | self.assertEqual(file_data, uncompressed) |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 505 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 506 | with gzip.open(self.filename, "rb") as f: |
| 507 | self.assertEqual(f.read(), uncompressed) |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 508 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 509 | with gzip.open(self.filename, "ab") as f: |
| 510 | f.write(uncompressed) |
| 511 | with open(self.filename, "rb") as f: |
| 512 | file_data = gzip.decompress(f.read()) |
| 513 | self.assertEqual(file_data, uncompressed * 2) |
| 514 | |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 515 | with self.assertRaises(FileExistsError): |
| 516 | gzip.open(self.filename, "xb") |
| 517 | support.unlink(self.filename) |
| 518 | with gzip.open(self.filename, "xb") as f: |
| 519 | f.write(uncompressed) |
| 520 | with open(self.filename, "rb") as f: |
| 521 | file_data = gzip.decompress(f.read()) |
| 522 | self.assertEqual(file_data, uncompressed) |
| 523 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 524 | def test_implicit_binary_modes(self): |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 525 | # Test implicit binary modes (no "b" or "t" in mode string). |
| 526 | uncompressed = data1 * 50 |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 527 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 528 | with gzip.open(self.filename, "w") as f: |
| 529 | f.write(uncompressed) |
| 530 | with open(self.filename, "rb") as f: |
| 531 | file_data = gzip.decompress(f.read()) |
| 532 | self.assertEqual(file_data, uncompressed) |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 533 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 534 | with gzip.open(self.filename, "r") as f: |
| 535 | self.assertEqual(f.read(), uncompressed) |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 536 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 537 | with gzip.open(self.filename, "a") as f: |
| 538 | f.write(uncompressed) |
| 539 | with open(self.filename, "rb") as f: |
| 540 | file_data = gzip.decompress(f.read()) |
| 541 | self.assertEqual(file_data, uncompressed * 2) |
| 542 | |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 543 | with self.assertRaises(FileExistsError): |
| 544 | gzip.open(self.filename, "x") |
| 545 | support.unlink(self.filename) |
| 546 | with gzip.open(self.filename, "x") as f: |
| 547 | f.write(uncompressed) |
| 548 | with open(self.filename, "rb") as f: |
| 549 | file_data = gzip.decompress(f.read()) |
| 550 | self.assertEqual(file_data, uncompressed) |
| 551 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 552 | def test_text_modes(self): |
Nadeem Vawda | 11328e4 | 2012-05-06 19:24:18 +0200 | [diff] [blame] | 553 | uncompressed = data1.decode("ascii") * 50 |
| 554 | uncompressed_raw = uncompressed.replace("\n", os.linesep) |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 555 | with gzip.open(self.filename, "wt") as f: |
| 556 | f.write(uncompressed) |
| 557 | with open(self.filename, "rb") as f: |
| 558 | file_data = gzip.decompress(f.read()).decode("ascii") |
Nadeem Vawda | 11328e4 | 2012-05-06 19:24:18 +0200 | [diff] [blame] | 559 | self.assertEqual(file_data, uncompressed_raw) |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 560 | with gzip.open(self.filename, "rt") as f: |
| 561 | self.assertEqual(f.read(), uncompressed) |
| 562 | with gzip.open(self.filename, "at") as f: |
| 563 | f.write(uncompressed) |
| 564 | with open(self.filename, "rb") as f: |
| 565 | file_data = gzip.decompress(f.read()).decode("ascii") |
Nadeem Vawda | 11328e4 | 2012-05-06 19:24:18 +0200 | [diff] [blame] | 566 | self.assertEqual(file_data, uncompressed_raw * 2) |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 567 | |
Nadeem Vawda | 6872101 | 2012-06-04 23:21:38 +0200 | [diff] [blame] | 568 | def test_fileobj(self): |
| 569 | uncompressed_bytes = data1 * 50 |
| 570 | uncompressed_str = uncompressed_bytes.decode("ascii") |
| 571 | compressed = gzip.compress(uncompressed_bytes) |
| 572 | with gzip.open(io.BytesIO(compressed), "r") as f: |
| 573 | self.assertEqual(f.read(), uncompressed_bytes) |
| 574 | with gzip.open(io.BytesIO(compressed), "rb") as f: |
| 575 | self.assertEqual(f.read(), uncompressed_bytes) |
| 576 | with gzip.open(io.BytesIO(compressed), "rt") as f: |
| 577 | self.assertEqual(f.read(), uncompressed_str) |
| 578 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 579 | def test_bad_params(self): |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 580 | # Test invalid parameter combinations. |
Nadeem Vawda | 6872101 | 2012-06-04 23:21:38 +0200 | [diff] [blame] | 581 | with self.assertRaises(TypeError): |
| 582 | gzip.open(123.456) |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 583 | with self.assertRaises(ValueError): |
| 584 | gzip.open(self.filename, "wbt") |
| 585 | with self.assertRaises(ValueError): |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 586 | gzip.open(self.filename, "xbt") |
| 587 | with self.assertRaises(ValueError): |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 588 | gzip.open(self.filename, "rb", encoding="utf-8") |
| 589 | with self.assertRaises(ValueError): |
| 590 | gzip.open(self.filename, "rb", errors="ignore") |
| 591 | with self.assertRaises(ValueError): |
| 592 | gzip.open(self.filename, "rb", newline="\n") |
| 593 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 594 | def test_encoding(self): |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 595 | # Test non-default encoding. |
Nadeem Vawda | 11328e4 | 2012-05-06 19:24:18 +0200 | [diff] [blame] | 596 | uncompressed = data1.decode("ascii") * 50 |
| 597 | uncompressed_raw = uncompressed.replace("\n", os.linesep) |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 598 | with gzip.open(self.filename, "wt", encoding="utf-16") as f: |
| 599 | f.write(uncompressed) |
| 600 | with open(self.filename, "rb") as f: |
| 601 | file_data = gzip.decompress(f.read()).decode("utf-16") |
Nadeem Vawda | 11328e4 | 2012-05-06 19:24:18 +0200 | [diff] [blame] | 602 | self.assertEqual(file_data, uncompressed_raw) |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 603 | with gzip.open(self.filename, "rt", encoding="utf-16") as f: |
| 604 | self.assertEqual(f.read(), uncompressed) |
| 605 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 606 | def test_encoding_error_handler(self): |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 607 | # Test with non-default encoding error handler. |
| 608 | with gzip.open(self.filename, "wb") as f: |
| 609 | f.write(b"foo\xffbar") |
| 610 | with gzip.open(self.filename, "rt", encoding="ascii", errors="ignore") \ |
| 611 | as f: |
| 612 | self.assertEqual(f.read(), "foobar") |
| 613 | |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 614 | def test_newline(self): |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 615 | # Test with explicit newline (universal newline mode disabled). |
| 616 | uncompressed = data1.decode("ascii") * 50 |
Nadeem Vawda | 9d9dc8e | 2012-05-06 16:25:35 +0200 | [diff] [blame] | 617 | with gzip.open(self.filename, "wt", newline="\n") as f: |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 618 | f.write(uncompressed) |
| 619 | with gzip.open(self.filename, "rt", newline="\r") as f: |
| 620 | self.assertEqual(f.readlines(), [uncompressed]) |
| 621 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 622 | def test_main(verbose=None): |
Nadeem Vawda | 1b8a14d | 2012-05-06 15:17:52 +0200 | [diff] [blame] | 623 | support.run_unittest(TestGzip, TestOpen) |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 624 | |
| 625 | if __name__ == "__main__": |
| 626 | test_main(verbose=True) |