Merged revisions 78623 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78623 | lars.gustaebel | 2010-03-03 12:55:48 +0100 (Wed, 03 Mar 2010) | 3 lines

  Issue #7232: Add support for the context manager protocol
  to the TarFile class.
........
This commit is contained in:
Lars Gustäbel 2010-03-03 12:08:54 +00:00
parent 5749e85b53
commit 0138581c43
4 changed files with 92 additions and 0 deletions

View file

@ -2391,6 +2391,20 @@ class TarFile(object):
"""
if level <= self.debug:
print(msg, file=sys.stderr)
def __enter__(self):
self._check()
return self
def __exit__(self, type, value, traceback):
if type is None:
self.close()
else:
# An exception occurred. We must not call close() because
# it would try to write end-of-archive blocks and padding.
if not self._extfileobj:
self.fileobj.close()
self.closed = True
# class TarFile
class TarIter: