mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Implement #7944. Use with
throughout the test suite.
This commit is contained in:
parent
01e3979757
commit
28f96b5b26
1 changed files with 104 additions and 117 deletions
|
@ -44,14 +44,15 @@ class TestGzip(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
def test_write(self):
|
def test_write(self):
|
||||||
f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
|
with gzip.GzipFile(self.filename, 'wb') as f:
|
||||||
|
f.write(data1 * 50)
|
||||||
|
|
||||||
# Try flush and fileno.
|
# Try flush and fileno.
|
||||||
f.flush()
|
f.flush()
|
||||||
f.fileno()
|
f.fileno()
|
||||||
if hasattr(os, 'fsync'):
|
if hasattr(os, 'fsync'):
|
||||||
os.fsync(f.fileno())
|
os.fsync(f.fileno())
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
# Test multiple close() calls.
|
# Test multiple close() calls.
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -59,7 +60,8 @@ class TestGzip(unittest.TestCase):
|
||||||
def test_read(self):
|
def test_read(self):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
# Try reading.
|
# Try reading.
|
||||||
f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
|
with gzip.GzipFile(self.filename, 'r') as f:
|
||||||
|
d = f.read()
|
||||||
self.assertEqual(d, data1*50)
|
self.assertEqual(d, data1*50)
|
||||||
|
|
||||||
def test_io_on_closed_object(self):
|
def test_io_on_closed_object(self):
|
||||||
|
@ -87,31 +89,30 @@ class TestGzip(unittest.TestCase):
|
||||||
def test_append(self):
|
def test_append(self):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
# Append to the previous file
|
# Append to the previous file
|
||||||
f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
|
with gzip.GzipFile(self.filename, 'ab') as f:
|
||||||
|
f.write(data2 * 15)
|
||||||
|
|
||||||
f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
|
with gzip.GzipFile(self.filename, 'rb') as f:
|
||||||
|
d = f.read()
|
||||||
self.assertEqual(d, (data1*50) + (data2*15))
|
self.assertEqual(d, (data1*50) + (data2*15))
|
||||||
|
|
||||||
def test_many_append(self):
|
def test_many_append(self):
|
||||||
# Bug #1074261 was triggered when reading a file that contained
|
# Bug #1074261 was triggered when reading a file that contained
|
||||||
# many, many members. Create such a file and verify that reading it
|
# many, many members. Create such a file and verify that reading it
|
||||||
# works.
|
# works.
|
||||||
f = gzip.open(self.filename, 'wb', 9)
|
with gzip.open(self.filename, 'wb', 9) as f:
|
||||||
f.write(b'a')
|
|
||||||
f.close()
|
|
||||||
for i in range(0, 200):
|
|
||||||
f = gzip.open(self.filename, "ab", 9) # append
|
|
||||||
f.write(b'a')
|
f.write(b'a')
|
||||||
f.close()
|
for i in range(0, 200):
|
||||||
|
with gzip.open(self.filename, "ab", 9) as f: # append
|
||||||
|
f.write(b'a')
|
||||||
|
|
||||||
# Try reading the file
|
# Try reading the file
|
||||||
zgfile = gzip.open(self.filename, "rb")
|
with gzip.open(self.filename, "rb") as zgfile:
|
||||||
contents = b""
|
contents = b""
|
||||||
while 1:
|
while 1:
|
||||||
ztxt = zgfile.read(8192)
|
ztxt = zgfile.read(8192)
|
||||||
contents += ztxt
|
contents += ztxt
|
||||||
if not ztxt: break
|
if not ztxt: break
|
||||||
zgfile.close()
|
|
||||||
self.assertEquals(contents, b'a'*201)
|
self.assertEquals(contents, b'a'*201)
|
||||||
|
|
||||||
def test_buffered_reader(self):
|
def test_buffered_reader(self):
|
||||||
|
@ -119,9 +120,9 @@ class TestGzip(unittest.TestCase):
|
||||||
# performance.
|
# performance.
|
||||||
self.test_write()
|
self.test_write()
|
||||||
|
|
||||||
f = gzip.GzipFile(self.filename, 'rb')
|
with gzip.GzipFile(self.filename, 'rb') as f:
|
||||||
with io.BufferedReader(f) as r:
|
with io.BufferedReader(f) as r:
|
||||||
lines = [line for line in r]
|
lines = [line for line in r]
|
||||||
|
|
||||||
self.assertEqual(lines, 50 * data1.splitlines(True))
|
self.assertEqual(lines, 50 * data1.splitlines(True))
|
||||||
|
|
||||||
|
@ -129,141 +130,127 @@ class TestGzip(unittest.TestCase):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
# Try .readline() with varying line lengths
|
# Try .readline() with varying line lengths
|
||||||
|
|
||||||
f = gzip.GzipFile(self.filename, 'rb')
|
with gzip.GzipFile(self.filename, 'rb') as f:
|
||||||
line_length = 0
|
line_length = 0
|
||||||
while 1:
|
while 1:
|
||||||
L = f.readline(line_length)
|
L = f.readline(line_length)
|
||||||
if not L and line_length != 0: break
|
if not L and line_length != 0: break
|
||||||
self.assertTrue(len(L) <= line_length)
|
self.assertTrue(len(L) <= line_length)
|
||||||
line_length = (line_length + 1) % 50
|
line_length = (line_length + 1) % 50
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_readlines(self):
|
def test_readlines(self):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
# Try .readlines()
|
# Try .readlines()
|
||||||
|
|
||||||
f = gzip.GzipFile(self.filename, 'rb')
|
with gzip.GzipFile(self.filename, 'rb') as f:
|
||||||
L = f.readlines()
|
L = f.readlines()
|
||||||
f.close()
|
|
||||||
|
|
||||||
f = gzip.GzipFile(self.filename, 'rb')
|
with gzip.GzipFile(self.filename, 'rb') as f:
|
||||||
while 1:
|
while 1:
|
||||||
L = f.readlines(150)
|
L = f.readlines(150)
|
||||||
if L == []: break
|
if L == []: break
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_seek_read(self):
|
def test_seek_read(self):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
# Try seek, read test
|
# Try seek, read test
|
||||||
|
|
||||||
f = gzip.GzipFile(self.filename)
|
with gzip.GzipFile(self.filename) as f:
|
||||||
while 1:
|
while 1:
|
||||||
oldpos = f.tell()
|
oldpos = f.tell()
|
||||||
line1 = f.readline()
|
line1 = f.readline()
|
||||||
if not line1: break
|
if not line1: break
|
||||||
newpos = f.tell()
|
newpos = f.tell()
|
||||||
f.seek(oldpos) # negative seek
|
f.seek(oldpos) # negative seek
|
||||||
if len(line1)>10:
|
if len(line1)>10:
|
||||||
amount = 10
|
amount = 10
|
||||||
else:
|
else:
|
||||||
amount = len(line1)
|
amount = len(line1)
|
||||||
line2 = f.read(amount)
|
line2 = f.read(amount)
|
||||||
self.assertEqual(line1[:amount], line2)
|
self.assertEqual(line1[:amount], line2)
|
||||||
f.seek(newpos) # positive seek
|
f.seek(newpos) # positive seek
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_seek_whence(self):
|
def test_seek_whence(self):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
# Try seek(whence=1), read test
|
# Try seek(whence=1), read test
|
||||||
|
|
||||||
f = gzip.GzipFile(self.filename)
|
with gzip.GzipFile(self.filename) as f:
|
||||||
f.read(10)
|
f.read(10)
|
||||||
f.seek(10, whence=1)
|
f.seek(10, whence=1)
|
||||||
y = f.read(10)
|
y = f.read(10)
|
||||||
f.close()
|
|
||||||
self.assertEquals(y, data1[20:30])
|
self.assertEquals(y, data1[20:30])
|
||||||
|
|
||||||
def test_seek_write(self):
|
def test_seek_write(self):
|
||||||
# Try seek, write test
|
# Try seek, write test
|
||||||
f = gzip.GzipFile(self.filename, 'w')
|
with gzip.GzipFile(self.filename, 'w') as f:
|
||||||
for pos in range(0, 256, 16):
|
for pos in range(0, 256, 16):
|
||||||
f.seek(pos)
|
f.seek(pos)
|
||||||
f.write(b'GZ\n')
|
f.write(b'GZ\n')
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_mode(self):
|
def test_mode(self):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
f = gzip.GzipFile(self.filename, 'r')
|
with gzip.GzipFile(self.filename, 'r') as f:
|
||||||
self.assertEqual(f.myfileobj.mode, 'rb')
|
self.assertEqual(f.myfileobj.mode, 'rb')
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_1647484(self):
|
def test_1647484(self):
|
||||||
for mode in ('wb', 'rb'):
|
for mode in ('wb', 'rb'):
|
||||||
f = gzip.GzipFile(self.filename, mode)
|
with gzip.GzipFile(self.filename, mode) as f:
|
||||||
self.assertTrue(hasattr(f, "name"))
|
self.assertTrue(hasattr(f, "name"))
|
||||||
self.assertEqual(f.name, self.filename)
|
self.assertEqual(f.name, self.filename)
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_mtime(self):
|
def test_mtime(self):
|
||||||
mtime = 123456789
|
mtime = 123456789
|
||||||
fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
|
with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
|
||||||
fWrite.write(data1)
|
fWrite.write(data1)
|
||||||
fWrite.close()
|
with gzip.GzipFile(self.filename) as fRead:
|
||||||
fRead = gzip.GzipFile(self.filename)
|
dataRead = fRead.read()
|
||||||
dataRead = fRead.read()
|
self.assertEqual(dataRead, data1)
|
||||||
self.assertEqual(dataRead, data1)
|
self.assertTrue(hasattr(fRead, 'mtime'))
|
||||||
self.assertTrue(hasattr(fRead, 'mtime'))
|
self.assertEqual(fRead.mtime, mtime)
|
||||||
self.assertEqual(fRead.mtime, mtime)
|
|
||||||
fRead.close()
|
|
||||||
|
|
||||||
def test_metadata(self):
|
def test_metadata(self):
|
||||||
mtime = 123456789
|
mtime = 123456789
|
||||||
|
|
||||||
fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
|
with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
|
||||||
fWrite.write(data1)
|
fWrite.write(data1)
|
||||||
fWrite.close()
|
|
||||||
|
|
||||||
fRead = open(self.filename, 'rb')
|
with open(self.filename, 'rb') as fRead:
|
||||||
|
# see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
|
||||||
|
|
||||||
# see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
|
idBytes = fRead.read(2)
|
||||||
|
self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
|
||||||
|
|
||||||
idBytes = fRead.read(2)
|
cmByte = fRead.read(1)
|
||||||
self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
|
self.assertEqual(cmByte, b'\x08') # deflate
|
||||||
|
|
||||||
cmByte = fRead.read(1)
|
flagsByte = fRead.read(1)
|
||||||
self.assertEqual(cmByte, b'\x08') # deflate
|
self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
|
||||||
|
|
||||||
flagsByte = fRead.read(1)
|
mtimeBytes = fRead.read(4)
|
||||||
self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
|
self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
|
||||||
|
|
||||||
mtimeBytes = fRead.read(4)
|
xflByte = fRead.read(1)
|
||||||
self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
|
self.assertEqual(xflByte, b'\x02') # maximum compression
|
||||||
|
|
||||||
xflByte = fRead.read(1)
|
osByte = fRead.read(1)
|
||||||
self.assertEqual(xflByte, b'\x02') # maximum compression
|
self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
|
||||||
|
|
||||||
osByte = fRead.read(1)
|
# Since the FNAME flag is set, the zero-terminated filename follows.
|
||||||
self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
|
# RFC 1952 specifies that this is the name of the input file, if any.
|
||||||
|
# However, the gzip module defaults to storing the name of the output
|
||||||
|
# file in this field.
|
||||||
|
expected = self.filename.encode('Latin-1') + b'\x00'
|
||||||
|
nameBytes = fRead.read(len(expected))
|
||||||
|
self.assertEqual(nameBytes, expected)
|
||||||
|
|
||||||
# Since the FNAME flag is set, the zero-terminated filename follows.
|
# Since no other flags were set, the header ends here.
|
||||||
# RFC 1952 specifies that this is the name of the input file, if any.
|
# Rather than process the compressed data, let's seek to the trailer.
|
||||||
# However, the gzip module defaults to storing the name of the output
|
fRead.seek(os.stat(self.filename).st_size - 8)
|
||||||
# file in this field.
|
|
||||||
expected = self.filename.encode('Latin-1') + b'\x00'
|
|
||||||
nameBytes = fRead.read(len(expected))
|
|
||||||
self.assertEqual(nameBytes, expected)
|
|
||||||
|
|
||||||
# Since no other flags were set, the header ends here.
|
crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
|
||||||
# Rather than process the compressed data, let's seek to the trailer.
|
self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
|
||||||
fRead.seek(os.stat(self.filename).st_size - 8)
|
|
||||||
|
|
||||||
crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
|
isizeBytes = fRead.read(4)
|
||||||
self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
|
self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
|
||||||
|
|
||||||
isizeBytes = fRead.read(4)
|
|
||||||
self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
|
|
||||||
|
|
||||||
fRead.close()
|
|
||||||
|
|
||||||
def test_with_open(self):
|
def test_with_open(self):
|
||||||
# GzipFile supports the context management protocol
|
# GzipFile supports the context management protocol
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue