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 |
| 5 | from test import test_support |
Christian Heimes | c5f05e4 | 2008-02-23 17:40:11 +0000 | [diff] [blame] | 6 | import os |
Antoine Pitrou | 673ddf9 | 2010-01-03 22:29:56 +0000 | [diff] [blame] | 7 | import io |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 8 | import struct |
Ezio Melotti | 1036a7f | 2009-09-12 14:43:43 +0000 | [diff] [blame] | 9 | gzip = test_support.import_module('gzip') |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 10 | |
| 11 | data1 = """ int length=DEFAULTALLOC, err = Z_OK; |
| 12 | PyObject *RetVal; |
| 13 | int flushmode = Z_FINISH; |
| 14 | unsigned long start_total_out; |
| 15 | |
| 16 | """ |
| 17 | |
| 18 | data2 = """/* 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 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 24 | class TestGzip(unittest.TestCase): |
| 25 | filename = test_support.TESTFN |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 26 | |
Georg Brandl | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 27 | def setUp(self): |
Neal Norwitz | 36a59b4 | 2008-04-10 05:46:39 +0000 | [diff] [blame] | 28 | test_support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 29 | |
Georg Brandl | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 30 | def tearDown(self): |
Neal Norwitz | 36a59b4 | 2008-04-10 05:46:39 +0000 | [diff] [blame] | 31 | test_support.unlink(self.filename) |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 32 | |
Serhiy Storchaka | 54edfb3 | 2014-10-12 22:23:28 +0300 | [diff] [blame] | 33 | @test_support.requires_unicode |
| 34 | def test_unicode_filename(self): |
| 35 | unicode_filename = test_support.TESTFN_UNICODE |
Serhiy Storchaka | fbddffa | 2014-10-13 10:33:32 +0300 | [diff] [blame^] | 36 | try: |
| 37 | unicode_filename.encode(test_support.TESTFN_ENCODING) |
| 38 | except (UnicodeError, TypeError): |
| 39 | self.skipTest("Requires unicode filenames support") |
Serhiy Storchaka | 54edfb3 | 2014-10-12 22:23:28 +0300 | [diff] [blame] | 40 | with gzip.GzipFile(unicode_filename, "wb") as f: |
| 41 | f.write(data1 * 50) |
| 42 | with gzip.GzipFile(unicode_filename, "rb") as f: |
| 43 | self.assertEqual(f.read(), data1 * 50) |
| 44 | # Sanity check that we are actually operating on the right file. |
| 45 | with open(unicode_filename, 'rb') as fobj, \ |
| 46 | gzip.GzipFile(fileobj=fobj, mode="rb") as f: |
| 47 | self.assertEqual(f.read(), data1 * 50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 48 | |
Georg Brandl | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 49 | def test_write(self): |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 50 | with gzip.GzipFile(self.filename, 'wb') as f: |
| 51 | f.write(data1 * 50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 52 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 53 | # Try flush and fileno. |
| 54 | f.flush() |
| 55 | f.fileno() |
| 56 | if hasattr(os, 'fsync'): |
| 57 | os.fsync(f.fileno()) |
| 58 | f.close() |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 59 | |
Georg Brandl | e08e3d0 | 2008-05-25 08:07:37 +0000 | [diff] [blame] | 60 | # Test multiple close() calls. |
| 61 | f.close() |
| 62 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 63 | def test_read(self): |
| 64 | self.test_write() |
| 65 | # Try reading. |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 66 | with gzip.GzipFile(self.filename, 'r') as f: |
| 67 | d = f.read() |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 68 | self.assertEqual(d, data1*50) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 69 | |
Nadeem Vawda | dd72b3f | 2012-10-21 18:15:05 +0200 | [diff] [blame] | 70 | def test_read_universal_newlines(self): |
| 71 | # Issue #5148: Reading breaks when mode contains 'U'. |
| 72 | self.test_write() |
| 73 | with gzip.GzipFile(self.filename, 'rU') as f: |
| 74 | d = f.read() |
| 75 | self.assertEqual(d, data1*50) |
| 76 | |
Antoine Pitrou | 76a66aa | 2010-10-06 21:26:52 +0000 | [diff] [blame] | 77 | def test_io_on_closed_object(self): |
| 78 | # Test that I/O operations on closed GzipFile objects raise a |
| 79 | # ValueError, just like the corresponding functions on file objects. |
| 80 | |
| 81 | # Write to a file, open it for reading, then close it. |
| 82 | self.test_write() |
| 83 | f = gzip.GzipFile(self.filename, 'r') |
| 84 | f.close() |
| 85 | with self.assertRaises(ValueError): |
| 86 | f.read(1) |
| 87 | with self.assertRaises(ValueError): |
| 88 | f.seek(0) |
| 89 | with self.assertRaises(ValueError): |
| 90 | f.tell() |
| 91 | # Open the file for writing, then close it. |
| 92 | f = gzip.GzipFile(self.filename, 'w') |
| 93 | f.close() |
| 94 | with self.assertRaises(ValueError): |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 95 | f.write('') |
Antoine Pitrou | 76a66aa | 2010-10-06 21:26:52 +0000 | [diff] [blame] | 96 | with self.assertRaises(ValueError): |
| 97 | f.flush() |
| 98 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 99 | def test_append(self): |
| 100 | self.test_write() |
| 101 | # Append to the previous file |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 102 | with gzip.GzipFile(self.filename, 'ab') as f: |
| 103 | f.write(data2 * 15) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 104 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 105 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 106 | d = f.read() |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 107 | self.assertEqual(d, (data1*50) + (data2*15)) |
Andrew M. Kuchling | 85ab738 | 2000-07-29 20:18:34 +0000 | [diff] [blame] | 108 | |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 109 | def test_many_append(self): |
| 110 | # Bug #1074261 was triggered when reading a file that contained |
| 111 | # many, many members. Create such a file and verify that reading it |
| 112 | # works. |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 113 | with gzip.open(self.filename, 'wb', 9) as f: |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 114 | f.write('a') |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 115 | for i in range(0, 200): |
| 116 | with gzip.open(self.filename, "ab", 9) as f: # append |
| 117 | f.write('a') |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 118 | |
| 119 | # Try reading the file |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 120 | with gzip.open(self.filename, "rb") as zgfile: |
| 121 | contents = "" |
| 122 | while 1: |
| 123 | ztxt = zgfile.read(8192) |
| 124 | contents += ztxt |
| 125 | if not ztxt: break |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 126 | self.assertEqual(contents, 'a'*201) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | 673ddf9 | 2010-01-03 22:29:56 +0000 | [diff] [blame] | 128 | def test_buffered_reader(self): |
| 129 | # Issue #7471: a GzipFile can be wrapped in a BufferedReader for |
| 130 | # performance. |
| 131 | self.test_write() |
| 132 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 133 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 134 | with io.BufferedReader(f) as r: |
| 135 | lines = [line for line in r] |
Antoine Pitrou | 673ddf9 | 2010-01-03 22:29:56 +0000 | [diff] [blame] | 136 | |
| 137 | self.assertEqual(lines, 50 * data1.splitlines(True)) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 138 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 139 | def test_readline(self): |
| 140 | self.test_write() |
| 141 | # Try .readline() with varying line lengths |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 142 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 143 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 144 | line_length = 0 |
| 145 | while 1: |
| 146 | L = f.readline(line_length) |
| 147 | if not L and line_length != 0: break |
| 148 | self.assertTrue(len(L) <= line_length) |
| 149 | line_length = (line_length + 1) % 50 |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 150 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 151 | def test_readlines(self): |
| 152 | self.test_write() |
| 153 | # Try .readlines() |
Andrew M. Kuchling | 605ebdd | 1999-03-25 21:50:27 +0000 | [diff] [blame] | 154 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 155 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 156 | L = f.readlines() |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 157 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 158 | with gzip.GzipFile(self.filename, 'rb') as f: |
| 159 | while 1: |
| 160 | L = f.readlines(150) |
| 161 | if L == []: break |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 162 | |
| 163 | def test_seek_read(self): |
| 164 | self.test_write() |
| 165 | # Try seek, read test |
| 166 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 167 | with gzip.GzipFile(self.filename) as f: |
| 168 | while 1: |
| 169 | oldpos = f.tell() |
| 170 | line1 = f.readline() |
| 171 | if not line1: break |
| 172 | newpos = f.tell() |
| 173 | f.seek(oldpos) # negative seek |
| 174 | if len(line1)>10: |
| 175 | amount = 10 |
| 176 | else: |
| 177 | amount = len(line1) |
| 178 | line2 = f.read(amount) |
| 179 | self.assertEqual(line1[:amount], line2) |
| 180 | f.seek(newpos) # positive seek |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 181 | |
Martin v. Löwis | 065f0c8 | 2006-11-12 10:41:39 +0000 | [diff] [blame] | 182 | def test_seek_whence(self): |
| 183 | self.test_write() |
| 184 | # Try seek(whence=1), read test |
| 185 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 186 | with gzip.GzipFile(self.filename) as f: |
| 187 | f.read(10) |
| 188 | f.seek(10, whence=1) |
| 189 | y = f.read(10) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 190 | self.assertEqual(y, data1[20:30]) |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 191 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 192 | def test_seek_write(self): |
| 193 | # Try seek, write test |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 194 | with gzip.GzipFile(self.filename, 'w') as f: |
| 195 | for pos in range(0, 256, 16): |
| 196 | f.seek(pos) |
| 197 | f.write('GZ\n') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 198 | |
| 199 | def test_mode(self): |
| 200 | self.test_write() |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 201 | with gzip.GzipFile(self.filename, 'r') as f: |
| 202 | self.assertEqual(f.myfileobj.mode, 'rb') |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 203 | |
Lars Gustäbel | 5b1a785 | 2007-02-13 16:09:24 +0000 | [diff] [blame] | 204 | def test_1647484(self): |
| 205 | for mode in ('wb', 'rb'): |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 206 | with gzip.GzipFile(self.filename, mode) as f: |
| 207 | self.assertTrue(hasattr(f, "name")) |
| 208 | self.assertEqual(f.name, self.filename) |
Lars Gustäbel | 5b1a785 | 2007-02-13 16:09:24 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 210 | def test_mtime(self): |
| 211 | mtime = 123456789 |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 212 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 213 | fWrite.write(data1) |
| 214 | with gzip.GzipFile(self.filename) as fRead: |
| 215 | dataRead = fRead.read() |
| 216 | self.assertEqual(dataRead, data1) |
| 217 | self.assertTrue(hasattr(fRead, 'mtime')) |
| 218 | self.assertEqual(fRead.mtime, mtime) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 219 | |
| 220 | def test_metadata(self): |
| 221 | mtime = 123456789 |
| 222 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 223 | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
| 224 | fWrite.write(data1) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 225 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 226 | with open(self.filename, 'rb') as fRead: |
| 227 | # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 228 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 229 | idBytes = fRead.read(2) |
| 230 | self.assertEqual(idBytes, '\x1f\x8b') # gzip ID |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 231 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 232 | cmByte = fRead.read(1) |
| 233 | self.assertEqual(cmByte, '\x08') # deflate |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 234 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 235 | flagsByte = fRead.read(1) |
| 236 | self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 237 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 238 | mtimeBytes = fRead.read(4) |
| 239 | self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 240 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 241 | xflByte = fRead.read(1) |
| 242 | self.assertEqual(xflByte, '\x02') # maximum compression |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 243 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 244 | osByte = fRead.read(1) |
| 245 | self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 246 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 247 | # Since the FNAME flag is set, the zero-terminated filename follows. |
| 248 | # RFC 1952 specifies that this is the name of the input file, if any. |
| 249 | # However, the gzip module defaults to storing the name of the output |
| 250 | # file in this field. |
| 251 | expected = self.filename.encode('Latin-1') + '\x00' |
| 252 | nameBytes = fRead.read(len(expected)) |
| 253 | self.assertEqual(nameBytes, expected) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 254 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 255 | # Since no other flags were set, the header ends here. |
| 256 | # Rather than process the compressed data, let's seek to the trailer. |
| 257 | fRead.seek(os.stat(self.filename).st_size - 8) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 258 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 259 | crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1] |
| 260 | self.assertEqual(crc32Bytes, '\xaf\xd7d\x83') |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 261 | |
Brian Curtin | 31cf8d0 | 2010-10-13 23:51:19 +0000 | [diff] [blame] | 262 | isizeBytes = fRead.read(4) |
| 263 | self.assertEqual(isizeBytes, struct.pack('<i', len(data1))) |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | b74fc2b | 2009-01-10 16:13:45 +0000 | [diff] [blame] | 265 | def test_with_open(self): |
| 266 | # GzipFile supports the context management protocol |
| 267 | with gzip.GzipFile(self.filename, "wb") as f: |
| 268 | f.write(b"xxx") |
| 269 | f = gzip.GzipFile(self.filename, "rb") |
| 270 | f.close() |
| 271 | try: |
| 272 | with f: |
| 273 | pass |
| 274 | except ValueError: |
| 275 | pass |
| 276 | else: |
| 277 | self.fail("__enter__ on a closed file didn't raise an exception") |
| 278 | try: |
| 279 | with gzip.GzipFile(self.filename, "wb") as f: |
Ezio Melotti | dde5b94 | 2010-02-03 05:37:26 +0000 | [diff] [blame] | 280 | 1 // 0 |
Antoine Pitrou | b74fc2b | 2009-01-10 16:13:45 +0000 | [diff] [blame] | 281 | except ZeroDivisionError: |
| 282 | pass |
| 283 | else: |
Ezio Melotti | dde5b94 | 2010-02-03 05:37:26 +0000 | [diff] [blame] | 284 | self.fail("1 // 0 didn't raise an exception") |
Antoine Pitrou | b74fc2b | 2009-01-10 16:13:45 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | 5a9112c | 2010-01-13 14:32:10 +0000 | [diff] [blame] | 286 | def test_zero_padded_file(self): |
| 287 | with gzip.GzipFile(self.filename, "wb") as f: |
| 288 | f.write(data1 * 50) |
| 289 | |
| 290 | # Pad the file with zeroes |
| 291 | with open(self.filename, "ab") as f: |
| 292 | f.write("\x00" * 50) |
| 293 | |
| 294 | with gzip.GzipFile(self.filename, "rb") as f: |
| 295 | d = f.read() |
| 296 | self.assertEqual(d, data1 * 50, "Incorrect data in file") |
| 297 | |
Nadeem Vawda | d7664de | 2012-01-19 00:40:46 +0200 | [diff] [blame] | 298 | def test_fileobj_from_fdopen(self): |
| 299 | # Issue #13781: Creating a GzipFile using a fileobj from os.fdopen() |
| 300 | # should not embed the fake filename "<fdopen>" in the output file. |
| 301 | fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT) |
| 302 | with os.fdopen(fd, "wb") as f: |
| 303 | with gzip.GzipFile(fileobj=f, mode="w") as g: |
| 304 | self.assertEqual(g.name, "") |
| 305 | |
Serhiy Storchaka | 371432b | 2013-04-08 22:33:55 +0300 | [diff] [blame] | 306 | def test_read_with_extra(self): |
| 307 | # Gzip data with an extra field |
| 308 | gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff' |
| 309 | b'\x05\x00Extra' |
| 310 | b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00') |
| 311 | with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f: |
| 312 | self.assertEqual(f.read(), b'Test') |
Serhiy Storchaka | 353e54e | 2013-01-22 17:13:26 +0200 | [diff] [blame] | 313 | |
Andrew M. Kuchling | a6f68e1 | 2005-06-09 14:12:36 +0000 | [diff] [blame] | 314 | def test_main(verbose=None): |
| 315 | test_support.run_unittest(TestGzip) |
| 316 | |
| 317 | if __name__ == "__main__": |
| 318 | test_main(verbose=True) |