mirror of
https://github.com/python/cpython.git
synced 2025-08-01 23:53:15 +00:00
Issue #3860: GzipFile and BZ2File now support the context manager protocol.
This commit is contained in:
parent
7c303e9a98
commit
b74fc2b5fe
4 changed files with 83 additions and 1 deletions
|
@ -166,7 +166,6 @@ class TestGzip(unittest.TestCase):
|
|||
fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
|
||||
fWrite.write(data1)
|
||||
fWrite.close()
|
||||
|
||||
fRead = gzip.GzipFile(self.filename)
|
||||
dataRead = fRead.read()
|
||||
self.assertEqual(dataRead, data1)
|
||||
|
@ -222,6 +221,27 @@ class TestGzip(unittest.TestCase):
|
|||
|
||||
fRead.close()
|
||||
|
||||
def test_with_open(self):
|
||||
# GzipFile supports the context management protocol
|
||||
with gzip.GzipFile(self.filename, "wb") as f:
|
||||
f.write(b"xxx")
|
||||
f = gzip.GzipFile(self.filename, "rb")
|
||||
f.close()
|
||||
try:
|
||||
with f:
|
||||
pass
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("__enter__ on a closed file didn't raise an exception")
|
||||
try:
|
||||
with gzip.GzipFile(self.filename, "wb") as f:
|
||||
1/0
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
else:
|
||||
self.fail("1/0 didn't raise an exception")
|
||||
|
||||
def test_main(verbose=None):
|
||||
test_support.run_unittest(TestGzip)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue