Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 2 | """Test script for the gzip module. |
| 3 | """ |
| 4 | |
| 5 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
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 |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 10 | gzip = support.import_module('gzip') |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 11 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 12 | data1 = b""" int length=DEFAULTALLOC, err = Z_OK; |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 13 | PyObject *RetVal; |
| 14 | int flushmode = Z_FINISH; |
| 15 | unsigned long start_total_out; |
| 16 | |
| 17 | """ |
| 18 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 19 | data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */ |
Neal Norwitz | 014f103 | 2004-07-29 03:55:56 +0000 | [diff] [blame] | 20 | /* See http://www.gzip.org/zlib/ |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 21 | /* See http://www.winimage.com/zLibDll for Windows */ |
| 22 | """ |
| 23 | |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 25 | class UnseekableIO(io.BytesIO): |
| 26 | def seekable(self): |
| 27 | return False |
| 28 | |
| 29 | def tell(self): |
| 30 | raise io.UnsupportedOperation |
| 31 | |
| 32 | def seek(self, *args): |
| 33 | raise io.UnsupportedOperation |
| 34 | |
| 35 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 36 | class TestGzip(unittest.TestCase): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 37 | filename = support.TESTFN |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 38 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 39 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 40 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 41 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 42 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 43 | support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 44 | |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 45 | |
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 | 7980eaa | 2010-10-06 21:21:18 +0000 | [diff] [blame] | 67 | def test_io_on_closed_object(self): |
| 68 | # Test that I/O operations on closed GzipFile objects raise a |
| 69 | # ValueError, just like the corresponding functions on file objects. |
| 70 | |
| 71 | # Write to a file, open it for reading, then close it. |
| 72 | self.test_write() |
| 73 | f = gzip.GzipFile(self.filename, 'r') |
| 74 | f.close() |
| 75 | with self.assertRaises(ValueError): |
| 76 | f.read(1) |
| 77 | with self.assertRaises(ValueError): |
| 78 | f.seek(0) |
| 79 | with self.assertRaises(ValueError): |
| 80 | f.tell() |
| 81 | # Open the file for writing, then close it. |
| 82 | f = gzip.GzipFile(self.filename, 'w') |
| 83 | f.close() |
| 84 | with self.assertRaises(ValueError): |
| 85 | f.write(b'') |
| 86 | with self.assertRaises(ValueError): |
| 87 | f.flush() |
| 88 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 89 | def test_append(self): |
| 90 | self.test_write() |
| 91 | # Append to the previous file |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 92 | with gzip.GzipFile(self.filename, 'ab') as f: |
| 93 | f.write(data2 * 15) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 94 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 95 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 96 | d = f.read() |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 97 | self.assertEqual(d, (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 98 | |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 99 | def test_many_append(self): |
| 100 | # Bug #1074261 was triggered when reading a file that contained |
| 101 | # many, many members. Create such a file and verify that reading it |
| 102 | # works. |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 103 | with gzip.open(self.filename, 'wb', 9) as f: |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 104 | f.write(b'a') |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 105 | for i in range(0, 200): |
| 106 | with gzip.open(self.filename, "ab", 9) as f: # append |
| 107 | f.write(b'a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 108 | |
| 109 | # Try reading the file |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 110 | with gzip.open(self.filename, "rb") as zgfile: |
| 111 | contents = b"" |
| 112 | while 1: |
| 113 | ztxt = zgfile.read(8192) |
| 114 | contents += ztxt |
| 115 | if not ztxt: break |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 116 | self.assertEquals(contents, b'a'*201) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 118 | def test_buffered_reader(self): |
| 119 | # Issue #7471: a GzipFile can be wrapped in a BufferedReader for |
| 120 | # performance. |
| 121 | self.test_write() |
| 122 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 123 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 124 | with io.BufferedReader(f) as r: |
| 125 | lines = [line for line in r] |
Antoine Pitrou | b1f8835 | 2010-01-03 22:37:40 +0000 | [diff] [blame] | 126 | |
| 127 | self.assertEqual(lines, 50 * data1.splitlines(True)) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 128 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 129 | def test_readline(self): |
| 130 | self.test_write() |
| 131 | # Try .readline() with varying line lengths |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 132 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 133 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 134 | line_length = 0 |
| 135 | while 1: |
| 136 | L = f.readline(line_length) |
| 137 | if not L and line_length != 0: break |
| 138 | self.assertTrue(len(L) <= line_length) |
| 139 | line_length = (line_length + 1) % 50 |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 140 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 141 | def test_readlines(self): |
| 142 | self.test_write() |
| 143 | # Try .readlines() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 144 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 145 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 146 | L = f.readlines() |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 147 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 148 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 149 | while 1: |
| 150 | L = f.readlines(150) |
| 151 | if L == []: break |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 152 | |
| 153 | def test_seek_read(self): |
| 154 | self.test_write() |
| 155 | # Try seek, read test |
| 156 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 157 | with gzip.GzipFile(self.filename) as f: |
| 158 | while 1: |
| 159 | oldpos = f.tell() |
| 160 | line1 = f.readline() |
| 161 | if not line1: break |
| 162 | newpos = f.tell() |
| 163 | f.seek(oldpos) # negative seek |
| 164 | if len(line1)>10: |
| 165 | amount = 10 |
| 166 | else: |
| 167 | amount = len(line1) |
| 168 | line2 = f.read(amount) |
| 169 | self.assertEqual(line1[:amount], line2) |
| 170 | f.seek(newpos) # positive seek |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 171 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 172 | def test_seek_whence(self): |
| 173 | self.test_write() |
| 174 | # Try seek(whence=1), read test |
| 175 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 176 | with gzip.GzipFile(self.filename) as f: |
| 177 | f.read(10) |
| 178 | f.seek(10, whence=1) |
| 179 | y = f.read(10) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 180 | self.assertEquals(y, data1[20:30]) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 181 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 182 | def test_seek_write(self): |
| 183 | # Try seek, write test |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 184 | with gzip.GzipFile(self.filename, 'w') as f: |
| 185 | for pos in range(0, 256, 16): |
| 186 | f.seek(pos) |
| 187 | f.write(b'GZ\n') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 188 | |
| 189 | def test_mode(self): |
| 190 | self.test_write() |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 191 | with gzip.GzipFile(self.filename, 'r') as f: |
| 192 | self.assertEqual(f.myfileobj.mode, 'rb') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 193 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 194 | def test_1647484(self): |
| 195 | for mode in ('wb', 'rb'): |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 196 | with gzip.GzipFile(self.filename, mode) as f: |
| 197 | self.assertTrue(hasattr(f, "name")) |
| 198 | self.assertEqual(f.name, self.filename) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 199 | |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 200 | def test_mtime(self): |
| 201 | mtime = 123456789 |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 202 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 203 | fWrite.write(data1) |
| 204 | with gzip.GzipFile(self.filename) as fRead: |
| 205 | dataRead = fRead.read() |
| 206 | self.assertEqual(dataRead, data1) |
| 207 | self.assertTrue(hasattr(fRead, 'mtime')) |
| 208 | self.assertEqual(fRead.mtime, mtime) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 209 | |
| 210 | def test_metadata(self): |
| 211 | mtime = 123456789 |
| 212 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 213 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 214 | fWrite.write(data1) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 215 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 216 | with open(self.filename, 'rb') as fRead: |
| 217 | # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 218 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 219 | idBytes = fRead.read(2) |
| 220 | self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 221 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 222 | cmByte = fRead.read(1) |
| 223 | self.assertEqual(cmByte, b'\x08') # deflate |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 224 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 225 | flagsByte = fRead.read(1) |
| 226 | self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 227 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 228 | mtimeBytes = fRead.read(4) |
| 229 | self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 230 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 231 | xflByte = fRead.read(1) |
| 232 | self.assertEqual(xflByte, b'\x02') # maximum compression |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 233 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 234 | osByte = fRead.read(1) |
| 235 | self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 236 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 237 | # Since the FNAME flag is set, the zero-terminated filename follows. |
| 238 | # RFC 1952 specifies that this is the name of the input file, if any. |
| 239 | # However, the gzip module defaults to storing the name of the output |
| 240 | # file in this field. |
| 241 | expected = self.filename.encode('Latin-1') + b'\x00' |
| 242 | nameBytes = fRead.read(len(expected)) |
| 243 | self.assertEqual(nameBytes, expected) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 244 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 245 | # Since no other flags were set, the header ends here. |
| 246 | # Rather than process the compressed data, let's seek to the trailer. |
| 247 | fRead.seek(os.stat(self.filename).st_size - 8) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 248 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 249 | crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1] |
| 250 | self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83') |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 251 | |
Brian Curtin | 28f96b5 | 2010-10-13 02:21:42 +0000 | [diff] [blame] | 252 | isizeBytes = fRead.read(4) |
| 253 | self.assertEqual(isizeBytes, struct.pack('<i', len(data1))) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | 308705e | 2009-01-10 16:22:51 +0000 | [diff] [blame] | 255 | def test_with_open(self): |
| 256 | # GzipFile supports the context management protocol |
| 257 | with gzip.GzipFile(self.filename, "wb") as f: |
| 258 | f.write(b"xxx") |
| 259 | f = gzip.GzipFile(self.filename, "rb") |
| 260 | f.close() |
| 261 | try: |
| 262 | with f: |
| 263 | pass |
| 264 | except ValueError: |
| 265 | pass |
| 266 | else: |
| 267 | self.fail("__enter__ on a closed file didn't raise an exception") |
| 268 | try: |
| 269 | with gzip.GzipFile(self.filename, "wb") as f: |
| 270 | 1/0 |
| 271 | except ZeroDivisionError: |
| 272 | pass |
| 273 | else: |
| 274 | self.fail("1/0 didn't raise an exception") |
| 275 | |
Antoine Pitrou | 8e33fd7 | 2010-01-13 14:37:26 +0000 | [diff] [blame] | 276 | def test_zero_padded_file(self): |
| 277 | with gzip.GzipFile(self.filename, "wb") as f: |
| 278 | f.write(data1 * 50) |
| 279 | |
| 280 | # Pad the file with zeroes |
| 281 | with open(self.filename, "ab") as f: |
| 282 | f.write(b"\x00" * 50) |
| 283 | |
| 284 | with gzip.GzipFile(self.filename, "rb") as f: |
| 285 | d = f.read() |
| 286 | self.assertEqual(d, data1 * 50, "Incorrect data in file") |
| 287 | |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 288 | def test_non_seekable_file(self): |
| 289 | uncompressed = data1 * 50 |
| 290 | buf = UnseekableIO() |
| 291 | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
| 292 | f.write(uncompressed) |
| 293 | compressed = buf.getvalue() |
| 294 | buf = UnseekableIO(compressed) |
| 295 | with gzip.GzipFile(fileobj=buf, mode="rb") as f: |
| 296 | self.assertEqual(f.read(), uncompressed) |
| 297 | |
Antoine Pitrou | c3ed2e7 | 2010-09-29 10:49:46 +0000 | [diff] [blame] | 298 | def test_peek(self): |
| 299 | uncompressed = data1 * 200 |
| 300 | with gzip.GzipFile(self.filename, "wb") as f: |
| 301 | f.write(uncompressed) |
| 302 | |
| 303 | def sizes(): |
| 304 | while True: |
| 305 | for n in range(5, 50, 10): |
| 306 | yield n |
| 307 | |
| 308 | with gzip.GzipFile(self.filename, "rb") as f: |
| 309 | f.max_read_chunk = 33 |
| 310 | nread = 0 |
| 311 | for n in sizes(): |
| 312 | s = f.peek(n) |
| 313 | if s == b'': |
| 314 | break |
| 315 | self.assertEqual(f.read(len(s)), s) |
| 316 | nread += len(s) |
| 317 | self.assertEqual(f.read(100), b'') |
| 318 | self.assertEqual(nread, len(uncompressed)) |
| 319 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 320 | # Testing compress/decompress shortcut functions |
| 321 | |
| 322 | def test_compress(self): |
| 323 | for data in [data1, data2]: |
| 324 | for args in [(), (1,), (6,), (9,)]: |
| 325 | datac = gzip.compress(data, *args) |
| 326 | self.assertEqual(type(datac), bytes) |
| 327 | with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f: |
| 328 | self.assertEqual(f.read(), data) |
| 329 | |
| 330 | def test_decompress(self): |
| 331 | for data in (data1, data2): |
| 332 | buf = io.BytesIO() |
| 333 | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
| 334 | f.write(data) |
| 335 | self.assertEqual(gzip.decompress(buf.getvalue()), data) |
| 336 | # Roundtrip with compress |
| 337 | datac = gzip.compress(data) |
| 338 | self.assertEqual(gzip.decompress(datac), data) |
| 339 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 340 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 341 | support.run_unittest(TestGzip) |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 342 | |
| 343 | if __name__ == "__main__": |
| 344 | test_main(verbose=True) |