blob: 37fe8538a79bff05b76188741a6a0039854af61a [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
Antoine Pitroub1f88352010-01-03 22:37:40 +0000134 def test_buffered_reader(self):
135 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
136 # performance.
137 self.test_write()
138
Brian Curtin28f96b52010-10-13 02:21:42 +0000139 with gzip.GzipFile(self.filename, 'rb') as f:
140 with io.BufferedReader(f) as r:
141 lines = [line for line in r]
Antoine Pitroub1f88352010-01-03 22:37:40 +0000142
Ezio Melottid8b509b2011-09-28 17:37:55 +0300143 self.assertEqual(lines, 50 * data1.splitlines(keepends=True))
Andrew M. Kuchling01cb47b2005-06-09 14:19:32 +0000144
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000145 def test_readline(self):
146 self.test_write()
147 # Try .readline() with varying line lengths
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000148
Brian Curtin28f96b52010-10-13 02:21:42 +0000149 with gzip.GzipFile(self.filename, 'rb') as f:
150 line_length = 0
151 while 1:
152 L = f.readline(line_length)
153 if not L and line_length != 0: break
154 self.assertTrue(len(L) <= line_length)
155 line_length = (line_length + 1) % 50
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000156
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000157 def test_readlines(self):
158 self.test_write()
159 # Try .readlines()
Andrew M. Kuchling605ebdd1999-03-25 21:50:27 +0000160
Brian Curtin28f96b52010-10-13 02:21:42 +0000161 with gzip.GzipFile(self.filename, 'rb') as f:
162 L = f.readlines()
Skip Montanaro12424bc2002-05-23 01:43:05 +0000163
Brian Curtin28f96b52010-10-13 02:21:42 +0000164 with gzip.GzipFile(self.filename, 'rb') as f:
165 while 1:
166 L = f.readlines(150)
167 if L == []: break
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000168
169 def test_seek_read(self):
170 self.test_write()
171 # Try seek, read test
172
Brian Curtin28f96b52010-10-13 02:21:42 +0000173 with gzip.GzipFile(self.filename) as f:
174 while 1:
175 oldpos = f.tell()
176 line1 = f.readline()
177 if not line1: break
178 newpos = f.tell()
179 f.seek(oldpos) # negative seek
180 if len(line1)>10:
181 amount = 10
182 else:
183 amount = len(line1)
184 line2 = f.read(amount)
185 self.assertEqual(line1[:amount], line2)
186 f.seek(newpos) # positive seek
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000187
Thomas Wouters89f507f2006-12-13 04:49:30 +0000188 def test_seek_whence(self):
189 self.test_write()
190 # Try seek(whence=1), read test
191
Brian Curtin28f96b52010-10-13 02:21:42 +0000192 with gzip.GzipFile(self.filename) as f:
193 f.read(10)
194 f.seek(10, whence=1)
195 y = f.read(10)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000196 self.assertEqual(y, data1[20:30])
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000197
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000198 def test_seek_write(self):
199 # Try seek, write test
Brian Curtin28f96b52010-10-13 02:21:42 +0000200 with gzip.GzipFile(self.filename, 'w') as f:
201 for pos in range(0, 256, 16):
202 f.seek(pos)
203 f.write(b'GZ\n')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000204
205 def test_mode(self):
206 self.test_write()
Brian Curtin28f96b52010-10-13 02:21:42 +0000207 with gzip.GzipFile(self.filename, 'r') as f:
208 self.assertEqual(f.myfileobj.mode, 'rb')
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000209
Thomas Wouterscf297e42007-02-23 15:07:44 +0000210 def test_1647484(self):
211 for mode in ('wb', 'rb'):
Brian Curtin28f96b52010-10-13 02:21:42 +0000212 with gzip.GzipFile(self.filename, mode) as f:
213 self.assertTrue(hasattr(f, "name"))
214 self.assertEqual(f.name, self.filename)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000215
Georg Brandl9f1c1dc2010-11-20 11:25:01 +0000216 def test_paddedfile_getattr(self):
217 self.test_write()
218 with gzip.GzipFile(self.filename, 'rb') as f:
219 self.assertTrue(hasattr(f.fileobj, "name"))
220 self.assertEqual(f.fileobj.name, self.filename)
221
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000222 def test_mtime(self):
223 mtime = 123456789
Brian Curtin28f96b52010-10-13 02:21:42 +0000224 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
225 fWrite.write(data1)
226 with gzip.GzipFile(self.filename) as fRead:
227 dataRead = fRead.read()
228 self.assertEqual(dataRead, data1)
229 self.assertTrue(hasattr(fRead, 'mtime'))
230 self.assertEqual(fRead.mtime, mtime)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000231
232 def test_metadata(self):
233 mtime = 123456789
234
Brian Curtin28f96b52010-10-13 02:21:42 +0000235 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
236 fWrite.write(data1)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000237
Brian Curtin28f96b52010-10-13 02:21:42 +0000238 with open(self.filename, 'rb') as fRead:
239 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000240
Brian Curtin28f96b52010-10-13 02:21:42 +0000241 idBytes = fRead.read(2)
242 self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000243
Brian Curtin28f96b52010-10-13 02:21:42 +0000244 cmByte = fRead.read(1)
245 self.assertEqual(cmByte, b'\x08') # deflate
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000246
Brian Curtin28f96b52010-10-13 02:21:42 +0000247 flagsByte = fRead.read(1)
248 self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000249
Brian Curtin28f96b52010-10-13 02:21:42 +0000250 mtimeBytes = fRead.read(4)
251 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000252
Brian Curtin28f96b52010-10-13 02:21:42 +0000253 xflByte = fRead.read(1)
254 self.assertEqual(xflByte, b'\x02') # maximum compression
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000255
Brian Curtin28f96b52010-10-13 02:21:42 +0000256 osByte = fRead.read(1)
257 self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000258
Brian Curtin28f96b52010-10-13 02:21:42 +0000259 # Since the FNAME flag is set, the zero-terminated filename follows.
260 # RFC 1952 specifies that this is the name of the input file, if any.
261 # However, the gzip module defaults to storing the name of the output
262 # file in this field.
263 expected = self.filename.encode('Latin-1') + b'\x00'
264 nameBytes = fRead.read(len(expected))
265 self.assertEqual(nameBytes, expected)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000266
Brian Curtin28f96b52010-10-13 02:21:42 +0000267 # Since no other flags were set, the header ends here.
268 # Rather than process the compressed data, let's seek to the trailer.
269 fRead.seek(os.stat(self.filename).st_size - 8)
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000270
Brian Curtin28f96b52010-10-13 02:21:42 +0000271 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
272 self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000273
Brian Curtin28f96b52010-10-13 02:21:42 +0000274 isizeBytes = fRead.read(4)
275 self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
Antoine Pitrou42db3ef2009-01-04 21:37:59 +0000276
Antoine Pitrou308705e2009-01-10 16:22:51 +0000277 def test_with_open(self):
278 # GzipFile supports the context management protocol
279 with gzip.GzipFile(self.filename, "wb") as f:
280 f.write(b"xxx")
281 f = gzip.GzipFile(self.filename, "rb")
282 f.close()
283 try:
284 with f:
285 pass
286 except ValueError:
287 pass
288 else:
289 self.fail("__enter__ on a closed file didn't raise an exception")
290 try:
291 with gzip.GzipFile(self.filename, "wb") as f:
292 1/0
293 except ZeroDivisionError:
294 pass
295 else:
296 self.fail("1/0 didn't raise an exception")
297
Antoine Pitrou8e33fd72010-01-13 14:37:26 +0000298 def test_zero_padded_file(self):
299 with gzip.GzipFile(self.filename, "wb") as f:
300 f.write(data1 * 50)
301
302 # Pad the file with zeroes
303 with open(self.filename, "ab") as f:
304 f.write(b"\x00" * 50)
305
306 with gzip.GzipFile(self.filename, "rb") as f:
307 d = f.read()
308 self.assertEqual(d, data1 * 50, "Incorrect data in file")
309
Antoine Pitrou7b969842010-09-23 16:22:51 +0000310 def test_non_seekable_file(self):
311 uncompressed = data1 * 50
312 buf = UnseekableIO()
313 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
314 f.write(uncompressed)
315 compressed = buf.getvalue()
316 buf = UnseekableIO(compressed)
317 with gzip.GzipFile(fileobj=buf, mode="rb") as f:
318 self.assertEqual(f.read(), uncompressed)
319
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +0000320 def test_peek(self):
321 uncompressed = data1 * 200
322 with gzip.GzipFile(self.filename, "wb") as f:
323 f.write(uncompressed)
324
325 def sizes():
326 while True:
327 for n in range(5, 50, 10):
328 yield n
329
330 with gzip.GzipFile(self.filename, "rb") as f:
331 f.max_read_chunk = 33
332 nread = 0
333 for n in sizes():
334 s = f.peek(n)
335 if s == b'':
336 break
337 self.assertEqual(f.read(len(s)), s)
338 nread += len(s)
339 self.assertEqual(f.read(100), b'')
340 self.assertEqual(nread, len(uncompressed))
341
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +0200342 def test_textio_readlines(self):
343 # Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile.
Ezio Melottid8b509b2011-09-28 17:37:55 +0300344 lines = (data1 * 50).decode("ascii").splitlines(keepends=True)
Antoine Pitrou4ec4b0c2011-04-04 21:00:37 +0200345 self.test_write()
346 with gzip.GzipFile(self.filename, 'r') as f:
347 with io.TextIOWrapper(f, encoding="ascii") as t:
348 self.assertEqual(t.readlines(), lines)
349
Nadeem Vawda892b0b92012-01-18 09:25:58 +0200350 def test_fileobj_from_fdopen(self):
351 # Issue #13781: Opening a GzipFile for writing fails when using a
352 # fileobj created with os.fdopen().
353 fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
354 with os.fdopen(fd, "wb") as f:
355 with gzip.GzipFile(fileobj=f, mode="w") as g:
356 pass
357
Nadeem Vawda103e8112012-06-20 01:35:22 +0200358 def test_bytes_filename(self):
359 str_filename = self.filename
360 try:
361 bytes_filename = str_filename.encode("ascii")
362 except UnicodeEncodeError:
363 self.skipTest("Temporary file name needs to be ASCII")
364 with gzip.GzipFile(bytes_filename, "wb") as f:
365 f.write(data1 * 50)
366 with gzip.GzipFile(bytes_filename, "rb") as f:
367 self.assertEqual(f.read(), data1 * 50)
368 # Sanity check that we are actually operating on the right file.
369 with gzip.GzipFile(str_filename, "rb") as f:
370 self.assertEqual(f.read(), data1 * 50)
371
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000372 # Testing compress/decompress shortcut functions
373
374 def test_compress(self):
375 for data in [data1, data2]:
376 for args in [(), (1,), (6,), (9,)]:
377 datac = gzip.compress(data, *args)
378 self.assertEqual(type(datac), bytes)
379 with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f:
380 self.assertEqual(f.read(), data)
381
382 def test_decompress(self):
383 for data in (data1, data2):
384 buf = io.BytesIO()
385 with gzip.GzipFile(fileobj=buf, mode="wb") as f:
386 f.write(data)
387 self.assertEqual(gzip.decompress(buf.getvalue()), data)
388 # Roundtrip with compress
389 datac = gzip.compress(data)
390 self.assertEqual(gzip.decompress(datac), data)
391
Serhiy Storchaka7c3922f2013-01-22 17:01:59 +0200392 def test_read_truncated(self):
393 data = data1*50
394 # Drop the CRC (4 bytes) and file size (4 bytes).
395 truncated = gzip.compress(data)[:-8]
396 with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f:
397 self.assertRaises(EOFError, f.read)
398 with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f:
399 self.assertEqual(f.read(len(data)), data)
400 self.assertRaises(EOFError, f.read, 1)
401 # Incomplete 10-byte header.
402 for i in range(2, 10):
403 with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f:
404 self.assertRaises(EOFError, f.read, 1)
405
Serhiy Storchaka7e69f002013-04-08 22:35:02 +0300406 def test_read_with_extra(self):
407 # Gzip data with an extra field
408 gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'
409 b'\x05\x00Extra'
410 b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00')
411 with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
412 self.assertEqual(f.read(), b'Test')
Nadeem Vawda7e126202012-05-06 15:04:01 +0200413
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200414class TestOpen(BaseTest):
415 def test_binary_modes(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200416 uncompressed = data1 * 50
417 with gzip.open(self.filename, "wb") as f:
418 f.write(uncompressed)
419 with open(self.filename, "rb") as f:
420 file_data = gzip.decompress(f.read())
421 self.assertEqual(file_data, uncompressed)
422 with gzip.open(self.filename, "rb") as f:
423 self.assertEqual(f.read(), uncompressed)
424 with gzip.open(self.filename, "ab") as f:
425 f.write(uncompressed)
426 with open(self.filename, "rb") as f:
427 file_data = gzip.decompress(f.read())
428 self.assertEqual(file_data, uncompressed * 2)
429
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200430 def test_implicit_binary_modes(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200431 # Test implicit binary modes (no "b" or "t" in mode string).
432 uncompressed = data1 * 50
433 with gzip.open(self.filename, "w") as f:
434 f.write(uncompressed)
435 with open(self.filename, "rb") as f:
436 file_data = gzip.decompress(f.read())
437 self.assertEqual(file_data, uncompressed)
438 with gzip.open(self.filename, "r") as f:
439 self.assertEqual(f.read(), uncompressed)
440 with gzip.open(self.filename, "a") as f:
441 f.write(uncompressed)
442 with open(self.filename, "rb") as f:
443 file_data = gzip.decompress(f.read())
444 self.assertEqual(file_data, uncompressed * 2)
445
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200446 def test_text_modes(self):
Nadeem Vawda11328e42012-05-06 19:24:18 +0200447 uncompressed = data1.decode("ascii") * 50
448 uncompressed_raw = uncompressed.replace("\n", os.linesep)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200449 with gzip.open(self.filename, "wt") as f:
450 f.write(uncompressed)
451 with open(self.filename, "rb") as f:
452 file_data = gzip.decompress(f.read()).decode("ascii")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200453 self.assertEqual(file_data, uncompressed_raw)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200454 with gzip.open(self.filename, "rt") as f:
455 self.assertEqual(f.read(), uncompressed)
456 with gzip.open(self.filename, "at") as f:
457 f.write(uncompressed)
458 with open(self.filename, "rb") as f:
459 file_data = gzip.decompress(f.read()).decode("ascii")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200460 self.assertEqual(file_data, uncompressed_raw * 2)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200461
Nadeem Vawda68721012012-06-04 23:21:38 +0200462 def test_fileobj(self):
463 uncompressed_bytes = data1 * 50
464 uncompressed_str = uncompressed_bytes.decode("ascii")
465 compressed = gzip.compress(uncompressed_bytes)
466 with gzip.open(io.BytesIO(compressed), "r") as f:
467 self.assertEqual(f.read(), uncompressed_bytes)
468 with gzip.open(io.BytesIO(compressed), "rb") as f:
469 self.assertEqual(f.read(), uncompressed_bytes)
470 with gzip.open(io.BytesIO(compressed), "rt") as f:
471 self.assertEqual(f.read(), uncompressed_str)
472
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200473 def test_bad_params(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200474 # Test invalid parameter combinations.
Nadeem Vawda68721012012-06-04 23:21:38 +0200475 with self.assertRaises(TypeError):
476 gzip.open(123.456)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200477 with self.assertRaises(ValueError):
478 gzip.open(self.filename, "wbt")
479 with self.assertRaises(ValueError):
480 gzip.open(self.filename, "rb", encoding="utf-8")
481 with self.assertRaises(ValueError):
482 gzip.open(self.filename, "rb", errors="ignore")
483 with self.assertRaises(ValueError):
484 gzip.open(self.filename, "rb", newline="\n")
485
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200486 def test_encoding(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200487 # Test non-default encoding.
Nadeem Vawda11328e42012-05-06 19:24:18 +0200488 uncompressed = data1.decode("ascii") * 50
489 uncompressed_raw = uncompressed.replace("\n", os.linesep)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200490 with gzip.open(self.filename, "wt", encoding="utf-16") as f:
491 f.write(uncompressed)
492 with open(self.filename, "rb") as f:
493 file_data = gzip.decompress(f.read()).decode("utf-16")
Nadeem Vawda11328e42012-05-06 19:24:18 +0200494 self.assertEqual(file_data, uncompressed_raw)
Nadeem Vawda7e126202012-05-06 15:04:01 +0200495 with gzip.open(self.filename, "rt", encoding="utf-16") as f:
496 self.assertEqual(f.read(), uncompressed)
497
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200498 def test_encoding_error_handler(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200499 # Test with non-default encoding error handler.
500 with gzip.open(self.filename, "wb") as f:
501 f.write(b"foo\xffbar")
502 with gzip.open(self.filename, "rt", encoding="ascii", errors="ignore") \
503 as f:
504 self.assertEqual(f.read(), "foobar")
505
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200506 def test_newline(self):
Nadeem Vawda7e126202012-05-06 15:04:01 +0200507 # Test with explicit newline (universal newline mode disabled).
508 uncompressed = data1.decode("ascii") * 50
Nadeem Vawda9d9dc8e2012-05-06 16:25:35 +0200509 with gzip.open(self.filename, "wt", newline="\n") as f:
Nadeem Vawda7e126202012-05-06 15:04:01 +0200510 f.write(uncompressed)
511 with gzip.open(self.filename, "rt", newline="\r") as f:
512 self.assertEqual(f.readlines(), [uncompressed])
513
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000514def test_main(verbose=None):
Nadeem Vawda1b8a14d2012-05-06 15:17:52 +0200515 support.run_unittest(TestGzip, TestOpen)
Andrew M. Kuchlinga6f68e12005-06-09 14:12:36 +0000516
517if __name__ == "__main__":
518 test_main(verbose=True)