1 | n/a | """Test script for the gzip module. |
---|
2 | n/a | """ |
---|
3 | n/a | |
---|
4 | n/a | import unittest |
---|
5 | n/a | from test import support |
---|
6 | n/a | from test.support import bigmemtest, _4G |
---|
7 | n/a | import os |
---|
8 | n/a | import pathlib |
---|
9 | n/a | import io |
---|
10 | n/a | import struct |
---|
11 | n/a | import array |
---|
12 | n/a | gzip = support.import_module('gzip') |
---|
13 | n/a | |
---|
14 | n/a | data1 = b""" int length=DEFAULTALLOC, err = Z_OK; |
---|
15 | n/a | PyObject *RetVal; |
---|
16 | n/a | int flushmode = Z_FINISH; |
---|
17 | n/a | unsigned long start_total_out; |
---|
18 | n/a | |
---|
19 | n/a | """ |
---|
20 | n/a | |
---|
21 | n/a | data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */ |
---|
22 | n/a | /* See http://www.gzip.org/zlib/ |
---|
23 | n/a | /* See http://www.winimage.com/zLibDll for Windows */ |
---|
24 | n/a | """ |
---|
25 | n/a | |
---|
26 | n/a | |
---|
27 | n/a | class UnseekableIO(io.BytesIO): |
---|
28 | n/a | def seekable(self): |
---|
29 | n/a | return False |
---|
30 | n/a | |
---|
31 | n/a | def tell(self): |
---|
32 | n/a | raise io.UnsupportedOperation |
---|
33 | n/a | |
---|
34 | n/a | def seek(self, *args): |
---|
35 | n/a | raise io.UnsupportedOperation |
---|
36 | n/a | |
---|
37 | n/a | |
---|
38 | n/a | class BaseTest(unittest.TestCase): |
---|
39 | n/a | filename = support.TESTFN |
---|
40 | n/a | |
---|
41 | n/a | def setUp(self): |
---|
42 | n/a | support.unlink(self.filename) |
---|
43 | n/a | |
---|
44 | n/a | def tearDown(self): |
---|
45 | n/a | support.unlink(self.filename) |
---|
46 | n/a | |
---|
47 | n/a | |
---|
48 | n/a | class TestGzip(BaseTest): |
---|
49 | n/a | def write_and_read_back(self, data, mode='b'): |
---|
50 | n/a | b_data = bytes(data) |
---|
51 | n/a | with gzip.GzipFile(self.filename, 'w'+mode) as f: |
---|
52 | n/a | l = f.write(data) |
---|
53 | n/a | self.assertEqual(l, len(b_data)) |
---|
54 | n/a | with gzip.GzipFile(self.filename, 'r'+mode) as f: |
---|
55 | n/a | self.assertEqual(f.read(), b_data) |
---|
56 | n/a | |
---|
57 | n/a | def test_write(self): |
---|
58 | n/a | with gzip.GzipFile(self.filename, 'wb') as f: |
---|
59 | n/a | f.write(data1 * 50) |
---|
60 | n/a | |
---|
61 | n/a | # Try flush and fileno. |
---|
62 | n/a | f.flush() |
---|
63 | n/a | f.fileno() |
---|
64 | n/a | if hasattr(os, 'fsync'): |
---|
65 | n/a | os.fsync(f.fileno()) |
---|
66 | n/a | f.close() |
---|
67 | n/a | |
---|
68 | n/a | # Test multiple close() calls. |
---|
69 | n/a | f.close() |
---|
70 | n/a | |
---|
71 | n/a | def test_write_read_with_pathlike_file(self): |
---|
72 | n/a | filename = pathlib.Path(self.filename) |
---|
73 | n/a | with gzip.GzipFile(filename, 'w') as f: |
---|
74 | n/a | f.write(data1 * 50) |
---|
75 | n/a | self.assertIsInstance(f.name, str) |
---|
76 | n/a | with gzip.GzipFile(filename, 'a') as f: |
---|
77 | n/a | f.write(data1) |
---|
78 | n/a | with gzip.GzipFile(filename) as f: |
---|
79 | n/a | d = f.read() |
---|
80 | n/a | self.assertEqual(d, data1 * 51) |
---|
81 | n/a | self.assertIsInstance(f.name, str) |
---|
82 | n/a | |
---|
83 | n/a | # The following test_write_xy methods test that write accepts |
---|
84 | n/a | # the corresponding bytes-like object type as input |
---|
85 | n/a | # and that the data written equals bytes(xy) in all cases. |
---|
86 | n/a | def test_write_memoryview(self): |
---|
87 | n/a | self.write_and_read_back(memoryview(data1 * 50)) |
---|
88 | n/a | m = memoryview(bytes(range(256))) |
---|
89 | n/a | data = m.cast('B', shape=[8,8,4]) |
---|
90 | n/a | self.write_and_read_back(data) |
---|
91 | n/a | |
---|
92 | n/a | def test_write_bytearray(self): |
---|
93 | n/a | self.write_and_read_back(bytearray(data1 * 50)) |
---|
94 | n/a | |
---|
95 | n/a | def test_write_array(self): |
---|
96 | n/a | self.write_and_read_back(array.array('I', data1 * 40)) |
---|
97 | n/a | |
---|
98 | n/a | def test_write_incompatible_type(self): |
---|
99 | n/a | # Test that non-bytes-like types raise TypeError. |
---|
100 | n/a | # Issue #21560: attempts to write incompatible types |
---|
101 | n/a | # should not affect the state of the fileobject |
---|
102 | n/a | with gzip.GzipFile(self.filename, 'wb') as f: |
---|
103 | n/a | with self.assertRaises(TypeError): |
---|
104 | n/a | f.write('') |
---|
105 | n/a | with self.assertRaises(TypeError): |
---|
106 | n/a | f.write([]) |
---|
107 | n/a | f.write(data1) |
---|
108 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
109 | n/a | self.assertEqual(f.read(), data1) |
---|
110 | n/a | |
---|
111 | n/a | def test_read(self): |
---|
112 | n/a | self.test_write() |
---|
113 | n/a | # Try reading. |
---|
114 | n/a | with gzip.GzipFile(self.filename, 'r') as f: |
---|
115 | n/a | d = f.read() |
---|
116 | n/a | self.assertEqual(d, data1*50) |
---|
117 | n/a | |
---|
118 | n/a | def test_read1(self): |
---|
119 | n/a | self.test_write() |
---|
120 | n/a | blocks = [] |
---|
121 | n/a | nread = 0 |
---|
122 | n/a | with gzip.GzipFile(self.filename, 'r') as f: |
---|
123 | n/a | while True: |
---|
124 | n/a | d = f.read1() |
---|
125 | n/a | if not d: |
---|
126 | n/a | break |
---|
127 | n/a | blocks.append(d) |
---|
128 | n/a | nread += len(d) |
---|
129 | n/a | # Check that position was updated correctly (see issue10791). |
---|
130 | n/a | self.assertEqual(f.tell(), nread) |
---|
131 | n/a | self.assertEqual(b''.join(blocks), data1 * 50) |
---|
132 | n/a | |
---|
133 | n/a | @bigmemtest(size=_4G, memuse=1) |
---|
134 | n/a | def test_read_large(self, size): |
---|
135 | n/a | # Read chunk size over UINT_MAX should be supported, despite zlib's |
---|
136 | n/a | # limitation per low-level call |
---|
137 | n/a | compressed = gzip.compress(data1, compresslevel=1) |
---|
138 | n/a | f = gzip.GzipFile(fileobj=io.BytesIO(compressed), mode='rb') |
---|
139 | n/a | self.assertEqual(f.read(size), data1) |
---|
140 | n/a | |
---|
141 | n/a | def test_io_on_closed_object(self): |
---|
142 | n/a | # Test that I/O operations on closed GzipFile objects raise a |
---|
143 | n/a | # ValueError, just like the corresponding functions on file objects. |
---|
144 | n/a | |
---|
145 | n/a | # Write to a file, open it for reading, then close it. |
---|
146 | n/a | self.test_write() |
---|
147 | n/a | f = gzip.GzipFile(self.filename, 'r') |
---|
148 | n/a | fileobj = f.fileobj |
---|
149 | n/a | self.assertFalse(fileobj.closed) |
---|
150 | n/a | f.close() |
---|
151 | n/a | self.assertTrue(fileobj.closed) |
---|
152 | n/a | with self.assertRaises(ValueError): |
---|
153 | n/a | f.read(1) |
---|
154 | n/a | with self.assertRaises(ValueError): |
---|
155 | n/a | f.seek(0) |
---|
156 | n/a | with self.assertRaises(ValueError): |
---|
157 | n/a | f.tell() |
---|
158 | n/a | # Open the file for writing, then close it. |
---|
159 | n/a | f = gzip.GzipFile(self.filename, 'w') |
---|
160 | n/a | fileobj = f.fileobj |
---|
161 | n/a | self.assertFalse(fileobj.closed) |
---|
162 | n/a | f.close() |
---|
163 | n/a | self.assertTrue(fileobj.closed) |
---|
164 | n/a | with self.assertRaises(ValueError): |
---|
165 | n/a | f.write(b'') |
---|
166 | n/a | with self.assertRaises(ValueError): |
---|
167 | n/a | f.flush() |
---|
168 | n/a | |
---|
169 | n/a | def test_append(self): |
---|
170 | n/a | self.test_write() |
---|
171 | n/a | # Append to the previous file |
---|
172 | n/a | with gzip.GzipFile(self.filename, 'ab') as f: |
---|
173 | n/a | f.write(data2 * 15) |
---|
174 | n/a | |
---|
175 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
176 | n/a | d = f.read() |
---|
177 | n/a | self.assertEqual(d, (data1*50) + (data2*15)) |
---|
178 | n/a | |
---|
179 | n/a | def test_many_append(self): |
---|
180 | n/a | # Bug #1074261 was triggered when reading a file that contained |
---|
181 | n/a | # many, many members. Create such a file and verify that reading it |
---|
182 | n/a | # works. |
---|
183 | n/a | with gzip.GzipFile(self.filename, 'wb', 9) as f: |
---|
184 | n/a | f.write(b'a') |
---|
185 | n/a | for i in range(0, 200): |
---|
186 | n/a | with gzip.GzipFile(self.filename, "ab", 9) as f: # append |
---|
187 | n/a | f.write(b'a') |
---|
188 | n/a | |
---|
189 | n/a | # Try reading the file |
---|
190 | n/a | with gzip.GzipFile(self.filename, "rb") as zgfile: |
---|
191 | n/a | contents = b"" |
---|
192 | n/a | while 1: |
---|
193 | n/a | ztxt = zgfile.read(8192) |
---|
194 | n/a | contents += ztxt |
---|
195 | n/a | if not ztxt: break |
---|
196 | n/a | self.assertEqual(contents, b'a'*201) |
---|
197 | n/a | |
---|
198 | n/a | def test_exclusive_write(self): |
---|
199 | n/a | with gzip.GzipFile(self.filename, 'xb') as f: |
---|
200 | n/a | f.write(data1 * 50) |
---|
201 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
202 | n/a | self.assertEqual(f.read(), data1 * 50) |
---|
203 | n/a | with self.assertRaises(FileExistsError): |
---|
204 | n/a | gzip.GzipFile(self.filename, 'xb') |
---|
205 | n/a | |
---|
206 | n/a | def test_buffered_reader(self): |
---|
207 | n/a | # Issue #7471: a GzipFile can be wrapped in a BufferedReader for |
---|
208 | n/a | # performance. |
---|
209 | n/a | self.test_write() |
---|
210 | n/a | |
---|
211 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
212 | n/a | with io.BufferedReader(f) as r: |
---|
213 | n/a | lines = [line for line in r] |
---|
214 | n/a | |
---|
215 | n/a | self.assertEqual(lines, 50 * data1.splitlines(keepends=True)) |
---|
216 | n/a | |
---|
217 | n/a | def test_readline(self): |
---|
218 | n/a | self.test_write() |
---|
219 | n/a | # Try .readline() with varying line lengths |
---|
220 | n/a | |
---|
221 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
222 | n/a | line_length = 0 |
---|
223 | n/a | while 1: |
---|
224 | n/a | L = f.readline(line_length) |
---|
225 | n/a | if not L and line_length != 0: break |
---|
226 | n/a | self.assertTrue(len(L) <= line_length) |
---|
227 | n/a | line_length = (line_length + 1) % 50 |
---|
228 | n/a | |
---|
229 | n/a | def test_readlines(self): |
---|
230 | n/a | self.test_write() |
---|
231 | n/a | # Try .readlines() |
---|
232 | n/a | |
---|
233 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
234 | n/a | L = f.readlines() |
---|
235 | n/a | |
---|
236 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
237 | n/a | while 1: |
---|
238 | n/a | L = f.readlines(150) |
---|
239 | n/a | if L == []: break |
---|
240 | n/a | |
---|
241 | n/a | def test_seek_read(self): |
---|
242 | n/a | self.test_write() |
---|
243 | n/a | # Try seek, read test |
---|
244 | n/a | |
---|
245 | n/a | with gzip.GzipFile(self.filename) as f: |
---|
246 | n/a | while 1: |
---|
247 | n/a | oldpos = f.tell() |
---|
248 | n/a | line1 = f.readline() |
---|
249 | n/a | if not line1: break |
---|
250 | n/a | newpos = f.tell() |
---|
251 | n/a | f.seek(oldpos) # negative seek |
---|
252 | n/a | if len(line1)>10: |
---|
253 | n/a | amount = 10 |
---|
254 | n/a | else: |
---|
255 | n/a | amount = len(line1) |
---|
256 | n/a | line2 = f.read(amount) |
---|
257 | n/a | self.assertEqual(line1[:amount], line2) |
---|
258 | n/a | f.seek(newpos) # positive seek |
---|
259 | n/a | |
---|
260 | n/a | def test_seek_whence(self): |
---|
261 | n/a | self.test_write() |
---|
262 | n/a | # Try seek(whence=1), read test |
---|
263 | n/a | |
---|
264 | n/a | with gzip.GzipFile(self.filename) as f: |
---|
265 | n/a | f.read(10) |
---|
266 | n/a | f.seek(10, whence=1) |
---|
267 | n/a | y = f.read(10) |
---|
268 | n/a | self.assertEqual(y, data1[20:30]) |
---|
269 | n/a | |
---|
270 | n/a | def test_seek_write(self): |
---|
271 | n/a | # Try seek, write test |
---|
272 | n/a | with gzip.GzipFile(self.filename, 'w') as f: |
---|
273 | n/a | for pos in range(0, 256, 16): |
---|
274 | n/a | f.seek(pos) |
---|
275 | n/a | f.write(b'GZ\n') |
---|
276 | n/a | |
---|
277 | n/a | def test_mode(self): |
---|
278 | n/a | self.test_write() |
---|
279 | n/a | with gzip.GzipFile(self.filename, 'r') as f: |
---|
280 | n/a | self.assertEqual(f.myfileobj.mode, 'rb') |
---|
281 | n/a | support.unlink(self.filename) |
---|
282 | n/a | with gzip.GzipFile(self.filename, 'x') as f: |
---|
283 | n/a | self.assertEqual(f.myfileobj.mode, 'xb') |
---|
284 | n/a | |
---|
285 | n/a | def test_1647484(self): |
---|
286 | n/a | for mode in ('wb', 'rb'): |
---|
287 | n/a | with gzip.GzipFile(self.filename, mode) as f: |
---|
288 | n/a | self.assertTrue(hasattr(f, "name")) |
---|
289 | n/a | self.assertEqual(f.name, self.filename) |
---|
290 | n/a | |
---|
291 | n/a | def test_paddedfile_getattr(self): |
---|
292 | n/a | self.test_write() |
---|
293 | n/a | with gzip.GzipFile(self.filename, 'rb') as f: |
---|
294 | n/a | self.assertTrue(hasattr(f.fileobj, "name")) |
---|
295 | n/a | self.assertEqual(f.fileobj.name, self.filename) |
---|
296 | n/a | |
---|
297 | n/a | def test_mtime(self): |
---|
298 | n/a | mtime = 123456789 |
---|
299 | n/a | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
---|
300 | n/a | fWrite.write(data1) |
---|
301 | n/a | with gzip.GzipFile(self.filename) as fRead: |
---|
302 | n/a | self.assertTrue(hasattr(fRead, 'mtime')) |
---|
303 | n/a | self.assertIsNone(fRead.mtime) |
---|
304 | n/a | dataRead = fRead.read() |
---|
305 | n/a | self.assertEqual(dataRead, data1) |
---|
306 | n/a | self.assertEqual(fRead.mtime, mtime) |
---|
307 | n/a | |
---|
308 | n/a | def test_metadata(self): |
---|
309 | n/a | mtime = 123456789 |
---|
310 | n/a | |
---|
311 | n/a | with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: |
---|
312 | n/a | fWrite.write(data1) |
---|
313 | n/a | |
---|
314 | n/a | with open(self.filename, 'rb') as fRead: |
---|
315 | n/a | # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html |
---|
316 | n/a | |
---|
317 | n/a | idBytes = fRead.read(2) |
---|
318 | n/a | self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID |
---|
319 | n/a | |
---|
320 | n/a | cmByte = fRead.read(1) |
---|
321 | n/a | self.assertEqual(cmByte, b'\x08') # deflate |
---|
322 | n/a | |
---|
323 | n/a | flagsByte = fRead.read(1) |
---|
324 | n/a | self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set |
---|
325 | n/a | |
---|
326 | n/a | mtimeBytes = fRead.read(4) |
---|
327 | n/a | self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian |
---|
328 | n/a | |
---|
329 | n/a | xflByte = fRead.read(1) |
---|
330 | n/a | self.assertEqual(xflByte, b'\x02') # maximum compression |
---|
331 | n/a | |
---|
332 | n/a | osByte = fRead.read(1) |
---|
333 | n/a | self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent) |
---|
334 | n/a | |
---|
335 | n/a | # Since the FNAME flag is set, the zero-terminated filename follows. |
---|
336 | n/a | # RFC 1952 specifies that this is the name of the input file, if any. |
---|
337 | n/a | # However, the gzip module defaults to storing the name of the output |
---|
338 | n/a | # file in this field. |
---|
339 | n/a | expected = self.filename.encode('Latin-1') + b'\x00' |
---|
340 | n/a | nameBytes = fRead.read(len(expected)) |
---|
341 | n/a | self.assertEqual(nameBytes, expected) |
---|
342 | n/a | |
---|
343 | n/a | # Since no other flags were set, the header ends here. |
---|
344 | n/a | # Rather than process the compressed data, let's seek to the trailer. |
---|
345 | n/a | fRead.seek(os.stat(self.filename).st_size - 8) |
---|
346 | n/a | |
---|
347 | n/a | crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1] |
---|
348 | n/a | self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83') |
---|
349 | n/a | |
---|
350 | n/a | isizeBytes = fRead.read(4) |
---|
351 | n/a | self.assertEqual(isizeBytes, struct.pack('<i', len(data1))) |
---|
352 | n/a | |
---|
353 | n/a | def test_with_open(self): |
---|
354 | n/a | # GzipFile supports the context management protocol |
---|
355 | n/a | with gzip.GzipFile(self.filename, "wb") as f: |
---|
356 | n/a | f.write(b"xxx") |
---|
357 | n/a | f = gzip.GzipFile(self.filename, "rb") |
---|
358 | n/a | f.close() |
---|
359 | n/a | try: |
---|
360 | n/a | with f: |
---|
361 | n/a | pass |
---|
362 | n/a | except ValueError: |
---|
363 | n/a | pass |
---|
364 | n/a | else: |
---|
365 | n/a | self.fail("__enter__ on a closed file didn't raise an exception") |
---|
366 | n/a | try: |
---|
367 | n/a | with gzip.GzipFile(self.filename, "wb") as f: |
---|
368 | n/a | 1/0 |
---|
369 | n/a | except ZeroDivisionError: |
---|
370 | n/a | pass |
---|
371 | n/a | else: |
---|
372 | n/a | self.fail("1/0 didn't raise an exception") |
---|
373 | n/a | |
---|
374 | n/a | def test_zero_padded_file(self): |
---|
375 | n/a | with gzip.GzipFile(self.filename, "wb") as f: |
---|
376 | n/a | f.write(data1 * 50) |
---|
377 | n/a | |
---|
378 | n/a | # Pad the file with zeroes |
---|
379 | n/a | with open(self.filename, "ab") as f: |
---|
380 | n/a | f.write(b"\x00" * 50) |
---|
381 | n/a | |
---|
382 | n/a | with gzip.GzipFile(self.filename, "rb") as f: |
---|
383 | n/a | d = f.read() |
---|
384 | n/a | self.assertEqual(d, data1 * 50, "Incorrect data in file") |
---|
385 | n/a | |
---|
386 | n/a | def test_non_seekable_file(self): |
---|
387 | n/a | uncompressed = data1 * 50 |
---|
388 | n/a | buf = UnseekableIO() |
---|
389 | n/a | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
---|
390 | n/a | f.write(uncompressed) |
---|
391 | n/a | compressed = buf.getvalue() |
---|
392 | n/a | buf = UnseekableIO(compressed) |
---|
393 | n/a | with gzip.GzipFile(fileobj=buf, mode="rb") as f: |
---|
394 | n/a | self.assertEqual(f.read(), uncompressed) |
---|
395 | n/a | |
---|
396 | n/a | def test_peek(self): |
---|
397 | n/a | uncompressed = data1 * 200 |
---|
398 | n/a | with gzip.GzipFile(self.filename, "wb") as f: |
---|
399 | n/a | f.write(uncompressed) |
---|
400 | n/a | |
---|
401 | n/a | def sizes(): |
---|
402 | n/a | while True: |
---|
403 | n/a | for n in range(5, 50, 10): |
---|
404 | n/a | yield n |
---|
405 | n/a | |
---|
406 | n/a | with gzip.GzipFile(self.filename, "rb") as f: |
---|
407 | n/a | f.max_read_chunk = 33 |
---|
408 | n/a | nread = 0 |
---|
409 | n/a | for n in sizes(): |
---|
410 | n/a | s = f.peek(n) |
---|
411 | n/a | if s == b'': |
---|
412 | n/a | break |
---|
413 | n/a | self.assertEqual(f.read(len(s)), s) |
---|
414 | n/a | nread += len(s) |
---|
415 | n/a | self.assertEqual(f.read(100), b'') |
---|
416 | n/a | self.assertEqual(nread, len(uncompressed)) |
---|
417 | n/a | |
---|
418 | n/a | def test_textio_readlines(self): |
---|
419 | n/a | # Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile. |
---|
420 | n/a | lines = (data1 * 50).decode("ascii").splitlines(keepends=True) |
---|
421 | n/a | self.test_write() |
---|
422 | n/a | with gzip.GzipFile(self.filename, 'r') as f: |
---|
423 | n/a | with io.TextIOWrapper(f, encoding="ascii") as t: |
---|
424 | n/a | self.assertEqual(t.readlines(), lines) |
---|
425 | n/a | |
---|
426 | n/a | def test_fileobj_from_fdopen(self): |
---|
427 | n/a | # Issue #13781: Opening a GzipFile for writing fails when using a |
---|
428 | n/a | # fileobj created with os.fdopen(). |
---|
429 | n/a | fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT) |
---|
430 | n/a | with os.fdopen(fd, "wb") as f: |
---|
431 | n/a | with gzip.GzipFile(fileobj=f, mode="w") as g: |
---|
432 | n/a | pass |
---|
433 | n/a | |
---|
434 | n/a | def test_bytes_filename(self): |
---|
435 | n/a | str_filename = self.filename |
---|
436 | n/a | try: |
---|
437 | n/a | bytes_filename = str_filename.encode("ascii") |
---|
438 | n/a | except UnicodeEncodeError: |
---|
439 | n/a | self.skipTest("Temporary file name needs to be ASCII") |
---|
440 | n/a | with gzip.GzipFile(bytes_filename, "wb") as f: |
---|
441 | n/a | f.write(data1 * 50) |
---|
442 | n/a | with gzip.GzipFile(bytes_filename, "rb") as f: |
---|
443 | n/a | self.assertEqual(f.read(), data1 * 50) |
---|
444 | n/a | # Sanity check that we are actually operating on the right file. |
---|
445 | n/a | with gzip.GzipFile(str_filename, "rb") as f: |
---|
446 | n/a | self.assertEqual(f.read(), data1 * 50) |
---|
447 | n/a | |
---|
448 | n/a | def test_decompress_limited(self): |
---|
449 | n/a | """Decompressed data buffering should be limited""" |
---|
450 | n/a | bomb = gzip.compress(b'\0' * int(2e6), compresslevel=9) |
---|
451 | n/a | self.assertLess(len(bomb), io.DEFAULT_BUFFER_SIZE) |
---|
452 | n/a | |
---|
453 | n/a | bomb = io.BytesIO(bomb) |
---|
454 | n/a | decomp = gzip.GzipFile(fileobj=bomb) |
---|
455 | n/a | self.assertEqual(decomp.read(1), b'\0') |
---|
456 | n/a | max_decomp = 1 + io.DEFAULT_BUFFER_SIZE |
---|
457 | n/a | self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp, |
---|
458 | n/a | "Excessive amount of data was decompressed") |
---|
459 | n/a | |
---|
460 | n/a | # Testing compress/decompress shortcut functions |
---|
461 | n/a | |
---|
462 | n/a | def test_compress(self): |
---|
463 | n/a | for data in [data1, data2]: |
---|
464 | n/a | for args in [(), (1,), (6,), (9,)]: |
---|
465 | n/a | datac = gzip.compress(data, *args) |
---|
466 | n/a | self.assertEqual(type(datac), bytes) |
---|
467 | n/a | with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f: |
---|
468 | n/a | self.assertEqual(f.read(), data) |
---|
469 | n/a | |
---|
470 | n/a | def test_decompress(self): |
---|
471 | n/a | for data in (data1, data2): |
---|
472 | n/a | buf = io.BytesIO() |
---|
473 | n/a | with gzip.GzipFile(fileobj=buf, mode="wb") as f: |
---|
474 | n/a | f.write(data) |
---|
475 | n/a | self.assertEqual(gzip.decompress(buf.getvalue()), data) |
---|
476 | n/a | # Roundtrip with compress |
---|
477 | n/a | datac = gzip.compress(data) |
---|
478 | n/a | self.assertEqual(gzip.decompress(datac), data) |
---|
479 | n/a | |
---|
480 | n/a | def test_read_truncated(self): |
---|
481 | n/a | data = data1*50 |
---|
482 | n/a | # Drop the CRC (4 bytes) and file size (4 bytes). |
---|
483 | n/a | truncated = gzip.compress(data)[:-8] |
---|
484 | n/a | with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: |
---|
485 | n/a | self.assertRaises(EOFError, f.read) |
---|
486 | n/a | with gzip.GzipFile(fileobj=io.BytesIO(truncated)) as f: |
---|
487 | n/a | self.assertEqual(f.read(len(data)), data) |
---|
488 | n/a | self.assertRaises(EOFError, f.read, 1) |
---|
489 | n/a | # Incomplete 10-byte header. |
---|
490 | n/a | for i in range(2, 10): |
---|
491 | n/a | with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f: |
---|
492 | n/a | self.assertRaises(EOFError, f.read, 1) |
---|
493 | n/a | |
---|
494 | n/a | def test_read_with_extra(self): |
---|
495 | n/a | # Gzip data with an extra field |
---|
496 | n/a | gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff' |
---|
497 | n/a | b'\x05\x00Extra' |
---|
498 | n/a | b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00') |
---|
499 | n/a | with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f: |
---|
500 | n/a | self.assertEqual(f.read(), b'Test') |
---|
501 | n/a | |
---|
502 | n/a | def test_prepend_error(self): |
---|
503 | n/a | # See issue #20875 |
---|
504 | n/a | with gzip.open(self.filename, "wb") as f: |
---|
505 | n/a | f.write(data1) |
---|
506 | n/a | with gzip.open(self.filename, "rb") as f: |
---|
507 | n/a | f._buffer.raw._fp.prepend() |
---|
508 | n/a | |
---|
509 | n/a | class TestOpen(BaseTest): |
---|
510 | n/a | def test_binary_modes(self): |
---|
511 | n/a | uncompressed = data1 * 50 |
---|
512 | n/a | |
---|
513 | n/a | with gzip.open(self.filename, "wb") as f: |
---|
514 | n/a | f.write(uncompressed) |
---|
515 | n/a | with open(self.filename, "rb") as f: |
---|
516 | n/a | file_data = gzip.decompress(f.read()) |
---|
517 | n/a | self.assertEqual(file_data, uncompressed) |
---|
518 | n/a | |
---|
519 | n/a | with gzip.open(self.filename, "rb") as f: |
---|
520 | n/a | self.assertEqual(f.read(), uncompressed) |
---|
521 | n/a | |
---|
522 | n/a | with gzip.open(self.filename, "ab") as f: |
---|
523 | n/a | f.write(uncompressed) |
---|
524 | n/a | with open(self.filename, "rb") as f: |
---|
525 | n/a | file_data = gzip.decompress(f.read()) |
---|
526 | n/a | self.assertEqual(file_data, uncompressed * 2) |
---|
527 | n/a | |
---|
528 | n/a | with self.assertRaises(FileExistsError): |
---|
529 | n/a | gzip.open(self.filename, "xb") |
---|
530 | n/a | support.unlink(self.filename) |
---|
531 | n/a | with gzip.open(self.filename, "xb") as f: |
---|
532 | n/a | f.write(uncompressed) |
---|
533 | n/a | with open(self.filename, "rb") as f: |
---|
534 | n/a | file_data = gzip.decompress(f.read()) |
---|
535 | n/a | self.assertEqual(file_data, uncompressed) |
---|
536 | n/a | |
---|
537 | n/a | def test_pathlike_file(self): |
---|
538 | n/a | filename = pathlib.Path(self.filename) |
---|
539 | n/a | with gzip.open(filename, "wb") as f: |
---|
540 | n/a | f.write(data1 * 50) |
---|
541 | n/a | with gzip.open(filename, "ab") as f: |
---|
542 | n/a | f.write(data1) |
---|
543 | n/a | with gzip.open(filename) as f: |
---|
544 | n/a | self.assertEqual(f.read(), data1 * 51) |
---|
545 | n/a | |
---|
546 | n/a | def test_implicit_binary_modes(self): |
---|
547 | n/a | # Test implicit binary modes (no "b" or "t" in mode string). |
---|
548 | n/a | uncompressed = data1 * 50 |
---|
549 | n/a | |
---|
550 | n/a | with gzip.open(self.filename, "w") as f: |
---|
551 | n/a | f.write(uncompressed) |
---|
552 | n/a | with open(self.filename, "rb") as f: |
---|
553 | n/a | file_data = gzip.decompress(f.read()) |
---|
554 | n/a | self.assertEqual(file_data, uncompressed) |
---|
555 | n/a | |
---|
556 | n/a | with gzip.open(self.filename, "r") as f: |
---|
557 | n/a | self.assertEqual(f.read(), uncompressed) |
---|
558 | n/a | |
---|
559 | n/a | with gzip.open(self.filename, "a") as f: |
---|
560 | n/a | f.write(uncompressed) |
---|
561 | n/a | with open(self.filename, "rb") as f: |
---|
562 | n/a | file_data = gzip.decompress(f.read()) |
---|
563 | n/a | self.assertEqual(file_data, uncompressed * 2) |
---|
564 | n/a | |
---|
565 | n/a | with self.assertRaises(FileExistsError): |
---|
566 | n/a | gzip.open(self.filename, "x") |
---|
567 | n/a | support.unlink(self.filename) |
---|
568 | n/a | with gzip.open(self.filename, "x") as f: |
---|
569 | n/a | f.write(uncompressed) |
---|
570 | n/a | with open(self.filename, "rb") as f: |
---|
571 | n/a | file_data = gzip.decompress(f.read()) |
---|
572 | n/a | self.assertEqual(file_data, uncompressed) |
---|
573 | n/a | |
---|
574 | n/a | def test_text_modes(self): |
---|
575 | n/a | uncompressed = data1.decode("ascii") * 50 |
---|
576 | n/a | uncompressed_raw = uncompressed.replace("\n", os.linesep) |
---|
577 | n/a | with gzip.open(self.filename, "wt") as f: |
---|
578 | n/a | f.write(uncompressed) |
---|
579 | n/a | with open(self.filename, "rb") as f: |
---|
580 | n/a | file_data = gzip.decompress(f.read()).decode("ascii") |
---|
581 | n/a | self.assertEqual(file_data, uncompressed_raw) |
---|
582 | n/a | with gzip.open(self.filename, "rt") as f: |
---|
583 | n/a | self.assertEqual(f.read(), uncompressed) |
---|
584 | n/a | with gzip.open(self.filename, "at") as f: |
---|
585 | n/a | f.write(uncompressed) |
---|
586 | n/a | with open(self.filename, "rb") as f: |
---|
587 | n/a | file_data = gzip.decompress(f.read()).decode("ascii") |
---|
588 | n/a | self.assertEqual(file_data, uncompressed_raw * 2) |
---|
589 | n/a | |
---|
590 | n/a | def test_fileobj(self): |
---|
591 | n/a | uncompressed_bytes = data1 * 50 |
---|
592 | n/a | uncompressed_str = uncompressed_bytes.decode("ascii") |
---|
593 | n/a | compressed = gzip.compress(uncompressed_bytes) |
---|
594 | n/a | with gzip.open(io.BytesIO(compressed), "r") as f: |
---|
595 | n/a | self.assertEqual(f.read(), uncompressed_bytes) |
---|
596 | n/a | with gzip.open(io.BytesIO(compressed), "rb") as f: |
---|
597 | n/a | self.assertEqual(f.read(), uncompressed_bytes) |
---|
598 | n/a | with gzip.open(io.BytesIO(compressed), "rt") as f: |
---|
599 | n/a | self.assertEqual(f.read(), uncompressed_str) |
---|
600 | n/a | |
---|
601 | n/a | def test_bad_params(self): |
---|
602 | n/a | # Test invalid parameter combinations. |
---|
603 | n/a | with self.assertRaises(TypeError): |
---|
604 | n/a | gzip.open(123.456) |
---|
605 | n/a | with self.assertRaises(ValueError): |
---|
606 | n/a | gzip.open(self.filename, "wbt") |
---|
607 | n/a | with self.assertRaises(ValueError): |
---|
608 | n/a | gzip.open(self.filename, "xbt") |
---|
609 | n/a | with self.assertRaises(ValueError): |
---|
610 | n/a | gzip.open(self.filename, "rb", encoding="utf-8") |
---|
611 | n/a | with self.assertRaises(ValueError): |
---|
612 | n/a | gzip.open(self.filename, "rb", errors="ignore") |
---|
613 | n/a | with self.assertRaises(ValueError): |
---|
614 | n/a | gzip.open(self.filename, "rb", newline="\n") |
---|
615 | n/a | |
---|
616 | n/a | def test_encoding(self): |
---|
617 | n/a | # Test non-default encoding. |
---|
618 | n/a | uncompressed = data1.decode("ascii") * 50 |
---|
619 | n/a | uncompressed_raw = uncompressed.replace("\n", os.linesep) |
---|
620 | n/a | with gzip.open(self.filename, "wt", encoding="utf-16") as f: |
---|
621 | n/a | f.write(uncompressed) |
---|
622 | n/a | with open(self.filename, "rb") as f: |
---|
623 | n/a | file_data = gzip.decompress(f.read()).decode("utf-16") |
---|
624 | n/a | self.assertEqual(file_data, uncompressed_raw) |
---|
625 | n/a | with gzip.open(self.filename, "rt", encoding="utf-16") as f: |
---|
626 | n/a | self.assertEqual(f.read(), uncompressed) |
---|
627 | n/a | |
---|
628 | n/a | def test_encoding_error_handler(self): |
---|
629 | n/a | # Test with non-default encoding error handler. |
---|
630 | n/a | with gzip.open(self.filename, "wb") as f: |
---|
631 | n/a | f.write(b"foo\xffbar") |
---|
632 | n/a | with gzip.open(self.filename, "rt", encoding="ascii", errors="ignore") \ |
---|
633 | n/a | as f: |
---|
634 | n/a | self.assertEqual(f.read(), "foobar") |
---|
635 | n/a | |
---|
636 | n/a | def test_newline(self): |
---|
637 | n/a | # Test with explicit newline (universal newline mode disabled). |
---|
638 | n/a | uncompressed = data1.decode("ascii") * 50 |
---|
639 | n/a | with gzip.open(self.filename, "wt", newline="\n") as f: |
---|
640 | n/a | f.write(uncompressed) |
---|
641 | n/a | with gzip.open(self.filename, "rt", newline="\r") as f: |
---|
642 | n/a | self.assertEqual(f.readlines(), [uncompressed]) |
---|
643 | n/a | |
---|
644 | n/a | def test_main(verbose=None): |
---|
645 | n/a | support.run_unittest(TestGzip, TestOpen) |
---|
646 | n/a | |
---|
647 | n/a | if __name__ == "__main__": |
---|
648 | n/a | test_main(verbose=True) |
---|