summaryrefslogtreecommitdiffstats
path: root/Lib/gzip.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2009-01-10 16:13:45 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2009-01-10 16:13:45 (GMT)
commitb74fc2b5feda3dbd56b2770a5a52c83db59c14d4 (patch)
treec2ef7352411220afaa1c190e2872def4ae5980ad /Lib/gzip.py
parent7c303e9a98e07768b7ae2d3deff760a7214dd826 (diff)
downloadcpython-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/gzip.py')
-rw-r--r--Lib/gzip.py8
1 files changed, 8 insertions, 0 deletions
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 @@ class GzipFile:
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.