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