blob: aeafc6536d7a43893c4d306e6df17c3145249dd2 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +00002"""Test script for the gzip module.
3"""
4
5import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Christian Heimes05e8be12008-02-23 18:30:17 +00007import os
Antoine Pitroub1f88352010-01-03 22:37:40 +00008import io
Antoine Pitrou42db3ef2009-01-04 21:37:59 +00009import struct
Ezio Melotti78ea2022009-09-12 18:41:20 +000010gzip = support.import_module('gzip')
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000011
Walter Dörwald5b1284d2007-06-06 16:43:59 +000012data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000013 PyObject *RetVal;
14 int flushmode = Z_FINISH;
15 unsigned long start_total_out;
16
17"""
18
Walter Dörwald5b1284d2007-06-06 16:43:59 +000019data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */
Neal Norwitz014f1032004-07-29 03:55:56 +000020/* See http://www.gzip.org/zlib/
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000021/* See http://www.winimage.com/zLibDll for Windows */
22"""
23
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000024
Antoine Pitrou7b969842010-09-23 16:22:51 +000025class 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
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +020036class BaseTest(unittest.TestCase):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000037 filename = support.TESTFN
Tim Peters5cfb05e2004-07-27 21:02:02 +000038
Georg Brandlb533e262008-05-25 18:19:30 +000039 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000040 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000041
Georg Brandlb533e262008-05-25 18:19:30 +000042 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000043 support.unlink(self.filename)
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +000044
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000045
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +020046class TestGzip(BaseTest):
Georg Brandlb533e262008-05-25 18:19:30 +000047 def test_write(self):
Brian Curtin28f96b52010-10-13 02:21:42 +000048 with gzip.GzipFile(self.filename, 'wb') as f:
49 f.write(data1 * 50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000050
Brian Curtin28f96b52010-10-13 02:21:42 +000051 # Try flush and fileno.
52 f.flush()
53 f.fileno()
54 if hasattr(os, 'fsync'):
55 os.fsync(f.fileno())
56 f.close()
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000057
Georg Brandlb533e262008-05-25 18:19:30 +000058 # Test multiple close() calls.
59 f.close()
60
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000061 def test_read(self):
62 self.test_write()
63 # Try reading.
Brian Curtin28f96b52010-10-13 02:21:42 +000064 with gzip.GzipFile(self.filename, 'r') as f:
65 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +000066 self.assertEqual(d, data1*50)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +000067
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +020068 def test_read1(self):
69 self.test_write()
70 blocks = []
71 nread = 0
72 with gzip.GzipFile(self.filename, 'r') as f:
73 while True:
74 d = f.read1()
75 if not d:
76 break
77 blocks.append(d)
78 nread += len(d)
79 # Check that position was updated correctly (see issue10791).
80 self.assertEqual(f.tell(), nread)
81 self.assertEqual(b''.join(blocks), data1 * 50)
82
Antoine Pitrou7980eaa2010-10-06 21:21:18 +000083 def test_io_on_closed_object(self):
84 # Test that I/O operations on closed GzipFile objects raise a
85 # ValueError, just like the corresponding functions on file objects.
86
87 # Write to a file, open it for reading, then close it.
88 self.test_write()
89 f = gzip.GzipFile(self.filename, 'r')
90 f.close()
91 with self.assertRaises(ValueError):
92 f.read(1)
93 with self.assertRaises(ValueError):
94 f.seek(0)
95 with self.assertRaises(ValueError):
96 f.tell()
97 # Open the file for writing, then close it.
98 f = gzip.GzipFile(self.filename, 'w')
99 f.close()
100 with self.assertRaises(ValueError):
101 f.write(b'')
102 with self.assertRaises(ValueError):
103 f.flush()
104
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000105 def test_append(self):
106 self.test_write()
107 # Append to the previous file
Brian Curtin28f96b52010-10-13 02:21:42 +0000108 with gzip.GzipFile(self.filename, 'ab') as f:
109 f.write(data2 * 15)
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +0000110
Brian Curtin28f96b52010-10-13 02:21:42 +0000111 with gzip.GzipFile(self.filename, 'rb') as f:
112 d = f.read()
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000113 self.assertEqual(d, (data1*50) + (data2*15))
Andrew M. Kuchling85ab7382000-07-29 20:18:34 +0000114
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000115 def test_many_append(self):
116 # Bug #1074261 was triggered when reading a file that contained
117 # many, many members. Create such a file and verify that reading it
118 # works.
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200119 with gzip.GzipFile(self.filename, 'wb', 9) as f:
Walter Dörwald5b1284d2007-06-06 16:43:59 +0000120 f.write(b'a')
Brian Curtin28f96b52010-10-13 02:21:42 +0000121 for i in range(0, 200):
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200122 with gzip.GzipFile(self.filename, "ab", 9) as f: # append
Brian Curtin28f96b52010-10-13 02:21:42 +0000123 f.write(b'a')
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000124
125 # Try reading the file
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200126 with gzip.GzipFile(self.filename, "rb") as zgfile:
Brian Curtin28f96b52010-10-13 02:21:42 +0000127 contents = b""
128 while 1:
129 ztxt = zgfile.read(8192)
130 contents += ztxt
131 if not ztxt: break
Ezio Melottib3aedd42010-11-20 19:04:17 +0000132 self.assertEqual(contents, b'a'*201)
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000133
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200134 def test_exclusive_write(self):
135 with gzip.GzipFile(self.filename, 'xb') as f:
136 f.write(data1 * 50)
137 with gzip.GzipFile(self.filename, 'rb') as f:
138 self.assertEqual(f.read(), data1 * 50)
139 with self.assertRaises(FileExistsError):
140 gzip.GzipFile(self.filename, 'xb')
141
Antoine Pitroub1f88352010-01-03 22:37:40 +0000142 def test_buffered_reader(self):
143 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
144 # performance.
145 self.test_write()
146
Brian Curtin28f96b52010-10-13 02:21:42 +0000147 with gzip.GzipFile(self.filename, 'rb') as f:
148 with io.BufferedReader(f) as r:
149 lines = [line for line in r]
Antoine Pitroub1f88352010-01-03 22:37:40 +0000150
Ezio Melottid8b509b2011-09-28 17:37:55 +0300151 self.assertEqual(lines, 50 * data1.splitlines(keepends=True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000152
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000153 def test_readline(self):
154 self.test_write()
155 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000156
Brian Curtin28f96b52010-10-13 02:21:42 +0000157 with gzip.GzipFile(self.filename, 'rb') as f:
158 line_length = 0
159 while 1:
160 L = f.readline(line_length)
161 if not L and line_length != 0: break
162 self.assertTrue(len(L) <= line_length)
163 line_length = (line_length + 1) % 50
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000164
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000165 def test_readlines(self):
166 self.test_write()
167 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000168
Brian Curtin28f96b52010-10-13 02:21:42 +0000169 with gzip.GzipFile(self.filename, 'rb') as f:
170 L = f.readlines()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000171
Brian Curtin28f96b52010-10-13 02:21:42 +0000172 with gzip.GzipFile(self.filename, 'rb') as f:
173 while 1:
174 L = f.readlines(150)
175 if L == []: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000176
177 def test_seek_read(self):
178 self.test_write()
179 # Try seek, read test
180
Brian Curtin28f96b52010-10-13 02:21:42 +0000181 with gzip.GzipFile(self.filename) as f:
182 while 1:
183 oldpos = f.tell()
184 line1 = f.readline()
185 if not line1: break
186 newpos = f.tell()
187 f.seek(oldpos) # negative seek
188 if len(line1)>10:
189 amount = 10
190 else:
191 amount = len(line1)
192 line2 = f.read(amount)
193 self.assertEqual(line1[:amount], line2)
194 f.seek(newpos) # positive seek
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000195
Thomas Wouters89f507f2006-12-13 04:49:30 +0000196 def test_seek_whence(self):
197 self.test_write()
198 # Try seek(whence=1), read test
199
Brian Curtin28f96b52010-10-13 02:21:42 +0000200 with gzip.GzipFile(self.filename) as f:
201 f.read(10)
202 f.seek(10, whence=1)
203 y = f.read(10)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000204 self.assertEqual(y, data1[20:30])
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000205
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000206 def test_seek_write(self):
207 # Try seek, write test
Brian Curtin28f96b52010-10-13 02:21:42 +0000208 with gzip.GzipFile(self.filename, 'w') as f:
209 for pos in range(0, 256, 16):
210 f.seek(pos)
211 f.write(b'GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000212
213 def test_mode(self):
214 self.test_write()
Brian Curtin28f96b52010-10-13 02:21:42 +0000215 with gzip.GzipFile(self.filename, 'r') as f:
216 self.assertEqual(f.myfileobj.mode, 'rb')
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200217 support.unlink(self.filename)
218 with gzip.GzipFile(self.filename, 'x') as f:
219 self.assertEqual(f.myfileobj.mode, 'xb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000220
Thomas Wouterscf297e42007-02-23 15:07:44 +0000221 def test_1647484(self):
222 for mode in ('wb', 'rb'):
Brian Curtin28f96b52010-10-13 02:21:42 +0000223 with gzip.GzipFile(self.filename, mode) as f:
224 self.assertTrue(hasattr(f, "name"))
225 self.assertEqual(f.name, self.filename)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000226
Georg Brandl9f1c1dc2010-11-20 11:25:01 +0000227 def test_paddedfile_getattr(self):
228 self.test_write()
229 with gzip.GzipFile(self.filename, 'rb') as f:
230 self.assertTrue(hasattr(f.fileobj, "name"))
231 self.assertEqual(f.fileobj.name, self.filename)
232
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000233 def test_mtime(self):
234 mtime = 123456789
Brian Curtin28f96b52010-10-13 02:21:42 +0000235 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
236 fWrite.write(data1)
237 with gzip.GzipFile(self.filename) as fRead:
238 dataRead = fRead.read()
239 self.assertEqual(dataRead, data1)
240 self.assertTrue(hasattr(fRead, 'mtime'))
241 self.assertEqual(fRead.mtime, mtime)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000242
243 def test_metadata(self):
244 mtime = 123456789
245
Brian Curtin28f96b52010-10-13 02:21:42 +0000246 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
247 fWrite.write(data1)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000248
Brian Curtin28f96b52010-10-13 02:21:42 +0000249 with open(self.filename, 'rb') as fRead:
250 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000251
Brian Curtin28f96b52010-10-13 02:21:42 +0000252 idBytes = fRead.read(2)
253 self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000254
Brian Curtin28f96b52010-10-13 02:21:42 +0000255 cmByte = fRead.read(1)
256 self.assertEqual(cmByte, b'\x08') # deflate
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000257
Brian Curtin28f96b52010-10-13 02:21:42 +0000258 flagsByte = fRead.read(1)
259 self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000260
Brian Curtin28f96b52010-10-13 02:21:42 +0000261 mtimeBytes = fRead.read(4)
262 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000263
Brian Curtin28f96b52010-10-13 02:21:42 +0000264 xflByte = fRead.read(1)
265 self.assertEqual(xflByte, b'\x02') # maximum compression
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000266
Brian Curtin28f96b52010-10-13 02:21:42 +0000267 osByte = fRead.read(1)
268 self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000269
Brian Curtin28f96b52010-10-13 02:21:42 +0000270 # Since the FNAME flag is set, the zero-terminated filename follows.
271 # RFC 1952 specifies that this is the name of the input file, if any.
272 # However, the gzip module defaults to storing the name of the output
273 # file in this field.
274 expected = self.filename.encode('Latin-1') + b'\x00'
275 nameBytes = fRead.read(len(expected))
276 self.assertEqual(nameBytes, expected)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000277
Brian Curtin28f96b52010-10-13 02:21:42 +0000278 # Since no other flags were set, the header ends here.
279 # Rather than process the compressed data, let's seek to the trailer.
280 fRead.seek(os.stat(self.filename).st_size - 8)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000281
Brian Curtin28f96b52010-10-13 02:21:42 +0000282 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
283 self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000284
Brian Curtin28f96b52010-10-13 02:21:42 +0000285 isizeBytes = fRead.read(4)
286 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000287
Antoine Pitrou308705e2009-01-10 16:22:51 +0000288 def test_with_open(self):
289 # GzipFile supports the context management protocol
290 with gzip.GzipFile(self.filename, "wb") as f:
291 f.write(b"xxx")
292 f = gzip.GzipFile(self.filename, "rb")
293 f.close()
294 try:
295 with f:
296 pass
297 except ValueError:
298 pass
299 else:
300 self.fail("__enter__ on a closed file didn't raise an exception")
301 try:
302 with gzip.GzipFile(self.filename, "wb") as f:
303 1/0
304 except ZeroDivisionError:
305 pass
306 else:
307 self.fail("1/0 didn't raise an exception")
308
Antoine Pitrou8e33fd72010-01-13 14:37:26 +0000309 def test_zero_padded_file(self):
310 with gzip.GzipFile(self.filename, "wb") as f:
311 f.write(data1 * 50)
312
313 # Pad the file with zeroes
314 with open(self.filename, "ab") as f:
315 f.write(b"\x00" * 50)
316
317 with gzip.GzipFile(self.filename, "rb") as f:
318 d = f.read()
319 self.assertEqual(d, data1 * 50, "Incorrect data in file")
320
Antoine Pitrou7b969842010-09-23 16:22:51 +0000321 def test_non_seekable_file(self):
322 uncompressed = data1 * 50
323 buf = UnseekableIO()
324 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
325 f.write(uncompressed)
326 compressed = buf.getvalue()
327 buf = UnseekableIO(compressed)
328 with gzip.GzipFile(fileobj=buf, mode="rb") as f:
329 self.assertEqual(f.read(), uncompressed)
330
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +0000331 def test_peek(self):
332 uncompressed = data1 * 200
333 with gzip.GzipFile(self.filename, "wb") as f:
334 f.write(uncompressed)
335
336 def sizes():
337 while True:
338 for n in range(5, 50, 10):
339 yield n
340
341 with gzip.GzipFile(self.filename, "rb") as f:
342 f.max_read_chunk = 33
343 nread = 0
344 for n in sizes():
345 s = f.peek(n)
346 if s == b'':
347 break
348 self.assertEqual(f.read(len(s)), s)
349 nread += len(s)
350 self.assertEqual(f.read(100), b'')
351 self.assertEqual(nread, len(uncompressed))
352
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +0200353 def test_textio_readlines(self):
354 # Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile.
Ezio Melottid8b509b2011-09-28 17:37:55 +0300355 lines = (data1 * 50).decode("ascii").splitlines(keepends=True)
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +0200356 self.test_write()
357 with gzip.GzipFile(self.filename, 'r') as f:
358 with io.TextIOWrapper(f, encoding="ascii") as t:
359 self.assertEqual(t.readlines(), lines)
360
Nadeem Vawda892b0b92012-01-18 09:25:58 +0200361 def test_fileobj_from_fdopen(self):
362 # Issue #13781: Opening a GzipFile for writing fails when using a
363 # fileobj created with os.fdopen().
364 fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
365 with os.fdopen(fd, "wb") as f:
366 with gzip.GzipFile(fileobj=f, mode="w") as g:
367 pass
368
Nadeem Vawda103e8112012-06-20 01:35:22 +0200369 def test_bytes_filename(self):
370 str_filename = self.filename
371 try:
372 bytes_filename = str_filename.encode("ascii")
373 except UnicodeEncodeError:
374 self.skipTest("Temporary file name needs to be ASCII")
375 with gzip.GzipFile(bytes_filename, "wb") as f:
376 f.write(data1 * 50)
377 with gzip.GzipFile(bytes_filename, "rb") as f:
378 self.assertEqual(f.read(), data1 * 50)
379 # Sanity check that we are actually operating on the right file.
380 with gzip.GzipFile(str_filename, "rb") as f:
381 self.assertEqual(f.read(), data1 * 50)
382
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000383 # Testing compress/decompress shortcut functions
384
385 def test_compress(self):
386 for data in [data1, data2]:
387 for args in [(), (1,), (6,), (9,)]:
388 datac = gzip.compress(data, *args)
389 self.assertEqual(type(datac), bytes)
390 with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f:
391 self.assertEqual(f.read(), data)
392
393 def test_decompress(self):
394 for data in (data1, data2):
395 buf = io.BytesIO()
396 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
397 f.write(data)
398 self.assertEqual(gzip.decompress(buf.getvalue()), data)
399 # Roundtrip with compress
400 datac = gzip.compress(data)
401 self.assertEqual(gzip.decompress(datac), data)
402
Serhiy Storchaka7c3922f2013-01-22 17:01:59 +0200403 def test_read_truncated(self):
404 data = data1*50
405 # Drop the CRC (4 bytes) and file size (4 bytes).
406 truncated = gzip.compress(data)[:-8]
407 with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f:
408 self.assertRaises(EOFError, f.read)
409 with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f:
410 self.assertEqual(f.read(len(data)), data)
411 self.assertRaises(EOFError, f.read, 1)
412 # Incomplete 10-byte header.
413 for i in range(2, 10):
414 with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f:
415 self.assertRaises(EOFError, f.read, 1)
416
Serhiy Storchaka7e69f002013-04-08 22:35:02 +0300417 def test_read_with_extra(self):
418 # Gzip data with an extra field
419 gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'
420 b'\x05\x00Extra'
421 b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00')
422 with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
423 self.assertEqual(f.read(), b'Test')
Nadeem Vawda7e126202012-05-06 15:04:01 +0200424
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200425class TestOpen(BaseTest):
426 def test_binary_modes(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200427 uncompressed = data1 * 50
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200428
Nadeem Vawda7e126202012-05-06 15:04:01 +0200429 with gzip.open(self.filename, "wb") as f:
430 f.write(uncompressed)
431 with open(self.filename, "rb") as f:
432 file_data = gzip.decompress(f.read())
433 self.assertEqual(file_data, uncompressed)
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200434
Nadeem Vawda7e126202012-05-06 15:04:01 +0200435 with gzip.open(self.filename, "rb") as f:
436 self.assertEqual(f.read(), uncompressed)
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200437
Nadeem Vawda7e126202012-05-06 15:04:01 +0200438 with gzip.open(self.filename, "ab") as f:
439 f.write(uncompressed)
440 with open(self.filename, "rb") as f:
441 file_data = gzip.decompress(f.read())
442 self.assertEqual(file_data, uncompressed * 2)
443
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200444 with self.assertRaises(FileExistsError):
445 gzip.open(self.filename, "xb")
446 support.unlink(self.filename)
447 with gzip.open(self.filename, "xb") as f:
448 f.write(uncompressed)
449 with open(self.filename, "rb") as f:
450 file_data = gzip.decompress(f.read())
451 self.assertEqual(file_data, uncompressed)
452
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200453 def test_implicit_binary_modes(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200454 # Test implicit binary modes (no "b" or "t" in mode string).
455 uncompressed = data1 * 50
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200456
Nadeem Vawda7e126202012-05-06 15:04:01 +0200457 with gzip.open(self.filename, "w") as f:
458 f.write(uncompressed)
459 with open(self.filename, "rb") as f:
460 file_data = gzip.decompress(f.read())
461 self.assertEqual(file_data, uncompressed)
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200462
Nadeem Vawda7e126202012-05-06 15:04:01 +0200463 with gzip.open(self.filename, "r") as f:
464 self.assertEqual(f.read(), uncompressed)
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200465
Nadeem Vawda7e126202012-05-06 15:04:01 +0200466 with gzip.open(self.filename, "a") as f:
467 f.write(uncompressed)
468 with open(self.filename, "rb") as f:
469 file_data = gzip.decompress(f.read())
470 self.assertEqual(file_data, uncompressed * 2)
471
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200472 with self.assertRaises(FileExistsError):
473 gzip.open(self.filename, "x")
474 support.unlink(self.filename)
475 with gzip.open(self.filename, "x") as f:
476 f.write(uncompressed)
477 with open(self.filename, "rb") as f:
478 file_data = gzip.decompress(f.read())
479 self.assertEqual(file_data, uncompressed)
480
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200481 def test_text_modes(self):
Nadeem Vawda11328e42012-05-06 19:24:18 +0200482 uncompressed = data1.decode("ascii") * 50
483 uncompressed_raw = uncompressed.replace("\n", os.linesep)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200484 with gzip.open(self.filename, "wt") as f:
485 f.write(uncompressed)
486 with open(self.filename, "rb") as f:
487 file_data = gzip.decompress(f.read()).decode("ascii")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200488 self.assertEqual(file_data, uncompressed_raw)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200489 with gzip.open(self.filename, "rt") as f:
490 self.assertEqual(f.read(), uncompressed)
491 with gzip.open(self.filename, "at") as f:
492 f.write(uncompressed)
493 with open(self.filename, "rb") as f:
494 file_data = gzip.decompress(f.read()).decode("ascii")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200495 self.assertEqual(file_data, uncompressed_raw * 2)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200496
Nadeem Vawda68721012012-06-04 23:21:38 +0200497 def test_fileobj(self):
498 uncompressed_bytes = data1 * 50
499 uncompressed_str = uncompressed_bytes.decode("ascii")
500 compressed = gzip.compress(uncompressed_bytes)
501 with gzip.open(io.BytesIO(compressed), "r") as f:
502 self.assertEqual(f.read(), uncompressed_bytes)
503 with gzip.open(io.BytesIO(compressed), "rb") as f:
504 self.assertEqual(f.read(), uncompressed_bytes)
505 with gzip.open(io.BytesIO(compressed), "rt") as f:
506 self.assertEqual(f.read(), uncompressed_str)
507
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200508 def test_bad_params(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200509 # Test invalid parameter combinations.
Nadeem Vawda68721012012-06-04 23:21:38 +0200510 with self.assertRaises(TypeError):
511 gzip.open(123.456)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200512 with self.assertRaises(ValueError):
513 gzip.open(self.filename, "wbt")
514 with self.assertRaises(ValueError):
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200515 gzip.open(self.filename, "xbt")
516 with self.assertRaises(ValueError):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200517 gzip.open(self.filename, "rb", encoding="utf-8")
518 with self.assertRaises(ValueError):
519 gzip.open(self.filename, "rb", errors="ignore")
520 with self.assertRaises(ValueError):
521 gzip.open(self.filename, "rb", newline="\n")
522
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200523 def test_encoding(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200524 # Test non-default encoding.
Nadeem Vawda11328e42012-05-06 19:24:18 +0200525 uncompressed = data1.decode("ascii") * 50
526 uncompressed_raw = uncompressed.replace("\n", os.linesep)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200527 with gzip.open(self.filename, "wt", encoding="utf-16") as f:
528 f.write(uncompressed)
529 with open(self.filename, "rb") as f:
530 file_data = gzip.decompress(f.read()).decode("utf-16")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200531 self.assertEqual(file_data, uncompressed_raw)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200532 with gzip.open(self.filename, "rt", encoding="utf-16") as f:
533 self.assertEqual(f.read(), uncompressed)
534
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200535 def test_encoding_error_handler(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200536 # Test with non-default encoding error handler.
537 with gzip.open(self.filename, "wb") as f:
538 f.write(b"foo\xffbar")
539 with gzip.open(self.filename, "rt", encoding="ascii", errors="ignore") \
540 as f:
541 self.assertEqual(f.read(), "foobar")
542
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200543 def test_newline(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200544 # Test with explicit newline (universal newline mode disabled).
545 uncompressed = data1.decode("ascii") * 50
Nadeem Vawda9d9dc8e2012-05-06 16:25:35 +0200546 with gzip.open(self.filename, "wt", newline="\n") as f:
Nadeem Vawda7e126202012-05-06 15:04:01 +0200547 f.write(uncompressed)
548 with gzip.open(self.filename, "rt", newline="\r") as f:
549 self.assertEqual(f.readlines(), [uncompressed])
550
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000551def test_main(verbose=None):
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200552 support.run_unittest(TestGzip, TestOpen)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000553
554if __name__ == "__main__":
555 test_main(verbose=True)