Issue #3860: GzipFile and BZ2File now support the context manager protocol.

This commit is contained in:
Antoine Pitrou 2009-01-10 16:13:45 +00:00
parent 7c303e9a98
commit b74fc2b5fe
4 changed files with 83 additions and 1 deletions

View file

@ -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.