ยปCore Development>Code coverage>Lib/test/test_gzip.py

Python code coverage for Lib/test/test_gzip.py

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