commit | b74fc2b5feda3dbd56b2770a5a52c83db59c14d4 | [log] [tgz] |
---|---|---|
author | Antoine Pitrou <solipsis@pitrou.net> | Sat Jan 10 16:13:45 2009 +0000 |
committer | Antoine Pitrou <solipsis@pitrou.net> | Sat Jan 10 16:13:45 2009 +0000 |
tree | c2ef7352411220afaa1c190e2872def4ae5980ad | |
parent | 7c303e9a98e07768b7ae2d3deff760a7214dd826 [diff] [blame] |
Issue #3860: GzipFile and BZ2File now support the context manager protocol.
diff --git a/Lib/gzip.py b/Lib/gzip.py index 931c9ef..f568796 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py
@@ -454,6 +454,14 @@ else: raise StopIteration + def __enter__(self): + if self.fileobj is None: + raise ValueError("I/O operation on closed GzipFile object") + return self + + def __exit__(self, *args): + self.close() + def _test(): # Act like gzip; with -d, act like gunzip.