blob: c94d54e02e62692201f41412aea5eb4dbc55bde9 [file] [log] [blame]
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +00001"""Test script for the gzip module.
2"""
3
4import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Christian Heimes05e8be12008-02-23 18:30:17 +00006import os
Antoine Pitroub1f88352010-01-03 22:37:40 +00007import io
Antoine Pitrou42db3ef2009-01-04 21:37:59 +00008import struct
Ezio Melotti78ea2022009-09-12 18:41:20 +00009gzip = support.import_module('gzip')
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000010
Walter Dörwald5b1284d2007-06-06 16:43:59 +000011data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000012 PyObject *RetVal;
13 int flushmode = Z_FINISH;
14 unsigned long start_total_out;
15
16"""
17
Walter Dörwald5b1284d2007-06-06 16:43:59 +000018data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */
Neal Norwitz014f1032004-07-29 03:55:56 +000019/* See http://www.gzip.org/zlib/
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000020/* See http://www.winimage.com/zLibDll for Windows */
21"""
22
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000023
Antoine Pitrou7b969842010-09-23 16:22:51 +000024class 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 Vawda1b8a14d2012-05-06 15:17:52 +020035class BaseTest(unittest.TestCase):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000036 filename = support.TESTFN
Tim Peters5cfb05e2004-07-27 21:02:02 +000037
Georg Brandlb533e262008-05-25 18:19:30 +000038 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000039 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000040
Georg Brandlb533e262008-05-25 18:19:30 +000041 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000042 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000043
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000044
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +020045class TestGzip(BaseTest):
Georg Brandlb533e262008-05-25 18:19:30 +000046 def test_write(self):
Brian Curtin28f96b52010-10-13 02:21:42 +000047 with gzip.GzipFile(self.filename, 'wb') as f:
48 f.write(data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000049
Brian Curtin28f96b52010-10-13 02:21:42 +000050 # 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. Kuchling85ab7382000-07-29 20:18:34 +000056
Georg Brandlb533e262008-05-25 18:19:30 +000057 # Test multiple close() calls.
58 f.close()
59
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000060 def test_read(self):
61 self.test_write()
62 # Try reading.
Brian Curtin28f96b52010-10-13 02:21:42 +000063 with gzip.GzipFile(self.filename, 'r') as f:
64 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000065 self.assertEqual(d, data1*50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000066
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +020067 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 Pitrou7980eaa2010-10-06 21:21:18 +000082 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. Kuchlinga6f68e12005-06-09 14:12:36 +0000104 def test_append(self):
105 self.test_write()
106 # Append to the previous file
Brian Curtin28f96b52010-10-13 02:21:42 +0000107 with gzip.GzipFile(self.filename, 'ab') as f:
108 f.write(data2 * 15)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +0000109
Brian Curtin28f96b52010-10-13 02:21:42 +0000110 with gzip.GzipFile(self.filename, 'rb') as f:
111 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000112 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +0000113
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000114 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 Vawda1b8a14d2012-05-06 15:17:52 +0200118 with gzip.GzipFile(self.filename, 'wb', 9) as f:
Walter Dörwald5b1284d2007-06-06 16:43:59 +0000119 f.write(b'a')
Brian Curtin28f96b52010-10-13 02:21:42 +0000120 for i in range(0, 200):
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200121 with gzip.GzipFile(self.filename, "ab", 9) as f: # append
Brian Curtin28f96b52010-10-13 02:21:42 +0000122 f.write(b'a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000123
124 # Try reading the file
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200125 with gzip.GzipFile(self.filename, "rb") as zgfile:
Brian Curtin28f96b52010-10-13 02:21:42 +0000126 contents = b""
127 while 1:
128 ztxt = zgfile.read(8192)
129 contents += ztxt
130 if not ztxt: break
Ezio Melottib3aedd42010-11-20 19:04:17 +0000131 self.assertEqual(contents, b'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000132
Antoine Pitroub1f88352010-01-03 22:37:40 +0000133 def test_buffered_reader(self):
134 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
135 # performance.
136 self.test_write()
137
Brian Curtin28f96b52010-10-13 02:21:42 +0000138 with gzip.GzipFile(self.filename, 'rb') as f:
139 with io.BufferedReader(f) as r:
140 lines = [line for line in r]
Antoine Pitroub1f88352010-01-03 22:37:40 +0000141
Ezio Melottid8b509b2011-09-28 17:37:55 +0300142 self.assertEqual(lines, 50 * data1.splitlines(keepends=True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000143
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000144 def test_readline(self):
145 self.test_write()
146 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000147
Brian Curtin28f96b52010-10-13 02:21:42 +0000148 with gzip.GzipFile(self.filename, 'rb') as f:
149 line_length = 0
150 while 1:
151 L = f.readline(line_length)
152 if not L and line_length != 0: break
153 self.assertTrue(len(L) <= line_length)
154 line_length = (line_length + 1) % 50
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000155
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000156 def test_readlines(self):
157 self.test_write()
158 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000159
Brian Curtin28f96b52010-10-13 02:21:42 +0000160 with gzip.GzipFile(self.filename, 'rb') as f:
161 L = f.readlines()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000162
Brian Curtin28f96b52010-10-13 02:21:42 +0000163 with gzip.GzipFile(self.filename, 'rb') as f:
164 while 1:
165 L = f.readlines(150)
166 if L == []: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000167
168 def test_seek_read(self):
169 self.test_write()
170 # Try seek, read test
171
Brian Curtin28f96b52010-10-13 02:21:42 +0000172 with gzip.GzipFile(self.filename) as f:
173 while 1:
174 oldpos = f.tell()
175 line1 = f.readline()
176 if not line1: break
177 newpos = f.tell()
178 f.seek(oldpos) # negative seek
179 if len(line1)>10:
180 amount = 10
181 else:
182 amount = len(line1)
183 line2 = f.read(amount)
184 self.assertEqual(line1[:amount], line2)
185 f.seek(newpos) # positive seek
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000186
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 def test_seek_whence(self):
188 self.test_write()
189 # Try seek(whence=1), read test
190
Brian Curtin28f96b52010-10-13 02:21:42 +0000191 with gzip.GzipFile(self.filename) as f:
192 f.read(10)
193 f.seek(10, whence=1)
194 y = f.read(10)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000195 self.assertEqual(y, data1[20:30])
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000196
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000197 def test_seek_write(self):
198 # Try seek, write test
Brian Curtin28f96b52010-10-13 02:21:42 +0000199 with gzip.GzipFile(self.filename, 'w') as f:
200 for pos in range(0, 256, 16):
201 f.seek(pos)
202 f.write(b'GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000203
204 def test_mode(self):
205 self.test_write()
Brian Curtin28f96b52010-10-13 02:21:42 +0000206 with gzip.GzipFile(self.filename, 'r') as f:
207 self.assertEqual(f.myfileobj.mode, 'rb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000208
Thomas Wouterscf297e42007-02-23 15:07:44 +0000209 def test_1647484(self):
210 for mode in ('wb', 'rb'):
Brian Curtin28f96b52010-10-13 02:21:42 +0000211 with gzip.GzipFile(self.filename, mode) as f:
212 self.assertTrue(hasattr(f, "name"))
213 self.assertEqual(f.name, self.filename)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000214
Georg Brandl9f1c1dc2010-11-20 11:25:01 +0000215 def test_paddedfile_getattr(self):
216 self.test_write()
217 with gzip.GzipFile(self.filename, 'rb') as f:
218 self.assertTrue(hasattr(f.fileobj, "name"))
219 self.assertEqual(f.fileobj.name, self.filename)
220
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000221 def test_mtime(self):
222 mtime = 123456789
Brian Curtin28f96b52010-10-13 02:21:42 +0000223 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
224 fWrite.write(data1)
225 with gzip.GzipFile(self.filename) as fRead:
226 dataRead = fRead.read()
227 self.assertEqual(dataRead, data1)
228 self.assertTrue(hasattr(fRead, 'mtime'))
229 self.assertEqual(fRead.mtime, mtime)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000230
231 def test_metadata(self):
232 mtime = 123456789
233
Brian Curtin28f96b52010-10-13 02:21:42 +0000234 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
235 fWrite.write(data1)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000236
Brian Curtin28f96b52010-10-13 02:21:42 +0000237 with open(self.filename, 'rb') as fRead:
238 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000239
Brian Curtin28f96b52010-10-13 02:21:42 +0000240 idBytes = fRead.read(2)
241 self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000242
Brian Curtin28f96b52010-10-13 02:21:42 +0000243 cmByte = fRead.read(1)
244 self.assertEqual(cmByte, b'\x08') # deflate
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000245
Brian Curtin28f96b52010-10-13 02:21:42 +0000246 flagsByte = fRead.read(1)
247 self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000248
Brian Curtin28f96b52010-10-13 02:21:42 +0000249 mtimeBytes = fRead.read(4)
250 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000251
Brian Curtin28f96b52010-10-13 02:21:42 +0000252 xflByte = fRead.read(1)
253 self.assertEqual(xflByte, b'\x02') # maximum compression
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000254
Brian Curtin28f96b52010-10-13 02:21:42 +0000255 osByte = fRead.read(1)
256 self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000257
Brian Curtin28f96b52010-10-13 02:21:42 +0000258 # Since the FNAME flag is set, the zero-terminated filename follows.
259 # RFC 1952 specifies that this is the name of the input file, if any.
260 # However, the gzip module defaults to storing the name of the output
261 # file in this field.
262 expected = self.filename.encode('Latin-1') + b'\x00'
263 nameBytes = fRead.read(len(expected))
264 self.assertEqual(nameBytes, expected)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000265
Brian Curtin28f96b52010-10-13 02:21:42 +0000266 # Since no other flags were set, the header ends here.
267 # Rather than process the compressed data, let's seek to the trailer.
268 fRead.seek(os.stat(self.filename).st_size - 8)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000269
Brian Curtin28f96b52010-10-13 02:21:42 +0000270 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
271 self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000272
Brian Curtin28f96b52010-10-13 02:21:42 +0000273 isizeBytes = fRead.read(4)
274 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000275
Antoine Pitrou308705e2009-01-10 16:22:51 +0000276 def test_with_open(self):
277 # GzipFile supports the context management protocol
278 with gzip.GzipFile(self.filename, "wb") as f:
279 f.write(b"xxx")
280 f = gzip.GzipFile(self.filename, "rb")
281 f.close()
282 try:
283 with f:
284 pass
285 except ValueError:
286 pass
287 else:
288 self.fail("__enter__ on a closed file didn't raise an exception")
289 try:
290 with gzip.GzipFile(self.filename, "wb") as f:
291 1/0
292 except ZeroDivisionError:
293 pass
294 else:
295 self.fail("1/0 didn't raise an exception")
296
Antoine Pitrou8e33fd72010-01-13 14:37:26 +0000297 def test_zero_padded_file(self):
298 with gzip.GzipFile(self.filename, "wb") as f:
299 f.write(data1 * 50)
300
301 # Pad the file with zeroes
302 with open(self.filename, "ab") as f:
303 f.write(b"\x00" * 50)
304
305 with gzip.GzipFile(self.filename, "rb") as f:
306 d = f.read()
307 self.assertEqual(d, data1 * 50, "Incorrect data in file")
308
Antoine Pitrou7b969842010-09-23 16:22:51 +0000309 def test_non_seekable_file(self):
310 uncompressed = data1 * 50
311 buf = UnseekableIO()
312 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
313 f.write(uncompressed)
314 compressed = buf.getvalue()
315 buf = UnseekableIO(compressed)
316 with gzip.GzipFile(fileobj=buf, mode="rb") as f:
317 self.assertEqual(f.read(), uncompressed)
318
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +0000319 def test_peek(self):
320 uncompressed = data1 * 200
321 with gzip.GzipFile(self.filename, "wb") as f:
322 f.write(uncompressed)
323
324 def sizes():
325 while True:
326 for n in range(5, 50, 10):
327 yield n
328
329 with gzip.GzipFile(self.filename, "rb") as f:
330 f.max_read_chunk = 33
331 nread = 0
332 for n in sizes():
333 s = f.peek(n)
334 if s == b'':
335 break
336 self.assertEqual(f.read(len(s)), s)
337 nread += len(s)
338 self.assertEqual(f.read(100), b'')
339 self.assertEqual(nread, len(uncompressed))
340
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +0200341 def test_textio_readlines(self):
342 # Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile.
Ezio Melottid8b509b2011-09-28 17:37:55 +0300343 lines = (data1 * 50).decode("ascii").splitlines(keepends=True)
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +0200344 self.test_write()
345 with gzip.GzipFile(self.filename, 'r') as f:
346 with io.TextIOWrapper(f, encoding="ascii") as t:
347 self.assertEqual(t.readlines(), lines)
348
Nadeem Vawda892b0b92012-01-18 09:25:58 +0200349 def test_fileobj_from_fdopen(self):
350 # Issue #13781: Opening a GzipFile for writing fails when using a
351 # fileobj created with os.fdopen().
352 fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
353 with os.fdopen(fd, "wb") as f:
354 with gzip.GzipFile(fileobj=f, mode="w") as g:
355 pass
356
Nadeem Vawda103e8112012-06-20 01:35:22 +0200357 def test_bytes_filename(self):
358 str_filename = self.filename
359 try:
360 bytes_filename = str_filename.encode("ascii")
361 except UnicodeEncodeError:
362 self.skipTest("Temporary file name needs to be ASCII")
363 with gzip.GzipFile(bytes_filename, "wb") as f:
364 f.write(data1 * 50)
365 with gzip.GzipFile(bytes_filename, "rb") as f:
366 self.assertEqual(f.read(), data1 * 50)
367 # Sanity check that we are actually operating on the right file.
368 with gzip.GzipFile(str_filename, "rb") as f:
369 self.assertEqual(f.read(), data1 * 50)
370
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000371 # Testing compress/decompress shortcut functions
372
373 def test_compress(self):
374 for data in [data1, data2]:
375 for args in [(), (1,), (6,), (9,)]:
376 datac = gzip.compress(data, *args)
377 self.assertEqual(type(datac), bytes)
378 with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f:
379 self.assertEqual(f.read(), data)
380
381 def test_decompress(self):
382 for data in (data1, data2):
383 buf = io.BytesIO()
384 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
385 f.write(data)
386 self.assertEqual(gzip.decompress(buf.getvalue()), data)
387 # Roundtrip with compress
388 datac = gzip.compress(data)
389 self.assertEqual(gzip.decompress(datac), data)
390
Serhiy Storchaka7e69f002013-04-08 22:35:02 +0300391 def test_read_with_extra(self):
392 # Gzip data with an extra field
393 gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'
394 b'\x05\x00Extra'
395 b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00')
396 with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
397 self.assertEqual(f.read(), b'Test')
Nadeem Vawda7e126202012-05-06 15:04:01 +0200398
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200399class TestOpen(BaseTest):
400 def test_binary_modes(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200401 uncompressed = data1 * 50
402 with gzip.open(self.filename, "wb") as f:
403 f.write(uncompressed)
404 with open(self.filename, "rb") as f:
405 file_data = gzip.decompress(f.read())
406 self.assertEqual(file_data, uncompressed)
407 with gzip.open(self.filename, "rb") as f:
408 self.assertEqual(f.read(), uncompressed)
409 with gzip.open(self.filename, "ab") as f:
410 f.write(uncompressed)
411 with open(self.filename, "rb") as f:
412 file_data = gzip.decompress(f.read())
413 self.assertEqual(file_data, uncompressed * 2)
414
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200415 def test_implicit_binary_modes(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200416 # Test implicit binary modes (no "b" or "t" in mode string).
417 uncompressed = data1 * 50
418 with gzip.open(self.filename, "w") as f:
419 f.write(uncompressed)
420 with open(self.filename, "rb") as f:
421 file_data = gzip.decompress(f.read())
422 self.assertEqual(file_data, uncompressed)
423 with gzip.open(self.filename, "r") as f:
424 self.assertEqual(f.read(), uncompressed)
425 with gzip.open(self.filename, "a") as f:
426 f.write(uncompressed)
427 with open(self.filename, "rb") as f:
428 file_data = gzip.decompress(f.read())
429 self.assertEqual(file_data, uncompressed * 2)
430
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200431 def test_text_modes(self):
Nadeem Vawda11328e42012-05-06 19:24:18 +0200432 uncompressed = data1.decode("ascii") * 50
433 uncompressed_raw = uncompressed.replace("\n", os.linesep)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200434 with gzip.open(self.filename, "wt") as f:
435 f.write(uncompressed)
436 with open(self.filename, "rb") as f:
437 file_data = gzip.decompress(f.read()).decode("ascii")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200438 self.assertEqual(file_data, uncompressed_raw)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200439 with gzip.open(self.filename, "rt") as f:
440 self.assertEqual(f.read(), uncompressed)
441 with gzip.open(self.filename, "at") as f:
442 f.write(uncompressed)
443 with open(self.filename, "rb") as f:
444 file_data = gzip.decompress(f.read()).decode("ascii")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200445 self.assertEqual(file_data, uncompressed_raw * 2)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200446
Nadeem Vawda68721012012-06-04 23:21:38 +0200447 def test_fileobj(self):
448 uncompressed_bytes = data1 * 50
449 uncompressed_str = uncompressed_bytes.decode("ascii")
450 compressed = gzip.compress(uncompressed_bytes)
451 with gzip.open(io.BytesIO(compressed), "r") as f:
452 self.assertEqual(f.read(), uncompressed_bytes)
453 with gzip.open(io.BytesIO(compressed), "rb") as f:
454 self.assertEqual(f.read(), uncompressed_bytes)
455 with gzip.open(io.BytesIO(compressed), "rt") as f:
456 self.assertEqual(f.read(), uncompressed_str)
457
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200458 def test_bad_params(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200459 # Test invalid parameter combinations.
Nadeem Vawda68721012012-06-04 23:21:38 +0200460 with self.assertRaises(TypeError):
461 gzip.open(123.456)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200462 with self.assertRaises(ValueError):
463 gzip.open(self.filename, "wbt")
464 with self.assertRaises(ValueError):
465 gzip.open(self.filename, "rb", encoding="utf-8")
466 with self.assertRaises(ValueError):
467 gzip.open(self.filename, "rb", errors="ignore")
468 with self.assertRaises(ValueError):
469 gzip.open(self.filename, "rb", newline="\n")
470
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200471 def test_encoding(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200472 # Test non-default encoding.
Nadeem Vawda11328e42012-05-06 19:24:18 +0200473 uncompressed = data1.decode("ascii") * 50
474 uncompressed_raw = uncompressed.replace("\n", os.linesep)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200475 with gzip.open(self.filename, "wt", encoding="utf-16") as f:
476 f.write(uncompressed)
477 with open(self.filename, "rb") as f:
478 file_data = gzip.decompress(f.read()).decode("utf-16")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200479 self.assertEqual(file_data, uncompressed_raw)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200480 with gzip.open(self.filename, "rt", encoding="utf-16") as f:
481 self.assertEqual(f.read(), uncompressed)
482
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200483 def test_encoding_error_handler(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200484 # Test with non-default encoding error handler.
485 with gzip.open(self.filename, "wb") as f:
486 f.write(b"foo\xffbar")
487 with gzip.open(self.filename, "rt", encoding="ascii", errors="ignore") \
488 as f:
489 self.assertEqual(f.read(), "foobar")
490
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200491 def test_newline(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200492 # Test with explicit newline (universal newline mode disabled).
493 uncompressed = data1.decode("ascii") * 50
Nadeem Vawda9d9dc8e2012-05-06 16:25:35 +0200494 with gzip.open(self.filename, "wt", newline="\n") as f:
Nadeem Vawda7e126202012-05-06 15:04:01 +0200495 f.write(uncompressed)
496 with gzip.open(self.filename, "rt", newline="\r") as f:
497 self.assertEqual(f.readlines(), [uncompressed])
498
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000499def test_main(verbose=None):
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200500 support.run_unittest(TestGzip, TestOpen)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000501
502if __name__ == "__main__":
503 test_main(verbose=True)