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