diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2009-01-10 16:13:45 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2009-01-10 16:13:45 (GMT) |
commit | b74fc2b5feda3dbd56b2770a5a52c83db59c14d4 (patch) | |
tree | c2ef7352411220afaa1c190e2872def4ae5980ad /Lib/test/test_bz2.py | |
parent | 7c303e9a98e07768b7ae2d3deff760a7214dd826 (diff) | |
download | cpython-b74fc2b5feda3dbd56b2770a5a52c83db59c14d4.zip cpython-b74fc2b5feda3dbd56b2770a5a52c83db59c14d4.tar.gz cpython-b74fc2b5feda3dbd56b2770a5a52c83db59c14d4.tar.bz2 |
Issue #3860: GzipFile and BZ2File now support the context manager protocol.
Diffstat (limited to 'Lib/test/test_bz2.py')
-rw-r--r-- | Lib/test/test_bz2.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index c4d9b69..2f627c3 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -284,6 +284,28 @@ class BZ2FileTest(BaseTest): bz2f.close() self.assertEqual(xlines, ['Test']) + def testContextProtocol(self): + # BZ2File supports the context management protocol + f = None + with BZ2File(self.filename, "wb") as f: + f.write(b"xxx") + f = BZ2File(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 BZ2File(self.filename, "wb") as f: + 1/0 + except ZeroDivisionError: + pass + else: + self.fail("1/0 didn't raise an exception") + class BZ2CompressorTest(BaseTest): def testCompress(self): |