Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_gzip.TestGzip Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_write
def test_read
def test_io_on_closed_object
def test_append
def test_many_append
def test_buffered_reader
def test_readline
def test_readlines
def test_seek_read
def test_seek_whence
def test_seek_write
def test_mode
def test_1647484
def test_paddedfile_getattr
def test_mtime
def test_metadata
def test_with_open
def test_zero_padded_file
def test_non_seekable_file
def test_peek
def test_compress
def test_decompress

Static Public Attributes

 filename = support.TESTFN

Detailed Description

Definition at line 36 of file test_gzip.py.


Member Function Documentation

Definition at line 39 of file test_gzip.py.

00039 
00040     def setUp(self):
00041         support.unlink(self.filename)

Here is the caller graph for this function:

Definition at line 42 of file test_gzip.py.

00042 
00043     def tearDown(self):
00044         support.unlink(self.filename)
00045 

Here is the caller graph for this function:

Definition at line 194 of file test_gzip.py.

00194 
00195     def test_1647484(self):
00196         for mode in ('wb', 'rb'):
00197             with gzip.GzipFile(self.filename, mode) as f:
00198                 self.assertTrue(hasattr(f, "name"))
00199                 self.assertEqual(f.name, self.filename)

Here is the call graph for this function:

Definition at line 89 of file test_gzip.py.

00089 
00090     def test_append(self):
00091         self.test_write()
00092         # Append to the previous file
00093         with gzip.GzipFile(self.filename, 'ab') as f:
00094             f.write(data2 * 15)
00095 
00096         with gzip.GzipFile(self.filename, 'rb') as f:
00097             d = f.read()
00098         self.assertEqual(d, (data1*50) + (data2*15))

Here is the call graph for this function:

Definition at line 118 of file test_gzip.py.

00118 
00119     def test_buffered_reader(self):
00120         # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
00121         # performance.
00122         self.test_write()
00123 
00124         with gzip.GzipFile(self.filename, 'rb') as f:
00125             with io.BufferedReader(f) as r:
00126                 lines = [line for line in r]
00127 
00128         self.assertEqual(lines, 50 * data1.splitlines(True))

Here is the call graph for this function:

Definition at line 328 of file test_gzip.py.

00328 
00329     def test_compress(self):
00330         for data in [data1, data2]:
00331             for args in [(), (1,), (6,), (9,)]:
00332                 datac = gzip.compress(data, *args)
00333                 self.assertEqual(type(datac), bytes)
00334                 with gzip.GzipFile(fileobj=io.BytesIO(datac), mode="rb") as f:
00335                     self.assertEqual(f.read(), data)

Here is the call graph for this function:

Definition at line 336 of file test_gzip.py.

00336 
00337     def test_decompress(self):
00338         for data in (data1, data2):
00339             buf = io.BytesIO()
00340             with gzip.GzipFile(fileobj=buf, mode="wb") as f:
00341                 f.write(data)
00342             self.assertEqual(gzip.decompress(buf.getvalue()), data)
00343             # Roundtrip with compress
00344             datac = gzip.compress(data)
00345             self.assertEqual(gzip.decompress(datac), data)

Here is the call graph for this function:

Definition at line 67 of file test_gzip.py.

00067 
00068     def test_io_on_closed_object(self):
00069         # Test that I/O operations on closed GzipFile objects raise a
00070         # ValueError, just like the corresponding functions on file objects.
00071 
00072         # Write to a file, open it for reading, then close it.
00073         self.test_write()
00074         f = gzip.GzipFile(self.filename, 'r')
00075         f.close()
00076         with self.assertRaises(ValueError):
00077             f.read(1)
00078         with self.assertRaises(ValueError):
00079             f.seek(0)
00080         with self.assertRaises(ValueError):
00081             f.tell()
00082         # Open the file for writing, then close it.
00083         f = gzip.GzipFile(self.filename, 'w')
00084         f.close()
00085         with self.assertRaises(ValueError):
00086             f.write(b'')
00087         with self.assertRaises(ValueError):
00088             f.flush()

Here is the call graph for this function:

Definition at line 99 of file test_gzip.py.

00099 
00100     def test_many_append(self):
00101         # Bug #1074261 was triggered when reading a file that contained
00102         # many, many members.  Create such a file and verify that reading it
00103         # works.
00104         with gzip.open(self.filename, 'wb', 9) as f:
00105             f.write(b'a')
00106         for i in range(0, 200):
00107             with gzip.open(self.filename, "ab", 9) as f: # append
00108                 f.write(b'a')
00109 
00110         # Try reading the file
00111         with gzip.open(self.filename, "rb") as zgfile:
00112             contents = b""
00113             while 1:
00114                 ztxt = zgfile.read(8192)
00115                 contents += ztxt
00116                 if not ztxt: break
00117         self.assertEqual(contents, b'a'*201)

Here is the call graph for this function:

Definition at line 216 of file test_gzip.py.

00216 
00217     def test_metadata(self):
00218         mtime = 123456789
00219 
00220         with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
00221             fWrite.write(data1)
00222 
00223         with open(self.filename, 'rb') as fRead:
00224             # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
00225 
00226             idBytes = fRead.read(2)
00227             self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
00228 
00229             cmByte = fRead.read(1)
00230             self.assertEqual(cmByte, b'\x08') # deflate
00231 
00232             flagsByte = fRead.read(1)
00233             self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
00234 
00235             mtimeBytes = fRead.read(4)
00236             self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
00237 
00238             xflByte = fRead.read(1)
00239             self.assertEqual(xflByte, b'\x02') # maximum compression
00240 
00241             osByte = fRead.read(1)
00242             self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
00243 
00244             # Since the FNAME flag is set, the zero-terminated filename follows.
00245             # RFC 1952 specifies that this is the name of the input file, if any.
00246             # However, the gzip module defaults to storing the name of the output
00247             # file in this field.
00248             expected = self.filename.encode('Latin-1') + b'\x00'
00249             nameBytes = fRead.read(len(expected))
00250             self.assertEqual(nameBytes, expected)
00251 
00252             # Since no other flags were set, the header ends here.
00253             # Rather than process the compressed data, let's seek to the trailer.
00254             fRead.seek(os.stat(self.filename).st_size - 8)
00255 
00256             crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
00257             self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
00258 
00259             isizeBytes = fRead.read(4)
00260             self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))

Here is the call graph for this function:

Definition at line 189 of file test_gzip.py.

00189 
00190     def test_mode(self):
00191         self.test_write()
00192         with gzip.GzipFile(self.filename, 'r') as f:
00193             self.assertEqual(f.myfileobj.mode, 'rb')

Here is the call graph for this function:

Definition at line 206 of file test_gzip.py.

00206 
00207     def test_mtime(self):
00208         mtime = 123456789
00209         with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
00210             fWrite.write(data1)
00211         with gzip.GzipFile(self.filename) as fRead:
00212             dataRead = fRead.read()
00213             self.assertEqual(dataRead, data1)
00214             self.assertTrue(hasattr(fRead, 'mtime'))
00215             self.assertEqual(fRead.mtime, mtime)

Here is the call graph for this function:

Definition at line 294 of file test_gzip.py.

00294 
00295     def test_non_seekable_file(self):
00296         uncompressed = data1 * 50
00297         buf = UnseekableIO()
00298         with gzip.GzipFile(fileobj=buf, mode="wb") as f:
00299             f.write(uncompressed)
00300         compressed = buf.getvalue()
00301         buf = UnseekableIO(compressed)
00302         with gzip.GzipFile(fileobj=buf, mode="rb") as f:
00303             self.assertEqual(f.read(), uncompressed)

Here is the call graph for this function:

Definition at line 200 of file test_gzip.py.

00200 
00201     def test_paddedfile_getattr(self):
00202         self.test_write()
00203         with gzip.GzipFile(self.filename, 'rb') as f:
00204             self.assertTrue(hasattr(f.fileobj, "name"))
00205             self.assertEqual(f.fileobj.name, self.filename)

Here is the call graph for this function:

Definition at line 304 of file test_gzip.py.

00304 
00305     def test_peek(self):
00306         uncompressed = data1 * 200
00307         with gzip.GzipFile(self.filename, "wb") as f:
00308             f.write(uncompressed)
00309 
00310         def sizes():
00311             while True:
00312                 for n in range(5, 50, 10):
00313                     yield n
00314 
00315         with gzip.GzipFile(self.filename, "rb") as f:
00316             f.max_read_chunk = 33
00317             nread = 0
00318             for n in sizes():
00319                 s = f.peek(n)
00320                 if s == b'':
00321                     break
00322                 self.assertEqual(f.read(len(s)), s)
00323                 nread += len(s)
00324             self.assertEqual(f.read(100), b'')
00325             self.assertEqual(nread, len(uncompressed))

Here is the call graph for this function:

Definition at line 60 of file test_gzip.py.

00060 
00061     def test_read(self):
00062         self.test_write()
00063         # Try reading.
00064         with gzip.GzipFile(self.filename, 'r') as f:
00065             d = f.read()
00066         self.assertEqual(d, data1*50)

Here is the call graph for this function:

Definition at line 129 of file test_gzip.py.

00129 
00130     def test_readline(self):
00131         self.test_write()
00132         # Try .readline() with varying line lengths
00133 
00134         with gzip.GzipFile(self.filename, 'rb') as f:
00135             line_length = 0
00136             while 1:
00137                 L = f.readline(line_length)
00138                 if not L and line_length != 0: break
00139                 self.assertTrue(len(L) <= line_length)
00140                 line_length = (line_length + 1) % 50

Here is the call graph for this function:

Definition at line 141 of file test_gzip.py.

00141 
00142     def test_readlines(self):
00143         self.test_write()
00144         # Try .readlines()
00145 
00146         with gzip.GzipFile(self.filename, 'rb') as f:
00147             L = f.readlines()
00148 
00149         with gzip.GzipFile(self.filename, 'rb') as f:
00150             while 1:
00151                 L = f.readlines(150)
00152                 if L == []: break

Here is the call graph for this function:

Definition at line 153 of file test_gzip.py.

00153 
00154     def test_seek_read(self):
00155         self.test_write()
00156         # Try seek, read test
00157 
00158         with gzip.GzipFile(self.filename) as f:
00159             while 1:
00160                 oldpos = f.tell()
00161                 line1 = f.readline()
00162                 if not line1: break
00163                 newpos = f.tell()
00164                 f.seek(oldpos)  # negative seek
00165                 if len(line1)>10:
00166                     amount = 10
00167                 else:
00168                     amount = len(line1)
00169                 line2 = f.read(amount)
00170                 self.assertEqual(line1[:amount], line2)
00171                 f.seek(newpos)  # positive seek

Here is the call graph for this function:

Definition at line 172 of file test_gzip.py.

00172 
00173     def test_seek_whence(self):
00174         self.test_write()
00175         # Try seek(whence=1), read test
00176 
00177         with gzip.GzipFile(self.filename) as f:
00178             f.read(10)
00179             f.seek(10, whence=1)
00180             y = f.read(10)
00181         self.assertEqual(y, data1[20:30])

Here is the call graph for this function:

Definition at line 182 of file test_gzip.py.

00182 
00183     def test_seek_write(self):
00184         # Try seek, write test
00185         with gzip.GzipFile(self.filename, 'w') as f:
00186             for pos in range(0, 256, 16):
00187                 f.seek(pos)
00188                 f.write(b'GZ\n')

Definition at line 261 of file test_gzip.py.

00261 
00262     def test_with_open(self):
00263         # GzipFile supports the context management protocol
00264         with gzip.GzipFile(self.filename, "wb") as f:
00265             f.write(b"xxx")
00266         f = gzip.GzipFile(self.filename, "rb")
00267         f.close()
00268         try:
00269             with f:
00270                 pass
00271         except ValueError:
00272             pass
00273         else:
00274             self.fail("__enter__ on a closed file didn't raise an exception")
00275         try:
00276             with gzip.GzipFile(self.filename, "wb") as f:
00277                 1/0
00278         except ZeroDivisionError:
00279             pass
00280         else:
00281             self.fail("1/0 didn't raise an exception")

Here is the call graph for this function:

Definition at line 46 of file test_gzip.py.

00046 
00047     def test_write(self):
00048         with gzip.GzipFile(self.filename, 'wb') as f:
00049             f.write(data1 * 50)
00050 
00051             # Try flush and fileno.
00052             f.flush()
00053             f.fileno()
00054             if hasattr(os, 'fsync'):
00055                 os.fsync(f.fileno())
00056             f.close()
00057 
00058         # Test multiple close() calls.
00059         f.close()

Here is the caller graph for this function:

Definition at line 282 of file test_gzip.py.

00282 
00283     def test_zero_padded_file(self):
00284         with gzip.GzipFile(self.filename, "wb") as f:
00285             f.write(data1 * 50)
00286 
00287         # Pad the file with zeroes
00288         with open(self.filename, "ab") as f:
00289             f.write(b"\x00" * 50)
00290 
00291         with gzip.GzipFile(self.filename, "rb") as f:
00292             d = f.read()
00293             self.assertEqual(d, data1 * 50, "Incorrect data in file")

Here is the call graph for this function:


Member Data Documentation

Definition at line 37 of file test_gzip.py.


The documentation for this class was generated from the following file: